1 /* Copyright (c) 2007-2015. 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 "mc/mc_replay.h"
11 #include "xbt/replay.h"
13 #include "simix/smx_private.h"
14 #include "surf/surf.h"
15 #include "simgrid/sg_config.h"
16 #include "colls/colls.h"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi, "Logging specific to SMPI (base)");
21 static int match_recv(void* a, void* b, smx_synchro_t ignored) {
22 MPI_Request ref = (MPI_Request)a;
23 MPI_Request req = (MPI_Request)b;
24 XBT_DEBUG("Trying to match a recv of src %d against %d, tag %d against %d",ref->src,req->src, ref->tag, req->tag);
26 xbt_assert(ref, "Cannot match recv against null reference");
27 xbt_assert(req, "Cannot match recv against null request");
28 if((ref->src == MPI_ANY_SOURCE || req->src == ref->src)
29 && ((ref->tag == MPI_ANY_TAG && req->tag >=0) || req->tag == ref->tag)){
30 //we match, we can transfer some values
31 // FIXME : move this to the copy function ?
32 if(ref->src == MPI_ANY_SOURCE)ref->real_src = req->src;
33 if(ref->tag == MPI_ANY_TAG)ref->real_tag = req->tag;
34 if(ref->real_size < req->real_size) ref->truncated = 1;
36 ref->detached_sender=req; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
38 XBT_DEBUG("match succeeded");
43 static int match_send(void* a, void* b,smx_synchro_t ignored) {
44 MPI_Request ref = (MPI_Request)a;
45 MPI_Request req = (MPI_Request)b;
46 XBT_DEBUG("Trying to match a send of src %d against %d, tag %d against %d",ref->src,req->src, ref->tag, req->tag);
47 xbt_assert(ref, "Cannot match send against null reference");
48 xbt_assert(req, "Cannot match send against null request");
50 if((req->src == MPI_ANY_SOURCE || req->src == ref->src)
51 && ((req->tag == MPI_ANY_TAG && ref->tag >=0)|| req->tag == ref->tag))
53 if(req->src == MPI_ANY_SOURCE)req->real_src = ref->src;
54 if(req->tag == MPI_ANY_TAG)req->real_tag = ref->tag;
55 if(req->real_size < ref->real_size) req->truncated = 1;
57 req->detached_sender=ref; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
59 XBT_DEBUG("match succeeded");
65 typedef struct s_smpi_factor *smpi_factor_t;
66 typedef struct s_smpi_factor {
69 double values[4];//arbitrary set to 4
71 xbt_dynar_t smpi_os_values = NULL;
72 xbt_dynar_t smpi_or_values = NULL;
73 xbt_dynar_t smpi_ois_values = NULL;
75 double smpi_wtime_sleep = 0.0;
76 double smpi_iprobe_sleep = 1e-4;
77 double smpi_test_sleep = 1e-4;
80 // Methods used to parse and store the values for timing injections in smpi
81 // These are taken from surf/network.c and generalized to have more factors
82 // These methods should be merged with those in surf/network.c (moved somewhere in xbt ?)
84 static int factor_cmp(const void *pa, const void *pb)
86 return (((s_smpi_factor_t*)pa)->factor > ((s_smpi_factor_t*)pb)->factor) ? 1 :
87 (((s_smpi_factor_t*)pa)->factor < ((s_smpi_factor_t*)pb)->factor) ? -1 : 0;
91 static xbt_dynar_t parse_factor(const char *smpi_coef_string)
94 unsigned int iter = 0;
98 xbt_dynar_t smpi_factor, radical_elements, radical_elements2 = NULL;
100 smpi_factor = xbt_dynar_new(sizeof(s_smpi_factor_t), NULL);
101 radical_elements = xbt_str_split(smpi_coef_string, ";");
102 xbt_dynar_foreach(radical_elements, iter, value) {
103 memset(&fact, 0, sizeof(s_smpi_factor_t));
104 radical_elements2 = xbt_str_split(value, ":");
105 if (xbt_dynar_length(radical_elements2) <2 || xbt_dynar_length(radical_elements2) > 5)
106 xbt_die("Malformed radical for smpi factor!");
107 for(i =0; i<xbt_dynar_length(radical_elements2);i++ ){
109 fact.factor = atol(xbt_dynar_get_as(radical_elements2, i, char *));
111 fact.values[fact.nb_values] = atof(xbt_dynar_get_as(radical_elements2, i, char *));
116 xbt_dynar_push_as(smpi_factor, s_smpi_factor_t, fact);
117 XBT_DEBUG("smpi_factor:\t%ld : %d values, first: %f", fact.factor, fact.nb_values ,fact.values[0]);
118 xbt_dynar_free(&radical_elements2);
120 xbt_dynar_free(&radical_elements);
122 xbt_dynar_sort(smpi_factor, &factor_cmp);
123 xbt_dynar_foreach(smpi_factor, iter, fact) {
124 XBT_DEBUG("smpi_factor:\t%ld : %d values, first: %f", fact.factor, fact.nb_values ,fact.values[0]);
129 static double smpi_os(double size)
131 if (!smpi_os_values) {
132 smpi_os_values = parse_factor(sg_cfg_get_string("smpi/os"));
133 smpi_register_static(smpi_os_values, xbt_dynar_free_voidp);
135 unsigned int iter = 0;
136 s_smpi_factor_t fact;
138 // Iterate over all the sections that were specified and find the right
139 // value. (fact.factor represents the interval sizes; we want to find the
140 // section that has fact.factor <= size and no other such fact.factor <= size)
141 // Note: parse_factor() (used before) already sorts the dynar we iterate over!
142 xbt_dynar_foreach(smpi_os_values, iter, fact) {
143 if (size <= fact.factor) { // Values already too large, use the previously
144 // computed value of current!
145 XBT_DEBUG("os : %f <= %ld return %f", size, fact.factor, current);
148 // If the next section is too large, the current section must be used.
149 // Hence, save the cost, as we might have to use it.
150 current = fact.values[0]+fact.values[1]*size;
153 XBT_DEBUG("os : %f > %ld return %f", size, fact.factor, current);
158 static double smpi_ois(double size)
160 if (!smpi_ois_values) {
161 smpi_ois_values = parse_factor(sg_cfg_get_string("smpi/ois"));
162 smpi_register_static(smpi_ois_values, xbt_dynar_free_voidp);
164 unsigned int iter = 0;
165 s_smpi_factor_t fact;
167 // Iterate over all the sections that were specified and find the right
168 // value. (fact.factor represents the interval sizes; we want to find the
169 // section that has fact.factor <= size and no other such fact.factor <= size)
170 // Note: parse_factor() (used before) already sorts the dynar we iterate over!
171 xbt_dynar_foreach(smpi_ois_values, iter, fact) {
172 if (size <= fact.factor) { // Values already too large, use the previously
173 // computed value of current!
174 XBT_DEBUG("ois : %f <= %ld return %f", size, fact.factor, current);
177 // If the next section is too large, the current section must be used.
178 // Hence, save the cost, as we might have to use it.
179 current = fact.values[0]+fact.values[1]*size;
182 XBT_DEBUG("ois : %f > %ld return %f", size, fact.factor, current);
187 static double smpi_or(double size)
189 if (!smpi_or_values) {
190 smpi_or_values = parse_factor(sg_cfg_get_string("smpi/or"));
191 smpi_register_static(smpi_or_values, xbt_dynar_free_voidp);
193 unsigned int iter = 0;
194 s_smpi_factor_t fact;
196 // Iterate over all the sections that were specified and find the right
197 // value. (fact.factor represents the interval sizes; we want to find the
198 // section that has fact.factor <= size and no other such fact.factor <= size)
199 // Note: parse_factor() (used before) already sorts the dynar we iterate over!
200 xbt_dynar_foreach(smpi_or_values, iter, fact) {
201 if (size <= fact.factor) { // Values already too large, use the previously
202 // computed value of current!
203 XBT_DEBUG("or : %f <= %ld return %f", size, fact.factor, current);
206 // If the next section is too large, the current section must be used.
207 // Hence, save the cost, as we might have to use it.
208 current=fact.values[0]+fact.values[1]*size;
211 XBT_DEBUG("or : %f > %ld return %f", size, fact.factor, current);
216 double smpi_mpi_wtime(){
218 if (smpi_process_initialized() && !smpi_process_finalized() && !smpi_process_get_sampling()) {
220 time = SIMIX_get_clock();
221 // to avoid deadlocks if used as a break condition, such as
222 // while (MPI_Wtime(...) < time_limit) {
225 // because the time will not normally advance when only calls to MPI_Wtime
226 // are made -> deadlock (MPI_Wtime never reaches the time limit)
227 if(smpi_wtime_sleep > 0) simcall_process_sleep(smpi_wtime_sleep);
230 time = SIMIX_get_clock();
235 static MPI_Request build_request(void *buf, int count,
236 MPI_Datatype datatype, int src, int dst,
237 int tag, MPI_Comm comm, unsigned flags)
239 MPI_Request request = NULL;
241 void *old_buf = NULL;
243 request = xbt_new(s_smpi_mpi_request_t, 1);
245 s_smpi_subtype_t *subtype = datatype->substruct;
247 if(((flags & RECV) && (flags & ACCUMULATE)) || (datatype->has_subtype == 1)){
248 // This part handles the problem of non-contiguous memory
250 buf = count==0 ? NULL : xbt_malloc(count*smpi_datatype_size(datatype));
251 if ((datatype->has_subtype == 1) && (flags & SEND)) {
252 subtype->serialize(old_buf, buf, count, datatype->substruct);
257 // This part handles the problem of non-contiguous memory (for the
258 // unserialisation at the reception)
259 request->old_buf = old_buf;
260 request->old_type = datatype;
262 request->size = smpi_datatype_size(datatype) * count;
266 request->comm = comm;
267 request->action = NULL;
268 request->flags = flags;
269 request->detached = 0;
270 request->detached_sender = NULL;
271 request->real_src = 0;
273 request->truncated = 0;
274 request->real_size = 0;
275 request->real_tag = 0;
276 if(flags & PERSISTENT)
277 request->refcount = 1;
279 request->refcount = 0;
280 request->op = MPI_REPLACE;
283 if (flags & SEND) smpi_datatype_unuse(datatype);
289 void smpi_empty_status(MPI_Status * status)
291 if(status != MPI_STATUS_IGNORE) {
292 status->MPI_SOURCE = MPI_ANY_SOURCE;
293 status->MPI_TAG = MPI_ANY_TAG;
294 status->MPI_ERROR = MPI_SUCCESS;
299 static void smpi_mpi_request_free_voidp(void* request)
301 MPI_Request req = request;
302 smpi_mpi_request_free(&req);
305 /* MPI Low level calls */
306 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
307 int dst, int tag, MPI_Comm comm)
309 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
310 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
311 comm, PERSISTENT | SEND | PREPARED);
315 MPI_Request smpi_mpi_ssend_init(void *buf, int count, MPI_Datatype datatype,
316 int dst, int tag, MPI_Comm comm)
318 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
319 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
320 comm, PERSISTENT | SSEND | SEND | PREPARED);
324 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
325 int src, int tag, MPI_Comm comm)
327 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
328 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,
329 comm, PERSISTENT | RECV | PREPARED);
333 void smpi_mpi_start(MPI_Request request)
337 xbt_assert(!request->action, "Cannot (re)start a non-finished communication");
338 request->flags &= ~PREPARED;
339 request->flags &= ~FINISHED;
342 if (request->flags & RECV) {
343 print_request("New recv", request);
345 int async_small_thres = sg_cfg_get_int("smpi/async_small_thres");
347 xbt_mutex_t mut = smpi_process_mailboxes_mutex();
348 if (async_small_thres != 0 ||request->flags & RMA)
349 xbt_mutex_acquire(mut);
351 if (async_small_thres == 0 && !(request->flags & RMA)) {
352 mailbox = smpi_process_mailbox();
354 else if (request->flags & RMA || request->size < async_small_thres){
355 //We have to check both mailboxes (because SSEND messages are sent to the large mbox). begin with the more appropriate one : the small one.
356 mailbox = smpi_process_mailbox_small();
357 XBT_DEBUG("Is there a corresponding send already posted in the small mailbox %p (in case of SSEND)?", mailbox);
358 smx_synchro_t action = simcall_comm_iprobe(mailbox, 0, request->src,request->tag, &match_recv, (void*)request);
361 mailbox = smpi_process_mailbox();
362 XBT_DEBUG("No, nothing in the small mailbox test the other one : %p", mailbox);
363 action = simcall_comm_iprobe(mailbox, 0, request->src,request->tag, &match_recv, (void*)request);
365 XBT_DEBUG("Still nothing, switch back to the small mailbox : %p", mailbox);
366 mailbox = smpi_process_mailbox_small();
369 XBT_DEBUG("yes there was something for us in the large mailbox");
372 mailbox = smpi_process_mailbox_small();
373 XBT_DEBUG("Is there a corresponding send already posted the small mailbox?");
374 smx_synchro_t action = simcall_comm_iprobe(mailbox, 0, request->src,request->tag, &match_recv, (void*)request);
377 XBT_DEBUG("No, nothing in the permanent receive mailbox");
378 mailbox = smpi_process_mailbox();
380 XBT_DEBUG("yes there was something for us in the small mailbox");
384 //integrate pseudo-timing for buffering of small messages, do not bother to execute the simcall if 0
385 double sleeptime = request->detached ? smpi_or(request->size) : 0.0;
387 simcall_process_sleep(sleeptime);
388 XBT_DEBUG("receiving size of %zu : sleep %f ", request->size, smpi_or(request->size));
391 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
392 request->real_size=request->size;
393 smpi_datatype_use(request->old_type);
394 smpi_comm_use(request->comm);
395 request->action = simcall_comm_irecv(SIMIX_process_self(), mailbox, request->buf,
396 &request->real_size, &match_recv,
397 !smpi_process_get_replaying()? &smpi_comm_copy_buffer_callback
398 : &smpi_comm_null_copy_buffer_callback,
400 XBT_DEBUG("recv simcall posted");
402 if (async_small_thres != 0 || request->flags & RMA)
403 xbt_mutex_release(mut);
407 int receiver = request->dst;
409 int rank = request->src;
410 if (TRACE_smpi_view_internals()) {
411 TRACE_smpi_send(rank, rank, receiver,request->size);
413 print_request("New send", request);
415 //if we are giving back the control to the user without waiting for completion, we have to inject timings
416 double sleeptime = 0.0;
417 if(request->detached || (request->flags & (ISEND|SSEND))){// issend should be treated as isend
418 //isend and send timings may be different
419 sleeptime = (request->flags & ISEND)? smpi_ois(request->size) : smpi_os(request->size);
422 if(sleeptime != 0.0){
423 simcall_process_sleep(sleeptime);
424 XBT_DEBUG("sending size of %zu : sleep %f ", request->size, smpi_os(request->size));
427 int async_small_thres = sg_cfg_get_int("smpi/async_small_thres");
429 xbt_mutex_t mut=smpi_process_remote_mailboxes_mutex(receiver);
431 if (async_small_thres != 0 || request->flags & RMA)
432 xbt_mutex_acquire(mut);
434 if (!(async_small_thres != 0 || request->flags & RMA)) {
435 mailbox = smpi_process_remote_mailbox(receiver);
437 else if (request->flags & RMA || request->size < async_small_thres) { // eager mode
438 mailbox = smpi_process_remote_mailbox(receiver);
439 XBT_DEBUG("Is there a corresponding recv already posted in the large mailbox %p?", mailbox);
440 smx_synchro_t action = simcall_comm_iprobe(mailbox, 1,request->dst, request->tag, &match_send, (void*)request);
442 if (! (request->flags & SSEND)){
443 mailbox = smpi_process_remote_mailbox_small(receiver);
444 XBT_DEBUG("No, nothing in the large mailbox, message is to be sent on the small one %p", mailbox);
446 mailbox = smpi_process_remote_mailbox_small(receiver);
447 XBT_DEBUG("SSEND : Is there a corresponding recv already posted in the small mailbox %p?", mailbox);
448 action = simcall_comm_iprobe(mailbox, 1,request->dst, request->tag, &match_send, (void*)request);
450 XBT_DEBUG("No, we are first, send to large mailbox");
451 mailbox = smpi_process_remote_mailbox(receiver);
455 XBT_DEBUG("Yes there was something for us in the large mailbox");
458 mailbox = smpi_process_remote_mailbox(receiver);
459 XBT_DEBUG("Send request %p is in the large mailbox %p (buf: %p)",mailbox, request,request->buf);
462 void* buf = request->buf;
463 if ( (! (request->flags & SSEND)) && (request->size < sg_cfg_get_int("smpi/send_is_detached_thres"))) {
465 request->detached = 1;
466 XBT_DEBUG("Send request %p is detached", request);
468 if(request->old_type->has_subtype == 0){
469 oldbuf = request->buf;
470 if (!smpi_process_get_replaying() && oldbuf && request->size!=0){
471 if((smpi_privatize_global_variables)
472 && ((char*) request->buf >= smpi_start_data_exe)
473 && ((char*)request->buf < smpi_start_data_exe + smpi_size_data_exe )){
474 XBT_DEBUG("Privatization : We are sending from a zone inside global memory. Switch data segment ");
475 smpi_switch_data_segment(request->src);
477 buf = xbt_malloc(request->size);
478 memcpy(buf,oldbuf,request->size);
479 XBT_DEBUG("buf %p copied into %p",oldbuf,buf);
484 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
485 request->real_size=request->size;
486 smpi_datatype_use(request->old_type);
487 smpi_comm_use(request->comm);
489 simcall_comm_isend(SIMIX_process_from_PID(request->src+1), mailbox, request->size, -1.0,
490 buf, request->real_size,
492 &xbt_free_f, // how to free the userdata if a detached send fails
493 !smpi_process_get_replaying()? &smpi_comm_copy_buffer_callback
494 : &smpi_comm_null_copy_buffer_callback,
496 // detach if msg size < eager/rdv switch limit
498 XBT_DEBUG("send simcall posted");
502 /* FIXME: detached sends are not traceable (request->action == NULL) */
504 simcall_set_category(request->action, TRACE_internal_smpi_get_category());
506 if (async_small_thres != 0 || request->flags & RMA)
507 xbt_mutex_release(mut);
512 void smpi_mpi_startall(int count, MPI_Request * requests)
515 if(requests==NULL) return;
517 for(i = 0; i < count; i++) {
518 smpi_mpi_start(requests[i]);
522 void smpi_mpi_request_free(MPI_Request * request)
524 if((*request) != MPI_REQUEST_NULL){
525 (*request)->refcount--;
526 if((*request)->refcount<0) xbt_die("wrong refcount");
528 if((*request)->refcount==0){
529 print_request("Destroying", (*request));
531 *request = MPI_REQUEST_NULL;
533 print_request("Decrementing", (*request));
536 xbt_die("freeing an already free request");
541 MPI_Request smpi_rma_send_init(void *buf, int count, MPI_Datatype datatype,
542 int src, int dst, int tag, MPI_Comm comm, MPI_Op op)
544 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
546 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf , count, datatype, src, dst, tag,
547 comm, RMA | NON_PERSISTENT | ISEND | SEND | PREPARED);
549 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, src, dst, tag,
550 comm, RMA | NON_PERSISTENT | ISEND | SEND | PREPARED | ACCUMULATE);
556 MPI_Request smpi_rma_recv_init(void *buf, int count, MPI_Datatype datatype,
557 int src, int dst, int tag, MPI_Comm comm, MPI_Op op)
559 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
561 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, src, dst, tag,
562 comm, RMA | NON_PERSISTENT | RECV | PREPARED);
564 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, src, dst, tag,
565 comm, RMA | NON_PERSISTENT | RECV | PREPARED | ACCUMULATE);
572 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
573 int dst, int tag, MPI_Comm comm)
575 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
576 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf , count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
577 comm, PERSISTENT | ISEND | SEND | PREPARED);
581 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
582 int dst, int tag, MPI_Comm comm)
584 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
585 request = build_request(buf==MPI_BOTTOM?(void*)0:buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
586 comm, NON_PERSISTENT | ISEND | SEND);
587 smpi_mpi_start(request);
591 MPI_Request smpi_mpi_issend(void *buf, int count, MPI_Datatype datatype,
592 int dst, int tag, MPI_Comm comm)
594 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
595 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
596 comm, NON_PERSISTENT | ISEND | SSEND | SEND);
597 smpi_mpi_start(request);
601 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
602 int src, int tag, MPI_Comm comm)
604 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
605 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,
606 comm, PERSISTENT | RECV | PREPARED);
610 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
611 int src, int tag, MPI_Comm comm)
613 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
614 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,
615 comm, NON_PERSISTENT | RECV);
616 smpi_mpi_start(request);
620 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
621 int tag, MPI_Comm comm, MPI_Status * status)
623 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
624 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
625 smpi_mpi_wait(&request, status);
631 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
632 int tag, MPI_Comm comm)
634 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
635 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
636 comm, NON_PERSISTENT | SEND);
638 smpi_mpi_start(request);
639 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
643 void smpi_mpi_ssend(void *buf, int count, MPI_Datatype datatype,
644 int dst, int tag, MPI_Comm comm)
646 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
647 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
648 comm, NON_PERSISTENT | SSEND | SEND);
650 smpi_mpi_start(request);
651 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
655 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
656 int dst, int sendtag, void *recvbuf, int recvcount,
657 MPI_Datatype recvtype, int src, int recvtag,
658 MPI_Comm comm, MPI_Status * status)
660 MPI_Request requests[2];
662 int myid=smpi_process_index();
663 if ((smpi_group_index(smpi_comm_group(comm), dst) == myid) && (smpi_group_index(smpi_comm_group(comm), src) == myid)) {
664 smpi_datatype_copy(sendbuf, sendcount, sendtype,
665 recvbuf, recvcount, recvtype);
669 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
671 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
672 smpi_mpi_startall(2, requests);
673 smpi_mpi_waitall(2, requests, stats);
674 smpi_mpi_request_free(&requests[0]);
675 smpi_mpi_request_free(&requests[1]);
676 if(status != MPI_STATUS_IGNORE) {
677 // Copy receive status
682 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
684 return status->count / smpi_datatype_size(datatype);
687 static void finish_wait(MPI_Request * request, MPI_Status * status)
689 MPI_Request req = *request;
690 smpi_empty_status(status);
692 if(!(req->detached && req->flags & SEND)
693 && !(req->flags & PREPARED)){
694 if(status != MPI_STATUS_IGNORE) {
695 int src = req->src == MPI_ANY_SOURCE ? req->real_src : req->src;
696 status->MPI_SOURCE = smpi_group_rank(smpi_comm_group(req->comm), src);
697 status->MPI_TAG = req->tag == MPI_ANY_TAG ? req->real_tag : req->tag;
698 status->MPI_ERROR = req->truncated ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
699 // this handles the case were size in receive differs from size in send
700 // FIXME: really this should just contain the count of receive-type blocks,
702 status->count = req->real_size;
705 print_request("Finishing", req);
706 MPI_Datatype datatype = req->old_type;
708 if((req->flags & ACCUMULATE) || (datatype->has_subtype == 1)){
709 if (!smpi_process_get_replaying()){
710 if( smpi_privatize_global_variables
711 && ((char*)req->old_buf >= smpi_start_data_exe)
712 && ((char*)req->old_buf < smpi_start_data_exe + smpi_size_data_exe )
714 XBT_VERB("Privatization : We are unserializing to a zone in global memory - Switch data segment ");
715 smpi_switch_data_segment(smpi_process_index());
719 if(datatype->has_subtype == 1){
720 // This part handles the problem of non-contignous memory
721 // the unserialization at the reception
722 s_smpi_subtype_t *subtype = datatype->substruct;
723 if(req->flags & RECV)
724 subtype->unserialize(req->buf, req->old_buf, req->real_size/smpi_datatype_size(datatype) , datatype->substruct, req->op);
725 if(req->detached == 0) free(req->buf);
726 }else if(req->flags & RECV){//apply op on contiguous buffer for accumulate
727 int n =req->real_size/smpi_datatype_size(datatype);
728 smpi_op_apply(req->op, req->buf, req->old_buf, &n, &datatype);
731 smpi_comm_unuse(req->comm);
732 smpi_datatype_unuse(datatype);
736 if (TRACE_smpi_view_internals()) {
737 if(req->flags & RECV){
738 int rank = smpi_process_index();
739 int src_traced = (req->src == MPI_ANY_SOURCE ? req->real_src : req->src);
740 TRACE_smpi_recv(rank, src_traced, rank);
744 if(req->detached_sender!=NULL){
745 smpi_mpi_request_free(&(req->detached_sender));
747 if(req->flags & PERSISTENT)
749 req->flags |= FINISHED;
751 smpi_mpi_request_free(request);
755 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
758 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or smpi_mpi_testall before)
760 // to avoid deadlocks if used as a break condition, such as
761 // while (MPI_Test(request, flag, status) && flag) {
763 // because the time will not normally advance when only calls to MPI_Test
764 // are made -> deadlock
765 // multiplier to the sleeptime, to increase speed of execution, each failed test will increase it
766 static int nsleeps = 1;
767 if(smpi_test_sleep > 0) simcall_process_sleep(nsleeps*smpi_test_sleep);
769 smpi_empty_status(status);
771 if (!((*request)->flags & PREPARED)) {
772 if ((*request)->action != NULL)
773 flag = simcall_comm_test((*request)->action);
775 finish_wait(request, status);
776 nsleeps=1;//reset the number of sleeps we will do next time
777 if (*request != MPI_REQUEST_NULL && !((*request)->flags & PERSISTENT))
778 *request = MPI_REQUEST_NULL;
786 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
793 *index = MPI_UNDEFINED;
795 comms = xbt_dynar_new(sizeof(smx_synchro_t), NULL);
796 map = xbt_new(int, count);
798 for(i = 0; i < count; i++) {
799 if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->action &&
800 !(requests[i]->flags & PREPARED)) {
801 xbt_dynar_push(comms, &requests[i]->action);
807 //multiplier to the sleeptime, to increase speed of execution, each failed testany will increase it
808 static int nsleeps = 1;
809 if(smpi_test_sleep > 0) simcall_process_sleep(nsleeps*smpi_test_sleep);
811 i = simcall_comm_testany(comms);
812 // not MPI_UNDEFINED, as this is a simix return code
815 finish_wait(&requests[*index], status);
816 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags & NON_PERSISTENT))
817 requests[*index] = MPI_REQUEST_NULL;
824 //all requests are null or inactive, return true
826 smpi_empty_status(status);
829 xbt_dynar_free(&comms);
835 int smpi_mpi_testall(int count, MPI_Request requests[],
839 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
842 for(i=0; i<count; i++){
843 if (requests[i] != MPI_REQUEST_NULL && !(requests[i]->flags & PREPARED)) {
844 if (smpi_mpi_test(&requests[i], pstat)!=1){
847 requests[i]=MPI_REQUEST_NULL;
850 smpi_empty_status(pstat);
852 if(status != MPI_STATUSES_IGNORE) {
859 void smpi_mpi_probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
861 //FIXME find another wait to avoid busy waiting ?
862 // the issue here is that we have to wait on a nonexistent comm
864 smpi_mpi_iprobe(source, tag, comm, &flag, status);
865 XBT_DEBUG("Busy Waiting on probing : %d", flag);
869 void smpi_mpi_iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
871 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,
872 comm, PERSISTENT | RECV);
874 // to avoid deadlock, we have to sleep some time here, or the timer won't advance and we will only do iprobe simcalls
875 // (especially when used as a break condition, such as while(MPI_Iprobe(...)) ... )
876 // multiplier to the sleeptime, to increase speed of execution, each failed iprobe will increase it
877 static int nsleeps = 1;
878 if(smpi_iprobe_sleep > 0) simcall_process_sleep(nsleeps*smpi_iprobe_sleep);
879 // behave like a receive, but don't do it
882 print_request("New iprobe", request);
883 // We have to test both mailboxes as we don't know if we will receive one one or another
884 if (sg_cfg_get_int("smpi/async_small_thres")>0){
885 mailbox = smpi_process_mailbox_small();
886 XBT_DEBUG("trying to probe the perm recv mailbox");
887 request->action = simcall_comm_iprobe(mailbox, 0, request->src, request->tag, &match_recv, (void*)request);
889 if (request->action==NULL){
890 mailbox = smpi_process_mailbox();
891 XBT_DEBUG("trying to probe the other mailbox");
892 request->action = simcall_comm_iprobe(mailbox, 0, request->src,request->tag, &match_recv, (void*)request);
895 if (request->action){
896 MPI_Request req = (MPI_Request)SIMIX_comm_get_src_data(request->action);
898 if(status != MPI_STATUS_IGNORE && !(req->flags & PREPARED)) {
899 status->MPI_SOURCE = smpi_group_rank(smpi_comm_group(comm), req->src);
900 status->MPI_TAG = req->tag;
901 status->MPI_ERROR = MPI_SUCCESS;
902 status->count = req->real_size;
904 nsleeps=1;//reset the number of sleeps we will do next time
910 smpi_mpi_request_free(&request);
915 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
917 print_request("Waiting", *request);
918 if ((*request)->flags & PREPARED) {
919 smpi_empty_status(status);
923 if ((*request)->action != NULL) { // this is not a detached send
924 simcall_comm_wait((*request)->action, -1.0);
926 if((MC_is_active() || MC_record_replay_is_active()) && (*request)->action)
927 (*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
930 finish_wait(request, status);
931 if (*request != MPI_REQUEST_NULL && ((*request)->flags & NON_PERSISTENT))
932 *request = MPI_REQUEST_NULL;
933 // FIXME for a detached send, finish_wait is not called:
936 int smpi_mpi_waitany(int count, MPI_Request requests[],
943 index = MPI_UNDEFINED;
945 // Wait for a request to complete
946 comms = xbt_dynar_new(sizeof(smx_synchro_t), NULL);
947 map = xbt_new(int, count);
949 XBT_DEBUG("Wait for one of %d", count);
950 for(i = 0; i < count; i++) {
951 if (requests[i] != MPI_REQUEST_NULL
952 && !(requests[i]->flags & PREPARED)
953 && !(requests[i]->flags & FINISHED)) {
954 if (requests[i]->action != NULL) {
955 XBT_DEBUG("Waiting any %p ", requests[i]);
956 xbt_dynar_push(comms, &requests[i]->action);
960 //This is a finished detached request, let's return this one
961 size=0;//so we free the dynar but don't do the waitany call
963 finish_wait(&requests[i], status);//cleanup if refcount = 0
964 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags & NON_PERSISTENT))
965 requests[i]=MPI_REQUEST_NULL;//set to null
971 i = simcall_comm_waitany(comms);
973 // not MPI_UNDEFINED, as this is a simix return code
976 finish_wait(&requests[index], status);
977 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags & NON_PERSISTENT))
978 requests[index] = MPI_REQUEST_NULL;
982 xbt_dynar_free(&comms);
985 if (index==MPI_UNDEFINED)
986 smpi_empty_status(status);
991 int smpi_mpi_waitall(int count, MPI_Request requests[],
996 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
997 int retvalue = MPI_SUCCESS;
998 //tag invalid requests in the set
999 if (status != MPI_STATUSES_IGNORE) {
1000 for (c = 0; c < count; c++) {
1001 if (requests[c] == MPI_REQUEST_NULL || requests[c]->dst == MPI_PROC_NULL ||
1002 (requests[c]->flags & PREPARED)) {
1003 smpi_empty_status(&status[c]);
1004 } else if (requests[c]->src == MPI_PROC_NULL) {
1005 smpi_empty_status(&status[c]);
1006 status[c].MPI_SOURCE = MPI_PROC_NULL;
1010 for(c = 0; c < count; c++) {
1012 if (MC_is_active() || MC_record_replay_is_active()) {
1013 smpi_mpi_wait(&requests[c], pstat);
1016 index = smpi_mpi_waitany(count, requests, pstat);
1017 if (index == MPI_UNDEFINED)
1019 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags & NON_PERSISTENT))
1020 requests[index]=MPI_REQUEST_NULL;
1022 if (status != MPI_STATUSES_IGNORE) {
1023 status[index] = *pstat;
1024 if (status[index].MPI_ERROR == MPI_ERR_TRUNCATE)
1025 retvalue = MPI_ERR_IN_STATUS;
1032 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
1033 MPI_Status status[])
1035 int i, count, index;
1037 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
1040 for(i = 0; i < incount; i++)
1042 index=smpi_mpi_waitany(incount, requests, pstat);
1043 if(index!=MPI_UNDEFINED){
1044 indices[count] = index;
1046 if(status != MPI_STATUSES_IGNORE) {
1047 status[index] = *pstat;
1049 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags & NON_PERSISTENT))
1050 requests[index]=MPI_REQUEST_NULL;
1052 return MPI_UNDEFINED;
1058 int smpi_mpi_testsome(int incount, MPI_Request requests[], int *indices,
1059 MPI_Status status[])
1061 int i, count, count_dead;
1063 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
1067 for(i = 0; i < incount; i++) {
1068 if((requests[i] != MPI_REQUEST_NULL)) {
1069 if(smpi_mpi_test(&requests[i], pstat)) {
1072 if(status != MPI_STATUSES_IGNORE) {
1075 if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->flags & NON_PERSISTENT)
1076 requests[i]=MPI_REQUEST_NULL;
1082 if(count_dead==incount)return MPI_UNDEFINED;
1086 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
1089 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
1090 nary_tree_bcast(buf, count, datatype, root, comm, 4);
1093 void smpi_mpi_barrier(MPI_Comm comm)
1095 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
1096 nary_tree_barrier(comm, 4);
1099 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1100 void *recvbuf, int recvcount, MPI_Datatype recvtype,
1101 int root, MPI_Comm comm)
1103 int system_tag = COLL_TAG_GATHER;
1104 int rank, size, src, index;
1105 MPI_Aint lb = 0, recvext = 0;
1106 MPI_Request *requests;
1108 rank = smpi_comm_rank(comm);
1109 size = smpi_comm_size(comm);
1111 // Send buffer to root
1112 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
1114 // FIXME: check for errors
1115 smpi_datatype_extent(recvtype, &lb, &recvext);
1116 // Local copy from root
1117 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1118 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
1119 // Receive buffers from senders
1120 requests = xbt_new(MPI_Request, size - 1);
1122 for(src = 0; src < size; src++) {
1124 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
1125 recvcount, recvtype,
1126 src, system_tag, comm);
1130 // Wait for completion of irecv's.
1131 smpi_mpi_startall(size - 1, requests);
1132 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1133 for(src = 0; src < size-1; src++) {
1134 smpi_mpi_request_free(&requests[src]);
1141 void smpi_mpi_reduce_scatter(void *sendbuf, void *recvbuf, int *recvcounts,
1142 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1146 int rank = smpi_process_index();
1149 /* arbitrarily choose root as rank 0 */
1150 size = smpi_comm_size(comm);
1152 displs = xbt_new(int, size);
1153 for (i = 0; i < size; i++) {
1155 count += recvcounts[i];
1157 tmpbuf=(void*)smpi_get_tmp_sendbuffer(count*smpi_datatype_get_extent(datatype));
1159 mpi_coll_reduce_fun(sendbuf, tmpbuf, count, datatype, op, 0, comm);
1160 smpi_mpi_scatterv(tmpbuf, recvcounts, displs, datatype, recvbuf,
1161 recvcounts[rank], datatype, 0, comm);
1163 smpi_free_tmp_buffer(tmpbuf);
1166 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1167 void *recvbuf, int *recvcounts, int *displs,
1168 MPI_Datatype recvtype, int root, MPI_Comm comm)
1170 int system_tag = COLL_TAG_GATHERV;
1171 int rank, size, src, index;
1172 MPI_Aint lb = 0, recvext = 0;
1173 MPI_Request *requests;
1175 rank = smpi_comm_rank(comm);
1176 size = smpi_comm_size(comm);
1178 // Send buffer to root
1179 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
1181 // FIXME: check for errors
1182 smpi_datatype_extent(recvtype, &lb, &recvext);
1183 // Local copy from root
1184 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1185 (char *)recvbuf + displs[root] * recvext,
1186 recvcounts[root], recvtype);
1187 // Receive buffers from senders
1188 requests = xbt_new(MPI_Request, size - 1);
1190 for(src = 0; src < size; src++) {
1193 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
1194 recvcounts[src], recvtype, src, system_tag, comm);
1198 // Wait for completion of irecv's.
1199 smpi_mpi_startall(size - 1, requests);
1200 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1201 for(src = 0; src < size-1; src++) {
1202 smpi_mpi_request_free(&requests[src]);
1208 void smpi_mpi_allgather(void *sendbuf, int sendcount,
1209 MPI_Datatype sendtype, void *recvbuf,
1210 int recvcount, MPI_Datatype recvtype,
1213 int system_tag = COLL_TAG_ALLGATHER;
1214 int rank, size, other, index;
1215 MPI_Aint lb = 0, recvext = 0;
1216 MPI_Request *requests;
1218 rank = smpi_comm_rank(comm);
1219 size = smpi_comm_size(comm);
1220 // FIXME: check for errors
1221 smpi_datatype_extent(recvtype, &lb, &recvext);
1222 // Local copy from self
1223 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1224 (char *)recvbuf + rank * recvcount * recvext, recvcount,
1226 // Send/Recv buffers to/from others;
1227 requests = xbt_new(MPI_Request, 2 * (size - 1));
1229 for(other = 0; other < size; other++) {
1232 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
1235 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
1236 recvcount, recvtype, other,
1241 // Wait for completion of all comms.
1242 smpi_mpi_startall(2 * (size - 1), requests);
1243 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
1244 for(other = 0; other < 2*(size-1); other++) {
1245 smpi_mpi_request_free(&requests[other]);
1250 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
1251 MPI_Datatype sendtype, void *recvbuf,
1252 int *recvcounts, int *displs,
1253 MPI_Datatype recvtype, MPI_Comm comm)
1255 int system_tag = COLL_TAG_ALLGATHERV;
1256 int rank, size, other, index;
1257 MPI_Aint lb = 0, recvext = 0;
1258 MPI_Request *requests;
1260 rank = smpi_comm_rank(comm);
1261 size = smpi_comm_size(comm);
1262 // FIXME: check for errors
1263 smpi_datatype_extent(recvtype, &lb, &recvext);
1264 // Local copy from self
1265 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1266 (char *)recvbuf + displs[rank] * recvext,
1267 recvcounts[rank], recvtype);
1268 // Send buffers to others;
1269 requests = xbt_new(MPI_Request, 2 * (size - 1));
1271 for(other = 0; other < size; other++) {
1274 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
1278 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
1279 recvtype, other, system_tag, comm);
1283 // Wait for completion of all comms.
1284 smpi_mpi_startall(2 * (size - 1), requests);
1285 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
1286 for(other = 0; other < 2*(size-1); other++) {
1287 smpi_mpi_request_free(&requests[other]);
1292 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1293 void *recvbuf, int recvcount, MPI_Datatype recvtype,
1294 int root, MPI_Comm comm)
1296 int system_tag = COLL_TAG_SCATTER;
1297 int rank, size, dst, index;
1298 MPI_Aint lb = 0, sendext = 0;
1299 MPI_Request *requests;
1301 rank = smpi_comm_rank(comm);
1302 size = smpi_comm_size(comm);
1304 // Recv buffer from root
1305 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
1308 // FIXME: check for errors
1309 smpi_datatype_extent(sendtype, &lb, &sendext);
1310 // Local copy from root
1311 if(recvbuf!=MPI_IN_PLACE){
1312 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
1313 sendcount, sendtype, recvbuf, recvcount, recvtype);
1315 // Send buffers to receivers
1316 requests = xbt_new(MPI_Request, size - 1);
1318 for(dst = 0; dst < size; dst++) {
1320 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
1321 sendcount, sendtype, dst,
1326 // Wait for completion of isend's.
1327 smpi_mpi_startall(size - 1, requests);
1328 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1329 for(dst = 0; dst < size-1; dst++) {
1330 smpi_mpi_request_free(&requests[dst]);
1336 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
1337 MPI_Datatype sendtype, void *recvbuf, int recvcount,
1338 MPI_Datatype recvtype, int root, MPI_Comm comm)
1340 int system_tag = COLL_TAG_SCATTERV;
1341 int rank, size, dst, index;
1342 MPI_Aint lb = 0, sendext = 0;
1343 MPI_Request *requests;
1345 rank = smpi_comm_rank(comm);
1346 size = smpi_comm_size(comm);
1348 // Recv buffer from root
1349 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
1352 // FIXME: check for errors
1353 smpi_datatype_extent(sendtype, &lb, &sendext);
1354 // Local copy from root
1355 if(recvbuf!=MPI_IN_PLACE){
1356 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
1357 sendtype, recvbuf, recvcount, recvtype);
1359 // Send buffers to receivers
1360 requests = xbt_new(MPI_Request, size - 1);
1362 for(dst = 0; dst < size; dst++) {
1365 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
1366 sendtype, dst, system_tag, comm);
1370 // Wait for completion of isend's.
1371 smpi_mpi_startall(size - 1, requests);
1372 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1373 for(dst = 0; dst < size-1; dst++) {
1374 smpi_mpi_request_free(&requests[dst]);
1380 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
1381 MPI_Datatype datatype, MPI_Op op, int root,
1384 int system_tag = COLL_TAG_REDUCE;
1385 int rank, size, src, index;
1386 MPI_Aint lb = 0, dataext = 0;
1387 MPI_Request *requests;
1391 char* sendtmpbuf = (char*) sendbuf;
1392 if( sendbuf == MPI_IN_PLACE ) {
1393 sendtmpbuf = (char *)smpi_get_tmp_sendbuffer(count*smpi_datatype_get_extent(datatype));
1394 smpi_datatype_copy(recvbuf, count, datatype,sendtmpbuf, count, datatype);
1397 rank = smpi_comm_rank(comm);
1398 size = smpi_comm_size(comm);
1399 //non commutative case, use a working algo from openmpi
1400 if(!smpi_op_is_commute(op)){
1401 smpi_coll_tuned_reduce_ompi_basic_linear(sendtmpbuf, recvbuf, count,
1402 datatype, op, root, comm);
1407 // Send buffer to root
1408 smpi_mpi_send(sendtmpbuf, count, datatype, root, system_tag, comm);
1410 // FIXME: check for errors
1411 smpi_datatype_extent(datatype, &lb, &dataext);
1412 // Local copy from root
1413 if (sendtmpbuf && recvbuf)
1414 smpi_datatype_copy(sendtmpbuf, count, datatype, recvbuf, count, datatype);
1415 // Receive buffers from senders
1416 //TODO: make a MPI_barrier here ?
1417 requests = xbt_new(MPI_Request, size - 1);
1418 tmpbufs = xbt_new(void *, size - 1);
1420 for(src = 0; src < size; src++) {
1422 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1424 if (!smpi_process_get_replaying())
1425 tmpbufs[index] = xbt_malloc(count * dataext);
1427 tmpbufs[index] = smpi_get_tmp_sendbuffer(count * dataext);
1429 smpi_irecv_init(tmpbufs[index], count, datatype, src,
1434 // Wait for completion of irecv's.
1435 smpi_mpi_startall(size - 1, requests);
1436 for(src = 0; src < size - 1; src++) {
1437 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1438 XBT_DEBUG("finished waiting any request with index %d", index);
1439 if(index == MPI_UNDEFINED) {
1442 smpi_mpi_request_free(&requests[index]);
1444 if(op) /* op can be MPI_OP_NULL that does nothing */
1445 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1447 for(index = 0; index < size - 1; index++) {
1448 smpi_free_tmp_buffer(tmpbufs[index]);
1453 if( sendbuf == MPI_IN_PLACE ) {
1454 smpi_free_tmp_buffer(sendtmpbuf);
1459 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
1460 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1462 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
1463 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
1466 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
1467 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1469 int system_tag = -888;
1470 int rank, size, other, index;
1471 MPI_Aint lb = 0, dataext = 0;
1472 MPI_Request *requests;
1475 rank = smpi_comm_rank(comm);
1476 size = smpi_comm_size(comm);
1478 // FIXME: check for errors
1479 smpi_datatype_extent(datatype, &lb, &dataext);
1481 // Local copy from self
1482 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
1484 // Send/Recv buffers to/from others;
1485 requests = xbt_new(MPI_Request, size - 1);
1486 tmpbufs = xbt_new(void *, rank);
1488 for(other = 0; other < rank; other++) {
1489 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1491 tmpbufs[index] = smpi_get_tmp_sendbuffer(count * dataext);
1493 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
1497 for(other = rank + 1; other < size; other++) {
1499 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1502 // Wait for completion of all comms.
1503 smpi_mpi_startall(size - 1, requests);
1505 if(smpi_op_is_commute(op)){
1506 for(other = 0; other < size - 1; other++) {
1507 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1508 if(index == MPI_UNDEFINED) {
1512 // #Request is below rank: it's a irecv
1513 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1517 //non commutative case, wait in order
1518 for(other = 0; other < size - 1; other++) {
1519 smpi_mpi_wait(&(requests[other]), MPI_STATUS_IGNORE);
1521 smpi_op_apply(op, tmpbufs[other], recvbuf, &count, &datatype);
1525 for(index = 0; index < rank; index++) {
1526 smpi_free_tmp_buffer(tmpbufs[index]);
1528 for(index = 0; index < size-1; index++) {
1529 smpi_mpi_request_free(&requests[index]);
1535 void smpi_mpi_exscan(void *sendbuf, void *recvbuf, int count,
1536 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1538 int system_tag = -888;
1539 int rank, size, other, index;
1540 MPI_Aint lb = 0, dataext = 0;
1541 MPI_Request *requests;
1543 int recvbuf_is_empty=1;
1544 rank = smpi_comm_rank(comm);
1545 size = smpi_comm_size(comm);
1547 // FIXME: check for errors
1548 smpi_datatype_extent(datatype, &lb, &dataext);
1550 // Send/Recv buffers to/from others;
1551 requests = xbt_new(MPI_Request, size - 1);
1552 tmpbufs = xbt_new(void *, rank);
1554 for(other = 0; other < rank; other++) {
1555 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1557 tmpbufs[index] = smpi_get_tmp_sendbuffer(count * dataext);
1559 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
1563 for(other = rank + 1; other < size; other++) {
1565 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1568 // Wait for completion of all comms.
1569 smpi_mpi_startall(size - 1, requests);
1570 if(smpi_op_is_commute(op)){
1571 for(other = 0; other < size - 1; other++) {
1572 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1573 if(index == MPI_UNDEFINED) {
1577 if(recvbuf_is_empty){
1578 smpi_datatype_copy(tmpbufs[index], count, datatype, recvbuf, count, datatype);
1581 // #Request is below rank: it's a irecv
1582 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1586 //non commutative case, wait in order
1587 for(other = 0; other < size - 1; other++) {
1588 smpi_mpi_wait(&(requests[other]), MPI_STATUS_IGNORE);
1590 if(recvbuf_is_empty){
1591 smpi_datatype_copy(tmpbufs[other], count, datatype, recvbuf, count, datatype);
1593 }else smpi_op_apply(op, tmpbufs[other], recvbuf, &count, &datatype);
1597 for(index = 0; index < rank; index++) {
1598 smpi_free_tmp_buffer(tmpbufs[index]);
1600 for(index = 0; index < size-1; index++) {
1601 smpi_mpi_request_free(&requests[index]);