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;
81 void smpi_empty_status(MPI_Status * status) {
82 if(status != MPI_STATUS_IGNORE) {
83 status->MPI_SOURCE=MPI_ANY_SOURCE;
84 status->MPI_TAG=MPI_ANY_TAG;
89 void smpi_action_trace_run(char *path)
93 xbt_dict_cursor_t cursor;
97 action_fp = fopen(path, "r");
98 xbt_assert(action_fp != NULL, "Cannot open %s: %s", path,
102 if (!xbt_dict_is_empty(action_queues)) {
104 ("Not all actions got consumed. If the simulation ended successfully (without deadlock), you may want to add new processes to your deployment file.");
107 xbt_dict_foreach(action_queues, cursor, name, todo) {
108 XBT_WARN("Still %lu actions for %s", xbt_dynar_length(todo), name);
114 xbt_dict_free(&action_queues);
115 action_queues = xbt_dict_new_homogeneous(NULL);
118 static void smpi_mpi_request_free_voidp(void* request)
120 MPI_Request req = request;
121 smpi_mpi_request_free(&req);
124 /* MPI Low level calls */
125 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
126 int dst, int tag, MPI_Comm comm)
128 MPI_Request request =
129 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
130 comm, PERSISTENT | SEND);
135 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
136 int src, int tag, MPI_Comm comm)
138 MPI_Request request =
139 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
140 comm, PERSISTENT | RECV);
145 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)
176 void *oldbuf = request->buf;
178 request->buf = malloc(request->size);
180 memcpy(request->buf,oldbuf,request->size);
181 XBT_DEBUG("Send request %p is detached; buf %p copied into %p",request,oldbuf,request->buf);
185 simcall_comm_isend(mailbox, request->size, -1.0,
186 request->buf, request->size,
188 &smpi_mpi_request_free_voidp, // how to free the userdata if a detached send fails
190 // detach if msg size < eager/rdv switch limit
194 /* FIXME: detached sends are not traceable (request->action == NULL) */
196 simcall_set_category(request->action, TRACE_internal_smpi_get_category());
203 void smpi_mpi_startall(int count, MPI_Request * requests)
207 for(i = 0; i < count; i++) {
208 smpi_mpi_start(requests[i]);
212 void smpi_mpi_request_free(MPI_Request * request)
215 *request = MPI_REQUEST_NULL;
218 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
219 int dst, int tag, MPI_Comm comm)
221 MPI_Request request =
222 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
223 comm, NON_PERSISTENT | SEND);
228 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
229 int dst, int tag, MPI_Comm comm)
231 MPI_Request request =
232 smpi_isend_init(buf, count, datatype, dst, tag, comm);
234 smpi_mpi_start(request);
238 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
239 int src, int tag, MPI_Comm comm)
241 MPI_Request request =
242 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
243 comm, NON_PERSISTENT | RECV);
247 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
248 int src, int tag, MPI_Comm comm)
250 MPI_Request request =
251 smpi_irecv_init(buf, count, datatype, src, tag, comm);
253 smpi_mpi_start(request);
257 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
258 int tag, MPI_Comm comm, MPI_Status * status)
261 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
262 smpi_mpi_wait(&request, status);
267 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
268 int tag, MPI_Comm comm)
272 request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
273 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
276 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
277 int dst, int sendtag, void *recvbuf, int recvcount,
278 MPI_Datatype recvtype, int src, int recvtag,
279 MPI_Comm comm, MPI_Status * status)
281 MPI_Request requests[2];
285 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
287 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
288 smpi_mpi_startall(2, requests);
289 smpi_mpi_waitall(2, requests, stats);
290 if(status != MPI_STATUS_IGNORE) {
291 // Copy receive status
292 memcpy(status, &stats[1], sizeof(MPI_Status));
296 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
298 return status->count / smpi_datatype_size(datatype);
301 static void finish_wait(MPI_Request * request, MPI_Status * status)
303 MPI_Request req = *request;
304 // if we have a sender, we should use its data, and not the data from the receive
306 (req->src==MPI_ANY_SOURCE || req->tag== MPI_ANY_TAG))
307 req = (MPI_Request)SIMIX_comm_get_src_data((*request)->action);
309 if(status != MPI_STATUS_IGNORE) {
310 status->MPI_SOURCE = req->src;
311 status->MPI_TAG = req->tag;
312 status->MPI_ERROR = MPI_SUCCESS;
313 // FIXME: really this should just contain the count of receive-type blocks,
315 status->count = req->size;
319 print_request("Finishing", req);
320 MPI_Datatype datatype = req->old_type;
321 if(datatype->has_subtype == 1){
322 // This part handles the problem of non-contignous memory
323 // the unserialization at the reception
324 s_smpi_subtype_t *subtype = datatype->substruct;
325 if(req->flags & RECV) {
326 subtype->unserialize(req->buf, req->old_buf, req->size/smpi_datatype_size(datatype) , datatype->substruct);
328 //FIXME: I am not sure that if the send is detached we have to free
329 //the sender buffer thus I do it only for the reciever
330 if(req->flags & RECV) free(req->buf);
333 if(req->flags & NON_PERSISTENT) {
334 smpi_mpi_request_free(request);
340 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
343 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or smpi_mpi_testall before)
344 if ((*request)->action == NULL)
347 flag = simcall_comm_test((*request)->action);
349 finish_wait(request, status);
351 smpi_empty_status(status);
356 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
363 *index = MPI_UNDEFINED;
366 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
367 map = xbt_new(int, count);
369 for(i = 0; i < count; i++) {
370 if((requests[i]!=MPI_REQUEST_NULL) && requests[i]->action) {
371 xbt_dynar_push(comms, &requests[i]->action);
377 i = simcall_comm_testany(comms);
378 // not MPI_UNDEFINED, as this is a simix return code
381 finish_wait(&requests[*index], status);
385 //all requests are null or inactive, return true
387 smpi_empty_status(status);
390 xbt_dynar_free(&comms);
397 int smpi_mpi_testall(int count, MPI_Request requests[],
401 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
404 for(i=0; i<count; i++){
405 if(requests[i]!= MPI_REQUEST_NULL){
406 if (smpi_mpi_test(&requests[i], pstat)!=1){
410 smpi_empty_status(pstat);
412 if(status != MPI_STATUSES_IGNORE) {
413 memcpy(&status[i], pstat, sizeof(*pstat));
419 void smpi_mpi_probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
421 //FIXME find another wait to avoid busy waiting ?
422 // the issue here is that we have to wait on a nonexistent comm
424 smpi_mpi_iprobe(source, tag, comm, &flag, status);
425 XBT_DEBUG("Busy Waiting on probing : %d", flag);
427 simcall_process_sleep(0.0001);
432 void smpi_mpi_iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
433 MPI_Request request =build_request(NULL, 0, MPI_CHAR, source, smpi_comm_rank(comm), tag,
434 comm, NON_PERSISTENT | RECV);
436 // behave like a receive, but don't do it
439 print_request("New iprobe", request);
440 // We have to test both mailboxes as we don't know if we will receive one one or another
441 if (xbt_cfg_get_int(_surf_cfg_set, "smpi/async_small_thres")>0){
442 mailbox = smpi_process_mailbox_small();
443 XBT_DEBUG("trying to probe the perm recv mailbox");
444 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
446 if (request->action==NULL){
447 mailbox = smpi_process_mailbox();
448 XBT_DEBUG("trying to probe the other mailbox");
449 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
453 MPI_Request req = (MPI_Request)SIMIX_comm_get_src_data(request->action);
455 if(status != MPI_STATUS_IGNORE) {
456 status->MPI_SOURCE = req->src;
457 status->MPI_TAG = req->tag;
458 status->MPI_ERROR = MPI_SUCCESS;
459 status->count = req->size;
463 smpi_mpi_request_free(&request);
468 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
470 print_request("Waiting", *request);
471 if ((*request)->action != NULL) { // this is not a detached send
472 simcall_comm_wait((*request)->action, -1.0);
473 finish_wait(request, status);
475 // FIXME for a detached send, finish_wait is not called:
478 int smpi_mpi_waitany(int count, MPI_Request requests[],
485 index = MPI_UNDEFINED;
487 // Wait for a request to complete
488 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
489 map = xbt_new(int, count);
491 XBT_DEBUG("Wait for one of");
492 for(i = 0; i < count; i++) {
493 if((requests[i] != MPI_REQUEST_NULL) && (requests[i]->action != NULL)) {
494 print_request("Waiting any ", requests[i]);
495 xbt_dynar_push(comms, &requests[i]->action);
501 i = simcall_comm_waitany(comms);
503 // not MPI_UNDEFINED, as this is a simix return code
506 finish_wait(&requests[index], status);
510 xbt_dynar_free(&comms);
513 if (index==MPI_UNDEFINED)
514 smpi_empty_status(status);
519 void smpi_mpi_waitall(int count, MPI_Request requests[],
524 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
525 //tag invalid requests in the set
526 for(c = 0; c < count; c++) {
527 if(requests[c]==MPI_REQUEST_NULL || requests[c]->dst == MPI_PROC_NULL ){
528 if(status != MPI_STATUSES_IGNORE)
529 smpi_empty_status(&status[c]);
530 }else if(requests[c]->src == MPI_PROC_NULL ){
531 if(status != MPI_STATUSES_IGNORE) {
532 smpi_empty_status(&status[c]);
533 status[c].MPI_SOURCE=MPI_PROC_NULL;
538 for(c = 0; c < count; c++) {
540 smpi_mpi_wait(&requests[c], pstat);
543 index = smpi_mpi_waitany(count, requests, pstat);
544 if(index == MPI_UNDEFINED) {
547 if(status != MPI_STATUSES_IGNORE) {
548 memcpy(&status[index], pstat, sizeof(*pstat));
555 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
560 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
563 for(i = 0; i < incount; i++)
565 index=smpi_mpi_waitany(incount, requests, pstat);
566 if(index!=MPI_UNDEFINED){
567 indices[count] = index;
569 if(status != MPI_STATUSES_IGNORE) {
570 memcpy(&status[index], pstat, sizeof(*pstat));
573 return MPI_UNDEFINED;
579 int smpi_mpi_testsome(int incount, MPI_Request requests[], int *indices,
582 int i, count, count_dead;
584 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
588 for(i = 0; i < incount; i++) {
589 if((requests[i] != MPI_REQUEST_NULL)) {
590 if(smpi_mpi_test(&requests[i], pstat)) {
593 if(status != MPI_STATUSES_IGNORE) {
594 memcpy(&status[i], pstat, sizeof(*pstat));
601 if(count_dead==incount)return MPI_UNDEFINED;
605 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
608 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
609 nary_tree_bcast(buf, count, datatype, root, comm, 4);
612 void smpi_mpi_barrier(MPI_Comm comm)
614 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
615 nary_tree_barrier(comm, 4);
618 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
619 void *recvbuf, int recvcount, MPI_Datatype recvtype,
620 int root, MPI_Comm comm)
622 int system_tag = 666;
623 int rank, size, src, index;
624 MPI_Aint lb = 0, recvext = 0;
625 MPI_Request *requests;
627 rank = smpi_comm_rank(comm);
628 size = smpi_comm_size(comm);
630 // Send buffer to root
631 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
633 // FIXME: check for errors
634 smpi_datatype_extent(recvtype, &lb, &recvext);
635 // Local copy from root
636 smpi_datatype_copy(sendbuf, sendcount, sendtype,
637 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
638 // Receive buffers from senders
639 requests = xbt_new(MPI_Request, size - 1);
641 for(src = 0; src < size; src++) {
643 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
645 src, system_tag, comm);
649 // Wait for completion of irecv's.
650 smpi_mpi_startall(size - 1, requests);
651 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
656 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
657 void *recvbuf, int *recvcounts, int *displs,
658 MPI_Datatype recvtype, int root, MPI_Comm comm)
660 int system_tag = 666;
661 int rank, size, src, index;
662 MPI_Aint lb = 0, recvext = 0;
663 MPI_Request *requests;
665 rank = smpi_comm_rank(comm);
666 size = smpi_comm_size(comm);
668 // Send buffer to root
669 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
671 // FIXME: check for errors
672 smpi_datatype_extent(recvtype, &lb, &recvext);
673 // Local copy from root
674 smpi_datatype_copy(sendbuf, sendcount, sendtype,
675 (char *)recvbuf + displs[root] * recvext,
676 recvcounts[root], recvtype);
677 // Receive buffers from senders
678 requests = xbt_new(MPI_Request, size - 1);
680 for(src = 0; src < size; src++) {
683 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
684 recvcounts[src], recvtype, src, system_tag, comm);
688 // Wait for completion of irecv's.
689 smpi_mpi_startall(size - 1, requests);
690 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
695 void smpi_mpi_allgather(void *sendbuf, int sendcount,
696 MPI_Datatype sendtype, void *recvbuf,
697 int recvcount, MPI_Datatype recvtype,
700 int system_tag = 666;
701 int rank, size, other, index;
702 MPI_Aint lb = 0, recvext = 0;
703 MPI_Request *requests;
705 rank = smpi_comm_rank(comm);
706 size = smpi_comm_size(comm);
707 // FIXME: check for errors
708 smpi_datatype_extent(recvtype, &lb, &recvext);
709 // Local copy from self
710 smpi_datatype_copy(sendbuf, sendcount, sendtype,
711 (char *)recvbuf + rank * recvcount * recvext, recvcount,
713 // Send/Recv buffers to/from others;
714 requests = xbt_new(MPI_Request, 2 * (size - 1));
716 for(other = 0; other < size; other++) {
719 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
722 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
723 recvcount, recvtype, other,
728 // Wait for completion of all comms.
729 smpi_mpi_startall(2 * (size - 1), requests);
730 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
734 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
735 MPI_Datatype sendtype, void *recvbuf,
736 int *recvcounts, int *displs,
737 MPI_Datatype recvtype, MPI_Comm comm)
739 int system_tag = 666;
740 int rank, size, other, index;
741 MPI_Aint lb = 0, recvext = 0;
742 MPI_Request *requests;
744 rank = smpi_comm_rank(comm);
745 size = smpi_comm_size(comm);
746 // FIXME: check for errors
747 smpi_datatype_extent(recvtype, &lb, &recvext);
748 // Local copy from self
749 smpi_datatype_copy(sendbuf, sendcount, sendtype,
750 (char *)recvbuf + displs[rank] * recvext,
751 recvcounts[rank], recvtype);
752 // Send buffers to others;
753 requests = xbt_new(MPI_Request, 2 * (size - 1));
755 for(other = 0; other < size; other++) {
758 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
762 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
763 recvtype, other, system_tag, comm);
767 // Wait for completion of all comms.
768 smpi_mpi_startall(2 * (size - 1), requests);
769 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
773 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
774 void *recvbuf, int recvcount, MPI_Datatype recvtype,
775 int root, MPI_Comm comm)
777 int system_tag = 666;
778 int rank, size, dst, index;
779 MPI_Aint lb = 0, sendext = 0;
780 MPI_Request *requests;
782 rank = smpi_comm_rank(comm);
783 size = smpi_comm_size(comm);
785 // Recv buffer from root
786 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
789 // FIXME: check for errors
790 smpi_datatype_extent(sendtype, &lb, &sendext);
791 // Local copy from root
792 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
793 sendcount, sendtype, recvbuf, recvcount, recvtype);
794 // Send buffers to receivers
795 requests = xbt_new(MPI_Request, size - 1);
797 for(dst = 0; dst < size; dst++) {
799 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
800 sendcount, sendtype, dst,
805 // Wait for completion of isend's.
806 smpi_mpi_startall(size - 1, requests);
807 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
812 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
813 MPI_Datatype sendtype, void *recvbuf, int recvcount,
814 MPI_Datatype recvtype, int root, MPI_Comm comm)
816 int system_tag = 666;
817 int rank, size, dst, index;
818 MPI_Aint lb = 0, sendext = 0;
819 MPI_Request *requests;
821 rank = smpi_comm_rank(comm);
822 size = smpi_comm_size(comm);
824 // Recv buffer from root
825 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
828 // FIXME: check for errors
829 smpi_datatype_extent(sendtype, &lb, &sendext);
830 // Local copy from root
831 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
832 sendtype, recvbuf, recvcount, recvtype);
833 // Send buffers to receivers
834 requests = xbt_new(MPI_Request, size - 1);
836 for(dst = 0; dst < size; dst++) {
839 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
840 sendtype, dst, system_tag, comm);
844 // Wait for completion of isend's.
845 smpi_mpi_startall(size - 1, requests);
846 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
851 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
852 MPI_Datatype datatype, MPI_Op op, int root,
855 int system_tag = 666;
856 int rank, size, src, index;
857 MPI_Aint lb = 0, dataext = 0;
858 MPI_Request *requests;
861 rank = smpi_comm_rank(comm);
862 size = smpi_comm_size(comm);
864 // Send buffer to root
865 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
867 // FIXME: check for errors
868 smpi_datatype_extent(datatype, &lb, &dataext);
869 // Local copy from root
870 if (sendbuf && recvbuf)
871 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
872 // Receive buffers from senders
873 //TODO: make a MPI_barrier here ?
874 requests = xbt_new(MPI_Request, size - 1);
875 tmpbufs = xbt_new(void *, size - 1);
877 for(src = 0; src < size; src++) {
879 // FIXME: possibly overkill we we have contiguous/noncontiguous data
881 tmpbufs[index] = xbt_malloc(count * dataext);
883 smpi_irecv_init(tmpbufs[index], count, datatype, src,
888 // Wait for completion of irecv's.
889 smpi_mpi_startall(size - 1, requests);
890 for(src = 0; src < size - 1; src++) {
891 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
892 XBT_DEBUG("finished waiting any request with index %d", index);
893 if(index == MPI_UNDEFINED) {
896 if(op) /* op can be MPI_OP_NULL that does nothing */
897 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
899 for(index = 0; index < size - 1; index++) {
900 xbt_free(tmpbufs[index]);
907 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
908 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
910 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
911 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
914 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
915 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
917 int system_tag = 666;
918 int rank, size, other, index;
919 MPI_Aint lb = 0, dataext = 0;
920 MPI_Request *requests;
923 rank = smpi_comm_rank(comm);
924 size = smpi_comm_size(comm);
926 // FIXME: check for errors
927 smpi_datatype_extent(datatype, &lb, &dataext);
929 // Local copy from self
930 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
932 // Send/Recv buffers to/from others;
933 requests = xbt_new(MPI_Request, size - 1);
934 tmpbufs = xbt_new(void *, rank);
936 for(other = 0; other < rank; other++) {
937 // FIXME: possibly overkill we we have contiguous/noncontiguous data
939 tmpbufs[index] = xbt_malloc(count * dataext);
941 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
945 for(other = rank + 1; other < size; other++) {
947 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
950 // Wait for completion of all comms.
951 smpi_mpi_startall(size - 1, requests);
952 for(other = 0; other < size - 1; other++) {
953 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
954 if(index == MPI_UNDEFINED) {
958 // #Request is below rank: it's a irecv
959 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
962 for(index = 0; index < rank; index++) {
963 xbt_free(tmpbufs[index]);