1 /* Copyright (c) 2007-2014. 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"
14 #include "simgrid/sg_config.h"
15 #include "colls/colls.h"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi, "Logging specific to SMPI (base)");
20 static int match_recv(void* a, void* b, smx_synchro_t ignored) {
21 MPI_Request ref = (MPI_Request)a;
22 MPI_Request req = (MPI_Request)b;
23 XBT_DEBUG("Trying to match a recv of src %d against %d, tag %d against %d",ref->src,req->src, ref->tag, req->tag);
25 xbt_assert(ref, "Cannot match recv against null reference");
26 xbt_assert(req, "Cannot match recv against null request");
27 if((ref->src == MPI_ANY_SOURCE || req->src == ref->src)
28 && ((ref->tag == MPI_ANY_TAG && req->tag >=0) || req->tag == ref->tag)){
29 //we match, we can transfer some values
30 // FIXME : move this to the copy function ?
31 if(ref->src == MPI_ANY_SOURCE)ref->real_src = req->src;
32 if(ref->tag == MPI_ANY_TAG)ref->real_tag = req->tag;
33 if(ref->real_size < req->real_size) ref->truncated = 1;
35 ref->detached_sender=req; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
37 XBT_DEBUG("match succeeded");
42 static int match_send(void* a, void* b,smx_synchro_t ignored) {
43 MPI_Request ref = (MPI_Request)a;
44 MPI_Request req = (MPI_Request)b;
45 XBT_DEBUG("Trying to match a send of src %d against %d, tag %d against %d",ref->src,req->src, ref->tag, req->tag);
46 xbt_assert(ref, "Cannot match send against null reference");
47 xbt_assert(req, "Cannot match send against null request");
49 if((req->src == MPI_ANY_SOURCE || req->src == ref->src)
50 && ((req->tag == MPI_ANY_TAG && ref->tag >=0)|| req->tag == ref->tag))
52 if(req->src == MPI_ANY_SOURCE)req->real_src = ref->src;
53 if(req->tag == MPI_ANY_TAG)req->real_tag = ref->tag;
54 if(req->real_size < ref->real_size) req->truncated = 1;
56 req->detached_sender=ref; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
58 XBT_DEBUG("match succeeded");
64 typedef struct s_smpi_factor *smpi_factor_t;
65 typedef struct s_smpi_factor {
68 double values[4];//arbitrary set to 4
70 xbt_dynar_t smpi_os_values = NULL;
71 xbt_dynar_t smpi_or_values = NULL;
72 xbt_dynar_t smpi_ois_values = NULL;
74 double smpi_wtime_sleep = 0.0;
75 double smpi_iprobe_sleep = 1e-4;
76 double smpi_test_sleep = 1e-4;
79 // Methods used to parse and store the values for timing injections in smpi
80 // These are taken from surf/network.c and generalized to have more factors
81 // These methods should be merged with those in surf/network.c (moved somewhere in xbt ?)
83 static int factor_cmp(const void *pa, const void *pb)
85 return (((s_smpi_factor_t*)pa)->factor > ((s_smpi_factor_t*)pb)->factor) ? 1 :
86 (((s_smpi_factor_t*)pa)->factor < ((s_smpi_factor_t*)pb)->factor) ? -1 : 0;
90 static xbt_dynar_t parse_factor(const char *smpi_coef_string)
93 unsigned int iter = 0;
97 xbt_dynar_t smpi_factor, radical_elements, radical_elements2 = NULL;
99 smpi_factor = xbt_dynar_new(sizeof(s_smpi_factor_t), NULL);
100 radical_elements = xbt_str_split(smpi_coef_string, ";");
101 xbt_dynar_foreach(radical_elements, iter, value) {
102 memset(&fact, 0, sizeof(s_smpi_factor_t));
103 radical_elements2 = xbt_str_split(value, ":");
104 if (xbt_dynar_length(radical_elements2) <2 || xbt_dynar_length(radical_elements2) > 5)
105 xbt_die("Malformed radical for smpi factor!");
106 for(i =0; i<xbt_dynar_length(radical_elements2);i++ ){
108 fact.factor = atol(xbt_dynar_get_as(radical_elements2, i, char *));
110 fact.values[fact.nb_values] = atof(xbt_dynar_get_as(radical_elements2, i, char *));
115 xbt_dynar_push_as(smpi_factor, s_smpi_factor_t, fact);
116 XBT_DEBUG("smpi_factor:\t%ld : %d values, first: %f", fact.factor, fact.nb_values ,fact.values[0]);
117 xbt_dynar_free(&radical_elements2);
119 xbt_dynar_free(&radical_elements);
121 xbt_dynar_sort(smpi_factor, &factor_cmp);
122 xbt_dynar_foreach(smpi_factor, iter, fact) {
123 XBT_DEBUG("smpi_factor:\t%ld : %d values, first: %f", fact.factor, fact.nb_values ,fact.values[0]);
128 static double smpi_os(double size)
130 if (!smpi_os_values) {
131 smpi_os_values = parse_factor(sg_cfg_get_string("smpi/os"));
132 smpi_register_static(smpi_os_values, xbt_dynar_free_voidp);
134 unsigned int iter = 0;
135 s_smpi_factor_t fact;
137 xbt_dynar_foreach(smpi_os_values, iter, fact) {
138 if (size <= fact.factor) {
139 XBT_DEBUG("os : %f <= %ld return %f", size, fact.factor, current);
142 current=fact.values[0]+fact.values[1]*size;
145 XBT_DEBUG("os : %f > %ld return %f", size, fact.factor, current);
150 static double smpi_ois(double size)
152 if (!smpi_ois_values) {
153 smpi_ois_values = parse_factor(sg_cfg_get_string("smpi/ois"));
154 smpi_register_static(smpi_ois_values, xbt_dynar_free_voidp);
156 unsigned int iter = 0;
157 s_smpi_factor_t fact;
159 xbt_dynar_foreach(smpi_ois_values, iter, fact) {
160 if (size <= fact.factor) {
161 XBT_DEBUG("ois : %f <= %ld return %f", size, fact.factor, current);
164 current=fact.values[0]+fact.values[1]*size;
167 XBT_DEBUG("ois : %f > %ld return %f", size, fact.factor, current);
172 static double smpi_or(double size)
174 if (!smpi_or_values) {
175 smpi_or_values = parse_factor(sg_cfg_get_string("smpi/or"));
176 smpi_register_static(smpi_or_values, xbt_dynar_free_voidp);
178 unsigned int iter = 0;
179 s_smpi_factor_t fact;
181 xbt_dynar_foreach(smpi_or_values, iter, fact) {
182 if (size <= fact.factor) {
183 XBT_DEBUG("or : %f <= %ld return %f", size, fact.factor, current);
186 current=fact.values[0]+fact.values[1]*size;
188 XBT_DEBUG("or : %f > %ld return %f", size, fact.factor, current);
193 double smpi_mpi_wtime(){
195 if (smpi_process_initialized() && !smpi_process_finalized() && !smpi_process_get_sampling()) {
197 time = SIMIX_get_clock();
198 //to avoid deadlocks if called too many times
199 if(smpi_wtime_sleep > 0) simcall_process_sleep(smpi_wtime_sleep);
202 time = SIMIX_get_clock();
207 static MPI_Request build_request(void *buf, int count,
208 MPI_Datatype datatype, int src, int dst,
209 int tag, MPI_Comm comm, unsigned flags)
211 MPI_Request request = NULL;
213 void *old_buf = NULL;
215 request = xbt_new(s_smpi_mpi_request_t, 1);
217 s_smpi_subtype_t *subtype = datatype->substruct;
219 if(((flags & RECV) && (flags & ACCUMULATE)) || (datatype->has_subtype == 1)){
220 // This part handles the problem of non-contiguous memory
222 buf = count==0 ? NULL : xbt_malloc(count*smpi_datatype_size(datatype));
223 if ((datatype->has_subtype == 1) && (flags & SEND)) {
224 subtype->serialize(old_buf, buf, count, datatype->substruct);
229 // This part handles the problem of non-contiguous memory (for the
230 // unserialisation at the reception)
231 request->old_buf = old_buf;
232 request->old_type = datatype;
234 request->size = smpi_datatype_size(datatype) * count;
238 request->comm = comm;
239 request->action = NULL;
240 request->flags = flags;
241 request->detached = 0;
242 request->detached_sender = NULL;
243 request->real_src = 0;
245 request->truncated = 0;
246 request->real_size = 0;
247 request->real_tag = 0;
248 if(flags & PERSISTENT)
249 request->refcount = 1;
251 request->refcount = 0;
252 request->op = MPI_REPLACE;
257 if (flags & SEND) smpi_datatype_unuse(datatype);
263 void smpi_empty_status(MPI_Status * status)
265 if(status != MPI_STATUS_IGNORE) {
266 status->MPI_SOURCE = MPI_ANY_SOURCE;
267 status->MPI_TAG = MPI_ANY_TAG;
268 status->MPI_ERROR = MPI_SUCCESS;
273 void smpi_action_trace_run(char *path)
277 xbt_dict_cursor_t cursor;
281 action_fp = fopen(path, "r");
282 if (action_fp == NULL)
283 xbt_die("Cannot open %s: %s", path, strerror(errno));
286 if (!xbt_dict_is_empty(action_queues)) {
288 ("Not all actions got consumed. If the simulation ended successfully (without deadlock), you may want to add new processes to your deployment file.");
291 xbt_dict_foreach(action_queues, cursor, name, todo) {
292 XBT_WARN("Still %lu actions for %s", xbt_dynar_length(todo), name);
298 xbt_dict_free(&action_queues);
299 action_queues = xbt_dict_new_homogeneous(NULL);
302 static void smpi_mpi_request_free_voidp(void* request)
304 MPI_Request req = request;
305 smpi_mpi_request_free(&req);
308 /* MPI Low level calls */
309 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
310 int dst, int tag, MPI_Comm comm)
312 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
313 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
314 comm, PERSISTENT | SEND | PREPARED);
318 MPI_Request smpi_mpi_ssend_init(void *buf, int count, MPI_Datatype datatype,
319 int dst, int tag, MPI_Comm comm)
321 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
322 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
323 comm, PERSISTENT | SSEND | SEND | PREPARED);
327 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
328 int src, int tag, MPI_Comm comm)
330 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
331 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : smpi_group_index(smpi_comm_group(comm), src), smpi_process_index(), tag,
332 comm, PERSISTENT | RECV | PREPARED);
336 void smpi_mpi_start(MPI_Request request)
340 xbt_assert(!request->action, "Cannot (re)start a non-finished communication");
341 request->flags &= ~PREPARED;
342 request->flags &= ~FINISHED;
345 if (request->flags & RECV) {
346 print_request("New recv", request);
348 if (request->flags & RMA || request->size < sg_cfg_get_int("smpi/async_small_thres")){
349 //We have to check both mailboxes (because SSEND messages are sent to the large mbox). begin with the more appropriate one : the small one.
350 mailbox = smpi_process_mailbox_small();
351 XBT_DEBUG("Is there a corresponding send already posted the small mailbox %p (in case of SSEND)?", mailbox);
352 smx_synchro_t action = simcall_comm_iprobe(mailbox, 0, request->src,request->tag, &match_recv, (void*)request);
355 mailbox = smpi_process_mailbox();
356 XBT_DEBUG("No, nothing in the small mailbox test the other one : %p", mailbox);
357 action = simcall_comm_iprobe(mailbox, 0, request->src,request->tag, &match_recv, (void*)request);
359 XBT_DEBUG("Still notching, switch back to the small mailbox : %p", mailbox);
360 mailbox = smpi_process_mailbox_small();
363 XBT_DEBUG("yes there was something for us in the large mailbox");
366 mailbox = smpi_process_mailbox_small();
367 XBT_DEBUG("Is there a corresponding send already posted the small mailbox?");
368 smx_synchro_t action = simcall_comm_iprobe(mailbox, 0, request->src,request->tag, &match_recv, (void*)request);
371 XBT_DEBUG("No, nothing in the permanent receive mailbox");
372 mailbox = smpi_process_mailbox();
374 XBT_DEBUG("yes there was something for us in the small mailbox");
378 //integrate pseudo-timing for buffering of small messages, do not bother to execute the simcall if 0
379 double sleeptime = request->detached ? smpi_or(request->size) : 0.0;
381 simcall_process_sleep(sleeptime);
382 XBT_DEBUG("receiving size of %zu : sleep %f ", request->size, smpi_or(request->size));
385 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
386 request->real_size=request->size;
387 smpi_datatype_use(request->old_type);
388 smpi_comm_use(request->comm);
389 request->action = simcall_comm_irecv(mailbox, request->buf,
390 &request->real_size, &match_recv,
391 !smpi_process_get_replaying()? &smpi_comm_copy_buffer_callback
392 : &smpi_comm_null_copy_buffer_callback,
394 XBT_DEBUG("recv simcall posted");
400 int receiver = request->dst;
403 int rank = request->src;
404 if (TRACE_smpi_view_internals()) {
405 TRACE_smpi_send(rank, rank, receiver,request->size);
408 print_request("New send", request);
410 //if we are giving back the control to the user without waiting for completion, we have to inject timings
411 double sleeptime = 0.0;
412 if(request->detached || (request->flags & (ISEND|SSEND))){// issend should be treated as isend
413 //isend and send timings may be different
414 sleeptime = (request->flags & ISEND)? smpi_ois(request->size) : smpi_os(request->size);
417 if(sleeptime != 0.0){
418 simcall_process_sleep(sleeptime);
419 XBT_DEBUG("sending size of %zu : sleep %f ", request->size, smpi_os(request->size));
422 if (request->flags & RMA || request->size < sg_cfg_get_int("smpi/async_small_thres")) { // eager mode
423 mailbox = smpi_process_remote_mailbox(receiver);
424 XBT_DEBUG("Is there a corresponding recv already posted in the large mailbox %p?", mailbox);
425 smx_synchro_t action = simcall_comm_iprobe(mailbox, 1,request->dst, request->tag, &match_send, (void*)request);
427 if (! (request->flags & SSEND)){
428 mailbox = smpi_process_remote_mailbox_small(receiver);
429 XBT_DEBUG("No, nothing in the large mailbox, message is to be sent on the small one %p", mailbox);
431 mailbox = smpi_process_remote_mailbox_small(receiver);
432 XBT_DEBUG("SSEND : Is there a corresponding recv already posted in the small mailbox %p?", mailbox);
433 action = simcall_comm_iprobe(mailbox, 1,request->dst, request->tag, &match_send, (void*)request);
435 XBT_DEBUG("No, we are first, send to large mailbox");
436 mailbox = smpi_process_remote_mailbox(receiver);
440 XBT_DEBUG("Yes there was something for us in the large mailbox");
443 mailbox = smpi_process_remote_mailbox(receiver);
444 XBT_DEBUG("Send request %p is in the large mailbox %p (buf: %p)",mailbox, request,request->buf);
447 void* buf = request->buf;
448 if ( (! (request->flags & SSEND)) && (request->size < sg_cfg_get_int("smpi/send_is_detached_thres"))) {
450 request->detached = 1;
451 XBT_DEBUG("Send request %p is detached", request);
453 if(request->old_type->has_subtype == 0){
454 oldbuf = request->buf;
455 if (!smpi_process_get_replaying() && oldbuf && request->size!=0){
456 if((smpi_privatize_global_variables)
457 && ((char*)request->buf >= start_data_exe)
458 && ((char*)request->buf < start_data_exe + size_data_exe )){
459 XBT_DEBUG("Privatization : We are sending from a zone inside global memory. Switch data segment ");
460 smpi_switch_data_segment(request->src);
462 buf = xbt_malloc(request->size);
463 memcpy(buf,oldbuf,request->size);
464 XBT_DEBUG("buf %p copied into %p",oldbuf,buf);
469 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
470 request->real_size=request->size;
471 smpi_datatype_use(request->old_type);
472 smpi_comm_use(request->comm);
474 simcall_comm_isend(SIMIX_process_from_PID(request->src+1), mailbox, request->size, -1.0,
475 buf, request->real_size,
477 &xbt_free_f, // how to free the userdata if a detached send fails
478 !smpi_process_get_replaying()? &smpi_comm_copy_buffer_callback
479 : &smpi_comm_null_copy_buffer_callback,
481 // detach if msg size < eager/rdv switch limit
483 XBT_DEBUG("send simcall posted");
488 /* FIXME: detached sends are not traceable (request->action == NULL) */
490 simcall_set_category(request->action, TRACE_internal_smpi_get_category());
498 void smpi_mpi_startall(int count, MPI_Request * requests)
501 if(requests==NULL) return;
503 for(i = 0; i < count; i++) {
504 smpi_mpi_start(requests[i]);
508 void smpi_mpi_request_free(MPI_Request * request)
510 if((*request) != MPI_REQUEST_NULL){
511 (*request)->refcount--;
512 if((*request)->refcount<0) xbt_die("wrong refcount");
514 if((*request)->refcount==0){
515 print_request("Destroying", (*request));
517 *request = MPI_REQUEST_NULL;
519 print_request("Decrementing", (*request));
522 xbt_die("freeing an already free request");
527 MPI_Request smpi_rma_send_init(void *buf, int count, MPI_Datatype datatype,
528 int src, int dst, int tag, MPI_Comm comm, MPI_Op op)
530 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
532 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf , count, datatype, src, dst, tag,
533 comm, RMA | NON_PERSISTENT | ISEND | SEND | PREPARED);
535 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, src, dst, tag,
536 comm, RMA | NON_PERSISTENT | ISEND | SEND | PREPARED | ACCUMULATE);
542 MPI_Request smpi_rma_recv_init(void *buf, int count, MPI_Datatype datatype,
543 int src, int dst, int tag, MPI_Comm comm, MPI_Op op)
545 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
547 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, src, dst, tag,
548 comm, RMA | NON_PERSISTENT | RECV | PREPARED);
550 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, src, dst, tag,
551 comm, RMA | NON_PERSISTENT | RECV | PREPARED | ACCUMULATE);
558 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
559 int dst, int tag, MPI_Comm comm)
561 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
562 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf , count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
563 comm, PERSISTENT | ISEND | SEND | PREPARED);
567 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
568 int dst, int tag, MPI_Comm comm)
570 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
571 request = build_request(buf==MPI_BOTTOM?(void*)0:buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
572 comm, NON_PERSISTENT | ISEND | SEND);
573 smpi_mpi_start(request);
577 MPI_Request smpi_mpi_issend(void *buf, int count, MPI_Datatype datatype,
578 int dst, int tag, MPI_Comm comm)
580 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
581 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
582 comm, NON_PERSISTENT | ISEND | SSEND | SEND);
583 smpi_mpi_start(request);
587 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
588 int src, int tag, MPI_Comm comm)
590 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
591 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : smpi_group_index(smpi_comm_group(comm), src), smpi_process_index(), tag,
592 comm, PERSISTENT | RECV | PREPARED);
596 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
597 int src, int tag, MPI_Comm comm)
599 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
600 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : smpi_group_index(smpi_comm_group(comm), src), smpi_process_index(), tag,
601 comm, NON_PERSISTENT | RECV);
602 smpi_mpi_start(request);
606 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
607 int tag, MPI_Comm comm, MPI_Status * status)
609 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
610 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
611 smpi_mpi_wait(&request, status);
617 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
618 int tag, MPI_Comm comm)
620 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
621 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
622 comm, NON_PERSISTENT | SEND);
624 smpi_mpi_start(request);
625 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
629 void smpi_mpi_ssend(void *buf, int count, MPI_Datatype datatype,
630 int dst, int tag, MPI_Comm comm)
632 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
633 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
634 comm, NON_PERSISTENT | SSEND | SEND);
636 smpi_mpi_start(request);
637 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
641 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
642 int dst, int sendtag, void *recvbuf, int recvcount,
643 MPI_Datatype recvtype, int src, int recvtag,
644 MPI_Comm comm, MPI_Status * status)
646 MPI_Request requests[2];
648 int myid=smpi_process_index();
649 if ((smpi_group_index(smpi_comm_group(comm), dst) == myid) && (smpi_group_index(smpi_comm_group(comm), src) == myid)) {
650 smpi_datatype_copy(sendbuf, sendcount, sendtype,
651 recvbuf, recvcount, recvtype);
655 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
657 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
658 smpi_mpi_startall(2, requests);
659 smpi_mpi_waitall(2, requests, stats);
660 smpi_mpi_request_free(&requests[0]);
661 smpi_mpi_request_free(&requests[1]);
662 if(status != MPI_STATUS_IGNORE) {
663 // Copy receive status
668 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
670 return status->count / smpi_datatype_size(datatype);
673 static void finish_wait(MPI_Request * request, MPI_Status * status)
675 MPI_Request req = *request;
676 smpi_empty_status(status);
678 if(!(req->detached && req->flags & SEND)
679 && !(req->flags & PREPARED)){
680 if(status != MPI_STATUS_IGNORE) {
681 int src = req->src == MPI_ANY_SOURCE ? req->real_src : req->src;
682 status->MPI_SOURCE = smpi_group_rank(smpi_comm_group(req->comm), src);
683 status->MPI_TAG = req->tag == MPI_ANY_TAG ? req->real_tag : req->tag;
684 status->MPI_ERROR = req->truncated ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
685 // this handles the case were size in receive differs from size in send
686 // FIXME: really this should just contain the count of receive-type blocks,
688 status->count = req->real_size;
691 print_request("Finishing", req);
692 MPI_Datatype datatype = req->old_type;
694 if((req->flags & ACCUMULATE) || (datatype->has_subtype == 1)){
695 if (!smpi_process_get_replaying()){
696 if( smpi_privatize_global_variables
697 && ((char*)req->old_buf >= start_data_exe)
698 && ((char*)req->old_buf < start_data_exe + size_data_exe )
700 XBT_VERB("Privatization : We are unserializing to a zone in global memory - Switch data segment ");
701 smpi_switch_data_segment(smpi_process_index());
705 if(datatype->has_subtype == 1){
706 // This part handles the problem of non-contignous memory
707 // the unserialization at the reception
708 s_smpi_subtype_t *subtype = datatype->substruct;
709 if(req->flags & RECV)
710 subtype->unserialize(req->buf, req->old_buf, req->real_size/smpi_datatype_size(datatype) , datatype->substruct, req->op);
711 if(req->detached == 0) free(req->buf);
712 }else if(req->flags & RECV){//apply op on contiguous buffer for accumulate
713 int n =req->real_size/smpi_datatype_size(datatype);
714 smpi_op_apply(req->op, req->buf, req->old_buf, &n, &datatype);
717 smpi_comm_unuse(req->comm);
718 smpi_datatype_unuse(datatype);
723 if (TRACE_smpi_view_internals()) {
724 if(req->flags & RECV){
725 int rank = smpi_process_index();
726 int src_traced = (req->src == MPI_ANY_SOURCE ? req->real_src : req->src);
727 TRACE_smpi_recv(rank, src_traced, rank);
732 if(req->detached_sender!=NULL){
733 smpi_mpi_request_free(&(req->detached_sender));
735 if(req->flags & PERSISTENT)
737 req->flags |= FINISHED;
739 smpi_mpi_request_free(request);
743 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
746 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or smpi_mpi_testall before)
749 //multiplier to the sleeptime, to increase speed of execution, each failed test will increase it
750 static int nsleeps = 1;
751 if(smpi_test_sleep > 0) simcall_process_sleep(nsleeps*smpi_test_sleep);
753 smpi_empty_status(status);
755 if (!((*request)->flags & PREPARED)) {
756 if ((*request)->action != NULL)
757 flag = simcall_comm_test((*request)->action);
759 finish_wait(request, status);
760 nsleeps=1;//reset the number of sleeps we will do next time
761 if (*request != MPI_REQUEST_NULL && !((*request)->flags & PERSISTENT))
762 *request = MPI_REQUEST_NULL;
770 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
777 *index = MPI_UNDEFINED;
779 comms = xbt_dynar_new(sizeof(smx_synchro_t), NULL);
780 map = xbt_new(int, count);
782 for(i = 0; i < count; i++) {
783 if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->action &&
784 !(requests[i]->flags & PREPARED)) {
785 xbt_dynar_push(comms, &requests[i]->action);
791 //multiplier to the sleeptime, to increase speed of execution, each failed testany will increase it
792 static int nsleeps = 1;
793 if(smpi_test_sleep > 0) simcall_process_sleep(nsleeps*smpi_test_sleep);
795 i = simcall_comm_testany(comms);
796 // not MPI_UNDEFINED, as this is a simix return code
799 finish_wait(&requests[*index], status);
800 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags & NON_PERSISTENT))
801 requests[*index] = MPI_REQUEST_NULL;
808 //all requests are null or inactive, return true
810 smpi_empty_status(status);
813 xbt_dynar_free(&comms);
819 int smpi_mpi_testall(int count, MPI_Request requests[],
823 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
826 for(i=0; i<count; i++){
827 if (requests[i] != MPI_REQUEST_NULL && !(requests[i]->flags & PREPARED)) {
828 if (smpi_mpi_test(&requests[i], pstat)!=1){
831 requests[i]=MPI_REQUEST_NULL;
834 smpi_empty_status(pstat);
836 if(status != MPI_STATUSES_IGNORE) {
843 void smpi_mpi_probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
845 //FIXME find another wait to avoid busy waiting ?
846 // the issue here is that we have to wait on a nonexistent comm
848 smpi_mpi_iprobe(source, tag, comm, &flag, status);
849 XBT_DEBUG("Busy Waiting on probing : %d", flag);
853 void smpi_mpi_iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
855 MPI_Request request =build_request(NULL, 0, MPI_CHAR, source == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : smpi_group_index(smpi_comm_group(comm), source), smpi_comm_rank(comm), tag,
856 comm, PERSISTENT | RECV);
858 //to avoid deadlock, we have to sleep some time here, or the timer won't advance and we will only do iprobe simcalls
859 //multiplier to the sleeptime, to increase speed of execution, each failed iprobe will increase it
860 static int nsleeps = 1;
861 if(smpi_iprobe_sleep > 0) simcall_process_sleep(nsleeps*smpi_iprobe_sleep);
862 // behave like a receive, but don't do it
865 print_request("New iprobe", request);
866 // We have to test both mailboxes as we don't know if we will receive one one or another
867 if (sg_cfg_get_int("smpi/async_small_thres")>0){
868 mailbox = smpi_process_mailbox_small();
869 XBT_DEBUG("trying to probe the perm recv mailbox");
870 request->action = simcall_comm_iprobe(mailbox, 0, request->src, request->tag, &match_recv, (void*)request);
872 if (request->action==NULL){
873 mailbox = smpi_process_mailbox();
874 XBT_DEBUG("trying to probe the other mailbox");
875 request->action = simcall_comm_iprobe(mailbox, 0, request->src,request->tag, &match_recv, (void*)request);
879 MPI_Request req = (MPI_Request)SIMIX_comm_get_src_data(request->action);
881 if(status != MPI_STATUS_IGNORE && !(req->flags & PREPARED)) {
882 status->MPI_SOURCE = smpi_group_rank(smpi_comm_group(comm), req->src);
883 status->MPI_TAG = req->tag;
884 status->MPI_ERROR = MPI_SUCCESS;
885 status->count = req->real_size;
887 nsleeps=1;//reset the number of sleeps we will do next time
893 smpi_mpi_request_free(&request);
898 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
900 print_request("Waiting", *request);
901 if ((*request)->flags & PREPARED) {
902 smpi_empty_status(status);
906 if ((*request)->action != NULL) { // this is not a detached send
907 simcall_comm_wait((*request)->action, -1.0);
909 if(MC_is_active() && (*request)->action)
910 (*request)->action->comm.dst_data = NULL; // dangling pointer : dst_data is freed with a wait, need to set it to NULL for system state comparison
914 finish_wait(request, status);
915 if (*request != MPI_REQUEST_NULL && ((*request)->flags & NON_PERSISTENT))
916 *request = MPI_REQUEST_NULL;
917 // FIXME for a detached send, finish_wait is not called:
920 int smpi_mpi_waitany(int count, MPI_Request requests[],
927 index = MPI_UNDEFINED;
929 // Wait for a request to complete
930 comms = xbt_dynar_new(sizeof(smx_synchro_t), NULL);
931 map = xbt_new(int, count);
933 XBT_DEBUG("Wait for one of %d", count);
934 for(i = 0; i < count; i++) {
935 if (requests[i] != MPI_REQUEST_NULL
936 && !(requests[i]->flags & PREPARED)
937 && !(requests[i]->flags & FINISHED)) {
938 if (requests[i]->action != NULL) {
939 XBT_DEBUG("Waiting any %p ", requests[i]);
940 xbt_dynar_push(comms, &requests[i]->action);
944 //This is a finished detached request, let's return this one
945 size=0;//so we free the dynar but don't do the waitany call
947 finish_wait(&requests[i], status);//cleanup if refcount = 0
948 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags & NON_PERSISTENT))
949 requests[i]=MPI_REQUEST_NULL;//set to null
955 i = simcall_comm_waitany(comms);
957 // not MPI_UNDEFINED, as this is a simix return code
960 finish_wait(&requests[index], status);
961 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags & NON_PERSISTENT))
962 requests[index] = MPI_REQUEST_NULL;
966 xbt_dynar_free(&comms);
969 if (index==MPI_UNDEFINED)
970 smpi_empty_status(status);
975 int smpi_mpi_waitall(int count, MPI_Request requests[],
980 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
981 int retvalue = MPI_SUCCESS;
982 //tag invalid requests in the set
983 if (status != MPI_STATUSES_IGNORE) {
984 for (c = 0; c < count; c++) {
985 if (requests[c] == MPI_REQUEST_NULL || requests[c]->dst == MPI_PROC_NULL ||
986 (requests[c]->flags & PREPARED)) {
987 smpi_empty_status(&status[c]);
988 } else if (requests[c]->src == MPI_PROC_NULL) {
989 smpi_empty_status(&status[c]);
990 status[c].MPI_SOURCE = MPI_PROC_NULL;
994 for(c = 0; c < count; c++) {
996 if (MC_is_active()) {
997 smpi_mpi_wait(&requests[c], pstat);
1000 index = smpi_mpi_waitany(count, requests, pstat);
1001 if (index == MPI_UNDEFINED)
1003 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags & NON_PERSISTENT))
1004 requests[index]=MPI_REQUEST_NULL;
1006 if (status != MPI_STATUSES_IGNORE) {
1007 status[index] = *pstat;
1008 if (status[index].MPI_ERROR == MPI_ERR_TRUNCATE)
1009 retvalue = MPI_ERR_IN_STATUS;
1016 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
1017 MPI_Status status[])
1019 int i, count, index;
1021 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
1024 for(i = 0; i < incount; i++)
1026 index=smpi_mpi_waitany(incount, requests, pstat);
1027 if(index!=MPI_UNDEFINED){
1028 indices[count] = index;
1030 if(status != MPI_STATUSES_IGNORE) {
1031 status[index] = *pstat;
1033 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags & NON_PERSISTENT))
1034 requests[index]=MPI_REQUEST_NULL;
1036 return MPI_UNDEFINED;
1042 int smpi_mpi_testsome(int incount, MPI_Request requests[], int *indices,
1043 MPI_Status status[])
1045 int i, count, count_dead;
1047 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
1051 for(i = 0; i < incount; i++) {
1052 if((requests[i] != MPI_REQUEST_NULL)) {
1053 if(smpi_mpi_test(&requests[i], pstat)) {
1056 if(status != MPI_STATUSES_IGNORE) {
1059 if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->flags & NON_PERSISTENT)
1060 requests[i]=MPI_REQUEST_NULL;
1066 if(count_dead==incount)return MPI_UNDEFINED;
1070 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
1073 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
1074 nary_tree_bcast(buf, count, datatype, root, comm, 4);
1077 void smpi_mpi_barrier(MPI_Comm comm)
1079 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
1080 nary_tree_barrier(comm, 4);
1083 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1084 void *recvbuf, int recvcount, MPI_Datatype recvtype,
1085 int root, MPI_Comm comm)
1087 int system_tag = COLL_TAG_GATHER;
1088 int rank, size, src, index;
1089 MPI_Aint lb = 0, recvext = 0;
1090 MPI_Request *requests;
1092 rank = smpi_comm_rank(comm);
1093 size = smpi_comm_size(comm);
1095 // Send buffer to root
1096 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
1098 // FIXME: check for errors
1099 smpi_datatype_extent(recvtype, &lb, &recvext);
1100 // Local copy from root
1101 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1102 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
1103 // Receive buffers from senders
1104 requests = xbt_new(MPI_Request, size - 1);
1106 for(src = 0; src < size; src++) {
1108 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
1109 recvcount, recvtype,
1110 src, system_tag, comm);
1114 // Wait for completion of irecv's.
1115 smpi_mpi_startall(size - 1, requests);
1116 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1117 for(src = 0; src < size-1; src++) {
1118 smpi_mpi_request_free(&requests[src]);
1125 void smpi_mpi_reduce_scatter(void *sendbuf, void *recvbuf, int *recvcounts,
1126 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1130 int rank = smpi_process_index();
1133 /* arbitrarily choose root as rank 0 */
1134 size = smpi_comm_size(comm);
1136 displs = xbt_new(int, size);
1137 for (i = 0; i < size; i++) {
1139 count += recvcounts[i];
1141 tmpbuf=(void*)smpi_get_tmp_sendbuffer(count*smpi_datatype_get_extent(datatype));
1143 mpi_coll_reduce_fun(sendbuf, tmpbuf, count, datatype, op, 0, comm);
1144 smpi_mpi_scatterv(tmpbuf, recvcounts, displs, datatype, recvbuf,
1145 recvcounts[rank], datatype, 0, comm);
1147 smpi_free_tmp_buffer(tmpbuf);
1150 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1151 void *recvbuf, int *recvcounts, int *displs,
1152 MPI_Datatype recvtype, int root, MPI_Comm comm)
1154 int system_tag = COLL_TAG_GATHERV;
1155 int rank, size, src, index;
1156 MPI_Aint lb = 0, recvext = 0;
1157 MPI_Request *requests;
1159 rank = smpi_comm_rank(comm);
1160 size = smpi_comm_size(comm);
1162 // Send buffer to root
1163 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
1165 // FIXME: check for errors
1166 smpi_datatype_extent(recvtype, &lb, &recvext);
1167 // Local copy from root
1168 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1169 (char *)recvbuf + displs[root] * recvext,
1170 recvcounts[root], recvtype);
1171 // Receive buffers from senders
1172 requests = xbt_new(MPI_Request, size - 1);
1174 for(src = 0; src < size; src++) {
1177 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
1178 recvcounts[src], recvtype, src, system_tag, comm);
1182 // Wait for completion of irecv's.
1183 smpi_mpi_startall(size - 1, requests);
1184 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1185 for(src = 0; src < size-1; src++) {
1186 smpi_mpi_request_free(&requests[src]);
1192 void smpi_mpi_allgather(void *sendbuf, int sendcount,
1193 MPI_Datatype sendtype, void *recvbuf,
1194 int recvcount, MPI_Datatype recvtype,
1197 int system_tag = COLL_TAG_ALLGATHER;
1198 int rank, size, other, index;
1199 MPI_Aint lb = 0, recvext = 0;
1200 MPI_Request *requests;
1202 rank = smpi_comm_rank(comm);
1203 size = smpi_comm_size(comm);
1204 // FIXME: check for errors
1205 smpi_datatype_extent(recvtype, &lb, &recvext);
1206 // Local copy from self
1207 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1208 (char *)recvbuf + rank * recvcount * recvext, recvcount,
1210 // Send/Recv buffers to/from others;
1211 requests = xbt_new(MPI_Request, 2 * (size - 1));
1213 for(other = 0; other < size; other++) {
1216 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
1219 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
1220 recvcount, recvtype, other,
1225 // Wait for completion of all comms.
1226 smpi_mpi_startall(2 * (size - 1), requests);
1227 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
1228 for(other = 0; other < 2*(size-1); other++) {
1229 smpi_mpi_request_free(&requests[other]);
1234 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
1235 MPI_Datatype sendtype, void *recvbuf,
1236 int *recvcounts, int *displs,
1237 MPI_Datatype recvtype, MPI_Comm comm)
1239 int system_tag = COLL_TAG_ALLGATHERV;
1240 int rank, size, other, index;
1241 MPI_Aint lb = 0, recvext = 0;
1242 MPI_Request *requests;
1244 rank = smpi_comm_rank(comm);
1245 size = smpi_comm_size(comm);
1246 // FIXME: check for errors
1247 smpi_datatype_extent(recvtype, &lb, &recvext);
1248 // Local copy from self
1249 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1250 (char *)recvbuf + displs[rank] * recvext,
1251 recvcounts[rank], recvtype);
1252 // Send buffers to others;
1253 requests = xbt_new(MPI_Request, 2 * (size - 1));
1255 for(other = 0; other < size; other++) {
1258 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
1262 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
1263 recvtype, other, system_tag, comm);
1267 // Wait for completion of all comms.
1268 smpi_mpi_startall(2 * (size - 1), requests);
1269 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
1270 for(other = 0; other < 2*(size-1); other++) {
1271 smpi_mpi_request_free(&requests[other]);
1276 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1277 void *recvbuf, int recvcount, MPI_Datatype recvtype,
1278 int root, MPI_Comm comm)
1280 int system_tag = COLL_TAG_SCATTER;
1281 int rank, size, dst, index;
1282 MPI_Aint lb = 0, sendext = 0;
1283 MPI_Request *requests;
1285 rank = smpi_comm_rank(comm);
1286 size = smpi_comm_size(comm);
1288 // Recv buffer from root
1289 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
1292 // FIXME: check for errors
1293 smpi_datatype_extent(sendtype, &lb, &sendext);
1294 // Local copy from root
1295 if(recvbuf!=MPI_IN_PLACE){
1296 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
1297 sendcount, sendtype, recvbuf, recvcount, recvtype);
1299 // Send buffers to receivers
1300 requests = xbt_new(MPI_Request, size - 1);
1302 for(dst = 0; dst < size; dst++) {
1304 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
1305 sendcount, sendtype, dst,
1310 // Wait for completion of isend's.
1311 smpi_mpi_startall(size - 1, requests);
1312 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1313 for(dst = 0; dst < size-1; dst++) {
1314 smpi_mpi_request_free(&requests[dst]);
1320 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
1321 MPI_Datatype sendtype, void *recvbuf, int recvcount,
1322 MPI_Datatype recvtype, int root, MPI_Comm comm)
1324 int system_tag = COLL_TAG_SCATTERV;
1325 int rank, size, dst, index;
1326 MPI_Aint lb = 0, sendext = 0;
1327 MPI_Request *requests;
1329 rank = smpi_comm_rank(comm);
1330 size = smpi_comm_size(comm);
1332 // Recv buffer from root
1333 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
1336 // FIXME: check for errors
1337 smpi_datatype_extent(sendtype, &lb, &sendext);
1338 // Local copy from root
1339 if(recvbuf!=MPI_IN_PLACE){
1340 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
1341 sendtype, recvbuf, recvcount, recvtype);
1343 // Send buffers to receivers
1344 requests = xbt_new(MPI_Request, size - 1);
1346 for(dst = 0; dst < size; dst++) {
1349 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
1350 sendtype, dst, system_tag, comm);
1354 // Wait for completion of isend's.
1355 smpi_mpi_startall(size - 1, requests);
1356 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1357 for(dst = 0; dst < size-1; dst++) {
1358 smpi_mpi_request_free(&requests[dst]);
1364 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
1365 MPI_Datatype datatype, MPI_Op op, int root,
1368 int system_tag = COLL_TAG_REDUCE;
1369 int rank, size, src, index;
1370 MPI_Aint lb = 0, dataext = 0;
1371 MPI_Request *requests;
1375 char* sendtmpbuf = (char*) sendbuf;
1376 if( sendbuf == MPI_IN_PLACE ) {
1377 sendtmpbuf = (char *)smpi_get_tmp_sendbuffer(count*smpi_datatype_get_extent(datatype));
1378 smpi_datatype_copy(recvbuf, count, datatype,sendtmpbuf, count, datatype);
1381 rank = smpi_comm_rank(comm);
1382 size = smpi_comm_size(comm);
1383 //non commutative case, use a working algo from openmpi
1384 if(!smpi_op_is_commute(op)){
1385 smpi_coll_tuned_reduce_ompi_basic_linear(sendtmpbuf, recvbuf, count,
1386 datatype, op, root, comm);
1391 // Send buffer to root
1392 smpi_mpi_send(sendtmpbuf, count, datatype, root, system_tag, comm);
1394 // FIXME: check for errors
1395 smpi_datatype_extent(datatype, &lb, &dataext);
1396 // Local copy from root
1397 if (sendtmpbuf && recvbuf)
1398 smpi_datatype_copy(sendtmpbuf, count, datatype, recvbuf, count, datatype);
1399 // Receive buffers from senders
1400 //TODO: make a MPI_barrier here ?
1401 requests = xbt_new(MPI_Request, size - 1);
1402 tmpbufs = xbt_new(void *, size - 1);
1404 for(src = 0; src < size; src++) {
1406 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1408 if (!smpi_process_get_replaying())
1409 tmpbufs[index] = xbt_malloc(count * dataext);
1411 tmpbufs[index] = smpi_get_tmp_sendbuffer(count * dataext);
1413 smpi_irecv_init(tmpbufs[index], count, datatype, src,
1418 // Wait for completion of irecv's.
1419 smpi_mpi_startall(size - 1, requests);
1420 for(src = 0; src < size - 1; src++) {
1421 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1422 XBT_DEBUG("finished waiting any request with index %d", index);
1423 if(index == MPI_UNDEFINED) {
1426 smpi_mpi_request_free(&requests[index]);
1428 if(op) /* op can be MPI_OP_NULL that does nothing */
1429 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1431 for(index = 0; index < size - 1; index++) {
1432 smpi_free_tmp_buffer(tmpbufs[index]);
1437 if( sendbuf == MPI_IN_PLACE ) {
1438 smpi_free_tmp_buffer(sendtmpbuf);
1443 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
1444 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1446 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
1447 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
1450 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
1451 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1453 int system_tag = -888;
1454 int rank, size, other, index;
1455 MPI_Aint lb = 0, dataext = 0;
1456 MPI_Request *requests;
1459 rank = smpi_comm_rank(comm);
1460 size = smpi_comm_size(comm);
1462 // FIXME: check for errors
1463 smpi_datatype_extent(datatype, &lb, &dataext);
1465 // Local copy from self
1466 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
1468 // Send/Recv buffers to/from others;
1469 requests = xbt_new(MPI_Request, size - 1);
1470 tmpbufs = xbt_new(void *, rank);
1472 for(other = 0; other < rank; other++) {
1473 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1475 tmpbufs[index] = smpi_get_tmp_sendbuffer(count * dataext);
1477 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
1481 for(other = rank + 1; other < size; other++) {
1483 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1486 // Wait for completion of all comms.
1487 smpi_mpi_startall(size - 1, requests);
1489 if(smpi_op_is_commute(op)){
1490 for(other = 0; other < size - 1; other++) {
1491 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1492 if(index == MPI_UNDEFINED) {
1496 // #Request is below rank: it's a irecv
1497 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1501 //non commutative case, wait in order
1502 for(other = 0; other < size - 1; other++) {
1503 smpi_mpi_wait(&(requests[other]), MPI_STATUS_IGNORE);
1505 smpi_op_apply(op, tmpbufs[other], recvbuf, &count, &datatype);
1509 for(index = 0; index < rank; index++) {
1510 smpi_free_tmp_buffer(tmpbufs[index]);
1512 for(index = 0; index < size-1; index++) {
1513 smpi_mpi_request_free(&requests[index]);
1519 void smpi_mpi_exscan(void *sendbuf, void *recvbuf, int count,
1520 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1522 int system_tag = -888;
1523 int rank, size, other, index;
1524 MPI_Aint lb = 0, dataext = 0;
1525 MPI_Request *requests;
1527 int recvbuf_is_empty=1;
1528 rank = smpi_comm_rank(comm);
1529 size = smpi_comm_size(comm);
1531 // FIXME: check for errors
1532 smpi_datatype_extent(datatype, &lb, &dataext);
1534 // Send/Recv buffers to/from others;
1535 requests = xbt_new(MPI_Request, size - 1);
1536 tmpbufs = xbt_new(void *, rank);
1538 for(other = 0; other < rank; other++) {
1539 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1541 tmpbufs[index] = smpi_get_tmp_sendbuffer(count * dataext);
1543 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
1547 for(other = rank + 1; other < size; other++) {
1549 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1552 // Wait for completion of all comms.
1553 smpi_mpi_startall(size - 1, requests);
1554 if(smpi_op_is_commute(op)){
1555 for(other = 0; other < size - 1; other++) {
1556 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1557 if(index == MPI_UNDEFINED) {
1561 if(recvbuf_is_empty){
1562 smpi_datatype_copy(tmpbufs[index], count, datatype, recvbuf, count, datatype);
1565 // #Request is below rank: it's a irecv
1566 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1570 //non commutative case, wait in order
1571 for(other = 0; other < size - 1; other++) {
1572 smpi_mpi_wait(&(requests[other]), MPI_STATUS_IGNORE);
1574 if(recvbuf_is_empty){
1575 smpi_datatype_copy(tmpbufs[other], count, datatype, recvbuf, count, datatype);
1577 }else smpi_op_apply(op, tmpbufs[other], recvbuf, &count, &datatype);
1581 for(index = 0; index < rank; index++) {
1582 smpi_free_tmp_buffer(tmpbufs[index]);
1584 for(index = 0; index < size-1; index++) {
1585 smpi_mpi_request_free(&requests[index]);