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"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi,
15 "Logging specific to SMPI (base)");
17 static int match_recv(void* a, void* b, smx_action_t ignored) {
18 MPI_Request ref = (MPI_Request)a;
19 MPI_Request req = (MPI_Request)b;
20 XBT_DEBUG("Trying to match a recv of src %d against %d, tag %d against %d",ref->src,req->src, ref->tag, req->tag);
22 xbt_assert(ref, "Cannot match recv against null reference");
23 xbt_assert(req, "Cannot match recv against null request");
24 return (ref->src == MPI_ANY_SOURCE || req->src == ref->src)
25 && (ref->tag == MPI_ANY_TAG || req->tag == ref->tag);
28 static int match_send(void* a, void* b,smx_action_t ignored) {
29 MPI_Request ref = (MPI_Request)a;
30 MPI_Request req = (MPI_Request)b;
31 XBT_DEBUG("Trying to match a send of src %d against %d, tag %d against %d",ref->src,req->src, ref->tag, req->tag);
32 xbt_assert(ref, "Cannot match send against null reference");
33 xbt_assert(req, "Cannot match send against null request");
34 return (req->src == MPI_ANY_SOURCE || req->src == ref->src)
35 && (req->tag == MPI_ANY_TAG || req->tag == ref->tag);
38 static MPI_Request build_request(void *buf, int count,
39 MPI_Datatype datatype, int src, int dst,
40 int tag, MPI_Comm comm, unsigned flags)
44 request = xbt_new(s_smpi_mpi_request_t, 1);
46 // FIXME: this will have to be changed to support non-contiguous datatypes
47 request->size = smpi_datatype_size(datatype) * count;
52 request->action = NULL;
53 request->flags = flags;
62 void smpi_empty_status(MPI_Status * status) {
63 if(status != MPI_STATUS_IGNORE) {
64 status->MPI_SOURCE=MPI_ANY_SOURCE;
65 status->MPI_TAG=MPI_ANY_TAG;
70 void smpi_action_trace_run(char *path)
74 xbt_dict_cursor_t cursor;
78 action_fp = fopen(path, "r");
79 xbt_assert(action_fp != NULL, "Cannot open %s: %s", path,
83 if (!xbt_dict_is_empty(action_queues)) {
85 ("Not all actions got consumed. If the simulation ended successfully (without deadlock), you may want to add new processes to your deployment file.");
88 xbt_dict_foreach(action_queues, cursor, name, todo) {
89 XBT_WARN("Still %lu actions for %s", xbt_dynar_length(todo), name);
95 xbt_dict_free(&action_queues);
96 action_queues = xbt_dict_new_homogeneous(NULL);
99 static void smpi_mpi_request_free_voidp(void* request)
101 MPI_Request req = request;
102 smpi_mpi_request_free(&req);
105 /* MPI Low level calls */
106 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
107 int dst, int tag, MPI_Comm comm)
109 MPI_Request request =
110 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
111 comm, PERSISTENT | SEND);
116 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
117 int src, int tag, MPI_Comm comm)
119 MPI_Request request =
120 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
121 comm, PERSISTENT | RECV);
126 void smpi_mpi_start(MPI_Request request)
131 xbt_assert(!request->action,
132 "Cannot (re)start a non-finished communication");
133 if(request->flags & RECV) {
134 print_request("New recv", request);
135 if (request->size < xbt_cfg_get_int(_surf_cfg_set, "smpi/async_small_thres"))
136 mailbox = smpi_process_mailbox_small();
138 mailbox = smpi_process_mailbox();
140 // FIXME: SIMIX does not yet support non-contiguous datatypes
141 request->action = simcall_comm_irecv(mailbox, request->buf, &request->size, &match_recv, request);
143 print_request("New send", request);
144 if (request->size < xbt_cfg_get_int(_surf_cfg_set, "smpi/async_small_thres")) { // eager mode
145 mailbox = smpi_process_remote_mailbox_small(
146 smpi_group_index(smpi_comm_group(request->comm), request->dst));
148 XBT_DEBUG("Send request %p is not in the permanent receive mailbox (buf: %p)",request,request->buf);
149 mailbox = smpi_process_remote_mailbox(
150 smpi_group_index(smpi_comm_group(request->comm), request->dst));
152 if (request->size < 64*1024 ) { //(FIXME: this limit should be configurable)
153 void *oldbuf = request->buf;
155 request->buf = malloc(request->size);
157 memcpy(request->buf,oldbuf,request->size);
158 XBT_DEBUG("Send request %p is detached; buf %p copied into %p",request,oldbuf,request->buf);
162 simcall_comm_isend(mailbox, request->size, -1.0,
163 request->buf, request->size,
165 &smpi_mpi_request_free_voidp, // how to free the userdata if a detached send fails
167 // detach if msg size < eager/rdv switch limit
171 /* FIXME: detached sends are not traceable (request->action == NULL) */
173 simcall_set_category(request->action, TRACE_internal_smpi_get_category());
180 void smpi_mpi_startall(int count, MPI_Request * requests)
184 for(i = 0; i < count; i++) {
185 smpi_mpi_start(requests[i]);
189 void smpi_mpi_request_free(MPI_Request * request)
192 *request = MPI_REQUEST_NULL;
195 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
196 int dst, int tag, MPI_Comm comm)
198 MPI_Request request =
199 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
200 comm, NON_PERSISTENT | SEND);
205 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
206 int dst, int tag, MPI_Comm comm)
208 MPI_Request request =
209 smpi_isend_init(buf, count, datatype, dst, tag, comm);
211 smpi_mpi_start(request);
215 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
216 int src, int tag, MPI_Comm comm)
218 MPI_Request request =
219 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
220 comm, NON_PERSISTENT | RECV);
225 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
226 int src, int tag, MPI_Comm comm)
228 MPI_Request request =
229 smpi_irecv_init(buf, count, datatype, src, tag, comm);
231 smpi_mpi_start(request);
235 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
236 int tag, MPI_Comm comm, MPI_Status * status)
240 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
241 smpi_mpi_wait(&request, status);
246 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
247 int tag, MPI_Comm comm)
250 request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
251 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
254 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
255 int dst, int sendtag, void *recvbuf, int recvcount,
256 MPI_Datatype recvtype, int src, int recvtag,
257 MPI_Comm comm, MPI_Status * status)
259 MPI_Request requests[2];
263 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
265 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
266 smpi_mpi_startall(2, requests);
267 smpi_mpi_waitall(2, requests, stats);
268 if(status != MPI_STATUS_IGNORE) {
269 // Copy receive status
270 memcpy(status, &stats[1], sizeof(MPI_Status));
274 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
276 return status->count / smpi_datatype_size(datatype);
279 static void finish_wait(MPI_Request * request, MPI_Status * status)
281 MPI_Request req = *request;
282 // if we have a sender, we should use its data, and not the data from the receive
284 (req->src==MPI_ANY_SOURCE || req->tag== MPI_ANY_TAG))
285 req = (MPI_Request)SIMIX_comm_get_src_data((*request)->action);
287 if(status != MPI_STATUS_IGNORE) {
288 status->MPI_SOURCE = req->src;
289 status->MPI_TAG = req->tag;
290 status->MPI_ERROR = MPI_SUCCESS;
291 // FIXME: really this should just contain the count of receive-type blocks,
293 status->count = req->size;
297 print_request("Finishing", req);
298 if(req->flags & NON_PERSISTENT) {
299 smpi_mpi_request_free(request);
305 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
307 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or smpi_mpi_testall before)
308 if ((*request)->action == NULL)
311 flag = simcall_comm_test((*request)->action);
313 finish_wait(request, status);
315 smpi_empty_status(status);
320 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
327 *index = MPI_UNDEFINED;
330 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
331 map = xbt_new(int, count);
333 for(i = 0; i < count; i++) {
334 if((requests[i]!=MPI_REQUEST_NULL) && requests[i]->action) {
335 xbt_dynar_push(comms, &requests[i]->action);
341 i = simcall_comm_testany(comms);
342 // not MPI_UNDEFINED, as this is a simix return code
345 finish_wait(&requests[*index], status);
349 //all requests are null or inactive, return true
351 smpi_empty_status(status);
354 xbt_dynar_free(&comms);
361 int smpi_mpi_testall(int count, MPI_Request requests[],
365 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
368 for(i=0; i<count; i++){
369 if(requests[i]!= MPI_REQUEST_NULL){
370 if (smpi_mpi_test(&requests[i], pstat)!=1){
374 smpi_empty_status(pstat);
376 if(status != MPI_STATUSES_IGNORE) {
377 memcpy(&status[i], pstat, sizeof(*pstat));
383 void smpi_mpi_probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
385 //FIXME find another wait to avoid busy waiting ?
386 // the issue here is that we have to wait on a nonexistent comm
388 smpi_mpi_iprobe(source, tag, comm, &flag, status);
389 XBT_DEBUG("Busy Waiting on probing : %d", flag);
391 simcall_process_sleep(0.0001);
396 void smpi_mpi_iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
397 MPI_Request request =build_request(NULL, 0, MPI_CHAR, source, smpi_comm_rank(comm), tag,
398 comm, NON_PERSISTENT | RECV);
400 // behave like a receive, but don't do it
403 print_request("New iprobe", request);
404 // We have to test both mailboxes as we don't know if we will receive one one or another
405 if (xbt_cfg_get_int(_surf_cfg_set, "smpi/async_small_thres")>0){
406 mailbox = smpi_process_mailbox_small();
407 XBT_DEBUG("trying to probe the perm recv mailbox");
408 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
410 if (request->action==NULL){
411 mailbox = smpi_process_mailbox();
412 XBT_DEBUG("trying to probe the other mailbox");
413 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
417 MPI_Request req = (MPI_Request)SIMIX_comm_get_src_data(request->action);
419 if(status != MPI_STATUS_IGNORE) {
420 status->MPI_SOURCE = req->src;
421 status->MPI_TAG = req->tag;
422 status->MPI_ERROR = MPI_SUCCESS;
423 status->count = req->size;
427 smpi_mpi_request_free(&request);
432 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
434 print_request("Waiting", *request);
435 if ((*request)->action != NULL) { // this is not a detached send
436 simcall_comm_wait((*request)->action, -1.0);
437 finish_wait(request, status);
439 // FIXME for a detached send, finish_wait is not called:
442 int smpi_mpi_waitany(int count, MPI_Request requests[],
449 index = MPI_UNDEFINED;
451 // Wait for a request to complete
452 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
453 map = xbt_new(int, count);
455 XBT_DEBUG("Wait for one of");
456 for(i = 0; i < count; i++) {
457 if((requests[i] != MPI_REQUEST_NULL) && (requests[i]->action != NULL)) {
458 print_request("Waiting any ", requests[i]);
459 xbt_dynar_push(comms, &requests[i]->action);
465 i = simcall_comm_waitany(comms);
467 // not MPI_UNDEFINED, as this is a simix return code
470 finish_wait(&requests[index], status);
474 xbt_dynar_free(&comms);
477 if (index==MPI_UNDEFINED)
478 smpi_empty_status(status);
483 void smpi_mpi_waitall(int count, MPI_Request requests[],
488 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
489 //tag invalid requests in the set
490 for(c = 0; c < count; c++) {
491 if(requests[c]==MPI_REQUEST_NULL || requests[c]->dst == MPI_PROC_NULL ){
492 if(status != MPI_STATUSES_IGNORE)
493 smpi_empty_status(&status[c]);
494 }else if(requests[c]->src == MPI_PROC_NULL ){
495 if(status != MPI_STATUSES_IGNORE) {
496 smpi_empty_status(&status[c]);
497 status[c].MPI_SOURCE=MPI_PROC_NULL;
502 for(c = 0; c < count; c++) {
504 smpi_mpi_wait(&requests[c], pstat);
507 index = smpi_mpi_waitany(count, requests, pstat);
508 if(index == MPI_UNDEFINED) {
511 if(status != MPI_STATUSES_IGNORE) {
512 memcpy(&status[index], pstat, sizeof(*pstat));
519 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
524 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
527 for(i = 0; i < incount; i++)
529 index=smpi_mpi_waitany(incount, requests, pstat);
530 if(index!=MPI_UNDEFINED){
531 indices[count] = index;
533 if(status != MPI_STATUSES_IGNORE) {
534 memcpy(&status[index], pstat, sizeof(*pstat));
537 return MPI_UNDEFINED;
543 int smpi_mpi_testsome(int incount, MPI_Request requests[], int *indices,
546 int i, count, count_dead;
548 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
551 for(i = 0; i < incount; i++) {
552 if((requests[i] != MPI_REQUEST_NULL)) {
553 if(smpi_mpi_test(&requests[i], pstat)) {
556 if(status != MPI_STATUSES_IGNORE) {
557 memcpy(&status[i], pstat, sizeof(*pstat));
564 if(count_dead==incount)return MPI_UNDEFINED;
568 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
571 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
572 nary_tree_bcast(buf, count, datatype, root, comm, 4);
575 void smpi_mpi_barrier(MPI_Comm comm)
577 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
578 nary_tree_barrier(comm, 4);
581 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
582 void *recvbuf, int recvcount, MPI_Datatype recvtype,
583 int root, MPI_Comm comm)
585 int system_tag = 666;
586 int rank, size, src, index;
587 MPI_Aint lb = 0, recvext = 0;
588 MPI_Request *requests;
590 rank = smpi_comm_rank(comm);
591 size = smpi_comm_size(comm);
593 // Send buffer to root
594 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
596 // FIXME: check for errors
597 smpi_datatype_extent(recvtype, &lb, &recvext);
598 // Local copy from root
599 smpi_datatype_copy(sendbuf, sendcount, sendtype,
600 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
601 // Receive buffers from senders
602 requests = xbt_new(MPI_Request, size - 1);
604 for(src = 0; src < size; src++) {
606 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
608 src, system_tag, comm);
612 // Wait for completion of irecv's.
613 smpi_mpi_startall(size - 1, requests);
614 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
619 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
620 void *recvbuf, int *recvcounts, int *displs,
621 MPI_Datatype recvtype, 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 + displs[root] * recvext,
639 recvcounts[root], recvtype);
640 // Receive buffers from senders
641 requests = xbt_new(MPI_Request, size - 1);
643 for(src = 0; src < size; src++) {
646 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
647 recvcounts[src], recvtype, src, system_tag, comm);
651 // Wait for completion of irecv's.
652 smpi_mpi_startall(size - 1, requests);
653 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
658 void smpi_mpi_allgather(void *sendbuf, int sendcount,
659 MPI_Datatype sendtype, void *recvbuf,
660 int recvcount, MPI_Datatype recvtype,
663 int system_tag = 666;
664 int rank, size, other, index;
665 MPI_Aint lb = 0, recvext = 0;
666 MPI_Request *requests;
668 rank = smpi_comm_rank(comm);
669 size = smpi_comm_size(comm);
670 // FIXME: check for errors
671 smpi_datatype_extent(recvtype, &lb, &recvext);
672 // Local copy from self
673 smpi_datatype_copy(sendbuf, sendcount, sendtype,
674 (char *)recvbuf + rank * recvcount * recvext, recvcount,
676 // Send/Recv buffers to/from others;
677 requests = xbt_new(MPI_Request, 2 * (size - 1));
679 for(other = 0; other < size; other++) {
682 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
685 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
686 recvcount, recvtype, other,
691 // Wait for completion of all comms.
692 smpi_mpi_startall(2 * (size - 1), requests);
693 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
697 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
698 MPI_Datatype sendtype, void *recvbuf,
699 int *recvcounts, int *displs,
700 MPI_Datatype recvtype, MPI_Comm comm)
702 int system_tag = 666;
703 int rank, size, other, index;
704 MPI_Aint lb = 0, recvext = 0;
705 MPI_Request *requests;
707 rank = smpi_comm_rank(comm);
708 size = smpi_comm_size(comm);
709 // FIXME: check for errors
710 smpi_datatype_extent(recvtype, &lb, &recvext);
711 // Local copy from self
712 smpi_datatype_copy(sendbuf, sendcount, sendtype,
713 (char *)recvbuf + displs[rank] * recvext,
714 recvcounts[rank], recvtype);
715 // Send buffers to others;
716 requests = xbt_new(MPI_Request, 2 * (size - 1));
718 for(other = 0; other < size; other++) {
721 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
725 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
726 recvtype, other, system_tag, comm);
730 // Wait for completion of all comms.
731 smpi_mpi_startall(2 * (size - 1), requests);
732 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
736 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
737 void *recvbuf, int recvcount, MPI_Datatype recvtype,
738 int root, MPI_Comm comm)
740 int system_tag = 666;
741 int rank, size, dst, index;
742 MPI_Aint lb = 0, sendext = 0;
743 MPI_Request *requests;
745 rank = smpi_comm_rank(comm);
746 size = smpi_comm_size(comm);
748 // Recv buffer from root
749 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
752 // FIXME: check for errors
753 smpi_datatype_extent(sendtype, &lb, &sendext);
754 // Local copy from root
755 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
756 sendcount, sendtype, recvbuf, recvcount, recvtype);
757 // Send buffers to receivers
758 requests = xbt_new(MPI_Request, size - 1);
760 for(dst = 0; dst < size; dst++) {
762 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
763 sendcount, sendtype, dst,
768 // Wait for completion of isend's.
769 smpi_mpi_startall(size - 1, requests);
770 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
775 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
776 MPI_Datatype sendtype, void *recvbuf, int recvcount,
777 MPI_Datatype recvtype, int root, MPI_Comm comm)
779 int system_tag = 666;
780 int rank, size, dst, index;
781 MPI_Aint lb = 0, sendext = 0;
782 MPI_Request *requests;
784 rank = smpi_comm_rank(comm);
785 size = smpi_comm_size(comm);
787 // Recv buffer from root
788 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
791 // FIXME: check for errors
792 smpi_datatype_extent(sendtype, &lb, &sendext);
793 // Local copy from root
794 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
795 sendtype, recvbuf, recvcount, recvtype);
796 // Send buffers to receivers
797 requests = xbt_new(MPI_Request, size - 1);
799 for(dst = 0; dst < size; dst++) {
802 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
803 sendtype, dst, system_tag, comm);
807 // Wait for completion of isend's.
808 smpi_mpi_startall(size - 1, requests);
809 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
814 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
815 MPI_Datatype datatype, MPI_Op op, int root,
818 int system_tag = 666;
819 int rank, size, src, index;
820 MPI_Aint lb = 0, dataext = 0;
821 MPI_Request *requests;
824 rank = smpi_comm_rank(comm);
825 size = smpi_comm_size(comm);
827 // Send buffer to root
828 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
830 // FIXME: check for errors
831 smpi_datatype_extent(datatype, &lb, &dataext);
832 // Local copy from root
833 if (sendbuf && recvbuf)
834 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
835 // Receive buffers from senders
836 //TODO: make a MPI_barrier here ?
837 requests = xbt_new(MPI_Request, size - 1);
838 tmpbufs = xbt_new(void *, size - 1);
840 for(src = 0; src < size; src++) {
842 // FIXME: possibly overkill we we have contiguous/noncontiguous data
844 tmpbufs[index] = xbt_malloc(count * dataext);
846 smpi_irecv_init(tmpbufs[index], count, datatype, src,
851 // Wait for completion of irecv's.
852 smpi_mpi_startall(size - 1, requests);
853 for(src = 0; src < size - 1; src++) {
854 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
855 XBT_DEBUG("finished waiting any request with index %d", index);
856 if(index == MPI_UNDEFINED) {
859 if(op) /* op can be MPI_OP_NULL that does nothing */
860 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
862 for(index = 0; index < size - 1; index++) {
863 xbt_free(tmpbufs[index]);
870 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
871 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
873 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
874 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
877 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
878 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
880 int system_tag = 666;
881 int rank, size, other, index;
882 MPI_Aint lb = 0, dataext = 0;
883 MPI_Request *requests;
886 rank = smpi_comm_rank(comm);
887 size = smpi_comm_size(comm);
889 // FIXME: check for errors
890 smpi_datatype_extent(datatype, &lb, &dataext);
892 // Local copy from self
893 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
895 // Send/Recv buffers to/from others;
896 requests = xbt_new(MPI_Request, size - 1);
897 tmpbufs = xbt_new(void *, rank);
899 for(other = 0; other < rank; other++) {
900 // FIXME: possibly overkill we we have contiguous/noncontiguous data
902 tmpbufs[index] = xbt_malloc(count * dataext);
904 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
908 for(other = rank + 1; other < size; other++) {
910 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
913 // Wait for completion of all comms.
914 smpi_mpi_startall(size - 1, requests);
915 for(other = 0; other < size - 1; other++) {
916 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
917 if(index == MPI_UNDEFINED) {
921 // #Request is below rank: it's a irecv
922 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
925 for(index = 0; index < rank; index++) {
926 xbt_free(tmpbufs[index]);