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 "surf/surf.h"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi, "Logging specific to SMPI (base)");
18 static int match_recv(void* a, void* b, smx_action_t ignored) {
19 MPI_Request ref = (MPI_Request)a;
20 MPI_Request req = (MPI_Request)b;
21 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 xbt_assert(ref, "Cannot match recv against null reference");
24 xbt_assert(req, "Cannot match recv against null request");
25 return (ref->src == MPI_ANY_SOURCE || req->src == ref->src)
26 && (ref->tag == MPI_ANY_TAG || req->tag == ref->tag);
29 static int match_send(void* a, void* b,smx_action_t ignored) {
30 MPI_Request ref = (MPI_Request)a;
31 MPI_Request req = (MPI_Request)b;
32 XBT_DEBUG("Trying to match a send of src %d against %d, tag %d against %d",ref->src,req->src, ref->tag, req->tag);
33 xbt_assert(ref, "Cannot match send against null reference");
34 xbt_assert(req, "Cannot match send against null request");
35 return (req->src == MPI_ANY_SOURCE || req->src == ref->src)
36 && (req->tag == MPI_ANY_TAG || req->tag == ref->tag);
39 static MPI_Request build_request(void *buf, int count,
40 MPI_Datatype datatype, int src, int dst,
41 int tag, MPI_Comm comm, unsigned flags)
47 request = xbt_new(s_smpi_mpi_request_t, 1);
49 s_smpi_subtype_t *subtype = datatype->substruct;
51 if(datatype->has_subtype == 1){
52 // This part handles the problem of non-contignous memory
54 buf = malloc(count*smpi_datatype_size(datatype));
56 subtype->serialize(old_buf, buf, count, datatype->substruct);
61 // This part handles the problem of non-contignous memory (for the
62 // unserialisation at the reception)
63 request->old_buf = old_buf;
64 request->old_type = datatype;
66 request->size = smpi_datatype_size(datatype) * count;
71 request->action = NULL;
72 request->flags = flags;
73 request->detached = 0;
82 void smpi_empty_status(MPI_Status * status) {
83 if(status != MPI_STATUS_IGNORE) {
84 status->MPI_SOURCE=MPI_ANY_SOURCE;
85 status->MPI_TAG=MPI_ANY_TAG;
90 void smpi_action_trace_run(char *path)
94 xbt_dict_cursor_t cursor;
98 action_fp = fopen(path, "r");
99 xbt_assert(action_fp != NULL, "Cannot open %s: %s", path,
103 if (!xbt_dict_is_empty(action_queues)) {
105 ("Not all actions got consumed. If the simulation ended successfully (without deadlock), you may want to add new processes to your deployment file.");
108 xbt_dict_foreach(action_queues, cursor, name, todo) {
109 XBT_WARN("Still %lu actions for %s", xbt_dynar_length(todo), name);
115 xbt_dict_free(&action_queues);
116 action_queues = xbt_dict_new_homogeneous(NULL);
119 static void smpi_mpi_request_free_voidp(void* request)
121 MPI_Request req = request;
122 smpi_mpi_request_free(&req);
125 /* MPI Low level calls */
126 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
127 int dst, int tag, MPI_Comm comm)
129 MPI_Request request =
130 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
131 comm, PERSISTENT | SEND);
136 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
137 int src, int tag, MPI_Comm comm)
139 MPI_Request request =
140 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
141 comm, PERSISTENT | RECV);
146 void smpi_mpi_start(MPI_Request request)
150 xbt_assert(!request->action,
151 "Cannot (re)start a non-finished communication");
152 if(request->flags & RECV) {
153 print_request("New recv", request);
154 if (request->size < xbt_cfg_get_int(_surf_cfg_set, "smpi/async_small_thres"))
155 mailbox = smpi_process_mailbox_small();
157 mailbox = smpi_process_mailbox();
159 // FIXME: SIMIX does not yet support non-contiguous datatypes
160 request->action = simcall_comm_irecv(mailbox, request->buf, &request->size, &match_recv, request);
163 int receiver = smpi_group_index(smpi_comm_group(request->comm), request->dst);
164 /* if(receiver == MPI_UNDEFINED) {*/
165 /* XBT_WARN("Trying to send a message to a wrong rank");*/
168 print_request("New send", request);
169 if (request->size < xbt_cfg_get_int(_surf_cfg_set, "smpi/async_small_thres")) { // eager mode
170 mailbox = smpi_process_remote_mailbox_small(receiver);
172 XBT_DEBUG("Send request %p is not in the permanent receive mailbox (buf: %p)",request,request->buf);
173 mailbox = smpi_process_remote_mailbox(receiver);
175 if (request->size < 64*1024 ) { //(FIXME: this limit should be configurable)
177 if(request->old_type->has_subtype == 0){
178 oldbuf = request->buf;
179 request->detached = 1;
180 request->buf = malloc(request->size);
182 memcpy(request->buf,oldbuf,request->size);
184 XBT_DEBUG("Send request %p is detached; buf %p copied into %p",request,oldbuf,request->buf);
188 simcall_comm_isend(mailbox, request->size, -1.0,
189 request->buf, request->size,
191 &smpi_mpi_request_free_voidp, // how to free the userdata if a detached send fails
193 // detach if msg size < eager/rdv switch limit
197 /* FIXME: detached sends are not traceable (request->action == NULL) */
199 simcall_set_category(request->action, TRACE_internal_smpi_get_category());
206 void smpi_mpi_startall(int count, MPI_Request * requests)
210 for(i = 0; i < count; i++) {
211 smpi_mpi_start(requests[i]);
215 void smpi_mpi_request_free(MPI_Request * request)
218 *request = MPI_REQUEST_NULL;
221 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
222 int dst, int tag, MPI_Comm comm)
224 MPI_Request request =
225 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
226 comm, NON_PERSISTENT | SEND);
231 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
232 int dst, int tag, MPI_Comm comm)
234 MPI_Request request =
235 smpi_isend_init(buf, count, datatype, dst, tag, comm);
237 smpi_mpi_start(request);
241 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
242 int src, int tag, MPI_Comm comm)
244 MPI_Request request =
245 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
246 comm, NON_PERSISTENT | RECV);
250 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
251 int src, int tag, MPI_Comm comm)
253 MPI_Request request =
254 smpi_irecv_init(buf, count, datatype, src, tag, comm);
256 smpi_mpi_start(request);
260 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
261 int tag, MPI_Comm comm, MPI_Status * status)
264 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
265 smpi_mpi_wait(&request, status);
270 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
271 int tag, MPI_Comm comm)
275 request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
276 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
279 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
280 int dst, int sendtag, void *recvbuf, int recvcount,
281 MPI_Datatype recvtype, int src, int recvtag,
282 MPI_Comm comm, MPI_Status * status)
284 MPI_Request requests[2];
288 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
290 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
291 smpi_mpi_startall(2, requests);
292 smpi_mpi_waitall(2, requests, stats);
293 if(status != MPI_STATUS_IGNORE) {
294 // Copy receive status
295 memcpy(status, &stats[1], sizeof(MPI_Status));
299 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
301 return status->count / smpi_datatype_size(datatype);
304 static void finish_wait(MPI_Request * request, MPI_Status * status)
306 MPI_Request req = *request;
307 // if we have a sender, we should use its data, and not the data from the receive
309 (req->src==MPI_ANY_SOURCE || req->tag== MPI_ANY_TAG))
310 req = (MPI_Request)SIMIX_comm_get_src_data((*request)->action);
312 if(status != MPI_STATUS_IGNORE) {
313 status->MPI_SOURCE = req->src;
314 status->MPI_TAG = req->tag;
315 status->MPI_ERROR = MPI_SUCCESS;
316 // FIXME: really this should just contain the count of receive-type blocks,
318 status->count = req->size;
322 print_request("Finishing", req);
323 MPI_Datatype datatype = req->old_type;
324 if(datatype->has_subtype == 1){
325 // This part handles the problem of non-contignous memory
326 // the unserialization at the reception
327 s_smpi_subtype_t *subtype = datatype->substruct;
328 if(req->flags & RECV) {
329 subtype->unserialize(req->buf, req->old_buf, req->size/smpi_datatype_size(datatype) , datatype->substruct);
331 if(req->detached == 0) free(req->buf);
334 if(req->flags & NON_PERSISTENT) {
335 smpi_mpi_request_free(request);
341 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
344 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or smpi_mpi_testall before)
345 if ((*request)->action == NULL)
348 flag = simcall_comm_test((*request)->action);
350 finish_wait(request, status);
352 smpi_empty_status(status);
357 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
364 *index = MPI_UNDEFINED;
367 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
368 map = xbt_new(int, count);
370 for(i = 0; i < count; i++) {
371 if((requests[i]!=MPI_REQUEST_NULL) && requests[i]->action) {
372 xbt_dynar_push(comms, &requests[i]->action);
378 i = simcall_comm_testany(comms);
379 // not MPI_UNDEFINED, as this is a simix return code
382 finish_wait(&requests[*index], status);
386 //all requests are null or inactive, return true
388 smpi_empty_status(status);
391 xbt_dynar_free(&comms);
398 int smpi_mpi_testall(int count, MPI_Request requests[],
402 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
405 for(i=0; i<count; i++){
406 if(requests[i]!= MPI_REQUEST_NULL){
407 if (smpi_mpi_test(&requests[i], pstat)!=1){
411 smpi_empty_status(pstat);
413 if(status != MPI_STATUSES_IGNORE) {
414 memcpy(&status[i], pstat, sizeof(*pstat));
420 void smpi_mpi_probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
422 //FIXME find another wait to avoid busy waiting ?
423 // the issue here is that we have to wait on a nonexistent comm
425 smpi_mpi_iprobe(source, tag, comm, &flag, status);
426 XBT_DEBUG("Busy Waiting on probing : %d", flag);
428 simcall_process_sleep(0.0001);
433 void smpi_mpi_iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
434 MPI_Request request =build_request(NULL, 0, MPI_CHAR, source, smpi_comm_rank(comm), tag,
435 comm, NON_PERSISTENT | RECV);
437 // behave like a receive, but don't do it
440 print_request("New iprobe", request);
441 // We have to test both mailboxes as we don't know if we will receive one one or another
442 if (xbt_cfg_get_int(_surf_cfg_set, "smpi/async_small_thres")>0){
443 mailbox = smpi_process_mailbox_small();
444 XBT_DEBUG("trying to probe the perm recv mailbox");
445 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
447 if (request->action==NULL){
448 mailbox = smpi_process_mailbox();
449 XBT_DEBUG("trying to probe the other mailbox");
450 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
454 MPI_Request req = (MPI_Request)SIMIX_comm_get_src_data(request->action);
456 if(status != MPI_STATUS_IGNORE) {
457 status->MPI_SOURCE = req->src;
458 status->MPI_TAG = req->tag;
459 status->MPI_ERROR = MPI_SUCCESS;
460 status->count = req->size;
464 smpi_mpi_request_free(&request);
469 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
471 print_request("Waiting", *request);
472 if ((*request)->action != NULL) { // this is not a detached send
473 simcall_comm_wait((*request)->action, -1.0);
474 finish_wait(request, status);
476 // FIXME for a detached send, finish_wait is not called:
479 int smpi_mpi_waitany(int count, MPI_Request requests[],
486 index = MPI_UNDEFINED;
488 // Wait for a request to complete
489 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
490 map = xbt_new(int, count);
492 XBT_DEBUG("Wait for one of");
493 for(i = 0; i < count; i++) {
494 if((requests[i] != MPI_REQUEST_NULL) && (requests[i]->action != NULL)) {
495 print_request("Waiting any ", requests[i]);
496 xbt_dynar_push(comms, &requests[i]->action);
502 i = simcall_comm_waitany(comms);
504 // not MPI_UNDEFINED, as this is a simix return code
507 finish_wait(&requests[index], status);
511 xbt_dynar_free(&comms);
514 if (index==MPI_UNDEFINED)
515 smpi_empty_status(status);
520 void smpi_mpi_waitall(int count, MPI_Request requests[],
525 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
526 //tag invalid requests in the set
527 for(c = 0; c < count; c++) {
528 if(requests[c]==MPI_REQUEST_NULL || requests[c]->dst == MPI_PROC_NULL ){
529 if(status != MPI_STATUSES_IGNORE)
530 smpi_empty_status(&status[c]);
531 }else if(requests[c]->src == MPI_PROC_NULL ){
532 if(status != MPI_STATUSES_IGNORE) {
533 smpi_empty_status(&status[c]);
534 status[c].MPI_SOURCE=MPI_PROC_NULL;
539 for(c = 0; c < count; c++) {
541 smpi_mpi_wait(&requests[c], pstat);
544 index = smpi_mpi_waitany(count, requests, pstat);
545 if(index == MPI_UNDEFINED) {
548 if(status != MPI_STATUSES_IGNORE) {
549 memcpy(&status[index], pstat, sizeof(*pstat));
556 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
561 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
564 for(i = 0; i < incount; i++)
566 index=smpi_mpi_waitany(incount, requests, pstat);
567 if(index!=MPI_UNDEFINED){
568 indices[count] = index;
570 if(status != MPI_STATUSES_IGNORE) {
571 memcpy(&status[index], pstat, sizeof(*pstat));
574 return MPI_UNDEFINED;
580 int smpi_mpi_testsome(int incount, MPI_Request requests[], int *indices,
583 int i, count, count_dead;
585 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
589 for(i = 0; i < incount; i++) {
590 if((requests[i] != MPI_REQUEST_NULL)) {
591 if(smpi_mpi_test(&requests[i], pstat)) {
594 if(status != MPI_STATUSES_IGNORE) {
595 memcpy(&status[i], pstat, sizeof(*pstat));
602 if(count_dead==incount)return MPI_UNDEFINED;
606 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
609 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
610 nary_tree_bcast(buf, count, datatype, root, comm, 4);
613 void smpi_mpi_barrier(MPI_Comm comm)
615 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
616 nary_tree_barrier(comm, 4);
619 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
620 void *recvbuf, int recvcount, MPI_Datatype recvtype,
621 int root, MPI_Comm comm)
623 int system_tag = 666;
624 int rank, size, src, index;
625 MPI_Aint lb = 0, recvext = 0;
626 MPI_Request *requests;
628 rank = smpi_comm_rank(comm);
629 size = smpi_comm_size(comm);
631 // Send buffer to root
632 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
634 // FIXME: check for errors
635 smpi_datatype_extent(recvtype, &lb, &recvext);
636 // Local copy from root
637 smpi_datatype_copy(sendbuf, sendcount, sendtype,
638 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
639 // Receive buffers from senders
640 requests = xbt_new(MPI_Request, size - 1);
642 for(src = 0; src < size; src++) {
644 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
646 src, system_tag, comm);
650 // Wait for completion of irecv's.
651 smpi_mpi_startall(size - 1, requests);
652 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
657 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
658 void *recvbuf, int *recvcounts, int *displs,
659 MPI_Datatype recvtype, int root, MPI_Comm comm)
661 int system_tag = 666;
662 int rank, size, src, index;
663 MPI_Aint lb = 0, recvext = 0;
664 MPI_Request *requests;
666 rank = smpi_comm_rank(comm);
667 size = smpi_comm_size(comm);
669 // Send buffer to root
670 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
672 // FIXME: check for errors
673 smpi_datatype_extent(recvtype, &lb, &recvext);
674 // Local copy from root
675 smpi_datatype_copy(sendbuf, sendcount, sendtype,
676 (char *)recvbuf + displs[root] * recvext,
677 recvcounts[root], recvtype);
678 // Receive buffers from senders
679 requests = xbt_new(MPI_Request, size - 1);
681 for(src = 0; src < size; src++) {
684 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
685 recvcounts[src], recvtype, src, system_tag, comm);
689 // Wait for completion of irecv's.
690 smpi_mpi_startall(size - 1, requests);
691 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
696 void smpi_mpi_allgather(void *sendbuf, int sendcount,
697 MPI_Datatype sendtype, void *recvbuf,
698 int recvcount, MPI_Datatype recvtype,
701 int system_tag = 666;
702 int rank, size, other, index;
703 MPI_Aint lb = 0, recvext = 0;
704 MPI_Request *requests;
706 rank = smpi_comm_rank(comm);
707 size = smpi_comm_size(comm);
708 // FIXME: check for errors
709 smpi_datatype_extent(recvtype, &lb, &recvext);
710 // Local copy from self
711 smpi_datatype_copy(sendbuf, sendcount, sendtype,
712 (char *)recvbuf + rank * recvcount * recvext, recvcount,
714 // Send/Recv buffers to/from others;
715 requests = xbt_new(MPI_Request, 2 * (size - 1));
717 for(other = 0; other < size; other++) {
720 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
723 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
724 recvcount, recvtype, other,
729 // Wait for completion of all comms.
730 smpi_mpi_startall(2 * (size - 1), requests);
731 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
735 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
736 MPI_Datatype sendtype, void *recvbuf,
737 int *recvcounts, int *displs,
738 MPI_Datatype recvtype, MPI_Comm comm)
740 int system_tag = 666;
741 int rank, size, other, index;
742 MPI_Aint lb = 0, recvext = 0;
743 MPI_Request *requests;
745 rank = smpi_comm_rank(comm);
746 size = smpi_comm_size(comm);
747 // FIXME: check for errors
748 smpi_datatype_extent(recvtype, &lb, &recvext);
749 // Local copy from self
750 smpi_datatype_copy(sendbuf, sendcount, sendtype,
751 (char *)recvbuf + displs[rank] * recvext,
752 recvcounts[rank], recvtype);
753 // Send buffers to others;
754 requests = xbt_new(MPI_Request, 2 * (size - 1));
756 for(other = 0; other < size; other++) {
759 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
763 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
764 recvtype, other, system_tag, comm);
768 // Wait for completion of all comms.
769 smpi_mpi_startall(2 * (size - 1), requests);
770 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
774 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
775 void *recvbuf, int recvcount, MPI_Datatype recvtype,
776 int root, MPI_Comm comm)
778 int system_tag = 666;
779 int rank, size, dst, index;
780 MPI_Aint lb = 0, sendext = 0;
781 MPI_Request *requests;
783 rank = smpi_comm_rank(comm);
784 size = smpi_comm_size(comm);
786 // Recv buffer from root
787 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
790 // FIXME: check for errors
791 smpi_datatype_extent(sendtype, &lb, &sendext);
792 // Local copy from root
793 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
794 sendcount, sendtype, recvbuf, recvcount, recvtype);
795 // Send buffers to receivers
796 requests = xbt_new(MPI_Request, size - 1);
798 for(dst = 0; dst < size; dst++) {
800 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
801 sendcount, sendtype, dst,
806 // Wait for completion of isend's.
807 smpi_mpi_startall(size - 1, requests);
808 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
813 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
814 MPI_Datatype sendtype, void *recvbuf, int recvcount,
815 MPI_Datatype recvtype, int root, MPI_Comm comm)
817 int system_tag = 666;
818 int rank, size, dst, index;
819 MPI_Aint lb = 0, sendext = 0;
820 MPI_Request *requests;
822 rank = smpi_comm_rank(comm);
823 size = smpi_comm_size(comm);
825 // Recv buffer from root
826 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
829 // FIXME: check for errors
830 smpi_datatype_extent(sendtype, &lb, &sendext);
831 // Local copy from root
832 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
833 sendtype, recvbuf, recvcount, recvtype);
834 // Send buffers to receivers
835 requests = xbt_new(MPI_Request, size - 1);
837 for(dst = 0; dst < size; dst++) {
840 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
841 sendtype, dst, system_tag, comm);
845 // Wait for completion of isend's.
846 smpi_mpi_startall(size - 1, requests);
847 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
852 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
853 MPI_Datatype datatype, MPI_Op op, int root,
856 int system_tag = 666;
857 int rank, size, src, index;
858 MPI_Aint lb = 0, dataext = 0;
859 MPI_Request *requests;
862 rank = smpi_comm_rank(comm);
863 size = smpi_comm_size(comm);
865 // Send buffer to root
866 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
868 // FIXME: check for errors
869 smpi_datatype_extent(datatype, &lb, &dataext);
870 // Local copy from root
871 if (sendbuf && recvbuf)
872 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
873 // Receive buffers from senders
874 //TODO: make a MPI_barrier here ?
875 requests = xbt_new(MPI_Request, size - 1);
876 tmpbufs = xbt_new(void *, size - 1);
878 for(src = 0; src < size; src++) {
880 // FIXME: possibly overkill we we have contiguous/noncontiguous data
882 tmpbufs[index] = xbt_malloc(count * dataext);
884 smpi_irecv_init(tmpbufs[index], count, datatype, src,
889 // Wait for completion of irecv's.
890 smpi_mpi_startall(size - 1, requests);
891 for(src = 0; src < size - 1; src++) {
892 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
893 XBT_DEBUG("finished waiting any request with index %d", index);
894 if(index == MPI_UNDEFINED) {
897 if(op) /* op can be MPI_OP_NULL that does nothing */
898 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
900 for(index = 0; index < size - 1; index++) {
901 xbt_free(tmpbufs[index]);
908 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
909 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
911 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
912 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
915 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
916 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
918 int system_tag = 666;
919 int rank, size, other, index;
920 MPI_Aint lb = 0, dataext = 0;
921 MPI_Request *requests;
924 rank = smpi_comm_rank(comm);
925 size = smpi_comm_size(comm);
927 // FIXME: check for errors
928 smpi_datatype_extent(datatype, &lb, &dataext);
930 // Local copy from self
931 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
933 // Send/Recv buffers to/from others;
934 requests = xbt_new(MPI_Request, size - 1);
935 tmpbufs = xbt_new(void *, rank);
937 for(other = 0; other < rank; other++) {
938 // FIXME: possibly overkill we we have contiguous/noncontiguous data
940 tmpbufs[index] = xbt_malloc(count * dataext);
942 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
946 for(other = rank + 1; other < size; other++) {
948 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
951 // Wait for completion of all comms.
952 smpi_mpi_startall(size - 1, requests);
953 for(other = 0; other < size - 1; other++) {
954 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
955 if(index == MPI_UNDEFINED) {
959 // #Request is below rank: it's a irecv
960 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
963 for(index = 0; index < rank; index++) {
964 xbt_free(tmpbufs[index]);