Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add a bunch of new fortran bindings, to make bigdft happy
[simgrid.git] / src / smpi / smpi_base.c
1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "private.h"
8 #include "xbt/virtu.h"
9 #include "mc/mc.h"
10 #include "xbt/replay.h"
11 #include <errno.h>
12 #include "simix/smx_private.h"
13 #include "surf/surf.h"
14
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi, "Logging specific to SMPI (base)");
17
18
19 static int match_recv(void* a, void* b, smx_action_t ignored) {
20    MPI_Request ref = (MPI_Request)a;
21    MPI_Request req = (MPI_Request)b;
22    XBT_DEBUG("Trying to match a recv of src %d against %d, tag %d against %d",ref->src,req->src, ref->tag, req->tag);
23
24   xbt_assert(ref, "Cannot match recv against null reference");
25   xbt_assert(req, "Cannot match recv against null request");
26   if((ref->src == MPI_ANY_SOURCE || req->src == ref->src)
27     && (ref->tag == MPI_ANY_TAG || req->tag == ref->tag)){
28     //we match, we can transfer some values
29     // FIXME : move this to the copy function ?
30     if(ref->src == MPI_ANY_SOURCE)ref->real_src = req->src;
31     if(ref->tag == MPI_ANY_TAG)ref->real_tag = req->tag;
32     if(ref->real_size < req->real_size) ref->truncated = 1;
33     else ref->truncated = 0;
34     return 1;
35   }else return 0;
36 }
37
38 static int match_send(void* a, void* b,smx_action_t ignored) {
39    MPI_Request ref = (MPI_Request)a;
40    MPI_Request req = (MPI_Request)b;
41    XBT_DEBUG("Trying to match a send of src %d against %d, tag %d against %d",ref->src,req->src, ref->tag, req->tag);
42    xbt_assert(ref, "Cannot match send against null reference");
43    xbt_assert(req, "Cannot match send against null request");
44
45    if((req->src == MPI_ANY_SOURCE || req->src == ref->src)
46              && (req->tag == MPI_ANY_TAG || req->tag == ref->tag))
47    {
48      if(req->src == MPI_ANY_SOURCE)req->real_src = ref->src;
49      if(req->tag == MPI_ANY_TAG)req->real_tag = ref->tag;
50      if(req->real_size < ref->real_size) req->truncated = 1;
51      else ref->truncated = 0;
52      return 1;
53    } else return 0;
54 }
55
56 static MPI_Request build_request(void *buf, int count,
57                                  MPI_Datatype datatype, int src, int dst,
58                                  int tag, MPI_Comm comm, unsigned flags)
59 {
60   MPI_Request request;
61
62   void *old_buf = NULL;
63
64   request = xbt_new(s_smpi_mpi_request_t, 1);
65
66   s_smpi_subtype_t *subtype = datatype->substruct;
67
68   if(datatype->has_subtype == 1){
69     // This part handles the problem of non-contiguous memory
70     old_buf = buf;
71     buf = malloc(count*smpi_datatype_size(datatype));
72     if (flags & SEND) {
73       subtype->serialize(old_buf, buf, count, datatype->substruct);
74     }
75   }
76
77   request->buf = buf;
78   // This part handles the problem of non-contiguous memory (for the
79   // unserialisation at the reception)
80   request->old_buf = old_buf;
81   request->old_type = datatype;
82
83   request->size = smpi_datatype_size(datatype) * count;
84   request->src = src;
85   request->dst = dst;
86   request->tag = tag;
87   request->comm = comm;
88   request->action = NULL;
89   request->flags = flags;
90   request->detached = 0;
91 #ifdef HAVE_TRACING
92   request->send = 0;
93   request->recv = 0;
94 #endif
95   if (flags & SEND) smpi_datatype_unuse(datatype);
96
97   return request;
98 }
99
100
101 void smpi_empty_status(MPI_Status * status) {
102   if(status != MPI_STATUS_IGNORE) {
103       status->MPI_SOURCE=MPI_ANY_SOURCE;
104       status->MPI_TAG=MPI_ANY_TAG;
105       status->count=0;
106   }
107 }
108
109 void smpi_action_trace_run(char *path)
110 {
111   char *name;
112   xbt_dynar_t todo;
113   xbt_dict_cursor_t cursor;
114
115   action_fp=NULL;
116   if (path) {
117     action_fp = fopen(path, "r");
118     xbt_assert(action_fp != NULL, "Cannot open %s: %s", path,
119                strerror(errno));
120   }
121
122   if (!xbt_dict_is_empty(action_queues)) {
123     XBT_WARN
124       ("Not all actions got consumed. If the simulation ended successfully (without deadlock), you may want to add new processes to your deployment file.");
125
126
127     xbt_dict_foreach(action_queues, cursor, name, todo) {
128       XBT_WARN("Still %lu actions for %s", xbt_dynar_length(todo), name);
129     }
130   }
131
132   if (path)
133     fclose(action_fp);
134   xbt_dict_free(&action_queues);
135   action_queues = xbt_dict_new_homogeneous(NULL);
136 }
137
138 static void smpi_mpi_request_free_voidp(void* request)
139 {
140   MPI_Request req = request;
141   smpi_mpi_request_free(&req);
142 }
143
144 /* MPI Low level calls */
145 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
146                                int dst, int tag, MPI_Comm comm)
147 {
148   MPI_Request request =
149     build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
150                   comm, PERSISTENT | SEND);
151
152   return request;
153 }
154
155 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
156                                int src, int tag, MPI_Comm comm)
157 {
158   MPI_Request request =
159     build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
160                   comm, PERSISTENT | RECV);
161
162   return request;
163 }
164
165 void smpi_mpi_start(MPI_Request request)
166 {
167   smx_rdv_t mailbox;
168
169   xbt_assert(!request->action,
170              "Cannot (re)start a non-finished communication");
171   if(request->flags & RECV) {
172     print_request("New recv", request);
173     if (request->size < surf_cfg_get_int("smpi/async_small_thres"))
174       mailbox = smpi_process_mailbox_small();
175     else
176       mailbox = smpi_process_mailbox();
177     // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
178     request->real_size=request->size;
179     smpi_datatype_use(request->old_type);
180     request->action = simcall_comm_irecv(mailbox, request->buf, &request->real_size, &match_recv, request);
181     if (request->action)request->action->comm.refcount++;
182   } else {
183
184     int receiver = smpi_group_index(smpi_comm_group(request->comm), request->dst);
185 /*    if(receiver == MPI_UNDEFINED) {*/
186 /*      XBT_WARN("Trying to send a message to a wrong rank");*/
187 /*      return;*/
188 /*    }*/
189     print_request("New send", request);
190     if (request->size < surf_cfg_get_int("smpi/async_small_thres")) { // eager mode
191       mailbox = smpi_process_remote_mailbox_small(receiver);
192     }else{
193       XBT_DEBUG("Send request %p is not in the permanent receive mailbox (buf: %p)",request,request->buf);
194       mailbox = smpi_process_remote_mailbox(receiver);
195     }
196     if (request->size < 64*1024 ) { //(FIXME: this limit should be configurable)
197       void *oldbuf = NULL;
198       if(request->old_type->has_subtype == 0){
199         oldbuf = request->buf;
200         request->detached = 1;
201         if (oldbuf){
202           request->buf = malloc(request->size);
203           memcpy(request->buf,oldbuf,request->size);
204         }
205       }
206       XBT_DEBUG("Send request %p is detached; buf %p copied into %p",request,oldbuf,request->buf);
207     }
208     // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
209     request->real_size=request->size;
210     smpi_datatype_use(request->old_type);
211
212     request->action =
213       simcall_comm_isend(mailbox, request->size, -1.0,
214                          request->buf, request->real_size,
215                          &match_send,
216                          &smpi_mpi_request_free_voidp, // how to free the userdata if a detached send fails
217                          request,
218                          // detach if msg size < eager/rdv switch limit
219                          request->detached);
220     if (request->action)request->action->comm.refcount++;
221
222 #ifdef HAVE_TRACING
223     /* FIXME: detached sends are not traceable (request->action == NULL) */
224     if (request->action)
225       simcall_set_category(request->action, TRACE_internal_smpi_get_category());
226 #endif
227
228   }
229
230 }
231
232 void smpi_mpi_startall(int count, MPI_Request * requests)
233 {
234   int i;
235
236   for(i = 0; i < count; i++) {
237     smpi_mpi_start(requests[i]);
238   }
239 }
240
241 void smpi_mpi_request_free(MPI_Request * request)
242 {
243   xbt_free(*request);
244   *request = MPI_REQUEST_NULL;
245 }
246
247 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
248                             int dst, int tag, MPI_Comm comm)
249 {
250   MPI_Request request =
251     build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
252                   comm, NON_PERSISTENT | SEND);
253
254   return request;
255 }
256
257 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
258                            int dst, int tag, MPI_Comm comm)
259 {
260   MPI_Request request =
261     smpi_isend_init(buf, count, datatype, dst, tag, comm);
262
263   smpi_mpi_start(request);
264   return request;
265 }
266
267 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
268                             int src, int tag, MPI_Comm comm)
269 {
270   MPI_Request request =
271     build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
272                   comm, NON_PERSISTENT | RECV);
273   return request;
274 }
275
276 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
277                            int src, int tag, MPI_Comm comm)
278 {
279   MPI_Request request =
280     smpi_irecv_init(buf, count, datatype, src, tag, comm);
281
282   smpi_mpi_start(request);
283   return request;
284 }
285
286 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
287                    int tag, MPI_Comm comm, MPI_Status * status)
288 {
289   MPI_Request request;
290   request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
291   smpi_mpi_wait(&request, status);
292 }
293
294
295
296 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
297                    int tag, MPI_Comm comm)
298 {
299   MPI_Request request =
300     build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
301                   comm, NON_PERSISTENT | SEND | RECV_DELETE);
302
303   smpi_mpi_start(request);
304   smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
305
306 }
307
308 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
309                        int dst, int sendtag, void *recvbuf, int recvcount,
310                        MPI_Datatype recvtype, int src, int recvtag,
311                        MPI_Comm comm, MPI_Status * status)
312 {
313   MPI_Request requests[2];
314   MPI_Status stats[2];
315
316   requests[0] =
317     smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
318   requests[1] =
319     smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
320   smpi_mpi_startall(2, requests);
321   smpi_mpi_waitall(2, requests, stats);
322   if(status != MPI_STATUS_IGNORE) {
323     // Copy receive status
324     memcpy(status, &stats[1], sizeof(MPI_Status));
325   }
326 }
327
328 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
329 {
330   return status->count / smpi_datatype_size(datatype);
331 }
332
333 static void finish_wait(MPI_Request * request, MPI_Status * status)
334 {
335   MPI_Request req = *request;
336
337   if(status != MPI_STATUS_IGNORE) {
338     status->MPI_SOURCE = req->src == MPI_ANY_SOURCE ? req->real_src : req->src;
339     status->MPI_TAG = req->tag == MPI_ANY_TAG ? req->real_tag : req->tag;
340     if(req->truncated)
341     status->MPI_ERROR = MPI_ERR_TRUNCATE;
342     else status->MPI_ERROR = MPI_SUCCESS ;
343     // this handles the case were size in receive differs from size in send
344     // FIXME: really this should just contain the count of receive-type blocks,
345     // right?
346     status->count = req->real_size;
347   }
348   req = *request;
349
350   print_request("Finishing", req);
351   MPI_Datatype datatype = req->old_type;
352
353   if(datatype->has_subtype == 1){
354       // This part handles the problem of non-contignous memory
355       // the unserialization at the reception
356     s_smpi_subtype_t *subtype = datatype->substruct;
357     if(req->flags & RECV) {
358       subtype->unserialize(req->buf, req->old_buf, req->real_size/smpi_datatype_size(datatype) , datatype->substruct);
359     }
360     if(req->detached == 0) free(req->buf);
361   }
362   smpi_datatype_unuse(datatype);
363
364
365   if(req->flags & NON_PERSISTENT) {
366     if(req->flags & RECV &&
367        req->action &&
368       (req->action->state == SIMIX_DONE))
369     {
370       MPI_Request sender_request = (MPI_Request)SIMIX_comm_get_src_data(req->action);
371       if((sender_request!=MPI_REQUEST_NULL) &&
372         ( sender_request->detached ) &&
373         ( sender_request->flags & RECV_DELETE))
374       {
375         //we are in a receiver's wait from a detached send
376         //we have to clean the sender's side request here.... but only if done by a send, not an isend
377         //the request lives senderside for an isend. As detached is currently for send + isend, we use RECV_DELETE to separate them
378         //FIXME : see if just removing detached status for isend is also good
379         smpi_mpi_request_free(&sender_request);
380       }
381     }
382
383
384     if(req->action){
385       //if we want to free our request, we have to invalidate it at the other end of the comm
386       if(req->flags & SEND){
387         req->action->comm.src_data=MPI_REQUEST_NULL;
388       }else{
389         req->action->comm.dst_data=MPI_REQUEST_NULL;
390       }
391
392       smx_action_t temp=req->action;
393       if(req->action->comm.refcount == 1)req->action = NULL;
394       SIMIX_comm_destroy(temp);
395     }
396
397
398
399     smpi_mpi_request_free(request);
400
401
402   } else {
403     if(req->action)SIMIX_comm_destroy(req->action);
404     req->action = NULL;
405   }
406 }
407
408 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
409   int flag;
410
411   //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or smpi_mpi_testall before)
412   if ((*request)->action == NULL)
413     flag = 1;
414   else
415     flag = simcall_comm_test((*request)->action);
416   if(flag) {
417     finish_wait(request, status);
418   }else{
419     smpi_empty_status(status);
420   }
421   return flag;
422 }
423
424 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
425                      MPI_Status * status)
426 {
427   xbt_dynar_t comms;
428   int i, flag, size;
429   int* map;
430
431   *index = MPI_UNDEFINED;
432   flag = 0;
433   if(count > 0) {
434     comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
435     map = xbt_new(int, count);
436     size = 0;
437     for(i = 0; i < count; i++) {
438       if((requests[i]!=MPI_REQUEST_NULL) && requests[i]->action) {
439          xbt_dynar_push(comms, &requests[i]->action);
440          map[size] = i;
441          size++;
442       }
443     }
444     if(size > 0) {
445       i = simcall_comm_testany(comms);
446       // not MPI_UNDEFINED, as this is a simix return code
447       if(i != -1) {
448         *index = map[i];
449         finish_wait(&requests[*index], status);
450         flag = 1;
451       }
452     }else{
453         //all requests are null or inactive, return true
454         flag=1;
455         smpi_empty_status(status);
456     }
457     xbt_free(map);
458     xbt_dynar_free(&comms);
459   }
460
461   return flag;
462 }
463
464
465 int smpi_mpi_testall(int count, MPI_Request requests[],
466                      MPI_Status status[])
467 {
468   MPI_Status stat;
469   MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
470   int flag=1;
471   int i;
472   for(i=0; i<count; i++){
473     if(requests[i]!= MPI_REQUEST_NULL){
474       if (smpi_mpi_test(&requests[i], pstat)!=1){
475         flag=0;
476       }
477     }else{
478       smpi_empty_status(pstat);
479     }
480     if(status != MPI_STATUSES_IGNORE) {
481       memcpy(&status[i], pstat, sizeof(*pstat));
482     }
483   }
484   return flag;
485 }
486
487 void smpi_mpi_probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
488   int flag=0;
489   //FIXME find another wait to avoid busy waiting ?
490   // the issue here is that we have to wait on a nonexistent comm
491   while(flag==0){
492     smpi_mpi_iprobe(source, tag, comm, &flag, status);
493     XBT_DEBUG("Busy Waiting on probing : %d", flag);
494     if(!flag) {
495       simcall_process_sleep(0.0001);
496     }
497   }
498 }
499
500 void smpi_mpi_iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
501   MPI_Request request =build_request(NULL, 0, MPI_CHAR, source, smpi_comm_rank(comm), tag,
502             comm, NON_PERSISTENT | RECV);
503
504   // behave like a receive, but don't do it
505   smx_rdv_t mailbox;
506
507   print_request("New iprobe", request);
508   // We have to test both mailboxes as we don't know if we will receive one one or another
509     if (surf_cfg_get_int("smpi/async_small_thres")>0){
510         mailbox = smpi_process_mailbox_small();
511         XBT_DEBUG("trying to probe the perm recv mailbox");
512         request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
513     }
514     if (request->action==NULL){
515         mailbox = smpi_process_mailbox();
516         XBT_DEBUG("trying to probe the other mailbox");
517         request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
518     }
519
520   if(request->action){
521     MPI_Request req = (MPI_Request)SIMIX_comm_get_src_data(request->action);
522     *flag = 1;
523     if(status != MPI_STATUS_IGNORE) {
524       status->MPI_SOURCE = req->src;
525       status->MPI_TAG = req->tag;
526       status->MPI_ERROR = MPI_SUCCESS;
527       status->count = req->real_size;
528     }
529   }
530   else *flag = 0;
531   smpi_mpi_request_free(&request);
532
533   return;
534 }
535
536 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
537 {
538   print_request("Waiting", *request);
539   if ((*request)->action != NULL) { // this is not a detached send
540     simcall_comm_wait((*request)->action, -1.0);
541     finish_wait(request, status);
542   }
543
544   // FIXME for a detached send, finish_wait is not called:
545 }
546
547 int smpi_mpi_waitany(int count, MPI_Request requests[],
548                      MPI_Status * status)
549 {
550   xbt_dynar_t comms;
551   int i, size, index;
552   int *map;
553
554   index = MPI_UNDEFINED;
555   if(count > 0) {
556     // Wait for a request to complete
557     comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
558     map = xbt_new(int, count);
559     size = 0;
560     XBT_DEBUG("Wait for one of");
561     for(i = 0; i < count; i++) {
562       if((requests[i] != MPI_REQUEST_NULL) && (requests[i]->action != NULL)) {
563         print_request("Waiting any ", requests[i]);
564         xbt_dynar_push(comms, &requests[i]->action);
565         map[size] = i;
566         size++;
567       }
568     }
569     if(size > 0) {
570       i = simcall_comm_waitany(comms);
571
572       // not MPI_UNDEFINED, as this is a simix return code
573       if (i != -1) {
574         index = map[i];
575         finish_wait(&requests[index], status);
576       }
577     }
578     xbt_free(map);
579     xbt_dynar_free(&comms);
580   }
581
582   if (index==MPI_UNDEFINED)
583     smpi_empty_status(status);
584
585   return index;
586 }
587
588 int smpi_mpi_waitall(int count, MPI_Request requests[],
589                       MPI_Status status[])
590 {
591   int  index, c;
592   MPI_Status stat;
593   MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
594   int retvalue=MPI_SUCCESS;
595   //tag invalid requests in the set
596   for(c = 0; c < count; c++) {
597     if(requests[c]==MPI_REQUEST_NULL || requests[c]->dst == MPI_PROC_NULL ){
598       if(status != MPI_STATUSES_IGNORE)
599         smpi_empty_status(&status[c]);
600     }else if(requests[c]->src == MPI_PROC_NULL ){
601       if(status != MPI_STATUSES_IGNORE) {
602         smpi_empty_status(&status[c]);
603         status[c].MPI_SOURCE=MPI_PROC_NULL;
604       }
605     }
606   }
607
608   for(c = 0; c < count; c++) {
609       if(MC_is_active()) {
610         smpi_mpi_wait(&requests[c], pstat);
611         index = c;
612       } else {
613         index = smpi_mpi_waitany(count, requests, pstat);
614         if(index == MPI_UNDEFINED) {
615           break;
616        }
617       if(status != MPI_STATUSES_IGNORE) {
618         memcpy(&status[index], pstat, sizeof(*pstat));
619         if(status[index].MPI_ERROR==MPI_ERR_TRUNCATE)retvalue=MPI_ERR_IN_STATUS;
620
621       }
622     }
623   }
624   return retvalue;
625 }
626
627 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
628                       MPI_Status status[])
629 {
630   int i, count, index;
631   MPI_Status stat;
632   MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
633
634   count = 0;
635   for(i = 0; i < incount; i++)
636   {
637     index=smpi_mpi_waitany(incount, requests, pstat);
638     if(index!=MPI_UNDEFINED){
639       indices[count] = index;
640       count++;
641       if(status != MPI_STATUSES_IGNORE) {
642         memcpy(&status[index], pstat, sizeof(*pstat));
643       }
644     }else{
645       return MPI_UNDEFINED;
646     }
647   }
648   return count;
649 }
650
651 int smpi_mpi_testsome(int incount, MPI_Request requests[], int *indices,
652                       MPI_Status status[])
653 {
654   int i, count, count_dead;
655   MPI_Status stat;
656   MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
657
658   count = 0;
659   count_dead = 0;
660   for(i = 0; i < incount; i++) {
661     if((requests[i] != MPI_REQUEST_NULL)) {
662       if(smpi_mpi_test(&requests[i], pstat)) {
663          indices[count] = i;
664          count++;
665          if(status != MPI_STATUSES_IGNORE) {
666             memcpy(&status[i], pstat, sizeof(*pstat));
667          }
668       }
669     }else{
670       count_dead++;
671     }
672   }
673   if(count_dead==incount)return MPI_UNDEFINED;
674   else return count;
675 }
676
677 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
678                     MPI_Comm comm)
679 {
680   // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
681   nary_tree_bcast(buf, count, datatype, root, comm, 4);
682 }
683
684 void smpi_mpi_barrier(MPI_Comm comm)
685 {
686   // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
687   nary_tree_barrier(comm, 4);
688 }
689
690 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
691                      void *recvbuf, int recvcount, MPI_Datatype recvtype,
692                      int root, MPI_Comm comm)
693 {
694   int system_tag = 666;
695   int rank, size, src, index;
696   MPI_Aint lb = 0, recvext = 0;
697   MPI_Request *requests;
698
699   rank = smpi_comm_rank(comm);
700   size = smpi_comm_size(comm);
701   if(rank != root) {
702     // Send buffer to root
703     smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
704   } else {
705     // FIXME: check for errors
706     smpi_datatype_extent(recvtype, &lb, &recvext);
707     // Local copy from root
708     smpi_datatype_copy(sendbuf, sendcount, sendtype,
709                        (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
710     // Receive buffers from senders
711     requests = xbt_new(MPI_Request, size - 1);
712     index = 0;
713     for(src = 0; src < size; src++) {
714       if(src != root) {
715         requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
716                                           recvcount, recvtype,
717                                           src, system_tag, comm);
718         index++;
719       }
720     }
721     // Wait for completion of irecv's.
722     smpi_mpi_startall(size - 1, requests);
723     smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
724     xbt_free(requests);
725   }
726 }
727
728 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
729                       void *recvbuf, int *recvcounts, int *displs,
730                       MPI_Datatype recvtype, int root, MPI_Comm comm)
731 {
732   int system_tag = 666;
733   int rank, size, src, index;
734   MPI_Aint lb = 0, recvext = 0;
735   MPI_Request *requests;
736
737   rank = smpi_comm_rank(comm);
738   size = smpi_comm_size(comm);
739   if(rank != root) {
740     // Send buffer to root
741     smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
742   } else {
743     // FIXME: check for errors
744     smpi_datatype_extent(recvtype, &lb, &recvext);
745     // Local copy from root
746     smpi_datatype_copy(sendbuf, sendcount, sendtype,
747                        (char *)recvbuf + displs[root] * recvext,
748                        recvcounts[root], recvtype);
749     // Receive buffers from senders
750     requests = xbt_new(MPI_Request, size - 1);
751     index = 0;
752     for(src = 0; src < size; src++) {
753       if(src != root) {
754         requests[index] =
755           smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
756                           recvcounts[src], recvtype, src, system_tag, comm);
757         index++;
758       }
759     }
760     // Wait for completion of irecv's.
761     smpi_mpi_startall(size - 1, requests);
762     smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
763     xbt_free(requests);
764   }
765 }
766
767 void smpi_mpi_allgather(void *sendbuf, int sendcount,
768                         MPI_Datatype sendtype, void *recvbuf,
769                         int recvcount, MPI_Datatype recvtype,
770                         MPI_Comm comm)
771 {
772   int system_tag = 666;
773   int rank, size, other, index;
774   MPI_Aint lb = 0, recvext = 0;
775   MPI_Request *requests;
776
777   rank = smpi_comm_rank(comm);
778   size = smpi_comm_size(comm);
779   // FIXME: check for errors
780   smpi_datatype_extent(recvtype, &lb, &recvext);
781   // Local copy from self
782   smpi_datatype_copy(sendbuf, sendcount, sendtype,
783                      (char *)recvbuf + rank * recvcount * recvext, recvcount,
784                      recvtype);
785   // Send/Recv buffers to/from others;
786   requests = xbt_new(MPI_Request, 2 * (size - 1));
787   index = 0;
788   for(other = 0; other < size; other++) {
789     if(other != rank) {
790       requests[index] =
791         smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
792                         comm);
793       index++;
794       requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
795                                         recvcount, recvtype, other,
796                                         system_tag, comm);
797       index++;
798     }
799   }
800   // Wait for completion of all comms.
801   smpi_mpi_startall(2 * (size - 1), requests);
802   smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
803   xbt_free(requests);
804 }
805
806 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
807                          MPI_Datatype sendtype, void *recvbuf,
808                          int *recvcounts, int *displs,
809                          MPI_Datatype recvtype, MPI_Comm comm)
810 {
811   int system_tag = 666;
812   int rank, size, other, index;
813   MPI_Aint lb = 0, recvext = 0;
814   MPI_Request *requests;
815
816   rank = smpi_comm_rank(comm);
817   size = smpi_comm_size(comm);
818   // FIXME: check for errors
819   smpi_datatype_extent(recvtype, &lb, &recvext);
820   // Local copy from self
821   smpi_datatype_copy(sendbuf, sendcount, sendtype,
822                      (char *)recvbuf + displs[rank] * recvext,
823                      recvcounts[rank], recvtype);
824   // Send buffers to others;
825   requests = xbt_new(MPI_Request, 2 * (size - 1));
826   index = 0;
827   for(other = 0; other < size; other++) {
828     if(other != rank) {
829       requests[index] =
830         smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
831                         comm);
832       index++;
833       requests[index] =
834         smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
835                         recvtype, other, system_tag, comm);
836       index++;
837     }
838   }
839   // Wait for completion of all comms.
840   smpi_mpi_startall(2 * (size - 1), requests);
841   smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
842   xbt_free(requests);
843 }
844
845 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
846                       void *recvbuf, int recvcount, MPI_Datatype recvtype,
847                       int root, MPI_Comm comm)
848 {
849   int system_tag = 666;
850   int rank, size, dst, index;
851   MPI_Aint lb = 0, sendext = 0;
852   MPI_Request *requests;
853
854   rank = smpi_comm_rank(comm);
855   size = smpi_comm_size(comm);
856   if(rank != root) {
857     // Recv buffer from root
858     smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
859                   MPI_STATUS_IGNORE);
860   } else {
861     // FIXME: check for errors
862     smpi_datatype_extent(sendtype, &lb, &sendext);
863     // Local copy from root
864     smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
865                        sendcount, sendtype, recvbuf, recvcount, recvtype);
866     // Send buffers to receivers
867     requests = xbt_new(MPI_Request, size - 1);
868     index = 0;
869     for(dst = 0; dst < size; dst++) {
870       if(dst != root) {
871         requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
872                                           sendcount, sendtype, dst,
873                                           system_tag, comm);
874         index++;
875       }
876     }
877     // Wait for completion of isend's.
878     smpi_mpi_startall(size - 1, requests);
879     smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
880     xbt_free(requests);
881   }
882 }
883
884 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
885                        MPI_Datatype sendtype, void *recvbuf, int recvcount,
886                        MPI_Datatype recvtype, int root, MPI_Comm comm)
887 {
888   int system_tag = 666;
889   int rank, size, dst, index;
890   MPI_Aint lb = 0, sendext = 0;
891   MPI_Request *requests;
892
893   rank = smpi_comm_rank(comm);
894   size = smpi_comm_size(comm);
895   if(rank != root) {
896     // Recv buffer from root
897     smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
898                   MPI_STATUS_IGNORE);
899   } else {
900     // FIXME: check for errors
901     smpi_datatype_extent(sendtype, &lb, &sendext);
902     // Local copy from root
903     smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
904                        sendtype, recvbuf, recvcount, recvtype);
905     // Send buffers to receivers
906     requests = xbt_new(MPI_Request, size - 1);
907     index = 0;
908     for(dst = 0; dst < size; dst++) {
909       if(dst != root) {
910         requests[index] =
911           smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
912                           sendtype, dst, system_tag, comm);
913         index++;
914       }
915     }
916     // Wait for completion of isend's.
917     smpi_mpi_startall(size - 1, requests);
918     smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
919     xbt_free(requests);
920   }
921 }
922
923 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
924                      MPI_Datatype datatype, MPI_Op op, int root,
925                      MPI_Comm comm)
926 {
927   int system_tag = 666;
928   int rank, size, src, index;
929   MPI_Aint lb = 0, dataext = 0;
930   MPI_Request *requests;
931   void **tmpbufs;
932
933   rank = smpi_comm_rank(comm);
934   size = smpi_comm_size(comm);
935   if(rank != root) {
936     // Send buffer to root
937     smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
938   } else {
939     // FIXME: check for errors
940     smpi_datatype_extent(datatype, &lb, &dataext);
941     // Local copy from root
942     if (sendbuf && recvbuf)
943       smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
944     // Receive buffers from senders
945     //TODO: make a MPI_barrier here ?
946     requests = xbt_new(MPI_Request, size - 1);
947     tmpbufs = xbt_new(void *, size - 1);
948     index = 0;
949     for(src = 0; src < size; src++) {
950       if(src != root) {
951         // FIXME: possibly overkill we we have contiguous/noncontiguous data
952         //  mapping...
953         tmpbufs[index] = xbt_malloc(count * dataext);
954         requests[index] =
955           smpi_irecv_init(tmpbufs[index], count, datatype, src,
956                           system_tag, comm);
957         index++;
958       }
959     }
960     // Wait for completion of irecv's.
961     smpi_mpi_startall(size - 1, requests);
962     for(src = 0; src < size - 1; src++) {
963       index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
964       XBT_DEBUG("finished waiting any request with index %d", index);
965       if(index == MPI_UNDEFINED) {
966         break;
967       }
968       if(op) /* op can be MPI_OP_NULL that does nothing */
969         smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
970     }
971     for(index = 0; index < size - 1; index++) {
972       xbt_free(tmpbufs[index]);
973     }
974     xbt_free(tmpbufs);
975     xbt_free(requests);
976   }
977 }
978
979 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
980                         MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
981 {
982   smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
983   smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
984 }
985
986 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
987                    MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
988 {
989   int system_tag = 666;
990   int rank, size, other, index;
991   MPI_Aint lb = 0, dataext = 0;
992   MPI_Request *requests;
993   void **tmpbufs;
994
995   rank = smpi_comm_rank(comm);
996   size = smpi_comm_size(comm);
997
998   // FIXME: check for errors
999   smpi_datatype_extent(datatype, &lb, &dataext);
1000
1001   // Local copy from self
1002   smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
1003
1004   // Send/Recv buffers to/from others;
1005   requests = xbt_new(MPI_Request, size - 1);
1006   tmpbufs = xbt_new(void *, rank);
1007   index = 0;
1008   for(other = 0; other < rank; other++) {
1009     // FIXME: possibly overkill we we have contiguous/noncontiguous data
1010     // mapping...
1011     tmpbufs[index] = xbt_malloc(count * dataext);
1012     requests[index] =
1013       smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
1014                       comm);
1015     index++;
1016   }
1017   for(other = rank + 1; other < size; other++) {
1018     requests[index] =
1019       smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1020     index++;
1021   }
1022   // Wait for completion of all comms.
1023   smpi_mpi_startall(size - 1, requests);
1024   for(other = 0; other < size - 1; other++) {
1025     index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1026     if(index == MPI_UNDEFINED) {
1027       break;
1028     }
1029     if(index < rank) {
1030       // #Request is below rank: it's a irecv
1031       smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1032     }
1033   }
1034   for(index = 0; index < rank; index++) {
1035     xbt_free(tmpbufs[index]);
1036   }
1037   xbt_free(tmpbufs);
1038   xbt_free(requests);
1039 }