1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>

/****************************************************************/
/* This implements a compare-split operation.			*/
/* nlocal is the number of elements stored here			*/
/* elmnts is my local copy of the array				*/
/* relmnts is the copy I've received				*/
/* wspace is a working array					*/
/* keepsmall tells local processor whether it takes the smaller	*/
/* or larger half.						*/
/****************************************************************/
void CompareSplit(int nlocal, int *elmnts, int *relmnts, int *wspace,
             int keepsmall) {
  int i, j, k;

  for (i = 0; i < nlocal; i++)
    wspace[i] = elmnts[i];	/* copy elts into wspace	*/

  /**************************************************************/
  /* copying the smaller values into my "elmnts" array.		*/
  /**************************************************************/
  if (keepsmall) {
    for (i = j = k = 0; k < nlocal; k++) {
      if (j == nlocal || (i < nlocal && wspace[i] < relmnts[j]))
        elmnts[k] = wspace[i++];
      else
        elmnts[k] = relmnts[j++];
    }
  }
  /**************************************************************/
  /* else - copying the larger values.				*/
  /**************************************************************/
  else {
    for (i = k = nlocal - 1, j = nlocal - 1; k >= 0; k--) {
      if (j == 0 || (i >= 0 && wspace[i] >= relmnts[j]))
        elmnts[k] = wspace[i--];
      else
        elmnts[k] = relmnts[j--];
    }
  }
}

/****************************************************************/
/* This is the comparison function for the call to qsort.	*/
/****************************************************************/
int IncOrder(const void *e1, const void *e2 ) {
  return (*((int *)e1) - *((int *)e2));
}

