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;
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_or(double size)
146 parse_factor(sg_cfg_get_string("smpi/or"));
148 unsigned int iter = 0;
149 s_smpi_factor_t fact;
151 xbt_dynar_foreach(smpi_or_values, iter, fact) {
152 if (size <= fact.factor) {
153 XBT_DEBUG("or : %lf <= %ld return %f", size, fact.factor, current);
156 current=fact.values[0]+fact.values[1]*size;
158 XBT_DEBUG("or : %lf > %ld return %f", size, fact.factor, current);
163 static MPI_Request build_request(void *buf, int count,
164 MPI_Datatype datatype, int src, int dst,
165 int tag, MPI_Comm comm, unsigned flags)
169 void *old_buf = NULL;
171 request = xbt_new(s_smpi_mpi_request_t, 1);
173 s_smpi_subtype_t *subtype = datatype->substruct;
175 if(datatype->has_subtype == 1){
176 // This part handles the problem of non-contiguous memory
178 buf = xbt_malloc(count*smpi_datatype_size(datatype));
180 subtype->serialize(old_buf, buf, count, datatype->substruct);
185 // This part handles the problem of non-contiguous memory (for the
186 // unserialisation at the reception)
187 request->old_buf = old_buf;
188 request->old_type = datatype;
190 request->size = smpi_datatype_size(datatype) * count;
194 request->comm = comm;
195 request->action = NULL;
196 request->flags = flags;
197 request->detached = 0;
198 request->detached_sender = NULL;
200 request->truncated = 0;
201 request->real_size = 0;
202 request->real_tag = 0;
209 if (flags & SEND) smpi_datatype_unuse(datatype);
215 void smpi_empty_status(MPI_Status * status) {
216 if(status != MPI_STATUS_IGNORE) {
217 status->MPI_SOURCE=MPI_ANY_SOURCE;
218 status->MPI_TAG=MPI_ANY_TAG;
223 void smpi_action_trace_run(char *path)
227 xbt_dict_cursor_t cursor;
231 action_fp = fopen(path, "r");
232 xbt_assert(action_fp != NULL, "Cannot open %s: %s", path,
236 if (!xbt_dict_is_empty(action_queues)) {
238 ("Not all actions got consumed. If the simulation ended successfully (without deadlock), you may want to add new processes to your deployment file.");
241 xbt_dict_foreach(action_queues, cursor, name, todo) {
242 XBT_WARN("Still %lu actions for %s", xbt_dynar_length(todo), name);
248 xbt_dict_free(&action_queues);
249 action_queues = xbt_dict_new_homogeneous(NULL);
252 static void smpi_mpi_request_free_voidp(void* request)
254 MPI_Request req = request;
255 smpi_mpi_request_free(&req);
258 /* MPI Low level calls */
259 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
260 int dst, int tag, MPI_Comm comm)
262 MPI_Request request =
263 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
264 comm, PERSISTENT | SEND);
269 MPI_Request smpi_mpi_ssend_init(void *buf, int count, MPI_Datatype datatype,
270 int dst, int tag, MPI_Comm comm)
272 MPI_Request request =
273 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
274 comm, PERSISTENT | SSEND | SEND);
279 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
280 int src, int tag, MPI_Comm comm)
282 MPI_Request request =
283 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
284 comm, PERSISTENT | RECV);
289 void smpi_mpi_start(MPI_Request request)
293 xbt_assert(!request->action,
294 "Cannot (re)start a non-finished communication");
295 if(request->flags & RECV) {
296 print_request("New recv", request);
297 if (request->size < sg_cfg_get_int("smpi/async_small_thres"))
298 mailbox = smpi_process_mailbox_small();
300 mailbox = smpi_process_mailbox();
301 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
302 request->real_size=request->size;
303 smpi_datatype_use(request->old_type);
304 request->action = simcall_comm_irecv(mailbox, request->buf, &request->real_size, &match_recv, request);
306 double sleeptime = smpi_or(request->size);
308 simcall_process_sleep(sleeptime);
309 XBT_DEBUG("receiving size of %zu : sleep %lf ", request->size, smpi_or(request->size));
314 int receiver = smpi_group_index(smpi_comm_group(request->comm), request->dst);
315 /* if(receiver == MPI_UNDEFINED) {*/
316 /* XBT_WARN("Trying to send a message to a wrong rank");*/
319 print_request("New send", request);
320 if (request->size < sg_cfg_get_int("smpi/async_small_thres")) { // eager mode
321 mailbox = smpi_process_remote_mailbox_small(receiver);
323 XBT_DEBUG("Send request %p is not in the permanent receive mailbox (buf: %p)",request,request->buf);
324 mailbox = smpi_process_remote_mailbox(receiver);
326 if ( (! (request->flags & SSEND)) && (request->size < sg_cfg_get_int("smpi/send_is_detached_thres"))) {
328 request->detached = 1;
330 if(request->old_type->has_subtype == 0){
331 oldbuf = request->buf;
333 request->buf = xbt_malloc(request->size);
334 memcpy(request->buf,oldbuf,request->size);
337 XBT_DEBUG("Send request %p is detached; buf %p copied into %p",request,oldbuf,request->buf);
339 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
340 request->real_size=request->size;
341 smpi_datatype_use(request->old_type);
342 double sleeptime = smpi_os(request->size);
344 simcall_process_sleep(sleeptime);
345 XBT_DEBUG("sending size of %zu : sleep %lf ", request->size, smpi_os(request->size));
348 simcall_comm_isend(mailbox, request->size, -1.0,
349 request->buf, request->real_size,
351 &smpi_mpi_request_free_voidp, // how to free the userdata if a detached send fails
353 // detach if msg size < eager/rdv switch limit
357 /* FIXME: detached sends are not traceable (request->action == NULL) */
359 simcall_set_category(request->action, TRACE_internal_smpi_get_category());
366 void smpi_mpi_startall(int count, MPI_Request * requests)
370 for(i = 0; i < count; i++) {
371 smpi_mpi_start(requests[i]);
375 void smpi_mpi_request_free(MPI_Request * request)
378 if((*request) != MPI_REQUEST_NULL){
379 (*request)->refcount--;
380 if((*request)->refcount<0) xbt_die("wrong refcount");
382 if((*request)->refcount==0){
384 *request = MPI_REQUEST_NULL;
387 xbt_die("freeing an already free request");
391 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
392 int dst, int tag, MPI_Comm comm)
394 MPI_Request request =
395 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
396 comm, NON_PERSISTENT | SEND);
401 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
402 int dst, int tag, MPI_Comm comm)
404 MPI_Request request =
405 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
406 comm, NON_PERSISTENT | ISEND | SEND);
408 smpi_mpi_start(request);
412 MPI_Request smpi_mpi_issend(void *buf, int count, MPI_Datatype datatype,
413 int dst, int tag, MPI_Comm comm)
415 MPI_Request request =
416 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
417 comm, NON_PERSISTENT | ISEND | SSEND | SEND);
418 smpi_mpi_start(request);
422 MPI_Request smpi_mpi_ssend(void *buf, int count, MPI_Datatype datatype,
423 int dst, int tag, MPI_Comm comm)
425 MPI_Request request = smpi_mpi_issend(buf, count, datatype, dst, tag, comm);
426 smpi_mpi_start(request);
430 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
431 int src, int tag, MPI_Comm comm)
433 MPI_Request request =
434 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
435 comm, NON_PERSISTENT | RECV);
439 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
440 int src, int tag, MPI_Comm comm)
442 MPI_Request request =
443 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
444 comm, NON_PERSISTENT | RECV);
446 smpi_mpi_start(request);
450 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
451 int tag, MPI_Comm comm, MPI_Status * status)
454 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
455 smpi_mpi_wait(&request, status);
460 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
461 int tag, MPI_Comm comm)
464 request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
465 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
469 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
470 int dst, int sendtag, void *recvbuf, int recvcount,
471 MPI_Datatype recvtype, int src, int recvtag,
472 MPI_Comm comm, MPI_Status * status)
474 MPI_Request requests[2];
478 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
480 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
481 smpi_mpi_startall(2, requests);
482 smpi_mpi_waitall(2, requests, stats);
483 if(status != MPI_STATUS_IGNORE) {
484 // Copy receive status
485 memcpy(status, &stats[1], sizeof(MPI_Status));
489 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
491 return status->count / smpi_datatype_size(datatype);
494 static void finish_wait(MPI_Request * request, MPI_Status * status)
496 MPI_Request req = *request;
497 if(!(req->detached && req->flags & SEND)){
498 if(status != MPI_STATUS_IGNORE) {
499 status->MPI_SOURCE = req->src == MPI_ANY_SOURCE ? req->real_src : req->src;
500 status->MPI_TAG = req->tag == MPI_ANY_TAG ? req->real_tag : req->tag;
502 status->MPI_ERROR = MPI_ERR_TRUNCATE;
503 else status->MPI_ERROR = MPI_SUCCESS ;
504 // this handles the case were size in receive differs from size in send
505 // FIXME: really this should just contain the count of receive-type blocks,
507 status->count = req->real_size;
510 print_request("Finishing", req);
511 MPI_Datatype datatype = req->old_type;
513 if(datatype->has_subtype == 1){
514 // This part handles the problem of non-contignous memory
515 // the unserialization at the reception
516 s_smpi_subtype_t *subtype = datatype->substruct;
517 if(req->flags & RECV) {
518 subtype->unserialize(req->buf, req->old_buf, req->real_size/smpi_datatype_size(datatype) , datatype->substruct);
520 if(req->detached == 0) free(req->buf);
522 smpi_datatype_unuse(datatype);
525 if(req->detached_sender!=NULL){
526 smpi_mpi_request_free(&(req->detached_sender));
529 if(req->flags & NON_PERSISTENT) {
530 smpi_mpi_request_free(request);
536 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
539 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or smpi_mpi_testall before)
540 if ((*request)->action == NULL)
543 flag = simcall_comm_test((*request)->action);
545 (*request)->refcount++;
546 finish_wait(request, status);
548 smpi_empty_status(status);
553 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
560 *index = MPI_UNDEFINED;
563 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
564 map = xbt_new(int, count);
566 for(i = 0; i < count; i++) {
567 if((requests[i]!=MPI_REQUEST_NULL) && requests[i]->action) {
568 xbt_dynar_push(comms, &requests[i]->action);
574 i = simcall_comm_testany(comms);
575 // not MPI_UNDEFINED, as this is a simix return code
578 finish_wait(&requests[*index], status);
582 //all requests are null or inactive, return true
584 smpi_empty_status(status);
587 xbt_dynar_free(&comms);
594 int smpi_mpi_testall(int count, MPI_Request requests[],
598 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
601 for(i=0; i<count; i++){
602 if(requests[i]!= MPI_REQUEST_NULL){
603 if (smpi_mpi_test(&requests[i], pstat)!=1){
607 smpi_empty_status(pstat);
609 if(status != MPI_STATUSES_IGNORE) {
610 memcpy(&status[i], pstat, sizeof(*pstat));
616 void smpi_mpi_probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
618 //FIXME find another wait to avoid busy waiting ?
619 // the issue here is that we have to wait on a nonexistent comm
621 smpi_mpi_iprobe(source, tag, comm, &flag, status);
622 XBT_DEBUG("Busy Waiting on probing : %d", flag);
624 simcall_process_sleep(0.0001);
629 void smpi_mpi_iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
630 MPI_Request request =build_request(NULL, 0, MPI_CHAR, source, smpi_comm_rank(comm), tag,
631 comm, NON_PERSISTENT | RECV);
633 // behave like a receive, but don't do it
636 print_request("New iprobe", request);
637 // We have to test both mailboxes as we don't know if we will receive one one or another
638 if (sg_cfg_get_int("smpi/async_small_thres")>0){
639 mailbox = smpi_process_mailbox_small();
640 XBT_DEBUG("trying to probe the perm recv mailbox");
641 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
643 if (request->action==NULL){
644 mailbox = smpi_process_mailbox();
645 XBT_DEBUG("trying to probe the other mailbox");
646 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
650 MPI_Request req = (MPI_Request)SIMIX_comm_get_src_data(request->action);
652 if(status != MPI_STATUS_IGNORE) {
653 status->MPI_SOURCE = req->src;
654 status->MPI_TAG = req->tag;
655 status->MPI_ERROR = MPI_SUCCESS;
656 status->count = req->real_size;
660 smpi_mpi_request_free(&request);
665 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
667 print_request("Waiting", *request);
668 if ((*request)->action != NULL) { // this is not a detached send
669 simcall_comm_wait((*request)->action, -1.0);
671 finish_wait(request, status);
673 // FIXME for a detached send, finish_wait is not called:
676 int smpi_mpi_waitany(int count, MPI_Request requests[],
683 index = MPI_UNDEFINED;
685 // Wait for a request to complete
686 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
687 map = xbt_new(int, count);
689 XBT_DEBUG("Wait for one of %d", count);
690 for(i = 0; i < count; i++) {
691 if(requests[i] != MPI_REQUEST_NULL) {
692 if (requests[i]->action != NULL) {
693 XBT_DEBUG("Waiting any %p ", requests[i]);
694 xbt_dynar_push(comms, &requests[i]->action);
698 //This is a finished detached request, let's return this one
699 size=0;//so we free the dynar but don't do the waitany call
701 finish_wait(&requests[i], status);//cleanup if refcount = 0
702 requests[i]=MPI_REQUEST_NULL;//set to null
708 i = simcall_comm_waitany(comms);
710 // not MPI_UNDEFINED, as this is a simix return code
713 finish_wait(&requests[index], status);
717 xbt_dynar_free(&comms);
720 if (index==MPI_UNDEFINED)
721 smpi_empty_status(status);
726 int smpi_mpi_waitall(int count, MPI_Request requests[],
731 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
732 int retvalue=MPI_SUCCESS;
733 //tag invalid requests in the set
734 for(c = 0; c < count; c++) {
735 if(requests[c]==MPI_REQUEST_NULL || requests[c]->dst == MPI_PROC_NULL ){
736 if(status != MPI_STATUSES_IGNORE)
737 smpi_empty_status(&status[c]);
738 }else if(requests[c]->src == MPI_PROC_NULL ){
739 if(status != MPI_STATUSES_IGNORE) {
740 smpi_empty_status(&status[c]);
741 status[c].MPI_SOURCE=MPI_PROC_NULL;
745 for(c = 0; c < count; c++) {
747 smpi_mpi_wait(&requests[c], pstat);
750 index = smpi_mpi_waitany(count, requests, pstat);
751 if(index == MPI_UNDEFINED) {
754 if(status != MPI_STATUSES_IGNORE) {
755 memcpy(&status[index], pstat, sizeof(*pstat));
756 if(status[index].MPI_ERROR==MPI_ERR_TRUNCATE)retvalue=MPI_ERR_IN_STATUS;
765 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
770 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
773 for(i = 0; i < incount; i++)
775 index=smpi_mpi_waitany(incount, requests, pstat);
776 if(index!=MPI_UNDEFINED){
777 indices[count] = index;
779 if(status != MPI_STATUSES_IGNORE) {
780 memcpy(&status[index], pstat, sizeof(*pstat));
783 return MPI_UNDEFINED;
789 int smpi_mpi_testsome(int incount, MPI_Request requests[], int *indices,
792 int i, count, count_dead;
794 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
798 for(i = 0; i < incount; i++) {
799 if((requests[i] != MPI_REQUEST_NULL)) {
800 if(smpi_mpi_test(&requests[i], pstat)) {
803 if(status != MPI_STATUSES_IGNORE) {
804 memcpy(&status[i], pstat, sizeof(*pstat));
811 if(count_dead==incount)return MPI_UNDEFINED;
815 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
818 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
819 nary_tree_bcast(buf, count, datatype, root, comm, 4);
822 void smpi_mpi_barrier(MPI_Comm comm)
824 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
825 nary_tree_barrier(comm, 4);
828 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
829 void *recvbuf, int recvcount, MPI_Datatype recvtype,
830 int root, MPI_Comm comm)
832 int system_tag = 666;
833 int rank, size, src, index;
834 MPI_Aint lb = 0, recvext = 0;
835 MPI_Request *requests;
837 rank = smpi_comm_rank(comm);
838 size = smpi_comm_size(comm);
840 // Send buffer to root
841 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
843 // FIXME: check for errors
844 smpi_datatype_extent(recvtype, &lb, &recvext);
845 // Local copy from root
846 smpi_datatype_copy(sendbuf, sendcount, sendtype,
847 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
848 // Receive buffers from senders
849 requests = xbt_new(MPI_Request, size - 1);
851 for(src = 0; src < size; src++) {
853 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
855 src, system_tag, comm);
859 // Wait for completion of irecv's.
860 smpi_mpi_startall(size - 1, requests);
861 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
866 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
867 void *recvbuf, int *recvcounts, int *displs,
868 MPI_Datatype recvtype, int root, MPI_Comm comm)
870 int system_tag = 666;
871 int rank, size, src, index;
872 MPI_Aint lb = 0, recvext = 0;
873 MPI_Request *requests;
875 rank = smpi_comm_rank(comm);
876 size = smpi_comm_size(comm);
878 // Send buffer to root
879 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
881 // FIXME: check for errors
882 smpi_datatype_extent(recvtype, &lb, &recvext);
883 // Local copy from root
884 smpi_datatype_copy(sendbuf, sendcount, sendtype,
885 (char *)recvbuf + displs[root] * recvext,
886 recvcounts[root], recvtype);
887 // Receive buffers from senders
888 requests = xbt_new(MPI_Request, size - 1);
890 for(src = 0; src < size; src++) {
893 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
894 recvcounts[src], recvtype, src, system_tag, comm);
898 // Wait for completion of irecv's.
899 smpi_mpi_startall(size - 1, requests);
900 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
905 void smpi_mpi_allgather(void *sendbuf, int sendcount,
906 MPI_Datatype sendtype, void *recvbuf,
907 int recvcount, MPI_Datatype recvtype,
910 int system_tag = 666;
911 int rank, size, other, index;
912 MPI_Aint lb = 0, recvext = 0;
913 MPI_Request *requests;
915 rank = smpi_comm_rank(comm);
916 size = smpi_comm_size(comm);
917 // FIXME: check for errors
918 smpi_datatype_extent(recvtype, &lb, &recvext);
919 // Local copy from self
920 smpi_datatype_copy(sendbuf, sendcount, sendtype,
921 (char *)recvbuf + rank * recvcount * recvext, recvcount,
923 // Send/Recv buffers to/from others;
924 requests = xbt_new(MPI_Request, 2 * (size - 1));
926 for(other = 0; other < size; other++) {
929 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
932 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
933 recvcount, recvtype, other,
938 // Wait for completion of all comms.
939 smpi_mpi_startall(2 * (size - 1), requests);
940 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
944 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
945 MPI_Datatype sendtype, void *recvbuf,
946 int *recvcounts, int *displs,
947 MPI_Datatype recvtype, MPI_Comm comm)
949 int system_tag = 666;
950 int rank, size, other, index;
951 MPI_Aint lb = 0, recvext = 0;
952 MPI_Request *requests;
954 rank = smpi_comm_rank(comm);
955 size = smpi_comm_size(comm);
956 // FIXME: check for errors
957 smpi_datatype_extent(recvtype, &lb, &recvext);
958 // Local copy from self
959 smpi_datatype_copy(sendbuf, sendcount, sendtype,
960 (char *)recvbuf + displs[rank] * recvext,
961 recvcounts[rank], recvtype);
962 // Send buffers to others;
963 requests = xbt_new(MPI_Request, 2 * (size - 1));
965 for(other = 0; other < size; other++) {
968 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
972 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
973 recvtype, other, system_tag, comm);
977 // Wait for completion of all comms.
978 smpi_mpi_startall(2 * (size - 1), requests);
979 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
983 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
984 void *recvbuf, int recvcount, MPI_Datatype recvtype,
985 int root, MPI_Comm comm)
987 int system_tag = 666;
988 int rank, size, dst, index;
989 MPI_Aint lb = 0, sendext = 0;
990 MPI_Request *requests;
992 rank = smpi_comm_rank(comm);
993 size = smpi_comm_size(comm);
995 // Recv buffer from root
996 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
999 // FIXME: check for errors
1000 smpi_datatype_extent(sendtype, &lb, &sendext);
1001 // Local copy from root
1002 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
1003 sendcount, sendtype, recvbuf, recvcount, recvtype);
1004 // Send buffers to receivers
1005 requests = xbt_new(MPI_Request, size - 1);
1007 for(dst = 0; dst < size; dst++) {
1009 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
1010 sendcount, sendtype, dst,
1015 // Wait for completion of isend's.
1016 smpi_mpi_startall(size - 1, requests);
1017 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1022 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
1023 MPI_Datatype sendtype, void *recvbuf, int recvcount,
1024 MPI_Datatype recvtype, int root, MPI_Comm comm)
1026 int system_tag = 666;
1027 int rank, size, dst, index;
1028 MPI_Aint lb = 0, sendext = 0;
1029 MPI_Request *requests;
1031 rank = smpi_comm_rank(comm);
1032 size = smpi_comm_size(comm);
1034 // Recv buffer from root
1035 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
1038 // FIXME: check for errors
1039 smpi_datatype_extent(sendtype, &lb, &sendext);
1040 // Local copy from root
1041 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
1042 sendtype, recvbuf, recvcount, recvtype);
1043 // Send buffers to receivers
1044 requests = xbt_new(MPI_Request, size - 1);
1046 for(dst = 0; dst < size; dst++) {
1049 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
1050 sendtype, dst, system_tag, comm);
1054 // Wait for completion of isend's.
1055 smpi_mpi_startall(size - 1, requests);
1056 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1061 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
1062 MPI_Datatype datatype, MPI_Op op, int root,
1065 int system_tag = 666;
1066 int rank, size, src, index;
1067 MPI_Aint lb = 0, dataext = 0;
1068 MPI_Request *requests;
1071 rank = smpi_comm_rank(comm);
1072 size = smpi_comm_size(comm);
1074 // Send buffer to root
1075 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
1077 // FIXME: check for errors
1078 smpi_datatype_extent(datatype, &lb, &dataext);
1079 // Local copy from root
1080 if (sendbuf && recvbuf)
1081 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
1082 // Receive buffers from senders
1083 //TODO: make a MPI_barrier here ?
1084 requests = xbt_new(MPI_Request, size - 1);
1085 tmpbufs = xbt_new(void *, size - 1);
1087 for(src = 0; src < size; src++) {
1089 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1091 tmpbufs[index] = xbt_malloc(count * dataext);
1093 smpi_irecv_init(tmpbufs[index], count, datatype, src,
1098 // Wait for completion of irecv's.
1099 smpi_mpi_startall(size - 1, requests);
1100 for(src = 0; src < size - 1; src++) {
1101 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1102 XBT_DEBUG("finished waiting any request with index %d", index);
1103 if(index == MPI_UNDEFINED) {
1106 if(op) /* op can be MPI_OP_NULL that does nothing */
1107 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1109 for(index = 0; index < size - 1; index++) {
1110 xbt_free(tmpbufs[index]);
1117 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
1118 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1120 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
1121 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
1124 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
1125 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1127 int system_tag = 666;
1128 int rank, size, other, index;
1129 MPI_Aint lb = 0, dataext = 0;
1130 MPI_Request *requests;
1133 rank = smpi_comm_rank(comm);
1134 size = smpi_comm_size(comm);
1136 // FIXME: check for errors
1137 smpi_datatype_extent(datatype, &lb, &dataext);
1139 // Local copy from self
1140 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
1142 // Send/Recv buffers to/from others;
1143 requests = xbt_new(MPI_Request, size - 1);
1144 tmpbufs = xbt_new(void *, rank);
1146 for(other = 0; other < rank; other++) {
1147 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1149 tmpbufs[index] = xbt_malloc(count * dataext);
1151 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
1155 for(other = rank + 1; other < size; other++) {
1157 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1160 // Wait for completion of all comms.
1161 smpi_mpi_startall(size - 1, requests);
1162 for(other = 0; other < size - 1; other++) {
1163 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1164 if(index == MPI_UNDEFINED) {
1168 // #Request is below rank: it's a irecv
1169 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1172 for(index = 0; index < rank; index++) {
1173 xbt_free(tmpbufs[index]);