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"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi, "Logging specific to SMPI (base)");
19 static int match_recv(void* a, void* b, smx_action_t ignored) {
20 MPI_Request ref = (MPI_Request)a;
21 MPI_Request req = (MPI_Request)b;
22 XBT_DEBUG("Trying to match a recv of src %d against %d, tag %d against %d",ref->src,req->src, ref->tag, req->tag);
24 xbt_assert(ref, "Cannot match recv against null reference");
25 xbt_assert(req, "Cannot match recv against null request");
26 return (ref->src == MPI_ANY_SOURCE || req->src == ref->src)
27 && (ref->tag == MPI_ANY_TAG || req->tag == ref->tag);
30 static int match_send(void* a, void* b,smx_action_t ignored) {
31 MPI_Request ref = (MPI_Request)a;
32 MPI_Request req = (MPI_Request)b;
33 XBT_DEBUG("Trying to match a send of src %d against %d, tag %d against %d",ref->src,req->src, ref->tag, req->tag);
34 xbt_assert(ref, "Cannot match send against null reference");
35 xbt_assert(req, "Cannot match send against null request");
36 return (req->src == MPI_ANY_SOURCE || req->src == ref->src)
37 && (req->tag == MPI_ANY_TAG || req->tag == ref->tag);
40 static MPI_Request build_request(void *buf, int count,
41 MPI_Datatype datatype, int src, int dst,
42 int tag, MPI_Comm comm, unsigned flags)
48 request = xbt_new(s_smpi_mpi_request_t, 1);
50 s_smpi_subtype_t *subtype = datatype->substruct;
52 if(datatype->has_subtype == 1){
53 // This part handles the problem of non-contignous memory
55 buf = malloc(count*smpi_datatype_size(datatype));
57 subtype->serialize(old_buf, buf, count, datatype->substruct);
62 // This part handles the problem of non-contignous memory (for the
63 // unserialisation at the reception)
64 request->old_buf = old_buf;
65 request->old_type = datatype;
67 request->size = smpi_datatype_size(datatype) * count;
72 request->action = NULL;
73 request->flags = flags;
74 request->detached = 0;
83 void smpi_empty_status(MPI_Status * status) {
84 if(status != MPI_STATUS_IGNORE) {
85 status->MPI_SOURCE=MPI_ANY_SOURCE;
86 status->MPI_TAG=MPI_ANY_TAG;
91 void smpi_action_trace_run(char *path)
95 xbt_dict_cursor_t cursor;
99 action_fp = fopen(path, "r");
100 xbt_assert(action_fp != NULL, "Cannot open %s: %s", path,
104 if (!xbt_dict_is_empty(action_queues)) {
106 ("Not all actions got consumed. If the simulation ended successfully (without deadlock), you may want to add new processes to your deployment file.");
109 xbt_dict_foreach(action_queues, cursor, name, todo) {
110 XBT_WARN("Still %lu actions for %s", xbt_dynar_length(todo), name);
116 xbt_dict_free(&action_queues);
117 action_queues = xbt_dict_new_homogeneous(NULL);
120 static void smpi_mpi_request_free_voidp(void* request)
122 MPI_Request req = request;
123 smpi_mpi_request_free(&req);
126 /* MPI Low level calls */
127 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
128 int dst, int tag, MPI_Comm comm)
130 MPI_Request request =
131 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
132 comm, PERSISTENT | SEND);
137 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
138 int src, int tag, MPI_Comm comm)
140 MPI_Request request =
141 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
142 comm, PERSISTENT | RECV);
147 void smpi_mpi_start(MPI_Request request)
151 xbt_assert(!request->action,
152 "Cannot (re)start a non-finished communication");
153 if(request->flags & RECV) {
154 print_request("New recv", request);
155 if (request->size < xbt_cfg_get_int(_surf_cfg_set, "smpi/async_small_thres"))
156 mailbox = smpi_process_mailbox_small();
158 mailbox = smpi_process_mailbox();
160 // FIXME: SIMIX does not yet support non-contiguous datatypes
161 request->action = simcall_comm_irecv(mailbox, request->buf, &request->size, &match_recv, request);
164 int receiver = smpi_group_index(smpi_comm_group(request->comm), request->dst);
165 /* if(receiver == MPI_UNDEFINED) {*/
166 /* XBT_WARN("Trying to send a message to a wrong rank");*/
169 print_request("New send", request);
170 if (request->size < xbt_cfg_get_int(_surf_cfg_set, "smpi/async_small_thres")) { // eager mode
171 mailbox = smpi_process_remote_mailbox_small(receiver);
173 XBT_DEBUG("Send request %p is not in the permanent receive mailbox (buf: %p)",request,request->buf);
174 mailbox = smpi_process_remote_mailbox(receiver);
176 if (request->size < 64*1024 ) { //(FIXME: this limit should be configurable)
178 if(request->old_type->has_subtype == 0){
179 oldbuf = request->buf;
180 request->detached = 1;
181 request->buf = malloc(request->size);
183 memcpy(request->buf,oldbuf,request->size);
185 XBT_DEBUG("Send request %p is detached; buf %p copied into %p",request,oldbuf,request->buf);
189 simcall_comm_isend(mailbox, request->size, -1.0,
190 request->buf, request->size,
192 &smpi_mpi_request_free_voidp, // how to free the userdata if a detached send fails
194 // detach if msg size < eager/rdv switch limit
198 /* FIXME: detached sends are not traceable (request->action == NULL) */
200 simcall_set_category(request->action, TRACE_internal_smpi_get_category());
207 void smpi_mpi_startall(int count, MPI_Request * requests)
211 for(i = 0; i < count; i++) {
212 smpi_mpi_start(requests[i]);
216 void smpi_mpi_request_free(MPI_Request * request)
219 *request = MPI_REQUEST_NULL;
222 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
223 int dst, int tag, MPI_Comm comm)
225 MPI_Request request =
226 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
227 comm, NON_PERSISTENT | SEND);
232 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
233 int dst, int tag, MPI_Comm comm)
235 MPI_Request request =
236 smpi_isend_init(buf, count, datatype, dst, tag, comm);
238 smpi_mpi_start(request);
242 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
243 int src, int tag, MPI_Comm comm)
245 MPI_Request request =
246 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
247 comm, NON_PERSISTENT | RECV);
251 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
252 int src, int tag, MPI_Comm comm)
254 MPI_Request request =
255 smpi_irecv_init(buf, count, datatype, src, tag, comm);
257 smpi_mpi_start(request);
261 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
262 int tag, MPI_Comm comm, MPI_Status * status)
265 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
266 smpi_mpi_wait(&request, status);
271 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
272 int tag, MPI_Comm comm)
276 request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
277 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
280 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
281 int dst, int sendtag, void *recvbuf, int recvcount,
282 MPI_Datatype recvtype, int src, int recvtag,
283 MPI_Comm comm, MPI_Status * status)
285 MPI_Request requests[2];
289 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
291 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
292 smpi_mpi_startall(2, requests);
293 smpi_mpi_waitall(2, requests, stats);
294 if(status != MPI_STATUS_IGNORE) {
295 // Copy receive status
296 memcpy(status, &stats[1], sizeof(MPI_Status));
300 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
302 return status->count / smpi_datatype_size(datatype);
305 static void finish_wait(MPI_Request * request, MPI_Status * status)
307 MPI_Request req = *request;
308 // if we have a sender, we should use its data, and not the data from the receive
310 (req->src==MPI_ANY_SOURCE || req->tag== MPI_ANY_TAG))
311 req = (MPI_Request)SIMIX_comm_get_src_data((*request)->action);
313 if(status != MPI_STATUS_IGNORE) {
314 status->MPI_SOURCE = req->src;
315 status->MPI_TAG = req->tag;
316 status->MPI_ERROR = MPI_SUCCESS;
317 // FIXME: really this should just contain the count of receive-type blocks,
319 status->count = req->size;
323 print_request("Finishing", req);
324 MPI_Datatype datatype = req->old_type;
325 if(datatype->has_subtype == 1){
326 // This part handles the problem of non-contignous memory
327 // the unserialization at the reception
328 s_smpi_subtype_t *subtype = datatype->substruct;
329 if(req->flags & RECV) {
330 subtype->unserialize(req->buf, req->old_buf, req->size/smpi_datatype_size(datatype) , datatype->substruct);
332 if(req->detached == 0) free(req->buf);
335 if(req->flags & NON_PERSISTENT) {
336 smpi_mpi_request_free(request);
342 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
345 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or smpi_mpi_testall before)
346 if ((*request)->action == NULL)
349 flag = simcall_comm_test((*request)->action);
351 finish_wait(request, status);
353 smpi_empty_status(status);
358 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
365 *index = MPI_UNDEFINED;
368 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
369 map = xbt_new(int, count);
371 for(i = 0; i < count; i++) {
372 if((requests[i]!=MPI_REQUEST_NULL) && requests[i]->action) {
373 xbt_dynar_push(comms, &requests[i]->action);
379 i = simcall_comm_testany(comms);
380 // not MPI_UNDEFINED, as this is a simix return code
383 finish_wait(&requests[*index], status);
387 //all requests are null or inactive, return true
389 smpi_empty_status(status);
392 xbt_dynar_free(&comms);
399 int smpi_mpi_testall(int count, MPI_Request requests[],
403 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
406 for(i=0; i<count; i++){
407 if(requests[i]!= MPI_REQUEST_NULL){
408 if (smpi_mpi_test(&requests[i], pstat)!=1){
412 smpi_empty_status(pstat);
414 if(status != MPI_STATUSES_IGNORE) {
415 memcpy(&status[i], pstat, sizeof(*pstat));
421 void smpi_mpi_probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
423 //FIXME find another wait to avoid busy waiting ?
424 // the issue here is that we have to wait on a nonexistent comm
426 smpi_mpi_iprobe(source, tag, comm, &flag, status);
427 XBT_DEBUG("Busy Waiting on probing : %d", flag);
429 simcall_process_sleep(0.0001);
434 void smpi_mpi_iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
435 MPI_Request request =build_request(NULL, 0, MPI_CHAR, source, smpi_comm_rank(comm), tag,
436 comm, NON_PERSISTENT | RECV);
438 // behave like a receive, but don't do it
441 print_request("New iprobe", request);
442 // We have to test both mailboxes as we don't know if we will receive one one or another
443 if (xbt_cfg_get_int(_surf_cfg_set, "smpi/async_small_thres")>0){
444 mailbox = smpi_process_mailbox_small();
445 XBT_DEBUG("trying to probe the perm recv mailbox");
446 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
448 if (request->action==NULL){
449 mailbox = smpi_process_mailbox();
450 XBT_DEBUG("trying to probe the other mailbox");
451 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
455 MPI_Request req = (MPI_Request)SIMIX_comm_get_src_data(request->action);
457 if(status != MPI_STATUS_IGNORE) {
458 status->MPI_SOURCE = req->src;
459 status->MPI_TAG = req->tag;
460 status->MPI_ERROR = MPI_SUCCESS;
461 status->count = req->size;
465 smpi_mpi_request_free(&request);
470 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
472 print_request("Waiting", *request);
473 if ((*request)->action != NULL) { // this is not a detached send
474 simcall_comm_wait((*request)->action, -1.0);
475 finish_wait(request, status);
477 // FIXME for a detached send, finish_wait is not called:
480 int smpi_mpi_waitany(int count, MPI_Request requests[],
487 index = MPI_UNDEFINED;
489 // Wait for a request to complete
490 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
491 map = xbt_new(int, count);
493 XBT_DEBUG("Wait for one of");
494 for(i = 0; i < count; i++) {
495 if((requests[i] != MPI_REQUEST_NULL) && (requests[i]->action != NULL)) {
496 print_request("Waiting any ", requests[i]);
497 xbt_dynar_push(comms, &requests[i]->action);
503 i = simcall_comm_waitany(comms);
505 // not MPI_UNDEFINED, as this is a simix return code
508 finish_wait(&requests[index], status);
512 xbt_dynar_free(&comms);
515 if (index==MPI_UNDEFINED)
516 smpi_empty_status(status);
521 void smpi_mpi_waitall(int count, MPI_Request requests[],
526 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
527 //tag invalid requests in the set
528 for(c = 0; c < count; c++) {
529 if(requests[c]==MPI_REQUEST_NULL || requests[c]->dst == MPI_PROC_NULL ){
530 if(status != MPI_STATUSES_IGNORE)
531 smpi_empty_status(&status[c]);
532 }else if(requests[c]->src == MPI_PROC_NULL ){
533 if(status != MPI_STATUSES_IGNORE) {
534 smpi_empty_status(&status[c]);
535 status[c].MPI_SOURCE=MPI_PROC_NULL;
540 for(c = 0; c < count; c++) {
542 smpi_mpi_wait(&requests[c], pstat);
545 index = smpi_mpi_waitany(count, requests, pstat);
546 if(index == MPI_UNDEFINED) {
549 if(status != MPI_STATUSES_IGNORE) {
550 memcpy(&status[index], pstat, sizeof(*pstat));
557 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
562 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
565 for(i = 0; i < incount; i++)
567 index=smpi_mpi_waitany(incount, requests, pstat);
568 if(index!=MPI_UNDEFINED){
569 indices[count] = index;
571 if(status != MPI_STATUSES_IGNORE) {
572 memcpy(&status[index], pstat, sizeof(*pstat));
575 return MPI_UNDEFINED;
581 int smpi_mpi_testsome(int incount, MPI_Request requests[], int *indices,
584 int i, count, count_dead;
586 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
590 for(i = 0; i < incount; i++) {
591 if((requests[i] != MPI_REQUEST_NULL)) {
592 if(smpi_mpi_test(&requests[i], pstat)) {
595 if(status != MPI_STATUSES_IGNORE) {
596 memcpy(&status[i], pstat, sizeof(*pstat));
603 if(count_dead==incount)return MPI_UNDEFINED;
607 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
610 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
611 nary_tree_bcast(buf, count, datatype, root, comm, 4);
614 void smpi_mpi_barrier(MPI_Comm comm)
616 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
617 nary_tree_barrier(comm, 4);
620 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
621 void *recvbuf, int recvcount, MPI_Datatype recvtype,
622 int root, MPI_Comm comm)
624 int system_tag = 666;
625 int rank, size, src, index;
626 MPI_Aint lb = 0, recvext = 0;
627 MPI_Request *requests;
629 rank = smpi_comm_rank(comm);
630 size = smpi_comm_size(comm);
632 // Send buffer to root
633 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
635 // FIXME: check for errors
636 smpi_datatype_extent(recvtype, &lb, &recvext);
637 // Local copy from root
638 smpi_datatype_copy(sendbuf, sendcount, sendtype,
639 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
640 // Receive buffers from senders
641 requests = xbt_new(MPI_Request, size - 1);
643 for(src = 0; src < size; src++) {
645 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
647 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_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
659 void *recvbuf, int *recvcounts, int *displs,
660 MPI_Datatype recvtype, int root, MPI_Comm comm)
662 int system_tag = 666;
663 int rank, size, src, index;
664 MPI_Aint lb = 0, recvext = 0;
665 MPI_Request *requests;
667 rank = smpi_comm_rank(comm);
668 size = smpi_comm_size(comm);
670 // Send buffer to root
671 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
673 // FIXME: check for errors
674 smpi_datatype_extent(recvtype, &lb, &recvext);
675 // Local copy from root
676 smpi_datatype_copy(sendbuf, sendcount, sendtype,
677 (char *)recvbuf + displs[root] * recvext,
678 recvcounts[root], recvtype);
679 // Receive buffers from senders
680 requests = xbt_new(MPI_Request, size - 1);
682 for(src = 0; src < size; src++) {
685 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
686 recvcounts[src], recvtype, src, system_tag, comm);
690 // Wait for completion of irecv's.
691 smpi_mpi_startall(size - 1, requests);
692 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
697 void smpi_mpi_allgather(void *sendbuf, int sendcount,
698 MPI_Datatype sendtype, void *recvbuf,
699 int recvcount, MPI_Datatype recvtype,
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 + rank * recvcount * recvext, recvcount,
715 // Send/Recv buffers to/from 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,
724 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
725 recvcount, recvtype, other,
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_allgatherv(void *sendbuf, int sendcount,
737 MPI_Datatype sendtype, void *recvbuf,
738 int *recvcounts, int *displs,
739 MPI_Datatype recvtype, MPI_Comm comm)
741 int system_tag = 666;
742 int rank, size, other, index;
743 MPI_Aint lb = 0, recvext = 0;
744 MPI_Request *requests;
746 rank = smpi_comm_rank(comm);
747 size = smpi_comm_size(comm);
748 // FIXME: check for errors
749 smpi_datatype_extent(recvtype, &lb, &recvext);
750 // Local copy from self
751 smpi_datatype_copy(sendbuf, sendcount, sendtype,
752 (char *)recvbuf + displs[rank] * recvext,
753 recvcounts[rank], recvtype);
754 // Send buffers to others;
755 requests = xbt_new(MPI_Request, 2 * (size - 1));
757 for(other = 0; other < size; other++) {
760 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
764 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
765 recvtype, other, system_tag, comm);
769 // Wait for completion of all comms.
770 smpi_mpi_startall(2 * (size - 1), requests);
771 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
775 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
776 void *recvbuf, int recvcount, MPI_Datatype recvtype,
777 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 + root * sendcount * sendext,
795 sendcount, sendtype, recvbuf, recvcount, recvtype);
796 // Send buffers to receivers
797 requests = xbt_new(MPI_Request, size - 1);
799 for(dst = 0; dst < size; dst++) {
801 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
802 sendcount, sendtype, dst,
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_scatterv(void *sendbuf, int *sendcounts, int *displs,
815 MPI_Datatype sendtype, void *recvbuf, int recvcount,
816 MPI_Datatype recvtype, int root, MPI_Comm comm)
818 int system_tag = 666;
819 int rank, size, dst, index;
820 MPI_Aint lb = 0, sendext = 0;
821 MPI_Request *requests;
823 rank = smpi_comm_rank(comm);
824 size = smpi_comm_size(comm);
826 // Recv buffer from root
827 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
830 // FIXME: check for errors
831 smpi_datatype_extent(sendtype, &lb, &sendext);
832 // Local copy from root
833 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
834 sendtype, recvbuf, recvcount, recvtype);
835 // Send buffers to receivers
836 requests = xbt_new(MPI_Request, size - 1);
838 for(dst = 0; dst < size; dst++) {
841 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
842 sendtype, dst, system_tag, comm);
846 // Wait for completion of isend's.
847 smpi_mpi_startall(size - 1, requests);
848 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
853 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
854 MPI_Datatype datatype, MPI_Op op, int root,
857 int system_tag = 666;
858 int rank, size, src, index;
859 MPI_Aint lb = 0, dataext = 0;
860 MPI_Request *requests;
863 rank = smpi_comm_rank(comm);
864 size = smpi_comm_size(comm);
866 // Send buffer to root
867 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
869 // FIXME: check for errors
870 smpi_datatype_extent(datatype, &lb, &dataext);
871 // Local copy from root
872 if (sendbuf && recvbuf)
873 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
874 // Receive buffers from senders
875 //TODO: make a MPI_barrier here ?
876 requests = xbt_new(MPI_Request, size - 1);
877 tmpbufs = xbt_new(void *, size - 1);
879 for(src = 0; src < size; src++) {
881 // FIXME: possibly overkill we we have contiguous/noncontiguous data
883 tmpbufs[index] = xbt_malloc(count * dataext);
885 smpi_irecv_init(tmpbufs[index], count, datatype, src,
890 // Wait for completion of irecv's.
891 smpi_mpi_startall(size - 1, requests);
892 for(src = 0; src < size - 1; src++) {
893 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
894 XBT_DEBUG("finished waiting any request with index %d", index);
895 if(index == MPI_UNDEFINED) {
898 if(op) /* op can be MPI_OP_NULL that does nothing */
899 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
901 for(index = 0; index < size - 1; index++) {
902 xbt_free(tmpbufs[index]);
909 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
910 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
912 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
913 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
916 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
917 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
919 int system_tag = 666;
920 int rank, size, other, index;
921 MPI_Aint lb = 0, dataext = 0;
922 MPI_Request *requests;
925 rank = smpi_comm_rank(comm);
926 size = smpi_comm_size(comm);
928 // FIXME: check for errors
929 smpi_datatype_extent(datatype, &lb, &dataext);
931 // Local copy from self
932 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
934 // Send/Recv buffers to/from others;
935 requests = xbt_new(MPI_Request, size - 1);
936 tmpbufs = xbt_new(void *, rank);
938 for(other = 0; other < rank; other++) {
939 // FIXME: possibly overkill we we have contiguous/noncontiguous data
941 tmpbufs[index] = xbt_malloc(count * dataext);
943 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
947 for(other = rank + 1; other < size; other++) {
949 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
952 // Wait for completion of all comms.
953 smpi_mpi_startall(size - 1, requests);
954 for(other = 0; other < size - 1; other++) {
955 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
956 if(index == MPI_UNDEFINED) {
960 // #Request is below rank: it's a irecv
961 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
964 for(index = 0; index < rank; index++) {
965 xbt_free(tmpbufs[index]);