main(int argc, char* argv[]) {
  int n;		/* total number of elts to be sorted	*/
  unsigned int seed;	/* random number seed			*/
  int npes, myrank;	/* total processes, self ID		*/
  int nlocal;		/* How much data on local PE		*/
  int *elmnts;		/* array storing local elements		*/
  int *relmnts;		/* array storing received elements	*/
  int oddrank;		/* my rank during odd-phase comm.	*/
  int evenrank;		/* my rank during even-phase comm.	*/
  int *wspace;		/* working space during compare-split	*/
  int i, j;
  MPI_Status status;
  
  MPI_Comm comm = MPI_COMM_WORLD;

  int tag = MPI_ANY_TAG;
  
  MPI_Init(&argc, &argv);
  MPI_Comm_size(MPI_COMM_WORLD, &npes);
  MPI_Comm_rank(MPI_COMM_WORLD, &myrank);

  seed = atoi(argv[1]);		/* Random number seed.		*/
  n = atoi(argv[2]);		/* How many elts to produce.	*/

  nlocal = n/npes;		/* How many I get.		*/

  elmnts = malloc(nlocal*npes*sizeof(int));
  relmnts = malloc(nlocal*sizeof(int));
  wspace = malloc(nlocal*sizeof(int));

  
  if (myrank == 0) {
	  printf("- there are %i total processes\n" ,npes);
	
	// Process 0
	// Generate all the random numbers
	  seed = seed * (myrank+1);
	  srand(seed);
	  
	  elmnts[0] = 7;
	  elmnts[1] = 6;
	  elmnts[2] = 3;
	  elmnts[3] = 10;
	  elmnts[4] = 1;
	  elmnts[5] = 2;
	  
	  printf("- p0 generates %i numbers: ", n);
	  for (i = 0; i < n; i++)
	  {
		  // Print intial elements
		  printf("%i ", elmnts[i]);
          }
	  printf("\n");
	  
	  printf("- p0 sends ");
	  for (i = 1; i < npes; i++) {
		for (j = i*nlocal; j < (i*nlocal)+nlocal; j++) {
			printf("%i,", elmnts[j]);
		}
		printf(" to p%i\n", i);
	  }
		
	  for (i = 1; i < npes; i++) {
			// Send the random numbers nlocal at a time to all other processes
			MPI_Send(&elmnts[i*nlocal], nlocal, MPI_INT, i, myrank, comm);
	  }
	  
	  // Run the algorithm for itself
          evenrank = myrank + 1;
	  oddrank = MPI_PROC_NULL;

	qsort(elmnts, nlocal, sizeof(int), IncOrder);
	printf("- p0 sorts its own elements, giving ");
	for ( i = 0; i < nlocal; i++) {
		printf("%i, ", elmnts[i]);
	}
	printf("\n");
	
	  for (i = 1; i < npes; i++) {
		if (i % 2 == 1)	// odd phase
		  MPI_Sendrecv(elmnts, nlocal, MPI_INT, oddrank, 1, relmnts,
					   nlocal, MPI_INT, oddrank, 1, MPI_COMM_WORLD, &status);
		else
		  MPI_Sendrecv(elmnts, nlocal, MPI_INT, evenrank, 1, relmnts,
					   nlocal, MPI_INT, evenrank, 1, MPI_COMM_WORLD, &status);
	
		CompareSplit(nlocal, elmnts, relmnts, wspace,
					 myrank < status.MPI_SOURCE);
	  }
	  
	  // Print my sublist
	  for (i = 0; i < nlocal; i++) 
	  {
		    printf("processor 0 element: %i\n", elmnts[i]);
	  }
	  
	  for (i = 1; i < npes; i++)
	  {
	  	// Receive all other processes sublists
		MPI_Recv(relmnts, nlocal, MPI_INT, i, tag, comm, &status);
	    
		for (j = 0; j < nlocal; j++)
		//for (j = i*nlocal; j < (i*nlocal)+nlocal; j++)
		{
			// Print all other processes sublist
			//printf("p0 Received element: %i from p%i\n", elmnts[j], i);
			printf("- p0 receives %i , from p%i\n", relmnts[j], i);
		}
	  }
	  
  }
  else {
  
  	// The rest of the processes
	printf("nlocal %i\n", nlocal);
	printf("n %i\n", n);
	printf("npes %i\n", npes);
	
	// Receive nlocal randomly generated elements from process 0
	MPI_Recv(relmnts, nlocal, MPI_INT, 0, tag, comm, &status); 
	
	// Print the received elements
	printf("- p%i receives ", myrank);
	for ( i = 0; i < n/npes; i++)
	{
		//printf("p%i receives elmnts[%i]: %i from p0\n", myrank, i, elmnts[i]);
		printf("%i, ", relmnts[i]);
	}
	printf("\n");
	
	qsort(relmnts, nlocal, sizeof(int), IncOrder); // does it matter where we sort at?
	
	printf("- p%i sorts, making ", myrank);
	for ( i = 0; i < n/npes; i++)
	{
		//printf("p%i sorted elmnts[%i]: %i\n", myrank, i, elmnts[i]);
		printf("%i, ", relmnts[i]);
	}
	
	  /**************************************************************/
	  /* Calculate the locations of my neighbors in advance.	*/
	  /**************************************************************/
	  if (myrank % 2 == 0) {
		oddrank = myrank - 1;
		evenrank = myrank + 1;
	  }
	  else {
		oddrank = myrank + 1;
		evenrank = myrank - 1;
	  }

	  
	  /**************************************************************/
	  /* Set ranks of processors at end of linear array - boundary	*/
	  /* conditions.						*/
	  /**************************************************************/
	  
	  if (oddrank == -1 || oddrank == npes)
		oddrank = MPI_PROC_NULL;
	  if (evenrank == -1 || evenrank == npes)
		evenrank = MPI_PROC_NULL;
  
		if (i % 2 == 1)	// odd phase
		  MPI_Sendrecv(elmnts, nlocal, MPI_INT, oddrank, 1, relmnts,
					   nlocal, MPI_INT, oddrank, 1, MPI_COMM_WORLD, &status);
		else
		  MPI_Sendrecv(elmnts, nlocal, MPI_INT, evenrank, 1, relmnts,
					   nlocal, MPI_INT, evenrank, 1, MPI_COMM_WORLD, &status);

		CompareSplit(nlocal, elmnts, relmnts, wspace,
					 myrank < status.MPI_SOURCE);
	
	printf("p%i sends ", myrank);
	
	for ( i = 0; i < n/npes; i++)
	//for ( i = 0; i < sizeof(elmnts)/sizeof(int); i++)
	{
		printf("%i, ", relmnts[i]);
	}
	printf("\n");
	
	// Send results back to process 0
	MPI_Send(relmnts, nlocal, MPI_INT, 0, myrank, comm);
  }

  free(elmnts);
  free(relmnts);
  free(wspace);
  MPI_Finalize();
}