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 static 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;
490 for(c = 0; c < count; c++) {
492 smpi_mpi_wait(&requests[c], pstat);
495 index = smpi_mpi_waitany(count, requests, pstat);
496 if(index == MPI_UNDEFINED) {
500 if(status != MPI_STATUSES_IGNORE) {
501 memcpy(&status[index], pstat, sizeof(*pstat));
506 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
511 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
514 for(i = 0; i < incount; i++) {
515 if(smpi_mpi_testany(incount, requests, &index, pstat)) {
516 if(index!=MPI_UNDEFINED){
517 indices[count] = index;
519 if(status != MPI_STATUSES_IGNORE) {
520 memcpy(&status[index], pstat, sizeof(*pstat));
523 return MPI_UNDEFINED;
530 int smpi_mpi_testsome(int incount, MPI_Request requests[], int *indices,
533 int i, count, count_dead;
535 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
538 for(i = 0; i < incount; i++) {
539 if((requests[i] != MPI_REQUEST_NULL)) {
540 if(smpi_mpi_test(&requests[i], pstat)) {
543 if(status != MPI_STATUSES_IGNORE) {
544 memcpy(&status[i], pstat, sizeof(*pstat));
551 if(count_dead==incount)return MPI_UNDEFINED;
555 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
558 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
559 nary_tree_bcast(buf, count, datatype, root, comm, 4);
562 void smpi_mpi_barrier(MPI_Comm comm)
564 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
565 nary_tree_barrier(comm, 4);
568 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
569 void *recvbuf, int recvcount, MPI_Datatype recvtype,
570 int root, MPI_Comm comm)
572 int system_tag = 666;
573 int rank, size, src, index;
574 MPI_Aint lb = 0, recvext = 0;
575 MPI_Request *requests;
577 rank = smpi_comm_rank(comm);
578 size = smpi_comm_size(comm);
580 // Send buffer to root
581 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
583 // FIXME: check for errors
584 smpi_datatype_extent(recvtype, &lb, &recvext);
585 // Local copy from root
586 smpi_datatype_copy(sendbuf, sendcount, sendtype,
587 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
588 // Receive buffers from senders
589 requests = xbt_new(MPI_Request, size - 1);
591 for(src = 0; src < size; src++) {
593 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
595 src, system_tag, comm);
599 // Wait for completion of irecv's.
600 smpi_mpi_startall(size - 1, requests);
601 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
606 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
607 void *recvbuf, int *recvcounts, int *displs,
608 MPI_Datatype recvtype, int root, MPI_Comm comm)
610 int system_tag = 666;
611 int rank, size, src, index;
612 MPI_Aint lb = 0, recvext = 0;
613 MPI_Request *requests;
615 rank = smpi_comm_rank(comm);
616 size = smpi_comm_size(comm);
618 // Send buffer to root
619 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
621 // FIXME: check for errors
622 smpi_datatype_extent(recvtype, &lb, &recvext);
623 // Local copy from root
624 smpi_datatype_copy(sendbuf, sendcount, sendtype,
625 (char *)recvbuf + displs[root] * recvext,
626 recvcounts[root], recvtype);
627 // Receive buffers from senders
628 requests = xbt_new(MPI_Request, size - 1);
630 for(src = 0; src < size; src++) {
633 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
634 recvcounts[src], recvtype, src, system_tag, comm);
638 // Wait for completion of irecv's.
639 smpi_mpi_startall(size - 1, requests);
640 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
645 void smpi_mpi_allgather(void *sendbuf, int sendcount,
646 MPI_Datatype sendtype, void *recvbuf,
647 int recvcount, MPI_Datatype recvtype,
650 int system_tag = 666;
651 int rank, size, other, index;
652 MPI_Aint lb = 0, recvext = 0;
653 MPI_Request *requests;
655 rank = smpi_comm_rank(comm);
656 size = smpi_comm_size(comm);
657 // FIXME: check for errors
658 smpi_datatype_extent(recvtype, &lb, &recvext);
659 // Local copy from self
660 smpi_datatype_copy(sendbuf, sendcount, sendtype,
661 (char *)recvbuf + rank * recvcount * recvext, recvcount,
663 // Send/Recv buffers to/from others;
664 requests = xbt_new(MPI_Request, 2 * (size - 1));
666 for(other = 0; other < size; other++) {
669 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
672 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
673 recvcount, recvtype, other,
678 // Wait for completion of all comms.
679 smpi_mpi_startall(2 * (size - 1), requests);
680 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
684 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
685 MPI_Datatype sendtype, void *recvbuf,
686 int *recvcounts, int *displs,
687 MPI_Datatype recvtype, MPI_Comm comm)
689 int system_tag = 666;
690 int rank, size, other, index;
691 MPI_Aint lb = 0, recvext = 0;
692 MPI_Request *requests;
694 rank = smpi_comm_rank(comm);
695 size = smpi_comm_size(comm);
696 // FIXME: check for errors
697 smpi_datatype_extent(recvtype, &lb, &recvext);
698 // Local copy from self
699 smpi_datatype_copy(sendbuf, sendcount, sendtype,
700 (char *)recvbuf + displs[rank] * recvext,
701 recvcounts[rank], recvtype);
702 // Send buffers to others;
703 requests = xbt_new(MPI_Request, 2 * (size - 1));
705 for(other = 0; other < size; other++) {
708 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
712 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
713 recvtype, other, system_tag, comm);
717 // Wait for completion of all comms.
718 smpi_mpi_startall(2 * (size - 1), requests);
719 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
723 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
724 void *recvbuf, int recvcount, MPI_Datatype recvtype,
725 int root, MPI_Comm comm)
727 int system_tag = 666;
728 int rank, size, dst, index;
729 MPI_Aint lb = 0, sendext = 0;
730 MPI_Request *requests;
732 rank = smpi_comm_rank(comm);
733 size = smpi_comm_size(comm);
735 // Recv buffer from root
736 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
739 // FIXME: check for errors
740 smpi_datatype_extent(sendtype, &lb, &sendext);
741 // Local copy from root
742 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
743 sendcount, sendtype, recvbuf, recvcount, recvtype);
744 // Send buffers to receivers
745 requests = xbt_new(MPI_Request, size - 1);
747 for(dst = 0; dst < size; dst++) {
749 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
750 sendcount, sendtype, dst,
755 // Wait for completion of isend's.
756 smpi_mpi_startall(size - 1, requests);
757 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
762 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
763 MPI_Datatype sendtype, void *recvbuf, int recvcount,
764 MPI_Datatype recvtype, int root, MPI_Comm comm)
766 int system_tag = 666;
767 int rank, size, dst, index;
768 MPI_Aint lb = 0, sendext = 0;
769 MPI_Request *requests;
771 rank = smpi_comm_rank(comm);
772 size = smpi_comm_size(comm);
774 // Recv buffer from root
775 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
778 // FIXME: check for errors
779 smpi_datatype_extent(sendtype, &lb, &sendext);
780 // Local copy from root
781 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
782 sendtype, recvbuf, recvcount, recvtype);
783 // Send buffers to receivers
784 requests = xbt_new(MPI_Request, size - 1);
786 for(dst = 0; dst < size; dst++) {
789 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
790 sendtype, dst, system_tag, comm);
794 // Wait for completion of isend's.
795 smpi_mpi_startall(size - 1, requests);
796 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
801 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
802 MPI_Datatype datatype, MPI_Op op, int root,
805 int system_tag = 666;
806 int rank, size, src, index;
807 MPI_Aint lb = 0, dataext = 0;
808 MPI_Request *requests;
811 rank = smpi_comm_rank(comm);
812 size = smpi_comm_size(comm);
814 // Send buffer to root
815 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
817 // FIXME: check for errors
818 smpi_datatype_extent(datatype, &lb, &dataext);
819 // Local copy from root
820 if (sendbuf && recvbuf)
821 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
822 // Receive buffers from senders
823 //TODO: make a MPI_barrier here ?
824 requests = xbt_new(MPI_Request, size - 1);
825 tmpbufs = xbt_new(void *, size - 1);
827 for(src = 0; src < size; src++) {
829 // FIXME: possibly overkill we we have contiguous/noncontiguous data
831 tmpbufs[index] = xbt_malloc(count * dataext);
833 smpi_irecv_init(tmpbufs[index], count, datatype, src,
838 // Wait for completion of irecv's.
839 smpi_mpi_startall(size - 1, requests);
840 for(src = 0; src < size - 1; src++) {
841 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
842 XBT_DEBUG("finished waiting any request with index %d", index);
843 if(index == MPI_UNDEFINED) {
846 if(op) /* op can be MPI_OP_NULL that does nothing */
847 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
849 for(index = 0; index < size - 1; index++) {
850 xbt_free(tmpbufs[index]);
857 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
858 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
860 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
861 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
864 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
865 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
867 int system_tag = 666;
868 int rank, size, other, index;
869 MPI_Aint lb = 0, dataext = 0;
870 MPI_Request *requests;
873 rank = smpi_comm_rank(comm);
874 size = smpi_comm_size(comm);
876 // FIXME: check for errors
877 smpi_datatype_extent(datatype, &lb, &dataext);
879 // Local copy from self
880 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
882 // Send/Recv buffers to/from others;
883 requests = xbt_new(MPI_Request, size - 1);
884 tmpbufs = xbt_new(void *, rank);
886 for(other = 0; other < rank; other++) {
887 // FIXME: possibly overkill we we have contiguous/noncontiguous data
889 tmpbufs[index] = xbt_malloc(count * dataext);
891 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
895 for(other = rank + 1; other < size; other++) {
897 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
900 // Wait for completion of all comms.
901 smpi_mpi_startall(size - 1, requests);
902 for(other = 0; other < size - 1; other++) {
903 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
904 if(index == MPI_UNDEFINED) {
908 // #Request is below rank: it's a irecv
909 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
912 for(index = 0; index < rank; index++) {
913 xbt_free(tmpbufs[index]);