1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
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. */
10 #include "xbt/replay.h"
12 #include "simix/smx_private.h"
13 #include "surf/surf.h"
14 #include "simgrid/sg_config.h"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi, "Logging specific to SMPI (base)");
20 static int match_recv(void* a, void* b, smx_action_t ignored) {
21 MPI_Request ref = (MPI_Request)a;
22 MPI_Request req = (MPI_Request)b;
23 XBT_DEBUG("Trying to match a recv of src %d against %d, tag %d against %d",ref->src,req->src, ref->tag, req->tag);
25 xbt_assert(ref, "Cannot match recv against null reference");
26 xbt_assert(req, "Cannot match recv against null request");
27 if((ref->src == MPI_ANY_SOURCE || req->src == ref->src)
28 && (ref->tag == MPI_ANY_TAG || req->tag == ref->tag)){
29 //we match, we can transfer some values
30 // FIXME : move this to the copy function ?
31 if(ref->src == MPI_ANY_SOURCE)ref->real_src = req->src;
32 if(ref->tag == MPI_ANY_TAG)ref->real_tag = req->tag;
33 if(ref->real_size < req->real_size) ref->truncated = 1;
35 ref->detached_sender=req; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
41 static int match_send(void* a, void* b,smx_action_t ignored) {
42 MPI_Request ref = (MPI_Request)a;
43 MPI_Request req = (MPI_Request)b;
44 XBT_DEBUG("Trying to match a send of src %d against %d, tag %d against %d",ref->src,req->src, ref->tag, req->tag);
45 xbt_assert(ref, "Cannot match send against null reference");
46 xbt_assert(req, "Cannot match send against null request");
48 if((req->src == MPI_ANY_SOURCE || req->src == ref->src)
49 && (req->tag == MPI_ANY_TAG || req->tag == ref->tag))
51 if(req->src == MPI_ANY_SOURCE)req->real_src = ref->src;
52 if(req->tag == MPI_ANY_TAG)req->real_tag = ref->tag;
53 if(req->real_size < ref->real_size) req->truncated = 1;
55 req->detached_sender=ref; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
63 typedef struct s_smpi_factor *smpi_factor_t;
64 typedef struct s_smpi_factor {
67 double values[4];//arbitrary set to 4
69 xbt_dynar_t smpi_os_values = NULL;
70 xbt_dynar_t smpi_or_values = NULL;
71 xbt_dynar_t smpi_ois_values = NULL;
73 // Methods used to parse and store the values for timing injections in smpi
74 // These are taken from surf/network.c and generalized to have more factors
75 // These methods should be merged with those in surf/network.c (moved somewhere in xbt ?)
77 static int factor_cmp(const void *pa, const void *pb)
79 return (((s_smpi_factor_t*)pa)->factor > ((s_smpi_factor_t*)pb)->factor);
83 static xbt_dynar_t parse_factor(const char *smpi_coef_string)
86 unsigned int iter = 0;
89 xbt_dynar_t smpi_factor, radical_elements, radical_elements2 = NULL;
91 smpi_factor = xbt_dynar_new(sizeof(s_smpi_factor_t), NULL);
92 radical_elements = xbt_str_split(smpi_coef_string, ";");
93 xbt_dynar_foreach(radical_elements, iter, value) {
95 radical_elements2 = xbt_str_split(value, ":");
96 if (xbt_dynar_length(radical_elements2) <2 || xbt_dynar_length(radical_elements2) > 5)
97 xbt_die("Malformed radical for smpi factor!");
98 for(i =0; i<xbt_dynar_length(radical_elements2);i++ ){
100 fact.factor = atol(xbt_dynar_get_as(radical_elements2, i, char *));
102 fact.values[fact.nb_values] = atof(xbt_dynar_get_as(radical_elements2, i, char *));
107 xbt_dynar_push_as(smpi_factor, s_smpi_factor_t, fact);
108 XBT_DEBUG("smpi_factor:\t%ld : %d values, first: %f", fact.factor, fact.nb_values ,fact.values[0]);
109 xbt_dynar_free(&radical_elements2);
111 xbt_dynar_free(&radical_elements);
113 xbt_dynar_sort(smpi_factor, &factor_cmp);
114 xbt_dynar_foreach(smpi_factor, iter, fact) {
115 XBT_DEBUG("smpi_factor:\t%ld : %d values, first: %f", fact.factor, fact.nb_values ,fact.values[0]);
120 static double smpi_os(double size)
124 parse_factor(sg_cfg_get_string("smpi/os"));
126 unsigned int iter = 0;
127 s_smpi_factor_t fact;
129 xbt_dynar_foreach(smpi_os_values, iter, fact) {
130 if (size <= fact.factor) {
131 XBT_DEBUG("os : %lf <= %ld return %f", size, fact.factor, current);
134 current=fact.values[0]+fact.values[1]*size;
137 XBT_DEBUG("os : %lf > %ld return %f", size, fact.factor, current);
142 static double smpi_ois(double size)
144 if (!smpi_ois_values)
146 parse_factor(sg_cfg_get_string("smpi/ois"));
148 unsigned int iter = 0;
149 s_smpi_factor_t fact;
151 xbt_dynar_foreach(smpi_ois_values, iter, fact) {
152 if (size <= fact.factor) {
153 XBT_DEBUG("ois : %lf <= %ld return %f", size, fact.factor, current);
156 current=fact.values[0]+fact.values[1]*size;
159 XBT_DEBUG("ois : %lf > %ld return %f", size, fact.factor, current);
164 static double smpi_or(double size)
168 parse_factor(sg_cfg_get_string("smpi/or"));
170 unsigned int iter = 0;
171 s_smpi_factor_t fact;
173 xbt_dynar_foreach(smpi_or_values, iter, fact) {
174 if (size <= fact.factor) {
175 XBT_DEBUG("or : %lf <= %ld return %f", size, fact.factor, current);
178 current=fact.values[0]+fact.values[1]*size;
180 XBT_DEBUG("or : %lf > %ld return %f", size, fact.factor, current);
185 static MPI_Request build_request(void *buf, int count,
186 MPI_Datatype datatype, int src, int dst,
187 int tag, MPI_Comm comm, unsigned flags)
191 void *old_buf = NULL;
193 request = xbt_new(s_smpi_mpi_request_t, 1);
195 s_smpi_subtype_t *subtype = datatype->substruct;
197 if(datatype->has_subtype == 1){
198 // This part handles the problem of non-contiguous memory
200 buf = xbt_malloc(count*smpi_datatype_size(datatype));
202 subtype->serialize(old_buf, buf, count, datatype->substruct);
207 // This part handles the problem of non-contiguous memory (for the
208 // unserialisation at the reception)
209 request->old_buf = old_buf;
210 request->old_type = datatype;
212 request->size = smpi_datatype_size(datatype) * count;
216 request->comm = comm;
217 request->action = NULL;
218 request->flags = flags;
219 request->detached = 0;
220 request->detached_sender = NULL;
222 request->truncated = 0;
223 request->real_size = 0;
224 request->real_tag = 0;
231 if (flags & SEND) smpi_datatype_unuse(datatype);
237 void smpi_empty_status(MPI_Status * status)
239 if(status != MPI_STATUS_IGNORE) {
240 status->MPI_SOURCE = MPI_ANY_SOURCE;
241 status->MPI_TAG = MPI_ANY_TAG;
242 status->MPI_ERROR = MPI_SUCCESS;
247 void smpi_action_trace_run(char *path)
251 xbt_dict_cursor_t cursor;
255 action_fp = fopen(path, "r");
256 xbt_assert(action_fp != NULL, "Cannot open %s: %s", path,
260 if (!xbt_dict_is_empty(action_queues)) {
262 ("Not all actions got consumed. If the simulation ended successfully (without deadlock), you may want to add new processes to your deployment file.");
265 xbt_dict_foreach(action_queues, cursor, name, todo) {
266 XBT_WARN("Still %lu actions for %s", xbt_dynar_length(todo), name);
272 xbt_dict_free(&action_queues);
273 action_queues = xbt_dict_new_homogeneous(NULL);
276 static void smpi_mpi_request_free_voidp(void* request)
278 MPI_Request req = request;
279 smpi_mpi_request_free(&req);
282 /* MPI Low level calls */
283 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
284 int dst, int tag, MPI_Comm comm)
286 MPI_Request request =
287 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
288 comm, PERSISTENT | SEND);
293 MPI_Request smpi_mpi_ssend_init(void *buf, int count, MPI_Datatype datatype,
294 int dst, int tag, MPI_Comm comm)
296 MPI_Request request =
297 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
298 comm, PERSISTENT | SSEND | SEND);
303 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
304 int src, int tag, MPI_Comm comm)
306 MPI_Request request =
307 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
308 comm, PERSISTENT | RECV);
313 void smpi_mpi_start(MPI_Request request)
317 xbt_assert(!request->action,
318 "Cannot (re)start a non-finished communication");
319 if(request->flags & RECV) {
320 print_request("New recv", request);
321 if (request->size < sg_cfg_get_int("smpi/async_small_thres"))
322 mailbox = smpi_process_mailbox_small();
324 mailbox = smpi_process_mailbox();
325 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
326 request->real_size=request->size;
327 smpi_datatype_use(request->old_type);
328 request->action = simcall_comm_irecv(mailbox, request->buf, &request->real_size, &match_recv, request);
330 //integrate pseudo-timing for buffering of small messages, do not bother to execute the simcall if 0
331 double sleeptime = request->detached ? smpi_or(request->size) : 0.0;
333 simcall_process_sleep(sleeptime);
334 XBT_DEBUG("receiving size of %zu : sleep %lf ", request->size, smpi_or(request->size));
339 int receiver = smpi_group_index(smpi_comm_group(request->comm), request->dst);
340 /* if(receiver == MPI_UNDEFINED) {*/
341 /* XBT_WARN("Trying to send a message to a wrong rank");*/
344 print_request("New send", request);
345 if (request->size < sg_cfg_get_int("smpi/async_small_thres")) { // eager mode
346 mailbox = smpi_process_remote_mailbox_small(receiver);
348 XBT_DEBUG("Send request %p is not in the permanent receive mailbox (buf: %p)",request,request->buf);
349 mailbox = smpi_process_remote_mailbox(receiver);
351 if ( (! (request->flags & SSEND)) && (request->size < sg_cfg_get_int("smpi/send_is_detached_thres"))) {
353 request->detached = 1;
355 if(request->old_type->has_subtype == 0){
356 oldbuf = request->buf;
358 request->buf = xbt_malloc(request->size);
359 memcpy(request->buf,oldbuf,request->size);
362 XBT_DEBUG("Send request %p is detached; buf %p copied into %p",request,oldbuf,request->buf);
364 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
365 request->real_size=request->size;
366 smpi_datatype_use(request->old_type);
368 //if we are giving back the control to the user without waiting for completion, we have to inject timings
369 double sleeptime =0.0;
370 if(request->detached || (request->flags & (ISEND|SSEND))){// issend should be treated as isend
371 //isend and send timings may be different
372 sleeptime = (request->flags & ISEND)? smpi_ois(request->size) : smpi_os(request->size);
376 simcall_process_sleep(sleeptime);
377 XBT_DEBUG("sending size of %zu : sleep %lf ", request->size, smpi_os(request->size));
381 simcall_comm_isend(mailbox, request->size, -1.0,
382 request->buf, request->real_size,
384 &smpi_mpi_request_free_voidp, // how to free the userdata if a detached send fails
386 // detach if msg size < eager/rdv switch limit
390 /* FIXME: detached sends are not traceable (request->action == NULL) */
392 simcall_set_category(request->action, TRACE_internal_smpi_get_category());
399 void smpi_mpi_startall(int count, MPI_Request * requests)
403 for(i = 0; i < count; i++) {
404 smpi_mpi_start(requests[i]);
408 void smpi_mpi_request_free(MPI_Request * request)
411 if((*request) != MPI_REQUEST_NULL){
412 (*request)->refcount--;
413 if((*request)->refcount<0) xbt_die("wrong refcount");
415 if((*request)->refcount==0){
417 *request = MPI_REQUEST_NULL;
420 xbt_die("freeing an already free request");
424 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
425 int dst, int tag, MPI_Comm comm)
427 MPI_Request request =
428 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
429 comm, NON_PERSISTENT | SEND);
434 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
435 int dst, int tag, MPI_Comm comm)
437 MPI_Request request =
438 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
439 comm, NON_PERSISTENT | ISEND | SEND);
441 smpi_mpi_start(request);
445 MPI_Request smpi_mpi_issend(void *buf, int count, MPI_Datatype datatype,
446 int dst, int tag, MPI_Comm comm)
448 MPI_Request request =
449 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
450 comm, NON_PERSISTENT | ISEND | SSEND | SEND);
451 smpi_mpi_start(request);
457 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
458 int src, int tag, MPI_Comm comm)
460 MPI_Request request =
461 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
462 comm, NON_PERSISTENT | RECV);
466 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
467 int src, int tag, MPI_Comm comm)
469 MPI_Request request =
470 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
471 comm, NON_PERSISTENT | RECV);
473 smpi_mpi_start(request);
477 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
478 int tag, MPI_Comm comm, MPI_Status * status)
481 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
482 smpi_mpi_wait(&request, status);
487 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
488 int tag, MPI_Comm comm)
490 MPI_Request request =
491 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
492 comm, NON_PERSISTENT | SEND);
494 smpi_mpi_start(request);
495 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
499 void smpi_mpi_ssend(void *buf, int count, MPI_Datatype datatype,
500 int dst, int tag, MPI_Comm comm)
502 MPI_Request request = smpi_mpi_issend(buf, count, datatype, dst, tag, comm);
503 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
506 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
507 int dst, int sendtag, void *recvbuf, int recvcount,
508 MPI_Datatype recvtype, int src, int recvtag,
509 MPI_Comm comm, MPI_Status * status)
511 MPI_Request requests[2];
515 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
517 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
518 smpi_mpi_startall(2, requests);
519 smpi_mpi_waitall(2, requests, stats);
520 if(status != MPI_STATUS_IGNORE) {
521 // Copy receive status
526 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
528 return status->count / smpi_datatype_size(datatype);
531 static void finish_wait(MPI_Request * request, MPI_Status * status)
533 MPI_Request req = *request;
534 if(status != MPI_STATUS_IGNORE)
535 smpi_empty_status(status);
537 if(!(req->detached && req->flags & SEND)){
538 if(status != MPI_STATUS_IGNORE) {
539 status->MPI_SOURCE = req->src == MPI_ANY_SOURCE ? req->real_src : req->src;
540 status->MPI_TAG = req->tag == MPI_ANY_TAG ? req->real_tag : req->tag;
541 status->MPI_ERROR = req->truncated ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
542 // this handles the case were size in receive differs from size in send
543 // FIXME: really this should just contain the count of receive-type blocks,
545 status->count = req->real_size;
548 print_request("Finishing", req);
549 MPI_Datatype datatype = req->old_type;
551 if(datatype->has_subtype == 1){
552 // This part handles the problem of non-contignous memory
553 // the unserialization at the reception
554 s_smpi_subtype_t *subtype = datatype->substruct;
555 if(req->flags & RECV) {
556 subtype->unserialize(req->buf, req->old_buf, req->real_size/smpi_datatype_size(datatype) , datatype->substruct);
558 if(req->detached == 0) free(req->buf);
560 smpi_datatype_unuse(datatype);
563 if(req->detached_sender!=NULL){
564 smpi_mpi_request_free(&(req->detached_sender));
567 if(req->flags & NON_PERSISTENT) {
568 smpi_mpi_request_free(request);
574 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
577 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or smpi_mpi_testall before)
578 if ((*request)->action == NULL)
581 flag = simcall_comm_test((*request)->action);
583 (*request)->refcount++;
584 finish_wait(request, status);
586 smpi_empty_status(status);
591 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
598 *index = MPI_UNDEFINED;
601 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
602 map = xbt_new(int, count);
604 for(i = 0; i < count; i++) {
605 if((requests[i]!=MPI_REQUEST_NULL) && requests[i]->action) {
606 xbt_dynar_push(comms, &requests[i]->action);
612 i = simcall_comm_testany(comms);
613 // not MPI_UNDEFINED, as this is a simix return code
616 finish_wait(&requests[*index], status);
620 //all requests are null or inactive, return true
622 smpi_empty_status(status);
625 xbt_dynar_free(&comms);
632 int smpi_mpi_testall(int count, MPI_Request requests[],
636 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
639 for(i=0; i<count; i++){
640 if(requests[i]!= MPI_REQUEST_NULL){
641 if (smpi_mpi_test(&requests[i], pstat)!=1){
645 smpi_empty_status(pstat);
647 if(status != MPI_STATUSES_IGNORE) {
654 void smpi_mpi_probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
656 //FIXME find another wait to avoid busy waiting ?
657 // the issue here is that we have to wait on a nonexistent comm
659 smpi_mpi_iprobe(source, tag, comm, &flag, status);
660 XBT_DEBUG("Busy Waiting on probing : %d", flag);
662 simcall_process_sleep(0.0001);
667 void smpi_mpi_iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
668 MPI_Request request =build_request(NULL, 0, MPI_CHAR, source, smpi_comm_rank(comm), tag,
669 comm, NON_PERSISTENT | RECV);
671 // behave like a receive, but don't do it
674 print_request("New iprobe", request);
675 // We have to test both mailboxes as we don't know if we will receive one one or another
676 if (sg_cfg_get_int("smpi/async_small_thres")>0){
677 mailbox = smpi_process_mailbox_small();
678 XBT_DEBUG("trying to probe the perm recv mailbox");
679 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
681 if (request->action==NULL){
682 mailbox = smpi_process_mailbox();
683 XBT_DEBUG("trying to probe the other mailbox");
684 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
688 MPI_Request req = (MPI_Request)SIMIX_comm_get_src_data(request->action);
690 if(status != MPI_STATUS_IGNORE) {
691 status->MPI_SOURCE = req->src;
692 status->MPI_TAG = req->tag;
693 status->MPI_ERROR = MPI_SUCCESS;
694 status->count = req->real_size;
698 smpi_mpi_request_free(&request);
703 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
705 print_request("Waiting", *request);
706 if ((*request)->action != NULL) { // this is not a detached send
707 simcall_comm_wait((*request)->action, -1.0);
709 finish_wait(request, status);
711 // FIXME for a detached send, finish_wait is not called:
714 int smpi_mpi_waitany(int count, MPI_Request requests[],
721 index = MPI_UNDEFINED;
723 // Wait for a request to complete
724 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
725 map = xbt_new(int, count);
727 XBT_DEBUG("Wait for one of %d", count);
728 for(i = 0; i < count; i++) {
729 if(requests[i] != MPI_REQUEST_NULL) {
730 if (requests[i]->action != NULL) {
731 XBT_DEBUG("Waiting any %p ", requests[i]);
732 xbt_dynar_push(comms, &requests[i]->action);
736 //This is a finished detached request, let's return this one
737 size=0;//so we free the dynar but don't do the waitany call
739 finish_wait(&requests[i], status);//cleanup if refcount = 0
740 requests[i]=MPI_REQUEST_NULL;//set to null
746 i = simcall_comm_waitany(comms);
748 // not MPI_UNDEFINED, as this is a simix return code
751 finish_wait(&requests[index], status);
755 xbt_dynar_free(&comms);
758 if (index==MPI_UNDEFINED)
759 smpi_empty_status(status);
764 int smpi_mpi_waitall(int count, MPI_Request requests[],
769 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
770 int retvalue = MPI_SUCCESS;
771 //tag invalid requests in the set
772 if (status != MPI_STATUSES_IGNORE) {
773 for (c = 0; c < count; c++) {
774 if (requests[c] == MPI_REQUEST_NULL || requests[c]->dst == MPI_PROC_NULL) {
775 smpi_empty_status(&status[c]);
776 } else if (requests[c]->src == MPI_PROC_NULL) {
777 smpi_empty_status(&status[c]);
778 status[c].MPI_SOURCE = MPI_PROC_NULL;
782 for(c = 0; c < count; c++) {
783 if (MC_is_active()) {
784 smpi_mpi_wait(&requests[c], pstat);
787 index = smpi_mpi_waitany(count, requests, pstat);
788 if (index == MPI_UNDEFINED)
791 if (status != MPI_STATUSES_IGNORE) {
792 status[index] = *pstat;
793 if (status[index].MPI_ERROR == MPI_ERR_TRUNCATE)
794 retvalue = MPI_ERR_IN_STATUS;
801 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
806 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
809 for(i = 0; i < incount; i++)
811 index=smpi_mpi_waitany(incount, requests, pstat);
812 if(index!=MPI_UNDEFINED){
813 indices[count] = index;
815 if(status != MPI_STATUSES_IGNORE) {
816 status[index] = *pstat;
819 return MPI_UNDEFINED;
825 int smpi_mpi_testsome(int incount, MPI_Request requests[], int *indices,
828 int i, count, count_dead;
830 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
834 for(i = 0; i < incount; i++) {
835 if((requests[i] != MPI_REQUEST_NULL)) {
836 if(smpi_mpi_test(&requests[i], pstat)) {
839 if(status != MPI_STATUSES_IGNORE) {
847 if(count_dead==incount)return MPI_UNDEFINED;
851 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
854 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
855 nary_tree_bcast(buf, count, datatype, root, comm, 4);
858 void smpi_mpi_barrier(MPI_Comm comm)
860 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
861 nary_tree_barrier(comm, 4);
864 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
865 void *recvbuf, int recvcount, MPI_Datatype recvtype,
866 int root, MPI_Comm comm)
868 int system_tag = 666;
869 int rank, size, src, index;
870 MPI_Aint lb = 0, recvext = 0;
871 MPI_Request *requests;
873 rank = smpi_comm_rank(comm);
874 size = smpi_comm_size(comm);
876 // Send buffer to root
877 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
879 // FIXME: check for errors
880 smpi_datatype_extent(recvtype, &lb, &recvext);
881 // Local copy from root
882 smpi_datatype_copy(sendbuf, sendcount, sendtype,
883 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
884 // Receive buffers from senders
885 requests = xbt_new(MPI_Request, size - 1);
887 for(src = 0; src < size; src++) {
889 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
891 src, system_tag, comm);
895 // Wait for completion of irecv's.
896 smpi_mpi_startall(size - 1, requests);
897 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
902 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
903 void *recvbuf, int *recvcounts, int *displs,
904 MPI_Datatype recvtype, int root, MPI_Comm comm)
906 int system_tag = 666;
907 int rank, size, src, index;
908 MPI_Aint lb = 0, recvext = 0;
909 MPI_Request *requests;
911 rank = smpi_comm_rank(comm);
912 size = smpi_comm_size(comm);
914 // Send buffer to root
915 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
917 // FIXME: check for errors
918 smpi_datatype_extent(recvtype, &lb, &recvext);
919 // Local copy from root
920 smpi_datatype_copy(sendbuf, sendcount, sendtype,
921 (char *)recvbuf + displs[root] * recvext,
922 recvcounts[root], recvtype);
923 // Receive buffers from senders
924 requests = xbt_new(MPI_Request, size - 1);
926 for(src = 0; src < size; src++) {
929 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
930 recvcounts[src], recvtype, src, system_tag, comm);
934 // Wait for completion of irecv's.
935 smpi_mpi_startall(size - 1, requests);
936 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
941 void smpi_mpi_allgather(void *sendbuf, int sendcount,
942 MPI_Datatype sendtype, void *recvbuf,
943 int recvcount, MPI_Datatype recvtype,
946 int system_tag = 666;
947 int rank, size, other, index;
948 MPI_Aint lb = 0, recvext = 0;
949 MPI_Request *requests;
951 rank = smpi_comm_rank(comm);
952 size = smpi_comm_size(comm);
953 // FIXME: check for errors
954 smpi_datatype_extent(recvtype, &lb, &recvext);
955 // Local copy from self
956 smpi_datatype_copy(sendbuf, sendcount, sendtype,
957 (char *)recvbuf + rank * recvcount * recvext, recvcount,
959 // Send/Recv buffers to/from others;
960 requests = xbt_new(MPI_Request, 2 * (size - 1));
962 for(other = 0; other < size; other++) {
965 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
968 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
969 recvcount, recvtype, other,
974 // Wait for completion of all comms.
975 smpi_mpi_startall(2 * (size - 1), requests);
976 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
980 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
981 MPI_Datatype sendtype, void *recvbuf,
982 int *recvcounts, int *displs,
983 MPI_Datatype recvtype, MPI_Comm comm)
985 int system_tag = 666;
986 int rank, size, other, index;
987 MPI_Aint lb = 0, recvext = 0;
988 MPI_Request *requests;
990 rank = smpi_comm_rank(comm);
991 size = smpi_comm_size(comm);
992 // FIXME: check for errors
993 smpi_datatype_extent(recvtype, &lb, &recvext);
994 // Local copy from self
995 smpi_datatype_copy(sendbuf, sendcount, sendtype,
996 (char *)recvbuf + displs[rank] * recvext,
997 recvcounts[rank], recvtype);
998 // Send buffers to others;
999 requests = xbt_new(MPI_Request, 2 * (size - 1));
1001 for(other = 0; other < size; other++) {
1004 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
1008 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
1009 recvtype, other, system_tag, comm);
1013 // Wait for completion of all comms.
1014 smpi_mpi_startall(2 * (size - 1), requests);
1015 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
1019 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1020 void *recvbuf, int recvcount, MPI_Datatype recvtype,
1021 int root, MPI_Comm comm)
1023 int system_tag = 666;
1024 int rank, size, dst, index;
1025 MPI_Aint lb = 0, sendext = 0;
1026 MPI_Request *requests;
1028 rank = smpi_comm_rank(comm);
1029 size = smpi_comm_size(comm);
1031 // Recv buffer from root
1032 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
1035 // FIXME: check for errors
1036 smpi_datatype_extent(sendtype, &lb, &sendext);
1037 // Local copy from root
1038 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
1039 sendcount, sendtype, recvbuf, recvcount, recvtype);
1040 // Send buffers to receivers
1041 requests = xbt_new(MPI_Request, size - 1);
1043 for(dst = 0; dst < size; dst++) {
1045 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
1046 sendcount, sendtype, dst,
1051 // Wait for completion of isend's.
1052 smpi_mpi_startall(size - 1, requests);
1053 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1058 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
1059 MPI_Datatype sendtype, void *recvbuf, int recvcount,
1060 MPI_Datatype recvtype, int root, MPI_Comm comm)
1062 int system_tag = 666;
1063 int rank, size, dst, index;
1064 MPI_Aint lb = 0, sendext = 0;
1065 MPI_Request *requests;
1067 rank = smpi_comm_rank(comm);
1068 size = smpi_comm_size(comm);
1070 // Recv buffer from root
1071 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
1074 // FIXME: check for errors
1075 smpi_datatype_extent(sendtype, &lb, &sendext);
1076 // Local copy from root
1077 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
1078 sendtype, recvbuf, recvcount, recvtype);
1079 // Send buffers to receivers
1080 requests = xbt_new(MPI_Request, size - 1);
1082 for(dst = 0; dst < size; dst++) {
1085 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
1086 sendtype, dst, system_tag, comm);
1090 // Wait for completion of isend's.
1091 smpi_mpi_startall(size - 1, requests);
1092 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1097 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
1098 MPI_Datatype datatype, MPI_Op op, int root,
1101 int system_tag = 666;
1102 int rank, size, src, index;
1103 MPI_Aint lb = 0, dataext = 0;
1104 MPI_Request *requests;
1107 rank = smpi_comm_rank(comm);
1108 size = smpi_comm_size(comm);
1110 // Send buffer to root
1111 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
1113 // FIXME: check for errors
1114 smpi_datatype_extent(datatype, &lb, &dataext);
1115 // Local copy from root
1116 if (sendbuf && recvbuf)
1117 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
1118 // Receive buffers from senders
1119 //TODO: make a MPI_barrier here ?
1120 requests = xbt_new(MPI_Request, size - 1);
1121 tmpbufs = xbt_new(void *, size - 1);
1123 for(src = 0; src < size; src++) {
1125 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1127 tmpbufs[index] = xbt_malloc(count * dataext);
1129 smpi_irecv_init(tmpbufs[index], count, datatype, src,
1134 // Wait for completion of irecv's.
1135 smpi_mpi_startall(size - 1, requests);
1136 for(src = 0; src < size - 1; src++) {
1137 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1138 XBT_DEBUG("finished waiting any request with index %d", index);
1139 if(index == MPI_UNDEFINED) {
1142 if(op) /* op can be MPI_OP_NULL that does nothing */
1143 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1145 for(index = 0; index < size - 1; index++) {
1146 xbt_free(tmpbufs[index]);
1153 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
1154 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1156 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
1157 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
1160 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
1161 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1163 int system_tag = 666;
1164 int rank, size, other, index;
1165 MPI_Aint lb = 0, dataext = 0;
1166 MPI_Request *requests;
1169 rank = smpi_comm_rank(comm);
1170 size = smpi_comm_size(comm);
1172 // FIXME: check for errors
1173 smpi_datatype_extent(datatype, &lb, &dataext);
1175 // Local copy from self
1176 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
1178 // Send/Recv buffers to/from others;
1179 requests = xbt_new(MPI_Request, size - 1);
1180 tmpbufs = xbt_new(void *, rank);
1182 for(other = 0; other < rank; other++) {
1183 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1185 tmpbufs[index] = xbt_malloc(count * dataext);
1187 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
1191 for(other = rank + 1; other < size; other++) {
1193 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1196 // Wait for completion of all comms.
1197 smpi_mpi_startall(size - 1, requests);
1198 for(other = 0; other < size - 1; other++) {
1199 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1200 if(index == MPI_UNDEFINED) {
1204 // #Request is below rank: it's a irecv
1205 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1208 for(index = 0; index < rank; index++) {
1209 xbt_free(tmpbufs[index]);