Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
still some mem cleanup to finish
[simgrid.git] / src / smpi / smpi_mpi.c
1 #include "private.h"
2
3 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_mpi, smpi,
4                                 "Logging specific to SMPI (mpi)");
5
6 int SMPI_MPI_Init(int *argc, char ***argv)
7 {
8   smpi_process_init(argc,argv);
9   smpi_bench_begin();
10   return MPI_SUCCESS;
11 }
12
13 int SMPI_MPI_Finalize()
14 {
15   smpi_bench_end();
16   smpi_process_finalize();
17   return MPI_SUCCESS;
18 }
19
20 // right now this just exits the current node, should send abort signal to all
21 // hosts in the communicator (TODO)
22 int SMPI_MPI_Abort(MPI_Comm comm, int errorcode)
23 {
24   smpi_exit(errorcode);
25   return 0;
26 }
27
28 int SMPI_MPI_Comm_size(MPI_Comm comm, int *size)
29 {
30   int retval = MPI_SUCCESS;
31
32   smpi_bench_end();
33
34   if (NULL == comm) {
35     retval = MPI_ERR_COMM;
36   } else if (NULL == size) {
37     retval = MPI_ERR_ARG;
38   } else {
39     *size = comm->size;
40   }
41
42   smpi_bench_begin();
43
44   return retval;
45 }
46
47 int SMPI_MPI_Comm_rank(MPI_Comm comm, int *rank)
48 {
49   int retval = MPI_SUCCESS;
50
51   smpi_bench_end();
52
53   if (NULL == comm) {
54     retval = MPI_ERR_COMM;
55   } else if (NULL == rank) {
56     retval = MPI_ERR_ARG;
57   } else {
58     *rank = smpi_mpi_comm_rank(comm);
59   }
60
61   smpi_bench_begin();
62
63   return retval;
64 }
65
66 int SMPI_MPI_Type_size(MPI_Datatype datatype, size_t * size)
67 {
68   int retval = MPI_SUCCESS;
69
70   smpi_bench_end();
71
72   if (NULL == datatype) {
73     retval = MPI_ERR_TYPE;
74   } else if (NULL == size) {
75     retval = MPI_ERR_ARG;
76   } else {
77     *size = datatype->size;
78   }
79
80   smpi_bench_begin();
81
82   return retval;
83 }
84
85 int SMPI_MPI_Barrier(MPI_Comm comm)
86 {
87   int retval = MPI_SUCCESS;
88
89   smpi_bench_end();
90
91   if (NULL == comm) {
92     retval = MPI_ERR_COMM;
93   } else {
94     retval = smpi_mpi_barrier(comm);
95   }
96
97   smpi_bench_begin();
98
99   return retval;
100 }
101
102 int SMPI_MPI_Irecv(void *buf, int count, MPI_Datatype datatype, int src,
103                    int tag, MPI_Comm comm, MPI_Request * request)
104 {
105   int retval = MPI_SUCCESS;
106
107   smpi_bench_end();
108
109   retval = smpi_create_request(buf, count, datatype, src, 0, tag, comm,
110                                request);
111   if (NULL != *request && MPI_SUCCESS == retval) {
112     retval = smpi_mpi_irecv(*request);
113   }
114
115   smpi_bench_begin();
116
117   return retval;
118 }
119
120 int SMPI_MPI_Recv(void *buf, int count, MPI_Datatype datatype, int src,
121                   int tag, MPI_Comm comm, MPI_Status * status)
122 {
123   int retval = MPI_SUCCESS;
124   smpi_mpi_request_t request;
125
126   smpi_bench_end();
127
128   retval = smpi_create_request(buf, count, datatype, src, 0, tag, comm,
129                                &request);
130   if (NULL != request && MPI_SUCCESS == retval) {
131     retval = smpi_mpi_irecv(request);
132     if (MPI_SUCCESS == retval) {
133       retval = smpi_mpi_wait(request, status);
134     }
135     xbt_mallocator_release(smpi_global->request_mallocator, request);
136   }
137
138   smpi_bench_begin();
139
140   return retval;
141 }
142
143 int SMPI_MPI_Isend(void *buf, int count, MPI_Datatype datatype, int dst,
144                    int tag, MPI_Comm comm, MPI_Request * request)
145 {
146   int retval = MPI_SUCCESS;
147
148   smpi_bench_end();
149
150   retval = smpi_create_request(buf, count, datatype, 0, dst, tag, comm,
151                                request);
152   if (NULL != *request && MPI_SUCCESS == retval) {
153     retval = smpi_mpi_isend(*request);
154   }
155
156   smpi_bench_begin();
157
158   return retval;
159 }
160
161 int SMPI_MPI_Send(void *buf, int count, MPI_Datatype datatype, int dst,
162                   int tag, MPI_Comm comm)
163 {
164   int retval = MPI_SUCCESS;
165   smpi_mpi_request_t request;
166
167   smpi_bench_end();
168
169   retval = smpi_create_request(buf, count, datatype, 0, dst, tag, comm,
170                                &request);
171   if (NULL != request && MPI_SUCCESS == retval) {
172     retval = smpi_mpi_isend(request);
173     if (MPI_SUCCESS == retval) {
174       smpi_mpi_wait(request, MPI_STATUS_IGNORE);
175     }
176     xbt_mallocator_release(smpi_global->request_mallocator, request);
177   }
178
179   smpi_bench_begin();
180
181   return retval;
182 }
183
184 /**
185  * MPI_Wait and friends
186  **/
187 int SMPI_MPI_Wait(MPI_Request * request, MPI_Status * status)
188 {
189   return smpi_mpi_wait(*request, status);
190 }
191
192 int SMPI_MPI_Waitall(int count, MPI_Request requests[], MPI_Status status[]) {
193         return smpi_mpi_waitall(count, requests,status);
194 }
195 int SMPI_MPI_Waitany(int count, MPI_Request requests[], int *index, MPI_Status status[]) {
196         return smpi_mpi_waitany(count, requests, index,status);
197 }
198 /**
199  * MPI_Bcast
200  **/
201 int SMPI_MPI_Bcast(void *buf, int count, MPI_Datatype datatype, int root,
202                    MPI_Comm comm)
203 {
204
205   int retval = MPI_SUCCESS;
206   int rank;
207   smpi_mpi_request_t request;
208
209   smpi_bench_end();
210
211   rank = smpi_mpi_comm_rank(comm);
212
213   if (rank == root) {
214     retval = smpi_create_request(buf, count, datatype, root,
215                                  (root + 1) % comm->size, 0, comm, &request);
216     request->forward = comm->size - 1;
217     smpi_mpi_isend(request);
218   } else {
219     retval = smpi_create_request(buf, count, datatype, MPI_ANY_SOURCE, rank,
220                                  0, comm, &request);
221     smpi_mpi_irecv(request);
222   }
223
224   smpi_mpi_wait(request, MPI_STATUS_IGNORE);
225   xbt_mallocator_release(smpi_global->request_mallocator, request);
226
227   smpi_bench_begin();
228
229   return retval;
230 }
231
232 /**
233  * debugging helper function
234  **/
235 void print_buffer_int( void *buf, int len, const char *msg) ;
236 void print_buffer_int( void *buf, int len, const char *msg) {
237           int tmp, *v;
238           printf("**%s: ",msg);
239           for (tmp=0;tmp<len;tmp++) {
240                     v = buf;
241                     printf("[%d]", v[tmp] );
242           }
243           printf("\n");
244 }
245
246
247 /**
248  * MPI_Reduce
249  **/
250
251 int SMPI_MPI_Reduce( void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype,
252                          MPI_Op op, int root, MPI_Comm comm )
253 {
254   int retval = MPI_SUCCESS;
255   int rank;
256   int size;
257   int i;
258   int tag=0;
259   smpi_mpi_request_t *tabrequest;
260   smpi_mpi_request_t request;
261
262
263   smpi_bench_end();
264
265   rank = smpi_mpi_comm_rank(comm);
266   size = comm->size;
267
268   //printf("-->rank %d. Entering ....\n",rank);
269   //print_buffer_int( sendbuf, count, "sendbuf");
270
271   if (rank != root) { // if i am not ROOT, simply send my buffer to root
272             retval = smpi_create_request(sendbuf, count, datatype, rank, root, tag , comm, &request);
273             smpi_mpi_isend(request);
274             smpi_mpi_wait(request, MPI_STATUS_IGNORE);
275             xbt_mallocator_release(smpi_global->request_mallocator, request);
276
277   } else {
278             // i am the ROOT: wait for all buffers by creating one request by sender
279             tabrequest = xbt_malloc((size-1)*sizeof(smpi_mpi_request_t));
280
281           void **tmpbufs = xbt_malloc((size-1)*sizeof(void *));
282             for (i=0; i<size-1; i++) {
283                   // we need 1 buffer per request to store intermediate receptions
284                 tmpbufs[i] = xbt_malloc(count*datatype->size); 
285             }
286             memcpy(recvbuf,sendbuf,count*datatype->size*sizeof(char)); // initiliaze recv buf with my own snd buf 
287
288             // i can not use: 'request->forward = size-1;' (which would progagate size-1 receive reqs)
289             // since we should op values as soon as one receiving request matches.
290             for (i=0; i<comm->size-1; i++) {
291                         // reminder: for smpi_create_request() the src is always the process sending.
292                         retval = smpi_create_request(tmpbufs[i], count, datatype, 
293                                             MPI_ANY_SOURCE, root,
294                                             tag, comm, &(tabrequest[i]));
295                         if (NULL != tabrequest[i] && MPI_SUCCESS == retval) {
296                                   if (MPI_SUCCESS == retval) {
297                                             smpi_mpi_irecv(tabrequest[i]);
298                                   }
299                         }
300             }
301             // now, wait for completion of all irecv's.
302             for (i=0; i<comm->size-1; i++) {
303                         int index = MPI_UNDEFINED;
304                         smpi_mpi_waitany(comm->size-1, tabrequest, &index, MPI_STATUS_IGNORE);
305
306                         //print_buffer_int( recvbuf, count, "rcvbuf");
307                         //printf("MPI_Waitany() unblocked: root received (completes req[index=%d]): ",index);
308                         //print_buffer_int( tmpbufs[index], count, "tmpbufs[index]");
309
310                         // arg 2 is modified 
311                         op->func (tmpbufs[index],recvbuf,&count,&datatype);
312
313                         // print_buffer_int( recvbuf, count, "recvbuf after func");
314                         //xbt_mallocator_release(smpi_global->request_mallocator, tabrequest[i]);
315                         //xbt_free( tmpbufs[index]);
316             }
317             xbt_free(tabrequest);
318             xbt_free(tmpbufs);
319   }
320
321   smpi_bench_begin();
322
323   return retval;
324 }
325
326 // used by comm_split to sort ranks based on key values
327 int smpi_compare_rankkeys(const void *a, const void *b);
328 int smpi_compare_rankkeys(const void *a, const void *b)
329 {
330           int *x = (int *) a;
331           int *y = (int *) b;
332
333   if (x[1] < y[1])
334     return -1;
335
336   if (x[1] == y[1]) {
337     if (x[0] < y[0])
338       return -1;
339     if (x[0] == y[0])
340       return 0;
341     return 1;
342   }
343
344   return 1;
345 }
346
347 int SMPI_MPI_Comm_split(MPI_Comm comm, int color, int key,
348                         MPI_Comm * comm_out)
349 {
350   int retval = MPI_SUCCESS;
351
352   int index, rank;
353   smpi_mpi_request_t request;
354   int colorkey[2];
355   smpi_mpi_status_t status;
356
357   smpi_bench_end();
358
359   // FIXME: need to test parameters
360
361   index = smpi_process_index();
362   rank = comm->index_to_rank_map[index];
363
364   // default output
365   comm_out = NULL;
366
367   // root node does most of the real work
368   if (0 == rank) {
369     int colormap[comm->size];
370     int keymap[comm->size];
371     int rankkeymap[comm->size * 2];
372     int i, j;
373     smpi_mpi_communicator_t tempcomm = NULL;
374     int count;
375     int indextmp;
376
377     colormap[0] = color;
378     keymap[0] = key;
379
380     // FIXME: use scatter/gather or similar instead of individual comms
381     for (i = 1; i < comm->size; i++) {
382       retval = smpi_create_request(colorkey, 2, MPI_INT, MPI_ANY_SOURCE,
383                                    rank, MPI_ANY_TAG, comm, &request);
384       smpi_mpi_irecv(request);
385       smpi_mpi_wait(request, &status);
386       colormap[status.MPI_SOURCE] = colorkey[0];
387       keymap[status.MPI_SOURCE] = colorkey[1];
388       xbt_mallocator_release(smpi_global->request_mallocator, request);
389     }
390
391     for (i = 0; i < comm->size; i++) {
392       if (MPI_UNDEFINED == colormap[i]) {
393         continue;
394       }
395       // make a list of nodes with current color and sort by keys
396       count = 0;
397       for (j = i; j < comm->size; j++) {
398         if (colormap[i] == colormap[j]) {
399           colormap[j] = MPI_UNDEFINED;
400           rankkeymap[count * 2] = j;
401           rankkeymap[count * 2 + 1] = keymap[j];
402           count++;
403         }
404       }
405       qsort(rankkeymap, count, sizeof(int) * 2, &smpi_compare_rankkeys);
406
407       // new communicator
408       tempcomm = xbt_new(s_smpi_mpi_communicator_t, 1);
409       tempcomm->barrier_count = 0;
410       tempcomm->size = count;
411       tempcomm->barrier_mutex = SIMIX_mutex_init();
412       tempcomm->barrier_cond = SIMIX_cond_init();
413       tempcomm->rank_to_index_map = xbt_new(int, count);
414       tempcomm->index_to_rank_map = xbt_new(int, smpi_global->process_count);
415       for (j = 0; j < smpi_global->process_count; j++) {
416         tempcomm->index_to_rank_map[j] = -1;
417       }
418       for (j = 0; j < count; j++) {
419         indextmp = comm->rank_to_index_map[rankkeymap[j * 2]];
420         tempcomm->rank_to_index_map[j] = indextmp;
421         tempcomm->index_to_rank_map[indextmp] = j;
422       }
423       for (j = 0; j < count; j++) {
424         if (rankkeymap[j * 2]) {
425           retval = smpi_create_request(&j, 1, MPI_INT, 0,
426                                        rankkeymap[j * 2], 0, comm, &request);
427           request->data = tempcomm;
428           smpi_mpi_isend(request);
429           smpi_mpi_wait(request, &status);
430           xbt_mallocator_release(smpi_global->request_mallocator, request);
431         } else {
432           *comm_out = tempcomm;
433         }
434       }
435     }
436   } else {
437     colorkey[0] = color;
438     colorkey[1] = key;
439     retval = smpi_create_request(colorkey, 2, MPI_INT, rank, 0, 0, comm,
440                                  &request);
441     smpi_mpi_isend(request);
442     smpi_mpi_wait(request, &status);
443     xbt_mallocator_release(smpi_global->request_mallocator, request);
444     if (MPI_UNDEFINED != color) {
445       retval = smpi_create_request(colorkey, 1, MPI_INT, 0, rank, 0, comm,
446                                    &request);
447       smpi_mpi_irecv(request);
448       smpi_mpi_wait(request, &status);
449       *comm_out = request->data;
450     }
451   }
452
453   smpi_bench_begin();
454
455   return retval;
456 }
457
458 double SMPI_MPI_Wtime( void )
459 {
460           return ( SIMIX_get_clock() );
461 }