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;
99 void smpi_empty_status(MPI_Status * status) {
100 if(status != MPI_STATUS_IGNORE) {
101 status->MPI_SOURCE=MPI_ANY_SOURCE;
102 status->MPI_TAG=MPI_ANY_TAG;
107 void smpi_action_trace_run(char *path)
111 xbt_dict_cursor_t cursor;
115 action_fp = fopen(path, "r");
116 xbt_assert(action_fp != NULL, "Cannot open %s: %s", path,
120 if (!xbt_dict_is_empty(action_queues)) {
122 ("Not all actions got consumed. If the simulation ended successfully (without deadlock), you may want to add new processes to your deployment file.");
125 xbt_dict_foreach(action_queues, cursor, name, todo) {
126 XBT_WARN("Still %lu actions for %s", xbt_dynar_length(todo), name);
132 xbt_dict_free(&action_queues);
133 action_queues = xbt_dict_new_homogeneous(NULL);
136 static void smpi_mpi_request_free_voidp(void* request)
138 MPI_Request req = request;
139 smpi_mpi_request_free(&req);
142 /* MPI Low level calls */
143 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
144 int dst, int tag, MPI_Comm comm)
146 MPI_Request request =
147 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
148 comm, PERSISTENT | SEND);
153 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
154 int src, int tag, MPI_Comm comm)
156 MPI_Request request =
157 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
158 comm, PERSISTENT | RECV);
163 void smpi_mpi_start(MPI_Request request)
167 xbt_assert(!request->action,
168 "Cannot (re)start a non-finished communication");
169 if(request->flags & RECV) {
170 print_request("New recv", request);
171 if (request->size < surf_cfg_get_int("smpi/async_small_thres"))
172 mailbox = smpi_process_mailbox_small();
174 mailbox = smpi_process_mailbox();
175 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
176 request->real_size=request->size;
177 request->action = simcall_comm_irecv(mailbox, request->buf, &request->real_size, &match_recv, request);
178 if (request->action)request->action->comm.refcount++;
181 int receiver = smpi_group_index(smpi_comm_group(request->comm), request->dst);
182 /* if(receiver == MPI_UNDEFINED) {*/
183 /* XBT_WARN("Trying to send a message to a wrong rank");*/
186 print_request("New send", request);
187 if (request->size < surf_cfg_get_int("smpi/async_small_thres")) { // eager mode
188 mailbox = smpi_process_remote_mailbox_small(receiver);
190 XBT_DEBUG("Send request %p is not in the permanent receive mailbox (buf: %p)",request,request->buf);
191 mailbox = smpi_process_remote_mailbox(receiver);
193 if (request->size < 64*1024 ) { //(FIXME: this limit should be configurable)
195 if(request->old_type->has_subtype == 0){
196 oldbuf = request->buf;
197 request->detached = 1;
199 request->buf = malloc(request->size);
200 memcpy(request->buf,oldbuf,request->size);
203 XBT_DEBUG("Send request %p is detached; buf %p copied into %p",request,oldbuf,request->buf);
205 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
206 request->real_size=request->size;
209 simcall_comm_isend(mailbox, request->size, -1.0,
210 request->buf, request->real_size,
212 &smpi_mpi_request_free_voidp, // how to free the userdata if a detached send fails
214 // detach if msg size < eager/rdv switch limit
216 if (request->action)request->action->comm.refcount++;
219 /* FIXME: detached sends are not traceable (request->action == NULL) */
221 simcall_set_category(request->action, TRACE_internal_smpi_get_category());
228 void smpi_mpi_startall(int count, MPI_Request * requests)
232 for(i = 0; i < count; i++) {
233 smpi_mpi_start(requests[i]);
237 void smpi_mpi_request_free(MPI_Request * request)
240 *request = MPI_REQUEST_NULL;
243 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
244 int dst, int tag, MPI_Comm comm)
246 MPI_Request request =
247 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
248 comm, NON_PERSISTENT | SEND);
253 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
254 int dst, int tag, MPI_Comm comm)
256 MPI_Request request =
257 smpi_isend_init(buf, count, datatype, dst, tag, comm);
259 smpi_mpi_start(request);
263 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
264 int src, int tag, MPI_Comm comm)
266 MPI_Request request =
267 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
268 comm, NON_PERSISTENT | RECV);
272 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
273 int src, int tag, MPI_Comm comm)
275 MPI_Request request =
276 smpi_irecv_init(buf, count, datatype, src, tag, comm);
278 smpi_mpi_start(request);
282 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
283 int tag, MPI_Comm comm, MPI_Status * status)
286 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
287 smpi_mpi_wait(&request, status);
292 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
293 int tag, MPI_Comm comm)
295 MPI_Request request =
296 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
297 comm, NON_PERSISTENT | SEND | RECV_DELETE);
299 smpi_mpi_start(request);
300 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
304 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
305 int dst, int sendtag, void *recvbuf, int recvcount,
306 MPI_Datatype recvtype, int src, int recvtag,
307 MPI_Comm comm, MPI_Status * status)
309 MPI_Request requests[2];
313 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
315 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
316 smpi_mpi_startall(2, requests);
317 smpi_mpi_waitall(2, requests, stats);
318 if(status != MPI_STATUS_IGNORE) {
319 // Copy receive status
320 memcpy(status, &stats[1], sizeof(MPI_Status));
324 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
326 return status->count / smpi_datatype_size(datatype);
329 static void finish_wait(MPI_Request * request, MPI_Status * status)
331 MPI_Request req = *request;
333 if(status != MPI_STATUS_IGNORE) {
334 status->MPI_SOURCE = req->src == MPI_ANY_SOURCE ? req->real_src : req->src;
335 status->MPI_TAG = req->tag == MPI_ANY_TAG ? req->real_tag : req->tag;
337 status->MPI_ERROR = MPI_ERR_TRUNCATE;
338 else status->MPI_ERROR = MPI_SUCCESS ;
339 // this handles the case were size in receive differs from size in send
340 // FIXME: really this should just contain the count of receive-type blocks,
342 status->count = req->real_size;
346 print_request("Finishing", req);
347 MPI_Datatype datatype = req->old_type;
349 if(datatype->has_subtype == 1){
350 // This part handles the problem of non-contignous memory
351 // the unserialization at the reception
352 s_smpi_subtype_t *subtype = datatype->substruct;
353 if(req->flags & RECV) {
354 subtype->unserialize(req->buf, req->old_buf, req->real_size/smpi_datatype_size(datatype) , datatype->substruct);
356 if(req->detached == 0) free(req->buf);
361 if(req->flags & NON_PERSISTENT) {
362 if(req->flags & RECV &&
364 (req->action->state == SIMIX_DONE))
366 MPI_Request sender_request = (MPI_Request)SIMIX_comm_get_src_data(req->action);
367 if((sender_request!=MPI_REQUEST_NULL) &&
368 ( sender_request->detached ) &&
369 ( sender_request->flags & RECV_DELETE))
371 //we are in a receiver's wait from a detached send
372 //we have to clean the sender's side request here.... but only if done by a send, not an isend
373 //the request lives senderside for an isend. As detached is currently for send + isend, we use RECV_DELETE to separate them
374 //FIXME : see if just removing detached status for isend is also good
375 smpi_mpi_request_free(&sender_request);
381 //if we want to free our request, we have to invalidate it at the other end of the comm
382 if(req->flags & SEND){
383 req->action->comm.src_data=MPI_REQUEST_NULL;
385 req->action->comm.dst_data=MPI_REQUEST_NULL;
388 smx_action_t temp=req->action;
389 if(req->action->comm.refcount == 1)req->action = NULL;
390 SIMIX_comm_destroy(temp);
395 smpi_mpi_request_free(request);
399 if(req->action)SIMIX_comm_destroy(req->action);
404 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
407 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or smpi_mpi_testall before)
408 if ((*request)->action == NULL)
411 flag = simcall_comm_test((*request)->action);
413 finish_wait(request, status);
415 smpi_empty_status(status);
420 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
427 *index = MPI_UNDEFINED;
430 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
431 map = xbt_new(int, count);
433 for(i = 0; i < count; i++) {
434 if((requests[i]!=MPI_REQUEST_NULL) && requests[i]->action) {
435 xbt_dynar_push(comms, &requests[i]->action);
441 i = simcall_comm_testany(comms);
442 // not MPI_UNDEFINED, as this is a simix return code
445 finish_wait(&requests[*index], status);
449 //all requests are null or inactive, return true
451 smpi_empty_status(status);
454 xbt_dynar_free(&comms);
461 int smpi_mpi_testall(int count, MPI_Request requests[],
465 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
468 for(i=0; i<count; i++){
469 if(requests[i]!= MPI_REQUEST_NULL){
470 if (smpi_mpi_test(&requests[i], pstat)!=1){
474 smpi_empty_status(pstat);
476 if(status != MPI_STATUSES_IGNORE) {
477 memcpy(&status[i], pstat, sizeof(*pstat));
483 void smpi_mpi_probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
485 //FIXME find another wait to avoid busy waiting ?
486 // the issue here is that we have to wait on a nonexistent comm
488 smpi_mpi_iprobe(source, tag, comm, &flag, status);
489 XBT_DEBUG("Busy Waiting on probing : %d", flag);
491 simcall_process_sleep(0.0001);
496 void smpi_mpi_iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
497 MPI_Request request =build_request(NULL, 0, MPI_CHAR, source, smpi_comm_rank(comm), tag,
498 comm, NON_PERSISTENT | RECV);
500 // behave like a receive, but don't do it
503 print_request("New iprobe", request);
504 // We have to test both mailboxes as we don't know if we will receive one one or another
505 if (surf_cfg_get_int("smpi/async_small_thres")>0){
506 mailbox = smpi_process_mailbox_small();
507 XBT_DEBUG("trying to probe the perm recv mailbox");
508 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
510 if (request->action==NULL){
511 mailbox = smpi_process_mailbox();
512 XBT_DEBUG("trying to probe the other mailbox");
513 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
517 MPI_Request req = (MPI_Request)SIMIX_comm_get_src_data(request->action);
519 if(status != MPI_STATUS_IGNORE) {
520 status->MPI_SOURCE = req->src;
521 status->MPI_TAG = req->tag;
522 status->MPI_ERROR = MPI_SUCCESS;
523 status->count = req->real_size;
527 smpi_mpi_request_free(&request);
532 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
534 print_request("Waiting", *request);
535 if ((*request)->action != NULL) { // this is not a detached send
536 simcall_comm_wait((*request)->action, -1.0);
537 finish_wait(request, status);
539 // FIXME for a detached send, finish_wait is not called:
542 int smpi_mpi_waitany(int count, MPI_Request requests[],
549 index = MPI_UNDEFINED;
551 // Wait for a request to complete
552 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
553 map = xbt_new(int, count);
555 XBT_DEBUG("Wait for one of");
556 for(i = 0; i < count; i++) {
557 if((requests[i] != MPI_REQUEST_NULL) && (requests[i]->action != NULL)) {
558 print_request("Waiting any ", requests[i]);
559 xbt_dynar_push(comms, &requests[i]->action);
565 i = simcall_comm_waitany(comms);
567 // not MPI_UNDEFINED, as this is a simix return code
570 finish_wait(&requests[index], status);
574 xbt_dynar_free(&comms);
577 if (index==MPI_UNDEFINED)
578 smpi_empty_status(status);
583 int smpi_mpi_waitall(int count, MPI_Request requests[],
588 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
589 int retvalue=MPI_SUCCESS;
590 //tag invalid requests in the set
591 for(c = 0; c < count; c++) {
592 if(requests[c]==MPI_REQUEST_NULL || requests[c]->dst == MPI_PROC_NULL ){
593 if(status != MPI_STATUSES_IGNORE)
594 smpi_empty_status(&status[c]);
595 }else if(requests[c]->src == MPI_PROC_NULL ){
596 if(status != MPI_STATUSES_IGNORE) {
597 smpi_empty_status(&status[c]);
598 status[c].MPI_SOURCE=MPI_PROC_NULL;
603 for(c = 0; c < count; c++) {
605 smpi_mpi_wait(&requests[c], pstat);
608 index = smpi_mpi_waitany(count, requests, pstat);
609 if(index == MPI_UNDEFINED) {
612 if(status != MPI_STATUSES_IGNORE) {
613 memcpy(&status[index], pstat, sizeof(*pstat));
614 if(status[index].MPI_ERROR==MPI_ERR_TRUNCATE)retvalue=MPI_ERR_IN_STATUS;
622 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
627 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
630 for(i = 0; i < incount; i++)
632 index=smpi_mpi_waitany(incount, requests, pstat);
633 if(index!=MPI_UNDEFINED){
634 indices[count] = index;
636 if(status != MPI_STATUSES_IGNORE) {
637 memcpy(&status[index], pstat, sizeof(*pstat));
640 return MPI_UNDEFINED;
646 int smpi_mpi_testsome(int incount, MPI_Request requests[], int *indices,
649 int i, count, count_dead;
651 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
655 for(i = 0; i < incount; i++) {
656 if((requests[i] != MPI_REQUEST_NULL)) {
657 if(smpi_mpi_test(&requests[i], pstat)) {
660 if(status != MPI_STATUSES_IGNORE) {
661 memcpy(&status[i], pstat, sizeof(*pstat));
668 if(count_dead==incount)return MPI_UNDEFINED;
672 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
675 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
676 nary_tree_bcast(buf, count, datatype, root, comm, 4);
679 void smpi_mpi_barrier(MPI_Comm comm)
681 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
682 nary_tree_barrier(comm, 4);
685 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
686 void *recvbuf, int recvcount, MPI_Datatype recvtype,
687 int root, MPI_Comm comm)
689 int system_tag = 666;
690 int rank, size, src, index;
691 MPI_Aint lb = 0, recvext = 0;
692 MPI_Request *requests;
694 rank = smpi_comm_rank(comm);
695 size = smpi_comm_size(comm);
697 // Send buffer to root
698 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
700 // FIXME: check for errors
701 smpi_datatype_extent(recvtype, &lb, &recvext);
702 // Local copy from root
703 smpi_datatype_copy(sendbuf, sendcount, sendtype,
704 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
705 // Receive buffers from senders
706 requests = xbt_new(MPI_Request, size - 1);
708 for(src = 0; src < size; src++) {
710 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
712 src, system_tag, comm);
716 // Wait for completion of irecv's.
717 smpi_mpi_startall(size - 1, requests);
718 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
723 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
724 void *recvbuf, int *recvcounts, int *displs,
725 MPI_Datatype recvtype, int root, MPI_Comm comm)
727 int system_tag = 666;
728 int rank, size, src, index;
729 MPI_Aint lb = 0, recvext = 0;
730 MPI_Request *requests;
732 rank = smpi_comm_rank(comm);
733 size = smpi_comm_size(comm);
735 // Send buffer to root
736 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
738 // FIXME: check for errors
739 smpi_datatype_extent(recvtype, &lb, &recvext);
740 // Local copy from root
741 smpi_datatype_copy(sendbuf, sendcount, sendtype,
742 (char *)recvbuf + displs[root] * recvext,
743 recvcounts[root], recvtype);
744 // Receive buffers from senders
745 requests = xbt_new(MPI_Request, size - 1);
747 for(src = 0; src < size; src++) {
750 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
751 recvcounts[src], recvtype, src, system_tag, comm);
755 // Wait for completion of irecv's.
756 smpi_mpi_startall(size - 1, requests);
757 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
762 void smpi_mpi_allgather(void *sendbuf, int sendcount,
763 MPI_Datatype sendtype, void *recvbuf,
764 int recvcount, MPI_Datatype recvtype,
767 int system_tag = 666;
768 int rank, size, other, index;
769 MPI_Aint lb = 0, recvext = 0;
770 MPI_Request *requests;
772 rank = smpi_comm_rank(comm);
773 size = smpi_comm_size(comm);
774 // FIXME: check for errors
775 smpi_datatype_extent(recvtype, &lb, &recvext);
776 // Local copy from self
777 smpi_datatype_copy(sendbuf, sendcount, sendtype,
778 (char *)recvbuf + rank * recvcount * recvext, recvcount,
780 // Send/Recv buffers to/from others;
781 requests = xbt_new(MPI_Request, 2 * (size - 1));
783 for(other = 0; other < size; other++) {
786 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
789 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
790 recvcount, recvtype, other,
795 // Wait for completion of all comms.
796 smpi_mpi_startall(2 * (size - 1), requests);
797 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
801 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
802 MPI_Datatype sendtype, void *recvbuf,
803 int *recvcounts, int *displs,
804 MPI_Datatype recvtype, MPI_Comm comm)
806 int system_tag = 666;
807 int rank, size, other, index;
808 MPI_Aint lb = 0, recvext = 0;
809 MPI_Request *requests;
811 rank = smpi_comm_rank(comm);
812 size = smpi_comm_size(comm);
813 // FIXME: check for errors
814 smpi_datatype_extent(recvtype, &lb, &recvext);
815 // Local copy from self
816 smpi_datatype_copy(sendbuf, sendcount, sendtype,
817 (char *)recvbuf + displs[rank] * recvext,
818 recvcounts[rank], recvtype);
819 // Send buffers to others;
820 requests = xbt_new(MPI_Request, 2 * (size - 1));
822 for(other = 0; other < size; other++) {
825 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
829 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
830 recvtype, other, system_tag, comm);
834 // Wait for completion of all comms.
835 smpi_mpi_startall(2 * (size - 1), requests);
836 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
840 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
841 void *recvbuf, int recvcount, MPI_Datatype recvtype,
842 int root, MPI_Comm comm)
844 int system_tag = 666;
845 int rank, size, dst, index;
846 MPI_Aint lb = 0, sendext = 0;
847 MPI_Request *requests;
849 rank = smpi_comm_rank(comm);
850 size = smpi_comm_size(comm);
852 // Recv buffer from root
853 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
856 // FIXME: check for errors
857 smpi_datatype_extent(sendtype, &lb, &sendext);
858 // Local copy from root
859 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
860 sendcount, sendtype, recvbuf, recvcount, recvtype);
861 // Send buffers to receivers
862 requests = xbt_new(MPI_Request, size - 1);
864 for(dst = 0; dst < size; dst++) {
866 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
867 sendcount, sendtype, dst,
872 // Wait for completion of isend's.
873 smpi_mpi_startall(size - 1, requests);
874 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
879 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
880 MPI_Datatype sendtype, void *recvbuf, int recvcount,
881 MPI_Datatype recvtype, int root, MPI_Comm comm)
883 int system_tag = 666;
884 int rank, size, dst, index;
885 MPI_Aint lb = 0, sendext = 0;
886 MPI_Request *requests;
888 rank = smpi_comm_rank(comm);
889 size = smpi_comm_size(comm);
891 // Recv buffer from root
892 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
895 // FIXME: check for errors
896 smpi_datatype_extent(sendtype, &lb, &sendext);
897 // Local copy from root
898 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
899 sendtype, recvbuf, recvcount, recvtype);
900 // Send buffers to receivers
901 requests = xbt_new(MPI_Request, size - 1);
903 for(dst = 0; dst < size; dst++) {
906 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
907 sendtype, dst, system_tag, comm);
911 // Wait for completion of isend's.
912 smpi_mpi_startall(size - 1, requests);
913 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
918 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
919 MPI_Datatype datatype, MPI_Op op, int root,
922 int system_tag = 666;
923 int rank, size, src, index;
924 MPI_Aint lb = 0, dataext = 0;
925 MPI_Request *requests;
928 rank = smpi_comm_rank(comm);
929 size = smpi_comm_size(comm);
931 // Send buffer to root
932 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
934 // FIXME: check for errors
935 smpi_datatype_extent(datatype, &lb, &dataext);
936 // Local copy from root
937 if (sendbuf && recvbuf)
938 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
939 // Receive buffers from senders
940 //TODO: make a MPI_barrier here ?
941 requests = xbt_new(MPI_Request, size - 1);
942 tmpbufs = xbt_new(void *, size - 1);
944 for(src = 0; src < size; src++) {
946 // FIXME: possibly overkill we we have contiguous/noncontiguous data
948 tmpbufs[index] = xbt_malloc(count * dataext);
950 smpi_irecv_init(tmpbufs[index], count, datatype, src,
955 // Wait for completion of irecv's.
956 smpi_mpi_startall(size - 1, requests);
957 for(src = 0; src < size - 1; src++) {
958 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
959 XBT_DEBUG("finished waiting any request with index %d", index);
960 if(index == MPI_UNDEFINED) {
963 if(op) /* op can be MPI_OP_NULL that does nothing */
964 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
966 for(index = 0; index < size - 1; index++) {
967 xbt_free(tmpbufs[index]);
974 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
975 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
977 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
978 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
981 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
982 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
984 int system_tag = 666;
985 int rank, size, other, index;
986 MPI_Aint lb = 0, dataext = 0;
987 MPI_Request *requests;
990 rank = smpi_comm_rank(comm);
991 size = smpi_comm_size(comm);
993 // FIXME: check for errors
994 smpi_datatype_extent(datatype, &lb, &dataext);
996 // Local copy from self
997 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
999 // Send/Recv buffers to/from others;
1000 requests = xbt_new(MPI_Request, size - 1);
1001 tmpbufs = xbt_new(void *, rank);
1003 for(other = 0; other < rank; other++) {
1004 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1006 tmpbufs[index] = xbt_malloc(count * dataext);
1008 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
1012 for(other = rank + 1; other < size; other++) {
1014 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1017 // Wait for completion of all comms.
1018 smpi_mpi_startall(size - 1, requests);
1019 for(other = 0; other < size - 1; other++) {
1020 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1021 if(index == MPI_UNDEFINED) {
1025 // #Request is below rank: it's a irecv
1026 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1029 for(index = 0; index < rank; index++) {
1030 xbt_free(tmpbufs[index]);