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-contiguous 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-contiguous 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);
162 if (request->action)request->action->comm.refcount++;
165 int receiver = smpi_group_index(smpi_comm_group(request->comm), request->dst);
166 /* if(receiver == MPI_UNDEFINED) {*/
167 /* XBT_WARN("Trying to send a message to a wrong rank");*/
170 print_request("New send", request);
171 if (request->size < surf_cfg_get_int("smpi/async_small_thres")) { // eager mode
172 mailbox = smpi_process_remote_mailbox_small(receiver);
174 XBT_DEBUG("Send request %p is not in the permanent receive mailbox (buf: %p)",request,request->buf);
175 mailbox = smpi_process_remote_mailbox(receiver);
177 if (request->size < 64*1024 ) { //(FIXME: this limit should be configurable)
179 if(request->old_type->has_subtype == 0){
180 oldbuf = request->buf;
181 request->detached = 1;
183 request->buf = malloc(request->size);
184 memcpy(request->buf,oldbuf,request->size);
187 XBT_DEBUG("Send request %p is detached; buf %p copied into %p",request,oldbuf,request->buf);
191 simcall_comm_isend(mailbox, request->size, -1.0,
192 request->buf, request->size,
194 &smpi_mpi_request_free_voidp, // how to free the userdata if a detached send fails
196 // detach if msg size < eager/rdv switch limit
198 if (request->action)request->action->comm.refcount++;
201 /* FIXME: detached sends are not traceable (request->action == NULL) */
203 simcall_set_category(request->action, TRACE_internal_smpi_get_category());
210 void smpi_mpi_startall(int count, MPI_Request * requests)
214 for(i = 0; i < count; i++) {
215 smpi_mpi_start(requests[i]);
219 void smpi_mpi_request_free(MPI_Request * request)
222 *request = MPI_REQUEST_NULL;
225 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
226 int dst, int tag, MPI_Comm comm)
228 MPI_Request request =
229 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
230 comm, NON_PERSISTENT | SEND);
235 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
236 int dst, int tag, MPI_Comm comm)
238 MPI_Request request =
239 smpi_isend_init(buf, count, datatype, dst, tag, comm);
241 smpi_mpi_start(request);
245 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
246 int src, int tag, MPI_Comm comm)
248 MPI_Request request =
249 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
250 comm, NON_PERSISTENT | RECV);
254 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
255 int src, int tag, MPI_Comm comm)
257 MPI_Request request =
258 smpi_irecv_init(buf, count, datatype, src, tag, comm);
260 smpi_mpi_start(request);
264 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
265 int tag, MPI_Comm comm, MPI_Status * status)
268 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
269 smpi_mpi_wait(&request, status);
274 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
275 int tag, MPI_Comm comm)
277 MPI_Request request =
278 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
279 comm, NON_PERSISTENT | SEND | RECV_DELETE);
281 smpi_mpi_start(request);
282 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
286 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
287 int dst, int sendtag, void *recvbuf, int recvcount,
288 MPI_Datatype recvtype, int src, int recvtag,
289 MPI_Comm comm, MPI_Status * status)
291 MPI_Request requests[2];
295 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
297 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
298 smpi_mpi_startall(2, requests);
299 smpi_mpi_waitall(2, requests, stats);
300 if(status != MPI_STATUS_IGNORE) {
301 // Copy receive status
302 memcpy(status, &stats[1], sizeof(MPI_Status));
306 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
308 return status->count / smpi_datatype_size(datatype);
311 static void finish_wait(MPI_Request * request, MPI_Status * status)
313 MPI_Request req = *request;
314 // if we have a sender, we should use its data, and not the data from the receive
315 //FIXME : may fail if req->action has already been freed, the pointer being invalid
317 (req->src==MPI_ANY_SOURCE || req->tag== MPI_ANY_TAG))
318 //req = (MPI_Request)SIMIX_comm_get_src_data((*request)->action);
320 if(status != MPI_STATUS_IGNORE) {
321 status->MPI_SOURCE = req->src;
322 status->MPI_TAG = req->tag;
323 //if((*request)->action && ((MPI_Request)SIMIX_comm_get_src_data((*request)->action))->size == (*request)->size)
324 status->MPI_ERROR = MPI_SUCCESS;
325 //else status->MPI_ERROR = MPI_ERR_TRUNCATE;
326 // this handles the case were size in receive differs from size in send
327 // FIXME: really this should just contain the count of receive-type blocks,
329 status->count = req->size;
333 print_request("Finishing", req);
334 MPI_Datatype datatype = req->old_type;
335 if(datatype->has_subtype == 1){
336 // This part handles the problem of non-contignous memory
337 // the unserialization at the reception
338 s_smpi_subtype_t *subtype = datatype->substruct;
339 if(req->flags & RECV) {
340 subtype->unserialize(req->buf, req->old_buf, req->size/smpi_datatype_size(datatype) , datatype->substruct);
342 if(req->detached == 0) free(req->buf);
347 if(req->flags & NON_PERSISTENT) {
348 if(req->flags & RECV &&
350 (req->action->state == SIMIX_DONE))
352 MPI_Request sender_request = (MPI_Request)SIMIX_comm_get_src_data(req->action);
353 if((sender_request!=MPI_REQUEST_NULL) &&
354 ( sender_request->detached ) &&
355 ( sender_request->flags & RECV_DELETE))
357 //we are in a receiver's wait from a detached send
358 //we have to clean the sender's side request here.... but only if done by a send, not an isend
359 //the request lives senderside for an isend. As detached is currently for send + isend, we use RECV_DELETE to separate them
360 //FIXME : see if just removing detached status for isend is also good
361 smpi_mpi_request_free(&sender_request);
367 //if we want to free our request, we have to invalidate it at the other end of the comm
369 if(req->flags & SEND){
370 req->action->comm.src_data=MPI_REQUEST_NULL;
372 req->action->comm.dst_data=MPI_REQUEST_NULL;
375 smx_action_t temp=req->action;
376 if(req->action->comm.refcount == 1)req->action = NULL;
377 SIMIX_comm_destroy(temp);
382 smpi_mpi_request_free(request);
386 if(req->action)SIMIX_comm_destroy(req->action);
391 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
394 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or smpi_mpi_testall before)
395 if ((*request)->action == NULL)
398 flag = simcall_comm_test((*request)->action);
400 finish_wait(request, status);
402 smpi_empty_status(status);
407 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
414 *index = MPI_UNDEFINED;
417 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
418 map = xbt_new(int, count);
420 for(i = 0; i < count; i++) {
421 if((requests[i]!=MPI_REQUEST_NULL) && requests[i]->action) {
422 xbt_dynar_push(comms, &requests[i]->action);
428 i = simcall_comm_testany(comms);
429 // not MPI_UNDEFINED, as this is a simix return code
432 finish_wait(&requests[*index], status);
436 //all requests are null or inactive, return true
438 smpi_empty_status(status);
441 xbt_dynar_free(&comms);
448 int smpi_mpi_testall(int count, MPI_Request requests[],
452 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
455 for(i=0; i<count; i++){
456 if(requests[i]!= MPI_REQUEST_NULL){
457 if (smpi_mpi_test(&requests[i], pstat)!=1){
461 smpi_empty_status(pstat);
463 if(status != MPI_STATUSES_IGNORE) {
464 memcpy(&status[i], pstat, sizeof(*pstat));
470 void smpi_mpi_probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
472 //FIXME find another wait to avoid busy waiting ?
473 // the issue here is that we have to wait on a nonexistent comm
475 smpi_mpi_iprobe(source, tag, comm, &flag, status);
476 XBT_DEBUG("Busy Waiting on probing : %d", flag);
478 simcall_process_sleep(0.0001);
483 void smpi_mpi_iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
484 MPI_Request request =build_request(NULL, 0, MPI_CHAR, source, smpi_comm_rank(comm), tag,
485 comm, NON_PERSISTENT | RECV);
487 // behave like a receive, but don't do it
490 print_request("New iprobe", request);
491 // We have to test both mailboxes as we don't know if we will receive one one or another
492 if (surf_cfg_get_int("smpi/async_small_thres")>0){
493 mailbox = smpi_process_mailbox_small();
494 XBT_DEBUG("trying to probe the perm recv mailbox");
495 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
497 if (request->action==NULL){
498 mailbox = smpi_process_mailbox();
499 XBT_DEBUG("trying to probe the other mailbox");
500 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
504 MPI_Request req = (MPI_Request)SIMIX_comm_get_src_data(request->action);
506 if(status != MPI_STATUS_IGNORE) {
507 status->MPI_SOURCE = req->src;
508 status->MPI_TAG = req->tag;
509 status->MPI_ERROR = MPI_SUCCESS;
510 status->count = req->size;
514 smpi_mpi_request_free(&request);
519 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
521 print_request("Waiting", *request);
522 if ((*request)->action != NULL) { // this is not a detached send
523 simcall_comm_wait((*request)->action, -1.0);
524 finish_wait(request, status);
526 // FIXME for a detached send, finish_wait is not called:
529 int smpi_mpi_waitany(int count, MPI_Request requests[],
536 index = MPI_UNDEFINED;
538 // Wait for a request to complete
539 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
540 map = xbt_new(int, count);
542 XBT_DEBUG("Wait for one of");
543 for(i = 0; i < count; i++) {
544 if((requests[i] != MPI_REQUEST_NULL) && (requests[i]->action != NULL)) {
545 print_request("Waiting any ", requests[i]);
546 xbt_dynar_push(comms, &requests[i]->action);
552 i = simcall_comm_waitany(comms);
554 // not MPI_UNDEFINED, as this is a simix return code
557 finish_wait(&requests[index], status);
561 xbt_dynar_free(&comms);
564 if (index==MPI_UNDEFINED)
565 smpi_empty_status(status);
570 int smpi_mpi_waitall(int count, MPI_Request requests[],
575 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
576 int retvalue=MPI_SUCCESS;
577 //tag invalid requests in the set
578 for(c = 0; c < count; c++) {
579 if(requests[c]==MPI_REQUEST_NULL || requests[c]->dst == MPI_PROC_NULL ){
580 if(status != MPI_STATUSES_IGNORE)
581 smpi_empty_status(&status[c]);
582 }else if(requests[c]->src == MPI_PROC_NULL ){
583 if(status != MPI_STATUSES_IGNORE) {
584 smpi_empty_status(&status[c]);
585 status[c].MPI_SOURCE=MPI_PROC_NULL;
590 for(c = 0; c < count; c++) {
592 smpi_mpi_wait(&requests[c], pstat);
595 index = smpi_mpi_waitany(count, requests, pstat);
596 if(index == MPI_UNDEFINED) {
599 if(status != MPI_STATUSES_IGNORE) {
600 memcpy(&status[index], pstat, sizeof(*pstat));
601 if(status[index].MPI_ERROR==MPI_ERR_TRUNCATE)retvalue=MPI_ERR_IN_STATUS;
609 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
614 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
617 for(i = 0; i < incount; i++)
619 index=smpi_mpi_waitany(incount, requests, pstat);
620 if(index!=MPI_UNDEFINED){
621 indices[count] = index;
623 if(status != MPI_STATUSES_IGNORE) {
624 memcpy(&status[index], pstat, sizeof(*pstat));
627 return MPI_UNDEFINED;
633 int smpi_mpi_testsome(int incount, MPI_Request requests[], int *indices,
636 int i, count, count_dead;
638 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
642 for(i = 0; i < incount; i++) {
643 if((requests[i] != MPI_REQUEST_NULL)) {
644 if(smpi_mpi_test(&requests[i], pstat)) {
647 if(status != MPI_STATUSES_IGNORE) {
648 memcpy(&status[i], pstat, sizeof(*pstat));
655 if(count_dead==incount)return MPI_UNDEFINED;
659 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
662 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
663 nary_tree_bcast(buf, count, datatype, root, comm, 4);
666 void smpi_mpi_barrier(MPI_Comm comm)
668 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
669 nary_tree_barrier(comm, 4);
672 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
673 void *recvbuf, int recvcount, MPI_Datatype recvtype,
674 int root, MPI_Comm comm)
676 int system_tag = 666;
677 int rank, size, src, index;
678 MPI_Aint lb = 0, recvext = 0;
679 MPI_Request *requests;
681 rank = smpi_comm_rank(comm);
682 size = smpi_comm_size(comm);
684 // Send buffer to root
685 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
687 // FIXME: check for errors
688 smpi_datatype_extent(recvtype, &lb, &recvext);
689 // Local copy from root
690 smpi_datatype_copy(sendbuf, sendcount, sendtype,
691 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
692 // Receive buffers from senders
693 requests = xbt_new(MPI_Request, size - 1);
695 for(src = 0; src < size; src++) {
697 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
699 src, system_tag, comm);
703 // Wait for completion of irecv's.
704 smpi_mpi_startall(size - 1, requests);
705 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
710 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
711 void *recvbuf, int *recvcounts, int *displs,
712 MPI_Datatype recvtype, int root, MPI_Comm comm)
714 int system_tag = 666;
715 int rank, size, src, index;
716 MPI_Aint lb = 0, recvext = 0;
717 MPI_Request *requests;
719 rank = smpi_comm_rank(comm);
720 size = smpi_comm_size(comm);
722 // Send buffer to root
723 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
725 // FIXME: check for errors
726 smpi_datatype_extent(recvtype, &lb, &recvext);
727 // Local copy from root
728 smpi_datatype_copy(sendbuf, sendcount, sendtype,
729 (char *)recvbuf + displs[root] * recvext,
730 recvcounts[root], recvtype);
731 // Receive buffers from senders
732 requests = xbt_new(MPI_Request, size - 1);
734 for(src = 0; src < size; src++) {
737 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
738 recvcounts[src], recvtype, src, system_tag, comm);
742 // Wait for completion of irecv's.
743 smpi_mpi_startall(size - 1, requests);
744 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
749 void smpi_mpi_allgather(void *sendbuf, int sendcount,
750 MPI_Datatype sendtype, void *recvbuf,
751 int recvcount, MPI_Datatype recvtype,
754 int system_tag = 666;
755 int rank, size, other, index;
756 MPI_Aint lb = 0, recvext = 0;
757 MPI_Request *requests;
759 rank = smpi_comm_rank(comm);
760 size = smpi_comm_size(comm);
761 // FIXME: check for errors
762 smpi_datatype_extent(recvtype, &lb, &recvext);
763 // Local copy from self
764 smpi_datatype_copy(sendbuf, sendcount, sendtype,
765 (char *)recvbuf + rank * recvcount * recvext, recvcount,
767 // Send/Recv buffers to/from others;
768 requests = xbt_new(MPI_Request, 2 * (size - 1));
770 for(other = 0; other < size; other++) {
773 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
776 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
777 recvcount, recvtype, other,
782 // Wait for completion of all comms.
783 smpi_mpi_startall(2 * (size - 1), requests);
784 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
788 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
789 MPI_Datatype sendtype, void *recvbuf,
790 int *recvcounts, int *displs,
791 MPI_Datatype recvtype, MPI_Comm comm)
793 int system_tag = 666;
794 int rank, size, other, index;
795 MPI_Aint lb = 0, recvext = 0;
796 MPI_Request *requests;
798 rank = smpi_comm_rank(comm);
799 size = smpi_comm_size(comm);
800 // FIXME: check for errors
801 smpi_datatype_extent(recvtype, &lb, &recvext);
802 // Local copy from self
803 smpi_datatype_copy(sendbuf, sendcount, sendtype,
804 (char *)recvbuf + displs[rank] * recvext,
805 recvcounts[rank], recvtype);
806 // Send buffers to others;
807 requests = xbt_new(MPI_Request, 2 * (size - 1));
809 for(other = 0; other < size; other++) {
812 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
816 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
817 recvtype, other, system_tag, comm);
821 // Wait for completion of all comms.
822 smpi_mpi_startall(2 * (size - 1), requests);
823 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
827 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
828 void *recvbuf, int recvcount, MPI_Datatype recvtype,
829 int root, MPI_Comm comm)
831 int system_tag = 666;
832 int rank, size, dst, index;
833 MPI_Aint lb = 0, sendext = 0;
834 MPI_Request *requests;
836 rank = smpi_comm_rank(comm);
837 size = smpi_comm_size(comm);
839 // Recv buffer from root
840 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
843 // FIXME: check for errors
844 smpi_datatype_extent(sendtype, &lb, &sendext);
845 // Local copy from root
846 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
847 sendcount, sendtype, recvbuf, recvcount, recvtype);
848 // Send buffers to receivers
849 requests = xbt_new(MPI_Request, size - 1);
851 for(dst = 0; dst < size; dst++) {
853 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
854 sendcount, sendtype, dst,
859 // Wait for completion of isend's.
860 smpi_mpi_startall(size - 1, requests);
861 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
866 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
867 MPI_Datatype sendtype, void *recvbuf, int recvcount,
868 MPI_Datatype recvtype, int root, MPI_Comm comm)
870 int system_tag = 666;
871 int rank, size, dst, index;
872 MPI_Aint lb = 0, sendext = 0;
873 MPI_Request *requests;
875 rank = smpi_comm_rank(comm);
876 size = smpi_comm_size(comm);
878 // Recv buffer from root
879 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
882 // FIXME: check for errors
883 smpi_datatype_extent(sendtype, &lb, &sendext);
884 // Local copy from root
885 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
886 sendtype, recvbuf, recvcount, recvtype);
887 // Send buffers to receivers
888 requests = xbt_new(MPI_Request, size - 1);
890 for(dst = 0; dst < size; dst++) {
893 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
894 sendtype, dst, system_tag, comm);
898 // Wait for completion of isend's.
899 smpi_mpi_startall(size - 1, requests);
900 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
905 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
906 MPI_Datatype datatype, MPI_Op op, int root,
909 int system_tag = 666;
910 int rank, size, src, index;
911 MPI_Aint lb = 0, dataext = 0;
912 MPI_Request *requests;
915 rank = smpi_comm_rank(comm);
916 size = smpi_comm_size(comm);
918 // Send buffer to root
919 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
921 // FIXME: check for errors
922 smpi_datatype_extent(datatype, &lb, &dataext);
923 // Local copy from root
924 if (sendbuf && recvbuf)
925 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
926 // Receive buffers from senders
927 //TODO: make a MPI_barrier here ?
928 requests = xbt_new(MPI_Request, size - 1);
929 tmpbufs = xbt_new(void *, size - 1);
931 for(src = 0; src < size; src++) {
933 // FIXME: possibly overkill we we have contiguous/noncontiguous data
935 tmpbufs[index] = xbt_malloc(count * dataext);
937 smpi_irecv_init(tmpbufs[index], count, datatype, src,
942 // Wait for completion of irecv's.
943 smpi_mpi_startall(size - 1, requests);
944 for(src = 0; src < size - 1; src++) {
945 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
946 XBT_DEBUG("finished waiting any request with index %d", index);
947 if(index == MPI_UNDEFINED) {
950 if(op) /* op can be MPI_OP_NULL that does nothing */
951 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
953 for(index = 0; index < size - 1; index++) {
954 xbt_free(tmpbufs[index]);
961 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
962 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
964 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
965 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
968 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
969 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
971 int system_tag = 666;
972 int rank, size, other, index;
973 MPI_Aint lb = 0, dataext = 0;
974 MPI_Request *requests;
977 rank = smpi_comm_rank(comm);
978 size = smpi_comm_size(comm);
980 // FIXME: check for errors
981 smpi_datatype_extent(datatype, &lb, &dataext);
983 // Local copy from self
984 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
986 // Send/Recv buffers to/from others;
987 requests = xbt_new(MPI_Request, size - 1);
988 tmpbufs = xbt_new(void *, rank);
990 for(other = 0; other < rank; other++) {
991 // FIXME: possibly overkill we we have contiguous/noncontiguous data
993 tmpbufs[index] = xbt_malloc(count * dataext);
995 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
999 for(other = rank + 1; other < size; other++) {
1001 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1004 // Wait for completion of all comms.
1005 smpi_mpi_startall(size - 1, requests);
1006 for(other = 0; other < size - 1; other++) {
1007 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1008 if(index == MPI_UNDEFINED) {
1012 // #Request is below rank: it's a irecv
1013 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1016 for(index = 0; index < rank; index++) {
1017 xbt_free(tmpbufs[index]);