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 < surf_cfg_get_int("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 < surf_cfg_get_int("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;
182 request->buf = malloc(request->size);
183 memcpy(request->buf,oldbuf,request->size);
186 XBT_DEBUG("Send request %p is detached; buf %p copied into %p",request,oldbuf,request->buf);
190 simcall_comm_isend(mailbox, request->size, -1.0,
191 request->buf, request->size,
193 &smpi_mpi_request_free_voidp, // how to free the userdata if a detached send fails
195 // detach if msg size < eager/rdv switch limit
199 /* FIXME: detached sends are not traceable (request->action == NULL) */
201 simcall_set_category(request->action, TRACE_internal_smpi_get_category());
208 void smpi_mpi_startall(int count, MPI_Request * requests)
212 for(i = 0; i < count; i++) {
213 smpi_mpi_start(requests[i]);
217 void smpi_mpi_request_free(MPI_Request * request)
220 *request = MPI_REQUEST_NULL;
223 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
224 int dst, int tag, MPI_Comm comm)
226 MPI_Request request =
227 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
228 comm, NON_PERSISTENT | SEND);
233 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
234 int dst, int tag, MPI_Comm comm)
236 MPI_Request request =
237 smpi_isend_init(buf, count, datatype, dst, tag, comm);
239 smpi_mpi_start(request);
243 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
244 int src, int tag, MPI_Comm comm)
246 MPI_Request request =
247 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
248 comm, NON_PERSISTENT | RECV);
252 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
253 int src, int tag, MPI_Comm comm)
255 MPI_Request request =
256 smpi_irecv_init(buf, count, datatype, src, tag, comm);
258 smpi_mpi_start(request);
262 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
263 int tag, MPI_Comm comm, MPI_Status * status)
266 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
267 smpi_mpi_wait(&request, status);
272 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
273 int tag, MPI_Comm comm)
277 request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
278 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
281 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
282 int dst, int sendtag, void *recvbuf, int recvcount,
283 MPI_Datatype recvtype, int src, int recvtag,
284 MPI_Comm comm, MPI_Status * status)
286 MPI_Request requests[2];
290 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
292 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
293 smpi_mpi_startall(2, requests);
294 smpi_mpi_waitall(2, requests, stats);
295 if(status != MPI_STATUS_IGNORE) {
296 // Copy receive status
297 memcpy(status, &stats[1], sizeof(MPI_Status));
301 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
303 return status->count / smpi_datatype_size(datatype);
306 static void finish_wait(MPI_Request * request, MPI_Status * status)
308 MPI_Request req = *request;
309 // if we have a sender, we should use its data, and not the data from the receive
310 //FIXME : mail fail if req->action has already been freed, the pointer being invalid
312 (req->src==MPI_ANY_SOURCE || req->tag== MPI_ANY_TAG))
313 req = (MPI_Request)SIMIX_comm_get_src_data((*request)->action);
315 if(status != MPI_STATUS_IGNORE) {
316 status->MPI_SOURCE = req->src;
317 status->MPI_TAG = req->tag;
318 //if((*request)->action && ((MPI_Request)SIMIX_comm_get_src_data((*request)->action))->size == (*request)->size)
319 status->MPI_ERROR = MPI_SUCCESS;
320 //else status->MPI_ERROR = MPI_ERR_TRUNCATE;
321 // this handles the case were size in receive differs from size in send
322 // FIXME: really this should just contain the count of receive-type blocks,
324 status->count = req->size;
328 print_request("Finishing", req);
329 MPI_Datatype datatype = req->old_type;
330 if(datatype->has_subtype == 1){
331 // This part handles the problem of non-contignous memory
332 // the unserialization at the reception
333 s_smpi_subtype_t *subtype = datatype->substruct;
334 if(req->flags & RECV) {
335 subtype->unserialize(req->buf, req->old_buf, req->size/smpi_datatype_size(datatype) , datatype->substruct);
337 if(req->detached == 0) free(req->buf);
340 if(req->flags & NON_PERSISTENT) {
341 smpi_mpi_request_free(request);
347 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
350 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or smpi_mpi_testall before)
351 if ((*request)->action == NULL)
354 flag = simcall_comm_test((*request)->action);
356 finish_wait(request, status);
358 smpi_empty_status(status);
363 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
370 *index = MPI_UNDEFINED;
373 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
374 map = xbt_new(int, count);
376 for(i = 0; i < count; i++) {
377 if((requests[i]!=MPI_REQUEST_NULL) && requests[i]->action) {
378 xbt_dynar_push(comms, &requests[i]->action);
384 i = simcall_comm_testany(comms);
385 // not MPI_UNDEFINED, as this is a simix return code
388 finish_wait(&requests[*index], status);
392 //all requests are null or inactive, return true
394 smpi_empty_status(status);
397 xbt_dynar_free(&comms);
404 int smpi_mpi_testall(int count, MPI_Request requests[],
408 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
411 for(i=0; i<count; i++){
412 if(requests[i]!= MPI_REQUEST_NULL){
413 if (smpi_mpi_test(&requests[i], pstat)!=1){
417 smpi_empty_status(pstat);
419 if(status != MPI_STATUSES_IGNORE) {
420 memcpy(&status[i], pstat, sizeof(*pstat));
426 void smpi_mpi_probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
428 //FIXME find another wait to avoid busy waiting ?
429 // the issue here is that we have to wait on a nonexistent comm
431 smpi_mpi_iprobe(source, tag, comm, &flag, status);
432 XBT_DEBUG("Busy Waiting on probing : %d", flag);
434 simcall_process_sleep(0.0001);
439 void smpi_mpi_iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
440 MPI_Request request =build_request(NULL, 0, MPI_CHAR, source, smpi_comm_rank(comm), tag,
441 comm, NON_PERSISTENT | RECV);
443 // behave like a receive, but don't do it
446 print_request("New iprobe", request);
447 // We have to test both mailboxes as we don't know if we will receive one one or another
448 if (surf_cfg_get_int("smpi/async_small_thres")>0){
449 mailbox = smpi_process_mailbox_small();
450 XBT_DEBUG("trying to probe the perm recv mailbox");
451 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
453 if (request->action==NULL){
454 mailbox = smpi_process_mailbox();
455 XBT_DEBUG("trying to probe the other mailbox");
456 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
460 MPI_Request req = (MPI_Request)SIMIX_comm_get_src_data(request->action);
462 if(status != MPI_STATUS_IGNORE) {
463 status->MPI_SOURCE = req->src;
464 status->MPI_TAG = req->tag;
465 status->MPI_ERROR = MPI_SUCCESS;
466 status->count = req->size;
470 smpi_mpi_request_free(&request);
475 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
477 print_request("Waiting", *request);
478 if ((*request)->action != NULL) { // this is not a detached send
479 simcall_comm_wait((*request)->action, -1.0);
480 finish_wait(request, status);
482 // FIXME for a detached send, finish_wait is not called:
485 int smpi_mpi_waitany(int count, MPI_Request requests[],
492 index = MPI_UNDEFINED;
494 // Wait for a request to complete
495 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
496 map = xbt_new(int, count);
498 XBT_DEBUG("Wait for one of");
499 for(i = 0; i < count; i++) {
500 if((requests[i] != MPI_REQUEST_NULL) && (requests[i]->action != NULL)) {
501 print_request("Waiting any ", requests[i]);
502 xbt_dynar_push(comms, &requests[i]->action);
508 i = simcall_comm_waitany(comms);
510 // not MPI_UNDEFINED, as this is a simix return code
513 finish_wait(&requests[index], status);
517 xbt_dynar_free(&comms);
520 if (index==MPI_UNDEFINED)
521 smpi_empty_status(status);
526 int smpi_mpi_waitall(int count, MPI_Request requests[],
531 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
532 int retvalue=MPI_SUCCESS;
533 //tag invalid requests in the set
534 for(c = 0; c < count; c++) {
535 if(requests[c]==MPI_REQUEST_NULL || requests[c]->dst == MPI_PROC_NULL ){
536 if(status != MPI_STATUSES_IGNORE)
537 smpi_empty_status(&status[c]);
538 }else if(requests[c]->src == MPI_PROC_NULL ){
539 if(status != MPI_STATUSES_IGNORE) {
540 smpi_empty_status(&status[c]);
541 status[c].MPI_SOURCE=MPI_PROC_NULL;
546 for(c = 0; c < count; c++) {
548 smpi_mpi_wait(&requests[c], pstat);
551 index = smpi_mpi_waitany(count, requests, pstat);
552 if(index == MPI_UNDEFINED) {
555 if(status != MPI_STATUSES_IGNORE) {
556 memcpy(&status[index], pstat, sizeof(*pstat));
557 if(status[index].MPI_ERROR==MPI_ERR_TRUNCATE)retvalue=MPI_ERR_IN_STATUS;
565 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
570 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
573 for(i = 0; i < incount; i++)
575 index=smpi_mpi_waitany(incount, requests, pstat);
576 if(index!=MPI_UNDEFINED){
577 indices[count] = index;
579 if(status != MPI_STATUSES_IGNORE) {
580 memcpy(&status[index], pstat, sizeof(*pstat));
583 return MPI_UNDEFINED;
589 int smpi_mpi_testsome(int incount, MPI_Request requests[], int *indices,
592 int i, count, count_dead;
594 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
598 for(i = 0; i < incount; i++) {
599 if((requests[i] != MPI_REQUEST_NULL)) {
600 if(smpi_mpi_test(&requests[i], pstat)) {
603 if(status != MPI_STATUSES_IGNORE) {
604 memcpy(&status[i], pstat, sizeof(*pstat));
611 if(count_dead==incount)return MPI_UNDEFINED;
615 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
618 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
619 nary_tree_bcast(buf, count, datatype, root, comm, 4);
622 void smpi_mpi_barrier(MPI_Comm comm)
624 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
625 nary_tree_barrier(comm, 4);
628 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
629 void *recvbuf, int recvcount, MPI_Datatype recvtype,
630 int root, MPI_Comm comm)
632 int system_tag = 666;
633 int rank, size, src, index;
634 MPI_Aint lb = 0, recvext = 0;
635 MPI_Request *requests;
637 rank = smpi_comm_rank(comm);
638 size = smpi_comm_size(comm);
640 // Send buffer to root
641 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
643 // FIXME: check for errors
644 smpi_datatype_extent(recvtype, &lb, &recvext);
645 // Local copy from root
646 smpi_datatype_copy(sendbuf, sendcount, sendtype,
647 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
648 // Receive buffers from senders
649 requests = xbt_new(MPI_Request, size - 1);
651 for(src = 0; src < size; src++) {
653 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
655 src, system_tag, comm);
659 // Wait for completion of irecv's.
660 smpi_mpi_startall(size - 1, requests);
661 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
666 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
667 void *recvbuf, int *recvcounts, int *displs,
668 MPI_Datatype recvtype, int root, MPI_Comm comm)
670 int system_tag = 666;
671 int rank, size, src, index;
672 MPI_Aint lb = 0, recvext = 0;
673 MPI_Request *requests;
675 rank = smpi_comm_rank(comm);
676 size = smpi_comm_size(comm);
678 // Send buffer to root
679 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
681 // FIXME: check for errors
682 smpi_datatype_extent(recvtype, &lb, &recvext);
683 // Local copy from root
684 smpi_datatype_copy(sendbuf, sendcount, sendtype,
685 (char *)recvbuf + displs[root] * recvext,
686 recvcounts[root], recvtype);
687 // Receive buffers from senders
688 requests = xbt_new(MPI_Request, size - 1);
690 for(src = 0; src < size; src++) {
693 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
694 recvcounts[src], recvtype, src, system_tag, comm);
698 // Wait for completion of irecv's.
699 smpi_mpi_startall(size - 1, requests);
700 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
705 void smpi_mpi_allgather(void *sendbuf, int sendcount,
706 MPI_Datatype sendtype, void *recvbuf,
707 int recvcount, MPI_Datatype recvtype,
710 int system_tag = 666;
711 int rank, size, other, index;
712 MPI_Aint lb = 0, recvext = 0;
713 MPI_Request *requests;
715 rank = smpi_comm_rank(comm);
716 size = smpi_comm_size(comm);
717 // FIXME: check for errors
718 smpi_datatype_extent(recvtype, &lb, &recvext);
719 // Local copy from self
720 smpi_datatype_copy(sendbuf, sendcount, sendtype,
721 (char *)recvbuf + rank * recvcount * recvext, recvcount,
723 // Send/Recv buffers to/from others;
724 requests = xbt_new(MPI_Request, 2 * (size - 1));
726 for(other = 0; other < size; other++) {
729 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
732 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
733 recvcount, recvtype, other,
738 // Wait for completion of all comms.
739 smpi_mpi_startall(2 * (size - 1), requests);
740 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
744 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
745 MPI_Datatype sendtype, void *recvbuf,
746 int *recvcounts, int *displs,
747 MPI_Datatype recvtype, MPI_Comm comm)
749 int system_tag = 666;
750 int rank, size, other, index;
751 MPI_Aint lb = 0, recvext = 0;
752 MPI_Request *requests;
754 rank = smpi_comm_rank(comm);
755 size = smpi_comm_size(comm);
756 // FIXME: check for errors
757 smpi_datatype_extent(recvtype, &lb, &recvext);
758 // Local copy from self
759 smpi_datatype_copy(sendbuf, sendcount, sendtype,
760 (char *)recvbuf + displs[rank] * recvext,
761 recvcounts[rank], recvtype);
762 // Send buffers to others;
763 requests = xbt_new(MPI_Request, 2 * (size - 1));
765 for(other = 0; other < size; other++) {
768 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
772 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
773 recvtype, other, system_tag, comm);
777 // Wait for completion of all comms.
778 smpi_mpi_startall(2 * (size - 1), requests);
779 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
783 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
784 void *recvbuf, int recvcount, MPI_Datatype recvtype,
785 int root, MPI_Comm comm)
787 int system_tag = 666;
788 int rank, size, dst, index;
789 MPI_Aint lb = 0, sendext = 0;
790 MPI_Request *requests;
792 rank = smpi_comm_rank(comm);
793 size = smpi_comm_size(comm);
795 // Recv buffer from root
796 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
799 // FIXME: check for errors
800 smpi_datatype_extent(sendtype, &lb, &sendext);
801 // Local copy from root
802 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
803 sendcount, sendtype, recvbuf, recvcount, recvtype);
804 // Send buffers to receivers
805 requests = xbt_new(MPI_Request, size - 1);
807 for(dst = 0; dst < size; dst++) {
809 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
810 sendcount, sendtype, dst,
815 // Wait for completion of isend's.
816 smpi_mpi_startall(size - 1, requests);
817 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
822 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
823 MPI_Datatype sendtype, void *recvbuf, int recvcount,
824 MPI_Datatype recvtype, int root, MPI_Comm comm)
826 int system_tag = 666;
827 int rank, size, dst, index;
828 MPI_Aint lb = 0, sendext = 0;
829 MPI_Request *requests;
831 rank = smpi_comm_rank(comm);
832 size = smpi_comm_size(comm);
834 // Recv buffer from root
835 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
838 // FIXME: check for errors
839 smpi_datatype_extent(sendtype, &lb, &sendext);
840 // Local copy from root
841 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
842 sendtype, recvbuf, recvcount, recvtype);
843 // Send buffers to receivers
844 requests = xbt_new(MPI_Request, size - 1);
846 for(dst = 0; dst < size; dst++) {
849 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
850 sendtype, dst, system_tag, comm);
854 // Wait for completion of isend's.
855 smpi_mpi_startall(size - 1, requests);
856 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
861 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
862 MPI_Datatype datatype, MPI_Op op, int root,
865 int system_tag = 666;
866 int rank, size, src, index;
867 MPI_Aint lb = 0, dataext = 0;
868 MPI_Request *requests;
871 rank = smpi_comm_rank(comm);
872 size = smpi_comm_size(comm);
874 // Send buffer to root
875 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
877 // FIXME: check for errors
878 smpi_datatype_extent(datatype, &lb, &dataext);
879 // Local copy from root
880 if (sendbuf && recvbuf)
881 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
882 // Receive buffers from senders
883 //TODO: make a MPI_barrier here ?
884 requests = xbt_new(MPI_Request, size - 1);
885 tmpbufs = xbt_new(void *, size - 1);
887 for(src = 0; src < size; src++) {
889 // FIXME: possibly overkill we we have contiguous/noncontiguous data
891 tmpbufs[index] = xbt_malloc(count * dataext);
893 smpi_irecv_init(tmpbufs[index], count, datatype, src,
898 // Wait for completion of irecv's.
899 smpi_mpi_startall(size - 1, requests);
900 for(src = 0; src < size - 1; src++) {
901 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
902 XBT_DEBUG("finished waiting any request with index %d", index);
903 if(index == MPI_UNDEFINED) {
906 if(op) /* op can be MPI_OP_NULL that does nothing */
907 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
909 for(index = 0; index < size - 1; index++) {
910 xbt_free(tmpbufs[index]);
917 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
918 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
920 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
921 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
924 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
925 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
927 int system_tag = 666;
928 int rank, size, other, index;
929 MPI_Aint lb = 0, dataext = 0;
930 MPI_Request *requests;
933 rank = smpi_comm_rank(comm);
934 size = smpi_comm_size(comm);
936 // FIXME: check for errors
937 smpi_datatype_extent(datatype, &lb, &dataext);
939 // Local copy from self
940 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
942 // Send/Recv buffers to/from others;
943 requests = xbt_new(MPI_Request, size - 1);
944 tmpbufs = xbt_new(void *, rank);
946 for(other = 0; other < rank; other++) {
947 // FIXME: possibly overkill we we have contiguous/noncontiguous data
949 tmpbufs[index] = xbt_malloc(count * dataext);
951 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
955 for(other = rank + 1; other < size; other++) {
957 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
960 // Wait for completion of all comms.
961 smpi_mpi_startall(size - 1, requests);
962 for(other = 0; other < size - 1; other++) {
963 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
964 if(index == MPI_UNDEFINED) {
968 // #Request is below rank: it's a irecv
969 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
972 for(index = 0; index < rank; index++) {
973 xbt_free(tmpbufs[index]);