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;
33 else ref->truncated = 0;
38 static int match_send(void* a, void* b,smx_action_t ignored) {
39 MPI_Request ref = (MPI_Request)a;
40 MPI_Request req = (MPI_Request)b;
41 XBT_DEBUG("Trying to match a send of src %d against %d, tag %d against %d",ref->src,req->src, ref->tag, req->tag);
42 xbt_assert(ref, "Cannot match send against null reference");
43 xbt_assert(req, "Cannot match send against null request");
45 if((req->src == MPI_ANY_SOURCE || req->src == ref->src)
46 && (req->tag == MPI_ANY_TAG || req->tag == ref->tag))
48 if(req->src == MPI_ANY_SOURCE)req->real_src = ref->src;
49 if(req->tag == MPI_ANY_TAG)req->real_tag = ref->tag;
50 if(req->real_size < ref->real_size) req->truncated = 1;
51 else ref->truncated = 0;
56 static MPI_Request build_request(void *buf, int count,
57 MPI_Datatype datatype, int src, int dst,
58 int tag, MPI_Comm comm, unsigned flags)
64 request = xbt_new(s_smpi_mpi_request_t, 1);
66 s_smpi_subtype_t *subtype = datatype->substruct;
68 if(datatype->has_subtype == 1){
69 // This part handles the problem of non-contiguous memory
71 buf = malloc(count*smpi_datatype_size(datatype));
73 subtype->serialize(old_buf, buf, count, datatype->substruct);
78 // This part handles the problem of non-contiguous memory (for the
79 // unserialisation at the reception)
80 request->old_buf = old_buf;
81 request->old_type = datatype;
83 request->size = smpi_datatype_size(datatype) * count;
88 request->action = NULL;
89 request->flags = flags;
90 request->detached = 0;
95 if (flags & SEND) smpi_datatype_unuse(datatype);
101 void smpi_empty_status(MPI_Status * status) {
102 if(status != MPI_STATUS_IGNORE) {
103 status->MPI_SOURCE=MPI_ANY_SOURCE;
104 status->MPI_TAG=MPI_ANY_TAG;
109 void smpi_action_trace_run(char *path)
113 xbt_dict_cursor_t cursor;
117 action_fp = fopen(path, "r");
118 xbt_assert(action_fp != NULL, "Cannot open %s: %s", path,
122 if (!xbt_dict_is_empty(action_queues)) {
124 ("Not all actions got consumed. If the simulation ended successfully (without deadlock), you may want to add new processes to your deployment file.");
127 xbt_dict_foreach(action_queues, cursor, name, todo) {
128 XBT_WARN("Still %lu actions for %s", xbt_dynar_length(todo), name);
134 xbt_dict_free(&action_queues);
135 action_queues = xbt_dict_new_homogeneous(NULL);
138 static void smpi_mpi_request_free_voidp(void* request)
140 MPI_Request req = request;
141 smpi_mpi_request_free(&req);
144 /* MPI Low level calls */
145 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
146 int dst, int tag, MPI_Comm comm)
148 MPI_Request request =
149 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
150 comm, PERSISTENT | SEND);
155 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
156 int src, int tag, MPI_Comm comm)
158 MPI_Request request =
159 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
160 comm, PERSISTENT | RECV);
165 void smpi_mpi_start(MPI_Request request)
169 xbt_assert(!request->action,
170 "Cannot (re)start a non-finished communication");
171 if(request->flags & RECV) {
172 print_request("New recv", request);
173 if (request->size < surf_cfg_get_int("smpi/async_small_thres"))
174 mailbox = smpi_process_mailbox_small();
176 mailbox = smpi_process_mailbox();
177 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
178 request->real_size=request->size;
179 smpi_datatype_use(request->old_type);
180 request->action = simcall_comm_irecv(mailbox, request->buf, &request->real_size, &match_recv, request);
181 if (request->action)request->action->comm.refcount++;
184 int receiver = smpi_group_index(smpi_comm_group(request->comm), request->dst);
185 /* if(receiver == MPI_UNDEFINED) {*/
186 /* XBT_WARN("Trying to send a message to a wrong rank");*/
189 print_request("New send", request);
190 if (request->size < surf_cfg_get_int("smpi/async_small_thres")) { // eager mode
191 mailbox = smpi_process_remote_mailbox_small(receiver);
193 XBT_DEBUG("Send request %p is not in the permanent receive mailbox (buf: %p)",request,request->buf);
194 mailbox = smpi_process_remote_mailbox(receiver);
196 if (request->size < 64*1024 ) { //(FIXME: this limit should be configurable)
198 if(request->old_type->has_subtype == 0){
199 oldbuf = request->buf;
200 request->detached = 1;
202 request->buf = malloc(request->size);
203 memcpy(request->buf,oldbuf,request->size);
206 XBT_DEBUG("Send request %p is detached; buf %p copied into %p",request,oldbuf,request->buf);
208 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
209 request->real_size=request->size;
210 smpi_datatype_use(request->old_type);
213 simcall_comm_isend(mailbox, request->size, -1.0,
214 request->buf, request->real_size,
216 &smpi_mpi_request_free_voidp, // how to free the userdata if a detached send fails
218 // detach if msg size < eager/rdv switch limit
220 if (request->action)request->action->comm.refcount++;
223 /* FIXME: detached sends are not traceable (request->action == NULL) */
225 simcall_set_category(request->action, TRACE_internal_smpi_get_category());
232 void smpi_mpi_startall(int count, MPI_Request * requests)
236 for(i = 0; i < count; i++) {
237 smpi_mpi_start(requests[i]);
241 void smpi_mpi_request_free(MPI_Request * request)
244 *request = MPI_REQUEST_NULL;
247 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
248 int dst, int tag, MPI_Comm comm)
250 MPI_Request request =
251 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
252 comm, NON_PERSISTENT | SEND);
257 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
258 int dst, int tag, MPI_Comm comm)
260 MPI_Request request =
261 smpi_isend_init(buf, count, datatype, dst, tag, comm);
263 smpi_mpi_start(request);
267 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
268 int src, int tag, MPI_Comm comm)
270 MPI_Request request =
271 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
272 comm, NON_PERSISTENT | RECV);
276 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
277 int src, int tag, MPI_Comm comm)
279 MPI_Request request =
280 smpi_irecv_init(buf, count, datatype, src, tag, comm);
282 smpi_mpi_start(request);
286 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
287 int tag, MPI_Comm comm, MPI_Status * status)
290 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
291 smpi_mpi_wait(&request, status);
296 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
297 int tag, MPI_Comm comm)
299 MPI_Request request =
300 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
301 comm, NON_PERSISTENT | SEND | RECV_DELETE);
303 smpi_mpi_start(request);
304 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
308 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
309 int dst, int sendtag, void *recvbuf, int recvcount,
310 MPI_Datatype recvtype, int src, int recvtag,
311 MPI_Comm comm, MPI_Status * status)
313 MPI_Request requests[2];
317 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
319 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
320 smpi_mpi_startall(2, requests);
321 smpi_mpi_waitall(2, requests, stats);
322 if(status != MPI_STATUS_IGNORE) {
323 // Copy receive status
324 memcpy(status, &stats[1], sizeof(MPI_Status));
328 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
330 return status->count / smpi_datatype_size(datatype);
333 static void finish_wait(MPI_Request * request, MPI_Status * status)
335 MPI_Request req = *request;
337 if(status != MPI_STATUS_IGNORE) {
338 status->MPI_SOURCE = req->src == MPI_ANY_SOURCE ? req->real_src : req->src;
339 status->MPI_TAG = req->tag == MPI_ANY_TAG ? req->real_tag : req->tag;
341 status->MPI_ERROR = MPI_ERR_TRUNCATE;
342 else status->MPI_ERROR = MPI_SUCCESS ;
343 // this handles the case were size in receive differs from size in send
344 // FIXME: really this should just contain the count of receive-type blocks,
346 status->count = req->real_size;
350 print_request("Finishing", req);
351 MPI_Datatype datatype = req->old_type;
353 if(datatype->has_subtype == 1){
354 // This part handles the problem of non-contignous memory
355 // the unserialization at the reception
356 s_smpi_subtype_t *subtype = datatype->substruct;
357 if(req->flags & RECV) {
358 subtype->unserialize(req->buf, req->old_buf, req->real_size/smpi_datatype_size(datatype) , datatype->substruct);
360 if(req->detached == 0) free(req->buf);
362 smpi_datatype_unuse(datatype);
365 if(req->flags & NON_PERSISTENT) {
366 if(req->flags & RECV &&
368 (req->action->state == SIMIX_DONE))
370 MPI_Request sender_request = (MPI_Request)SIMIX_comm_get_src_data(req->action);
371 if((sender_request!=MPI_REQUEST_NULL) &&
372 ( sender_request->detached ) &&
373 ( sender_request->flags & RECV_DELETE))
375 //we are in a receiver's wait from a detached send
376 //we have to clean the sender's side request here.... but only if done by a send, not an isend
377 //the request lives senderside for an isend. As detached is currently for send + isend, we use RECV_DELETE to separate them
378 //FIXME : see if just removing detached status for isend is also good
379 smpi_mpi_request_free(&sender_request);
385 //if we want to free our request, we have to invalidate it at the other end of the comm
386 if(req->flags & SEND){
387 req->action->comm.src_data=MPI_REQUEST_NULL;
389 req->action->comm.dst_data=MPI_REQUEST_NULL;
392 smx_action_t temp=req->action;
393 if(req->action->comm.refcount == 1)req->action = NULL;
394 SIMIX_comm_destroy(temp);
399 smpi_mpi_request_free(request);
403 if(req->action)SIMIX_comm_destroy(req->action);
408 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
411 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or smpi_mpi_testall before)
412 if ((*request)->action == NULL)
415 flag = simcall_comm_test((*request)->action);
417 finish_wait(request, status);
419 smpi_empty_status(status);
424 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
431 *index = MPI_UNDEFINED;
434 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
435 map = xbt_new(int, count);
437 for(i = 0; i < count; i++) {
438 if((requests[i]!=MPI_REQUEST_NULL) && requests[i]->action) {
439 xbt_dynar_push(comms, &requests[i]->action);
445 i = simcall_comm_testany(comms);
446 // not MPI_UNDEFINED, as this is a simix return code
449 finish_wait(&requests[*index], status);
453 //all requests are null or inactive, return true
455 smpi_empty_status(status);
458 xbt_dynar_free(&comms);
465 int smpi_mpi_testall(int count, MPI_Request requests[],
469 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
472 for(i=0; i<count; i++){
473 if(requests[i]!= MPI_REQUEST_NULL){
474 if (smpi_mpi_test(&requests[i], pstat)!=1){
478 smpi_empty_status(pstat);
480 if(status != MPI_STATUSES_IGNORE) {
481 memcpy(&status[i], pstat, sizeof(*pstat));
487 void smpi_mpi_probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
489 //FIXME find another wait to avoid busy waiting ?
490 // the issue here is that we have to wait on a nonexistent comm
492 smpi_mpi_iprobe(source, tag, comm, &flag, status);
493 XBT_DEBUG("Busy Waiting on probing : %d", flag);
495 simcall_process_sleep(0.0001);
500 void smpi_mpi_iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
501 MPI_Request request =build_request(NULL, 0, MPI_CHAR, source, smpi_comm_rank(comm), tag,
502 comm, NON_PERSISTENT | RECV);
504 // behave like a receive, but don't do it
507 print_request("New iprobe", request);
508 // We have to test both mailboxes as we don't know if we will receive one one or another
509 if (surf_cfg_get_int("smpi/async_small_thres")>0){
510 mailbox = smpi_process_mailbox_small();
511 XBT_DEBUG("trying to probe the perm recv mailbox");
512 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
514 if (request->action==NULL){
515 mailbox = smpi_process_mailbox();
516 XBT_DEBUG("trying to probe the other mailbox");
517 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
521 MPI_Request req = (MPI_Request)SIMIX_comm_get_src_data(request->action);
523 if(status != MPI_STATUS_IGNORE) {
524 status->MPI_SOURCE = req->src;
525 status->MPI_TAG = req->tag;
526 status->MPI_ERROR = MPI_SUCCESS;
527 status->count = req->real_size;
531 smpi_mpi_request_free(&request);
536 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
538 print_request("Waiting", *request);
539 if ((*request)->action != NULL) { // this is not a detached send
540 simcall_comm_wait((*request)->action, -1.0);
541 finish_wait(request, status);
544 // FIXME for a detached send, finish_wait is not called:
547 int smpi_mpi_waitany(int count, MPI_Request requests[],
554 index = MPI_UNDEFINED;
556 // Wait for a request to complete
557 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
558 map = xbt_new(int, count);
560 XBT_DEBUG("Wait for one of");
561 for(i = 0; i < count; i++) {
562 if((requests[i] != MPI_REQUEST_NULL) && (requests[i]->action != NULL)) {
563 print_request("Waiting any ", requests[i]);
564 xbt_dynar_push(comms, &requests[i]->action);
570 i = simcall_comm_waitany(comms);
572 // not MPI_UNDEFINED, as this is a simix return code
575 finish_wait(&requests[index], status);
579 xbt_dynar_free(&comms);
582 if (index==MPI_UNDEFINED)
583 smpi_empty_status(status);
588 int smpi_mpi_waitall(int count, MPI_Request requests[],
593 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
594 int retvalue=MPI_SUCCESS;
595 //tag invalid requests in the set
596 for(c = 0; c < count; c++) {
597 if(requests[c]==MPI_REQUEST_NULL || requests[c]->dst == MPI_PROC_NULL ){
598 if(status != MPI_STATUSES_IGNORE)
599 smpi_empty_status(&status[c]);
600 }else if(requests[c]->src == MPI_PROC_NULL ){
601 if(status != MPI_STATUSES_IGNORE) {
602 smpi_empty_status(&status[c]);
603 status[c].MPI_SOURCE=MPI_PROC_NULL;
608 for(c = 0; c < count; c++) {
610 smpi_mpi_wait(&requests[c], pstat);
613 index = smpi_mpi_waitany(count, requests, pstat);
614 if(index == MPI_UNDEFINED) {
617 if(status != MPI_STATUSES_IGNORE) {
618 memcpy(&status[index], pstat, sizeof(*pstat));
619 if(status[index].MPI_ERROR==MPI_ERR_TRUNCATE)retvalue=MPI_ERR_IN_STATUS;
627 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
632 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
635 for(i = 0; i < incount; i++)
637 index=smpi_mpi_waitany(incount, requests, pstat);
638 if(index!=MPI_UNDEFINED){
639 indices[count] = index;
641 if(status != MPI_STATUSES_IGNORE) {
642 memcpy(&status[index], pstat, sizeof(*pstat));
645 return MPI_UNDEFINED;
651 int smpi_mpi_testsome(int incount, MPI_Request requests[], int *indices,
654 int i, count, count_dead;
656 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
660 for(i = 0; i < incount; i++) {
661 if((requests[i] != MPI_REQUEST_NULL)) {
662 if(smpi_mpi_test(&requests[i], pstat)) {
665 if(status != MPI_STATUSES_IGNORE) {
666 memcpy(&status[i], pstat, sizeof(*pstat));
673 if(count_dead==incount)return MPI_UNDEFINED;
677 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
680 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
681 nary_tree_bcast(buf, count, datatype, root, comm, 4);
684 void smpi_mpi_barrier(MPI_Comm comm)
686 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
687 nary_tree_barrier(comm, 4);
690 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
691 void *recvbuf, int recvcount, MPI_Datatype recvtype,
692 int root, MPI_Comm comm)
694 int system_tag = 666;
695 int rank, size, src, index;
696 MPI_Aint lb = 0, recvext = 0;
697 MPI_Request *requests;
699 rank = smpi_comm_rank(comm);
700 size = smpi_comm_size(comm);
702 // Send buffer to root
703 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
705 // FIXME: check for errors
706 smpi_datatype_extent(recvtype, &lb, &recvext);
707 // Local copy from root
708 smpi_datatype_copy(sendbuf, sendcount, sendtype,
709 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
710 // Receive buffers from senders
711 requests = xbt_new(MPI_Request, size - 1);
713 for(src = 0; src < size; src++) {
715 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
717 src, system_tag, comm);
721 // Wait for completion of irecv's.
722 smpi_mpi_startall(size - 1, requests);
723 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
728 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
729 void *recvbuf, int *recvcounts, int *displs,
730 MPI_Datatype recvtype, int root, MPI_Comm comm)
732 int system_tag = 666;
733 int rank, size, src, index;
734 MPI_Aint lb = 0, recvext = 0;
735 MPI_Request *requests;
737 rank = smpi_comm_rank(comm);
738 size = smpi_comm_size(comm);
740 // Send buffer to root
741 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
743 // FIXME: check for errors
744 smpi_datatype_extent(recvtype, &lb, &recvext);
745 // Local copy from root
746 smpi_datatype_copy(sendbuf, sendcount, sendtype,
747 (char *)recvbuf + displs[root] * recvext,
748 recvcounts[root], recvtype);
749 // Receive buffers from senders
750 requests = xbt_new(MPI_Request, size - 1);
752 for(src = 0; src < size; src++) {
755 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
756 recvcounts[src], recvtype, src, system_tag, comm);
760 // Wait for completion of irecv's.
761 smpi_mpi_startall(size - 1, requests);
762 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
767 void smpi_mpi_allgather(void *sendbuf, int sendcount,
768 MPI_Datatype sendtype, void *recvbuf,
769 int recvcount, MPI_Datatype recvtype,
772 int system_tag = 666;
773 int rank, size, other, index;
774 MPI_Aint lb = 0, recvext = 0;
775 MPI_Request *requests;
777 rank = smpi_comm_rank(comm);
778 size = smpi_comm_size(comm);
779 // FIXME: check for errors
780 smpi_datatype_extent(recvtype, &lb, &recvext);
781 // Local copy from self
782 smpi_datatype_copy(sendbuf, sendcount, sendtype,
783 (char *)recvbuf + rank * recvcount * recvext, recvcount,
785 // Send/Recv buffers to/from others;
786 requests = xbt_new(MPI_Request, 2 * (size - 1));
788 for(other = 0; other < size; other++) {
791 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
794 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
795 recvcount, recvtype, other,
800 // Wait for completion of all comms.
801 smpi_mpi_startall(2 * (size - 1), requests);
802 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
806 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
807 MPI_Datatype sendtype, void *recvbuf,
808 int *recvcounts, int *displs,
809 MPI_Datatype recvtype, MPI_Comm comm)
811 int system_tag = 666;
812 int rank, size, other, index;
813 MPI_Aint lb = 0, recvext = 0;
814 MPI_Request *requests;
816 rank = smpi_comm_rank(comm);
817 size = smpi_comm_size(comm);
818 // FIXME: check for errors
819 smpi_datatype_extent(recvtype, &lb, &recvext);
820 // Local copy from self
821 smpi_datatype_copy(sendbuf, sendcount, sendtype,
822 (char *)recvbuf + displs[rank] * recvext,
823 recvcounts[rank], recvtype);
824 // Send buffers to others;
825 requests = xbt_new(MPI_Request, 2 * (size - 1));
827 for(other = 0; other < size; other++) {
830 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
834 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
835 recvtype, other, system_tag, comm);
839 // Wait for completion of all comms.
840 smpi_mpi_startall(2 * (size - 1), requests);
841 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
845 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
846 void *recvbuf, int recvcount, MPI_Datatype recvtype,
847 int root, MPI_Comm comm)
849 int system_tag = 666;
850 int rank, size, dst, index;
851 MPI_Aint lb = 0, sendext = 0;
852 MPI_Request *requests;
854 rank = smpi_comm_rank(comm);
855 size = smpi_comm_size(comm);
857 // Recv buffer from root
858 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
861 // FIXME: check for errors
862 smpi_datatype_extent(sendtype, &lb, &sendext);
863 // Local copy from root
864 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
865 sendcount, sendtype, recvbuf, recvcount, recvtype);
866 // Send buffers to receivers
867 requests = xbt_new(MPI_Request, size - 1);
869 for(dst = 0; dst < size; dst++) {
871 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
872 sendcount, sendtype, dst,
877 // Wait for completion of isend's.
878 smpi_mpi_startall(size - 1, requests);
879 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
884 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
885 MPI_Datatype sendtype, void *recvbuf, int recvcount,
886 MPI_Datatype recvtype, int root, MPI_Comm comm)
888 int system_tag = 666;
889 int rank, size, dst, index;
890 MPI_Aint lb = 0, sendext = 0;
891 MPI_Request *requests;
893 rank = smpi_comm_rank(comm);
894 size = smpi_comm_size(comm);
896 // Recv buffer from root
897 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
900 // FIXME: check for errors
901 smpi_datatype_extent(sendtype, &lb, &sendext);
902 // Local copy from root
903 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
904 sendtype, recvbuf, recvcount, recvtype);
905 // Send buffers to receivers
906 requests = xbt_new(MPI_Request, size - 1);
908 for(dst = 0; dst < size; dst++) {
911 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
912 sendtype, dst, system_tag, comm);
916 // Wait for completion of isend's.
917 smpi_mpi_startall(size - 1, requests);
918 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
923 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
924 MPI_Datatype datatype, MPI_Op op, int root,
927 int system_tag = 666;
928 int rank, size, src, index;
929 MPI_Aint lb = 0, dataext = 0;
930 MPI_Request *requests;
933 rank = smpi_comm_rank(comm);
934 size = smpi_comm_size(comm);
936 // Send buffer to root
937 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
939 // FIXME: check for errors
940 smpi_datatype_extent(datatype, &lb, &dataext);
941 // Local copy from root
942 if (sendbuf && recvbuf)
943 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
944 // Receive buffers from senders
945 //TODO: make a MPI_barrier here ?
946 requests = xbt_new(MPI_Request, size - 1);
947 tmpbufs = xbt_new(void *, size - 1);
949 for(src = 0; src < size; src++) {
951 // FIXME: possibly overkill we we have contiguous/noncontiguous data
953 tmpbufs[index] = xbt_malloc(count * dataext);
955 smpi_irecv_init(tmpbufs[index], count, datatype, src,
960 // Wait for completion of irecv's.
961 smpi_mpi_startall(size - 1, requests);
962 for(src = 0; src < size - 1; src++) {
963 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
964 XBT_DEBUG("finished waiting any request with index %d", index);
965 if(index == MPI_UNDEFINED) {
968 if(op) /* op can be MPI_OP_NULL that does nothing */
969 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
971 for(index = 0; index < size - 1; index++) {
972 xbt_free(tmpbufs[index]);
979 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
980 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
982 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
983 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
986 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
987 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
989 int system_tag = 666;
990 int rank, size, other, index;
991 MPI_Aint lb = 0, dataext = 0;
992 MPI_Request *requests;
995 rank = smpi_comm_rank(comm);
996 size = smpi_comm_size(comm);
998 // FIXME: check for errors
999 smpi_datatype_extent(datatype, &lb, &dataext);
1001 // Local copy from self
1002 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
1004 // Send/Recv buffers to/from others;
1005 requests = xbt_new(MPI_Request, size - 1);
1006 tmpbufs = xbt_new(void *, rank);
1008 for(other = 0; other < rank; other++) {
1009 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1011 tmpbufs[index] = xbt_malloc(count * dataext);
1013 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
1017 for(other = rank + 1; other < size; other++) {
1019 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1022 // Wait for completion of all comms.
1023 smpi_mpi_startall(size - 1, requests);
1024 for(other = 0; other < size - 1; other++) {
1025 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1026 if(index == MPI_UNDEFINED) {
1030 // #Request is below rank: it's a irecv
1031 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1034 for(index = 0; index < rank; index++) {
1035 xbt_free(tmpbufs[index]);