7
7
#define DEBUG_INITIAL_SORT 0
8
8
#define DEBUG_SPLITTERS 0
9
9
#define DEBUG_BUCKETS 0
10
- #define DEBUG_FINAL_SORT 1
11
- #define USE_SMALL_ARRAY 1
10
+ #define DEBUG_FINAL_SORT 0
11
+ #define USE_SMALL_ARRAY 0
12
12
13
13
char * ws ;//strong or weak scaling indicator
14
14
int rank ,npes , root = 0 ;
@@ -147,17 +147,17 @@ void samplesort(long int *a,size_t count,size_t size,int (compare)(const void *,
147
147
148
148
#if DEBUG_BUCKETS
149
149
for (i = 0 ;i < count ;i ++ ) {
150
- printf ("Element in rank: %d %d %lu\n" ,rank ,i ,a [i ]);
150
+ printf ("rank= %d i= %d a[i]= %lu\n" ,rank ,i ,a [i ]);
151
151
}
152
152
MPI_Barrier (comm );
153
153
for (i = 0 ;i < npes ;i ++ ){
154
- printf ("Counts in rank: %d %lu %lu\n" ,rank ,global_splitters [i ],send_counts [i ]);
154
+ printf ("rank= %d global_splitters[i]= %lu send_counts[i]= %lu\n" ,rank ,global_splitters [i ],send_counts [i ]);
155
155
}
156
156
#endif
157
157
recv_counts = (int * ) malloc (npes * sizeof (int ));
158
158
//each processor now tells each other how many elements they will recieve
159
159
//i.e. how many elements are in their bucket
160
- MPI_Alltoall (send_counts ,1 ,MPI_LONG ,recv_counts ,1 ,MPI_LONG ,comm );
160
+ MPI_Alltoall (send_counts ,1 ,MPI_INT ,recv_counts ,1 ,MPI_INT ,comm );
161
161
//imagine npes=3. Then
162
162
//process 0: recv_counts[0]=send_counts[0] from p0,
163
163
//recv_counts[1]=send_counts[0] from p1,recv_counts[2]=send_counts[0]
@@ -167,7 +167,7 @@ void samplesort(long int *a,size_t count,size_t size,int (compare)(const void *,
167
167
#if DEBUG_BUCKETS
168
168
MPI_Barrier (comm );
169
169
for (i = 0 ;i < npes ;i ++ ){
170
- printf ("%d %lu %lu\n" ,rank ,send_counts [i ],recv_counts [i ]);
170
+ printf ("rank= %d send_counts= %lu recv_counts= %lu\n" ,rank ,send_counts [i ],recv_counts [i ]);
171
171
}
172
172
#endif
173
173
//The processors now have to send the proper elements to each other.
@@ -184,14 +184,10 @@ void samplesort(long int *a,size_t count,size_t size,int (compare)(const void *,
184
184
185
185
recv_offsets [i ] = j ;
186
186
j += recv_counts [i ];
187
- printf ("recv_counts %d %d\n" ,rank ,recv_counts [i ]);
188
187
}
189
188
//buckets have different sizes given by j
190
- printf ("jj %d %d\n" ,rank ,j );
191
- for (i = 0 ;i < (npes - 1 );i ++ )
192
- printf ("%d %d\n" ,rank ,global_splitters [i ]);
193
189
buckets = (long int * )malloc (j * size );
194
- // MPI_Alltoallv(a,send_counts,send_offsets,MPI_LONG,buckets,recv_counts,recv_offsets,MPI_LONG,comm);
190
+ MPI_Alltoallv (a ,send_counts ,send_offsets ,MPI_LONG ,buckets ,recv_counts ,recv_offsets ,MPI_LONG ,comm );
195
191
//alltoallv: Imagine sends for p0, send_counts[0]=5,send_counts[1]=2,
196
192
//send_counts[2]=7. Then a[0-4] from p0 goes to buckets[0-4] of p0
197
193
//a[5-6] from p0 goes to buckets[0-1] of p1 and a[7-13] from p0 goes to
@@ -211,13 +207,23 @@ void samplesort(long int *a,size_t count,size_t size,int (compare)(const void *,
211
207
}
212
208
MPI_Barrier (comm );
213
209
for (i = 0 ;i < j ;i ++ ) {
214
- printf ("Rank:%d buckets[i]=%lu\n" ,rank ,buckets [i ]);
210
+ printf ("Rank:%d i=%lu buckets[i]=%lu\n" ,rank , i ,buckets [i ]);
215
211
}
212
+ MPI_Barrier (comm );
216
213
#endif
217
- /* for (i=0;i<psize;i++) {
218
- printf("Rank after %d %lu\n",rank,a[i]);
219
- }*/
220
-
214
+ /*//Gather at the root the lengths of all the buckets
215
+ MPI_Gather(&j,1,MPI_LONG,recv_counts,1,MPI_LONG,root,comm);
216
+ //Compute index offsets where to put the bucket elements
217
+ if (rank == root) {
218
+ k = 0;
219
+ for (i=0;i<count;i++) {
220
+ recv_offsets[i]=k;
221
+ k += recv_counts[i];
222
+ }
223
+ //gather sorted data
224
+ */
225
+ //Now we have to put all the buckets together in the original array a
226
+
221
227
free (local_splitters );
222
228
free (all_splitters );
223
229
free (global_splitters );
@@ -226,7 +232,7 @@ void samplesort(long int *a,size_t count,size_t size,int (compare)(const void *,
226
232
free (recv_counts );
227
233
free (send_offsets );
228
234
free (recv_offsets );
229
- free (buckets );
235
+ // free(buckets);
230
236
}
231
237
232
238
void sort (long int n )
@@ -258,9 +264,12 @@ void sort(long int n)
258
264
a [i ] = rand_interval (rank ,rank * 2 + 30 );
259
265
}
260
266
#endif
267
+ double t1 ,t2 ;
268
+ t1 = MPI_Wtime ();
261
269
samplesort (a ,psize ,sizeof (long int ),compare ,MPI_COMM_WORLD );
262
-
263
- free (a );
270
+ t2 = MPI_Wtime ();
271
+ printf ("Time elapsed in rank %d was %f\n" ,rank , t2 - t1 );
272
+ //free(a);
264
273
}
265
274
266
275
int main (int argc ,char * * argv )
@@ -303,5 +312,4 @@ int main(int argc,char** argv)
303
312
//Finalize
304
313
MPI_Finalize ();
305
314
return 0 ;
306
- }
307
-
315
+ }
0 commit comments