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 if((ref->src == MPI_ANY_SOURCE || req->src == ref->src)
27 && (ref->tag == MPI_ANY_TAG || req->tag == ref->tag)){
28 //we match, we can transfer some values
29 // FIXME : move this to the copy function ?
30 if(ref->src == MPI_ANY_SOURCE)ref->real_src = req->src;
31 if(ref->tag == MPI_ANY_TAG)ref->real_tag = req->tag;
32 if(ref->real_size < req->real_size) ref->truncated = 1;
34 ref->detached_sender=req; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
40 static int match_send(void* a, void* b,smx_action_t ignored) {
41 MPI_Request ref = (MPI_Request)a;
42 MPI_Request req = (MPI_Request)b;
43 XBT_DEBUG("Trying to match a send of src %d against %d, tag %d against %d",ref->src,req->src, ref->tag, req->tag);
44 xbt_assert(ref, "Cannot match send against null reference");
45 xbt_assert(req, "Cannot match send against null request");
47 if((req->src == MPI_ANY_SOURCE || req->src == ref->src)
48 && (req->tag == MPI_ANY_TAG || req->tag == ref->tag))
50 if(req->src == MPI_ANY_SOURCE)req->real_src = ref->src;
51 if(req->tag == MPI_ANY_TAG)req->real_tag = ref->tag;
52 if(req->real_size < ref->real_size) req->truncated = 1;
54 req->detached_sender=ref; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
61 static MPI_Request build_request(void *buf, int count,
62 MPI_Datatype datatype, int src, int dst,
63 int tag, MPI_Comm comm, unsigned flags)
69 request = xbt_new(s_smpi_mpi_request_t, 1);
71 s_smpi_subtype_t *subtype = datatype->substruct;
73 if(datatype->has_subtype == 1){
74 // This part handles the problem of non-contiguous memory
76 buf = malloc(count*smpi_datatype_size(datatype));
78 subtype->serialize(old_buf, buf, count, datatype->substruct);
83 // This part handles the problem of non-contiguous memory (for the
84 // unserialisation at the reception)
85 request->old_buf = old_buf;
86 request->old_type = datatype;
88 request->size = smpi_datatype_size(datatype) * count;
93 request->action = NULL;
94 request->flags = flags;
95 request->detached = 0;
96 request->detached_sender = NULL;
98 request->truncated = 0;
99 request->real_size = 0;
100 request->real_tag = 0;
107 if (flags & SEND) smpi_datatype_unuse(datatype);
113 void smpi_empty_status(MPI_Status * status) {
114 if(status != MPI_STATUS_IGNORE) {
115 status->MPI_SOURCE=MPI_ANY_SOURCE;
116 status->MPI_TAG=MPI_ANY_TAG;
121 void smpi_action_trace_run(char *path)
125 xbt_dict_cursor_t cursor;
129 action_fp = fopen(path, "r");
130 xbt_assert(action_fp != NULL, "Cannot open %s: %s", path,
134 if (!xbt_dict_is_empty(action_queues)) {
136 ("Not all actions got consumed. If the simulation ended successfully (without deadlock), you may want to add new processes to your deployment file.");
139 xbt_dict_foreach(action_queues, cursor, name, todo) {
140 XBT_WARN("Still %lu actions for %s", xbt_dynar_length(todo), name);
146 xbt_dict_free(&action_queues);
147 action_queues = xbt_dict_new_homogeneous(NULL);
150 static void smpi_mpi_request_free_voidp(void* request)
152 MPI_Request req = request;
153 smpi_mpi_request_free(&req);
156 /* MPI Low level calls */
157 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
158 int dst, int tag, MPI_Comm comm)
160 MPI_Request request =
161 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
162 comm, PERSISTENT | SEND);
167 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
168 int src, int tag, MPI_Comm comm)
170 MPI_Request request =
171 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
172 comm, PERSISTENT | RECV);
177 void smpi_mpi_start(MPI_Request request)
181 xbt_assert(!request->action,
182 "Cannot (re)start a non-finished communication");
183 if(request->flags & RECV) {
184 print_request("New recv", request);
185 if (request->size < surf_cfg_get_int("smpi/async_small_thres"))
186 mailbox = smpi_process_mailbox_small();
188 mailbox = smpi_process_mailbox();
189 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
190 request->real_size=request->size;
191 smpi_datatype_use(request->old_type);
192 request->action = simcall_comm_irecv(mailbox, request->buf, &request->real_size, &match_recv, request);
195 int receiver = smpi_group_index(smpi_comm_group(request->comm), request->dst);
196 /* if(receiver == MPI_UNDEFINED) {*/
197 /* XBT_WARN("Trying to send a message to a wrong rank");*/
200 print_request("New send", request);
201 if (request->size < surf_cfg_get_int("smpi/async_small_thres")) { // eager mode
202 mailbox = smpi_process_remote_mailbox_small(receiver);
204 XBT_DEBUG("Send request %p is not in the permanent receive mailbox (buf: %p)",request,request->buf);
205 mailbox = smpi_process_remote_mailbox(receiver);
207 if (request->size < 64*1024 ) { //(FIXME: this limit should be configurable)
209 request->detached = 1;
211 if(request->old_type->has_subtype == 0){
212 oldbuf = request->buf;
214 request->buf = malloc(request->size);
215 memcpy(request->buf,oldbuf,request->size);
218 XBT_DEBUG("Send request %p is detached; buf %p copied into %p",request,oldbuf,request->buf);
220 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
221 request->real_size=request->size;
222 smpi_datatype_use(request->old_type);
225 simcall_comm_isend(mailbox, request->size, -1.0,
226 request->buf, request->real_size,
228 &smpi_mpi_request_free_voidp, // how to free the userdata if a detached send fails
230 // detach if msg size < eager/rdv switch limit
234 /* FIXME: detached sends are not traceable (request->action == NULL) */
236 simcall_set_category(request->action, TRACE_internal_smpi_get_category());
243 void smpi_mpi_startall(int count, MPI_Request * requests)
247 for(i = 0; i < count; i++) {
248 smpi_mpi_start(requests[i]);
252 void smpi_mpi_request_free(MPI_Request * request)
255 if((*request) != MPI_REQUEST_NULL){
256 (*request)->refcount--;
257 if((*request)->refcount<0) xbt_die("wrong refcount");
259 if((*request)->refcount==0){
261 *request = MPI_REQUEST_NULL;
264 xbt_die("freeing an already free request");
268 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
269 int dst, int tag, MPI_Comm comm)
271 MPI_Request request =
272 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
273 comm, NON_PERSISTENT | SEND);
278 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
279 int dst, int tag, MPI_Comm comm)
281 MPI_Request request =
282 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
283 comm, NON_PERSISTENT | SEND);
285 smpi_mpi_start(request);
289 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
290 int src, int tag, MPI_Comm comm)
292 MPI_Request request =
293 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
294 comm, NON_PERSISTENT | RECV);
298 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
299 int src, int tag, MPI_Comm comm)
301 MPI_Request request =
302 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
303 comm, NON_PERSISTENT | RECV);
305 smpi_mpi_start(request);
309 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
310 int tag, MPI_Comm comm, MPI_Status * status)
313 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
314 smpi_mpi_wait(&request, status);
319 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
320 int tag, MPI_Comm comm)
323 request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
324 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
328 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
329 int dst, int sendtag, void *recvbuf, int recvcount,
330 MPI_Datatype recvtype, int src, int recvtag,
331 MPI_Comm comm, MPI_Status * status)
333 MPI_Request requests[2];
337 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
339 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
340 smpi_mpi_startall(2, requests);
341 smpi_mpi_waitall(2, requests, stats);
342 if(status != MPI_STATUS_IGNORE) {
343 // Copy receive status
344 memcpy(status, &stats[1], sizeof(MPI_Status));
348 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
350 return status->count / smpi_datatype_size(datatype);
353 static void finish_wait(MPI_Request * request, MPI_Status * status)
355 MPI_Request req = *request;
356 if(!(req->detached && req->flags & SEND)){
357 if(status != MPI_STATUS_IGNORE) {
358 status->MPI_SOURCE = req->src == MPI_ANY_SOURCE ? req->real_src : req->src;
359 status->MPI_TAG = req->tag == MPI_ANY_TAG ? req->real_tag : req->tag;
361 status->MPI_ERROR = MPI_ERR_TRUNCATE;
362 else status->MPI_ERROR = MPI_SUCCESS ;
363 // this handles the case were size in receive differs from size in send
364 // FIXME: really this should just contain the count of receive-type blocks,
366 status->count = req->real_size;
369 print_request("Finishing", req);
370 MPI_Datatype datatype = req->old_type;
372 if(datatype->has_subtype == 1){
373 // This part handles the problem of non-contignous memory
374 // the unserialization at the reception
375 s_smpi_subtype_t *subtype = datatype->substruct;
376 if(req->flags & RECV) {
377 subtype->unserialize(req->buf, req->old_buf, req->real_size/smpi_datatype_size(datatype) , datatype->substruct);
379 if(req->detached == 0) free(req->buf);
381 smpi_datatype_unuse(datatype);
384 if(req->detached_sender!=NULL){
385 smpi_mpi_request_free(&(req->detached_sender));
388 if(req->flags & NON_PERSISTENT) {
389 smpi_mpi_request_free(request);
395 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
398 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or smpi_mpi_testall before)
399 if ((*request)->action == NULL)
402 flag = simcall_comm_test((*request)->action);
404 (*request)->refcount++;
405 finish_wait(request, status);
407 smpi_empty_status(status);
412 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
419 *index = MPI_UNDEFINED;
422 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
423 map = xbt_new(int, count);
425 for(i = 0; i < count; i++) {
426 if((requests[i]!=MPI_REQUEST_NULL) && requests[i]->action) {
427 xbt_dynar_push(comms, &requests[i]->action);
433 i = simcall_comm_testany(comms);
434 // not MPI_UNDEFINED, as this is a simix return code
437 finish_wait(&requests[*index], status);
441 //all requests are null or inactive, return true
443 smpi_empty_status(status);
446 xbt_dynar_free(&comms);
453 int smpi_mpi_testall(int count, MPI_Request requests[],
457 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
460 for(i=0; i<count; i++){
461 if(requests[i]!= MPI_REQUEST_NULL){
462 if (smpi_mpi_test(&requests[i], pstat)!=1){
466 smpi_empty_status(pstat);
468 if(status != MPI_STATUSES_IGNORE) {
469 memcpy(&status[i], pstat, sizeof(*pstat));
475 void smpi_mpi_probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
477 //FIXME find another wait to avoid busy waiting ?
478 // the issue here is that we have to wait on a nonexistent comm
480 smpi_mpi_iprobe(source, tag, comm, &flag, status);
481 XBT_DEBUG("Busy Waiting on probing : %d", flag);
483 simcall_process_sleep(0.0001);
488 void smpi_mpi_iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
489 MPI_Request request =build_request(NULL, 0, MPI_CHAR, source, smpi_comm_rank(comm), tag,
490 comm, NON_PERSISTENT | RECV);
492 // behave like a receive, but don't do it
495 print_request("New iprobe", request);
496 // We have to test both mailboxes as we don't know if we will receive one one or another
497 if (surf_cfg_get_int("smpi/async_small_thres")>0){
498 mailbox = smpi_process_mailbox_small();
499 XBT_DEBUG("trying to probe the perm recv mailbox");
500 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
502 if (request->action==NULL){
503 mailbox = smpi_process_mailbox();
504 XBT_DEBUG("trying to probe the other mailbox");
505 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
509 MPI_Request req = (MPI_Request)SIMIX_comm_get_src_data(request->action);
511 if(status != MPI_STATUS_IGNORE) {
512 status->MPI_SOURCE = req->src;
513 status->MPI_TAG = req->tag;
514 status->MPI_ERROR = MPI_SUCCESS;
515 status->count = req->real_size;
519 smpi_mpi_request_free(&request);
524 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
526 print_request("Waiting", *request);
527 if ((*request)->action != NULL) { // this is not a detached send
528 simcall_comm_wait((*request)->action, -1.0);
530 finish_wait(request, status);
532 // FIXME for a detached send, finish_wait is not called:
535 int smpi_mpi_waitany(int count, MPI_Request requests[],
542 index = MPI_UNDEFINED;
544 // Wait for a request to complete
545 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
546 map = xbt_new(int, count);
548 XBT_DEBUG("Wait for one of %d", count);
549 for(i = 0; i < count; i++) {
550 if(requests[i] != MPI_REQUEST_NULL) {
551 if (requests[i]->action != NULL) {
552 XBT_DEBUG("Waiting any %p ", requests[i]);
553 xbt_dynar_push(comms, &requests[i]->action);
557 //This is a finished detached request, let's return this one
558 size=0;//so we free the dynar but don't do the waitany call
560 finish_wait(&requests[i], status);//cleanup if refcount = 0
561 requests[i]=MPI_REQUEST_NULL;//set to null
567 i = simcall_comm_waitany(comms);
569 // not MPI_UNDEFINED, as this is a simix return code
572 finish_wait(&requests[index], status);
576 xbt_dynar_free(&comms);
579 if (index==MPI_UNDEFINED)
580 smpi_empty_status(status);
585 int smpi_mpi_waitall(int count, MPI_Request requests[],
590 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
591 int retvalue=MPI_SUCCESS;
592 //tag invalid requests in the set
593 for(c = 0; c < count; c++) {
594 if(requests[c]==MPI_REQUEST_NULL || requests[c]->dst == MPI_PROC_NULL ){
595 if(status != MPI_STATUSES_IGNORE)
596 smpi_empty_status(&status[c]);
597 }else if(requests[c]->src == MPI_PROC_NULL ){
598 if(status != MPI_STATUSES_IGNORE) {
599 smpi_empty_status(&status[c]);
600 status[c].MPI_SOURCE=MPI_PROC_NULL;
604 for(c = 0; c < count; c++) {
606 smpi_mpi_wait(&requests[c], pstat);
609 index = smpi_mpi_waitany(count, requests, pstat);
610 if(index == MPI_UNDEFINED) {
613 if(status != MPI_STATUSES_IGNORE) {
614 memcpy(&status[index], pstat, sizeof(*pstat));
615 if(status[index].MPI_ERROR==MPI_ERR_TRUNCATE)retvalue=MPI_ERR_IN_STATUS;
624 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
629 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
632 for(i = 0; i < incount; i++)
634 index=smpi_mpi_waitany(incount, requests, pstat);
635 if(index!=MPI_UNDEFINED){
636 indices[count] = index;
638 if(status != MPI_STATUSES_IGNORE) {
639 memcpy(&status[index], pstat, sizeof(*pstat));
642 return MPI_UNDEFINED;
648 int smpi_mpi_testsome(int incount, MPI_Request requests[], int *indices,
651 int i, count, count_dead;
653 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
657 for(i = 0; i < incount; i++) {
658 if((requests[i] != MPI_REQUEST_NULL)) {
659 if(smpi_mpi_test(&requests[i], pstat)) {
662 if(status != MPI_STATUSES_IGNORE) {
663 memcpy(&status[i], pstat, sizeof(*pstat));
670 if(count_dead==incount)return MPI_UNDEFINED;
674 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
677 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
678 nary_tree_bcast(buf, count, datatype, root, comm, 4);
681 void smpi_mpi_barrier(MPI_Comm comm)
683 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
684 nary_tree_barrier(comm, 4);
687 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
688 void *recvbuf, int recvcount, MPI_Datatype recvtype,
689 int root, MPI_Comm comm)
691 int system_tag = 666;
692 int rank, size, src, index;
693 MPI_Aint lb = 0, recvext = 0;
694 MPI_Request *requests;
696 rank = smpi_comm_rank(comm);
697 size = smpi_comm_size(comm);
699 // Send buffer to root
700 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
702 // FIXME: check for errors
703 smpi_datatype_extent(recvtype, &lb, &recvext);
704 // Local copy from root
705 smpi_datatype_copy(sendbuf, sendcount, sendtype,
706 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
707 // Receive buffers from senders
708 requests = xbt_new(MPI_Request, size - 1);
710 for(src = 0; src < size; src++) {
712 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
714 src, system_tag, comm);
718 // Wait for completion of irecv's.
719 smpi_mpi_startall(size - 1, requests);
720 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
725 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
726 void *recvbuf, int *recvcounts, int *displs,
727 MPI_Datatype recvtype, int root, MPI_Comm comm)
729 int system_tag = 666;
730 int rank, size, src, index;
731 MPI_Aint lb = 0, recvext = 0;
732 MPI_Request *requests;
734 rank = smpi_comm_rank(comm);
735 size = smpi_comm_size(comm);
737 // Send buffer to root
738 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
740 // FIXME: check for errors
741 smpi_datatype_extent(recvtype, &lb, &recvext);
742 // Local copy from root
743 smpi_datatype_copy(sendbuf, sendcount, sendtype,
744 (char *)recvbuf + displs[root] * recvext,
745 recvcounts[root], recvtype);
746 // Receive buffers from senders
747 requests = xbt_new(MPI_Request, size - 1);
749 for(src = 0; src < size; src++) {
752 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
753 recvcounts[src], recvtype, src, system_tag, comm);
757 // Wait for completion of irecv's.
758 smpi_mpi_startall(size - 1, requests);
759 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
764 void smpi_mpi_allgather(void *sendbuf, int sendcount,
765 MPI_Datatype sendtype, void *recvbuf,
766 int recvcount, MPI_Datatype recvtype,
769 int system_tag = 666;
770 int rank, size, other, index;
771 MPI_Aint lb = 0, recvext = 0;
772 MPI_Request *requests;
774 rank = smpi_comm_rank(comm);
775 size = smpi_comm_size(comm);
776 // FIXME: check for errors
777 smpi_datatype_extent(recvtype, &lb, &recvext);
778 // Local copy from self
779 smpi_datatype_copy(sendbuf, sendcount, sendtype,
780 (char *)recvbuf + rank * recvcount * recvext, recvcount,
782 // Send/Recv buffers to/from others;
783 requests = xbt_new(MPI_Request, 2 * (size - 1));
785 for(other = 0; other < size; other++) {
788 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
791 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
792 recvcount, recvtype, other,
797 // Wait for completion of all comms.
798 smpi_mpi_startall(2 * (size - 1), requests);
799 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
803 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
804 MPI_Datatype sendtype, void *recvbuf,
805 int *recvcounts, int *displs,
806 MPI_Datatype recvtype, MPI_Comm comm)
808 int system_tag = 666;
809 int rank, size, other, index;
810 MPI_Aint lb = 0, recvext = 0;
811 MPI_Request *requests;
813 rank = smpi_comm_rank(comm);
814 size = smpi_comm_size(comm);
815 // FIXME: check for errors
816 smpi_datatype_extent(recvtype, &lb, &recvext);
817 // Local copy from self
818 smpi_datatype_copy(sendbuf, sendcount, sendtype,
819 (char *)recvbuf + displs[rank] * recvext,
820 recvcounts[rank], recvtype);
821 // Send buffers to others;
822 requests = xbt_new(MPI_Request, 2 * (size - 1));
824 for(other = 0; other < size; other++) {
827 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
831 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
832 recvtype, other, system_tag, comm);
836 // Wait for completion of all comms.
837 smpi_mpi_startall(2 * (size - 1), requests);
838 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
842 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
843 void *recvbuf, int recvcount, MPI_Datatype recvtype,
844 int root, MPI_Comm comm)
846 int system_tag = 666;
847 int rank, size, dst, index;
848 MPI_Aint lb = 0, sendext = 0;
849 MPI_Request *requests;
851 rank = smpi_comm_rank(comm);
852 size = smpi_comm_size(comm);
854 // Recv buffer from root
855 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
858 // FIXME: check for errors
859 smpi_datatype_extent(sendtype, &lb, &sendext);
860 // Local copy from root
861 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
862 sendcount, sendtype, recvbuf, recvcount, recvtype);
863 // Send buffers to receivers
864 requests = xbt_new(MPI_Request, size - 1);
866 for(dst = 0; dst < size; dst++) {
868 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
869 sendcount, sendtype, dst,
874 // Wait for completion of isend's.
875 smpi_mpi_startall(size - 1, requests);
876 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
881 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
882 MPI_Datatype sendtype, void *recvbuf, int recvcount,
883 MPI_Datatype recvtype, int root, MPI_Comm comm)
885 int system_tag = 666;
886 int rank, size, dst, index;
887 MPI_Aint lb = 0, sendext = 0;
888 MPI_Request *requests;
890 rank = smpi_comm_rank(comm);
891 size = smpi_comm_size(comm);
893 // Recv buffer from root
894 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
897 // FIXME: check for errors
898 smpi_datatype_extent(sendtype, &lb, &sendext);
899 // Local copy from root
900 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
901 sendtype, recvbuf, recvcount, recvtype);
902 // Send buffers to receivers
903 requests = xbt_new(MPI_Request, size - 1);
905 for(dst = 0; dst < size; dst++) {
908 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
909 sendtype, dst, system_tag, comm);
913 // Wait for completion of isend's.
914 smpi_mpi_startall(size - 1, requests);
915 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
920 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
921 MPI_Datatype datatype, MPI_Op op, int root,
924 int system_tag = 666;
925 int rank, size, src, index;
926 MPI_Aint lb = 0, dataext = 0;
927 MPI_Request *requests;
930 rank = smpi_comm_rank(comm);
931 size = smpi_comm_size(comm);
933 // Send buffer to root
934 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
936 // FIXME: check for errors
937 smpi_datatype_extent(datatype, &lb, &dataext);
938 // Local copy from root
939 if (sendbuf && recvbuf)
940 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
941 // Receive buffers from senders
942 //TODO: make a MPI_barrier here ?
943 requests = xbt_new(MPI_Request, size - 1);
944 tmpbufs = xbt_new(void *, size - 1);
946 for(src = 0; src < size; src++) {
948 // FIXME: possibly overkill we we have contiguous/noncontiguous data
950 tmpbufs[index] = xbt_malloc(count * dataext);
952 smpi_irecv_init(tmpbufs[index], count, datatype, src,
957 // Wait for completion of irecv's.
958 smpi_mpi_startall(size - 1, requests);
959 for(src = 0; src < size - 1; src++) {
960 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
961 XBT_DEBUG("finished waiting any request with index %d", index);
962 if(index == MPI_UNDEFINED) {
965 if(op) /* op can be MPI_OP_NULL that does nothing */
966 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
968 for(index = 0; index < size - 1; index++) {
969 xbt_free(tmpbufs[index]);
976 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
977 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
979 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
980 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
983 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
984 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
986 int system_tag = 666;
987 int rank, size, other, index;
988 MPI_Aint lb = 0, dataext = 0;
989 MPI_Request *requests;
992 rank = smpi_comm_rank(comm);
993 size = smpi_comm_size(comm);
995 // FIXME: check for errors
996 smpi_datatype_extent(datatype, &lb, &dataext);
998 // Local copy from self
999 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
1001 // Send/Recv buffers to/from others;
1002 requests = xbt_new(MPI_Request, size - 1);
1003 tmpbufs = xbt_new(void *, rank);
1005 for(other = 0; other < rank; other++) {
1006 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1008 tmpbufs[index] = xbt_malloc(count * dataext);
1010 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
1014 for(other = rank + 1; other < size; other++) {
1016 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1019 // Wait for completion of all comms.
1020 smpi_mpi_startall(size - 1, requests);
1021 for(other = 0; other < size - 1; other++) {
1022 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1023 if(index == MPI_UNDEFINED) {
1027 // #Request is below rank: it's a irecv
1028 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1031 for(index = 0; index < rank; index++) {
1032 xbt_free(tmpbufs[index]);