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 "src/mc/mc_replay.h"
11 #include "xbt/replay.h"
13 #include "src/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 // Methods used to parse and store the values for timing injections in smpi
66 // These are taken from surf/network.c and generalized to have more values for each factor
67 typedef struct s_smpi_factor_multival *smpi_os_factor_multival_t;
68 typedef struct s_smpi_factor_multival { // FIXME: this should be merged (deduplicated) with s_smpi_factor defined in network_smpi.c
71 double values[4];//arbitrary set to 4
72 } s_smpi_factor_multival_t;
73 xbt_dynar_t smpi_os_values = NULL;
74 xbt_dynar_t smpi_or_values = NULL;
75 xbt_dynar_t smpi_ois_values = NULL;
77 double smpi_wtime_sleep = 0.0;
78 double smpi_iprobe_sleep = 1e-4;
79 double smpi_test_sleep = 1e-4;
81 static int factor_cmp(const void *pa, const void *pb)
83 return (((s_smpi_factor_multival_t*)pa)->factor > ((s_smpi_factor_multival_t*)pb)->factor) ? 1 :
84 (((s_smpi_factor_multival_t*)pa)->factor < ((s_smpi_factor_multival_t*)pb)->factor) ? -1 : 0;
88 static xbt_dynar_t parse_factor(const char *smpi_coef_string)
91 unsigned int iter = 0;
92 s_smpi_factor_multival_t fact;
95 xbt_dynar_t smpi_factor, radical_elements, radical_elements2 = NULL;
97 smpi_factor = xbt_dynar_new(sizeof(s_smpi_factor_multival_t), NULL);
98 radical_elements = xbt_str_split(smpi_coef_string, ";");
99 xbt_dynar_foreach(radical_elements, iter, value) {
100 memset(&fact, 0, sizeof(s_smpi_factor_multival_t));
101 radical_elements2 = xbt_str_split(value, ":");
102 if (xbt_dynar_length(radical_elements2) <2 || xbt_dynar_length(radical_elements2) > 5)
103 xbt_die("Malformed radical for smpi factor: '%s'", smpi_coef_string);
104 for(i =0; i<xbt_dynar_length(radical_elements2);i++ ){
107 errmsg = bprintf("Invalid factor in chunk #%d: %%s", iter+1);
108 fact.factor = xbt_str_parse_int(xbt_dynar_get_as(radical_elements2, i, char *), errmsg);
110 errmsg = bprintf("Invalid factor value %d in chunk #%d: %%s", i, iter+1);
111 fact.values[fact.nb_values] = xbt_str_parse_double(xbt_dynar_get_as(radical_elements2, i, char *), errmsg);
117 xbt_dynar_push_as(smpi_factor, s_smpi_factor_multival_t, fact);
118 XBT_DEBUG("smpi_factor:\t%ld : %d values, first: %f", fact.factor, fact.nb_values ,fact.values[0]);
119 xbt_dynar_free(&radical_elements2);
121 xbt_dynar_free(&radical_elements);
123 xbt_dynar_sort(smpi_factor, &factor_cmp);
124 xbt_dynar_foreach(smpi_factor, iter, fact) {
125 XBT_DEBUG("smpi_factor:\t%ld : %d values, first: %f", fact.factor, fact.nb_values ,fact.values[0]);
130 static double smpi_os(double size)
132 if (!smpi_os_values) {
133 smpi_os_values = parse_factor(sg_cfg_get_string("smpi/os"));
134 smpi_register_static(smpi_os_values, xbt_dynar_free_voidp);
136 unsigned int iter = 0;
137 s_smpi_factor_multival_t fact;
139 // Iterate over all the sections that were specified and find the right
140 // value. (fact.factor represents the interval sizes; we want to find the
141 // section that has fact.factor <= size and no other such fact.factor <= size)
142 // Note: parse_factor() (used before) already sorts the dynar we iterate over!
143 xbt_dynar_foreach(smpi_os_values, iter, fact) {
144 if (size <= fact.factor) { // Values already too large, use the previously
145 // computed value of current!
146 XBT_DEBUG("os : %f <= %ld return %f", size, fact.factor, current);
149 // If the next section is too large, the current section must be used.
150 // Hence, save the cost, as we might have to use it.
151 current = fact.values[0]+fact.values[1]*size;
154 XBT_DEBUG("os : %f > %ld return %f", size, fact.factor, current);
159 static double smpi_ois(double size)
161 if (!smpi_ois_values) {
162 smpi_ois_values = parse_factor(sg_cfg_get_string("smpi/ois"));
163 smpi_register_static(smpi_ois_values, xbt_dynar_free_voidp);
165 unsigned int iter = 0;
166 s_smpi_factor_multival_t fact;
168 // Iterate over all the sections that were specified and find the right
169 // value. (fact.factor represents the interval sizes; we want to find the
170 // section that has fact.factor <= size and no other such fact.factor <= size)
171 // Note: parse_factor() (used before) already sorts the dynar we iterate over!
172 xbt_dynar_foreach(smpi_ois_values, iter, fact) {
173 if (size <= fact.factor) { // Values already too large, use the previously
174 // computed value of current!
175 XBT_DEBUG("ois : %f <= %ld return %f", size, fact.factor, current);
178 // If the next section is too large, the current section must be used.
179 // Hence, save the cost, as we might have to use it.
180 current = fact.values[0]+fact.values[1]*size;
183 XBT_DEBUG("ois : %f > %ld return %f", size, fact.factor, current);
188 static double smpi_or(double size)
190 if (!smpi_or_values) {
191 smpi_or_values = parse_factor(sg_cfg_get_string("smpi/or"));
192 smpi_register_static(smpi_or_values, xbt_dynar_free_voidp);
194 unsigned int iter = 0;
195 s_smpi_factor_multival_t fact;
197 // Iterate over all the sections that were specified and find the right
198 // value. (fact.factor represents the interval sizes; we want to find the
199 // section that has fact.factor <= size and no other such fact.factor <= size)
200 // Note: parse_factor() (used before) already sorts the dynar we iterate over!
201 xbt_dynar_foreach(smpi_or_values, iter, fact) {
202 if (size <= fact.factor) { // Values already too large, use the previously
203 // computed value of current!
204 XBT_DEBUG("or : %f <= %ld return %f", size, fact.factor, current);
207 // If the next section is too large, the current section must be used.
208 // Hence, save the cost, as we might have to use it.
209 current=fact.values[0]+fact.values[1]*size;
212 XBT_DEBUG("or : %f > %ld return %f", size, fact.factor, current);
217 double smpi_mpi_wtime(){
219 if (smpi_process_initialized() && !smpi_process_finalized() && !smpi_process_get_sampling()) {
221 time = SIMIX_get_clock();
222 // to avoid deadlocks if used as a break condition, such as
223 // while (MPI_Wtime(...) < time_limit) {
226 // because the time will not normally advance when only calls to MPI_Wtime
227 // are made -> deadlock (MPI_Wtime never reaches the time limit)
228 if(smpi_wtime_sleep > 0) simcall_process_sleep(smpi_wtime_sleep);
231 time = SIMIX_get_clock();
236 static MPI_Request build_request(void *buf, int count,
237 MPI_Datatype datatype, int src, int dst,
238 int tag, MPI_Comm comm, unsigned flags)
240 MPI_Request request = NULL;
242 void *old_buf = NULL;
244 request = xbt_new(s_smpi_mpi_request_t, 1);
246 s_smpi_subtype_t *subtype = static_cast<s_smpi_subtype_t*>(datatype->substruct);
248 if(((flags & RECV) && (flags & ACCUMULATE)) || (datatype->has_subtype == 1)){
249 // This part handles the problem of non-contiguous memory
251 buf = count==0 ? NULL : xbt_malloc(count*smpi_datatype_size(datatype));
252 if ((datatype->has_subtype == 1) && (flags & SEND)) {
253 subtype->serialize(old_buf, buf, count, datatype->substruct);
258 // This part handles the problem of non-contiguous memory (for the
259 // unserialisation at the reception)
260 request->old_buf = old_buf;
261 request->old_type = datatype;
263 request->size = smpi_datatype_size(datatype) * count;
267 request->comm = comm;
268 request->action = NULL;
269 request->flags = flags;
270 request->detached = 0;
271 request->detached_sender = NULL;
272 request->real_src = 0;
274 request->truncated = 0;
275 request->real_size = 0;
276 request->real_tag = 0;
277 if(flags & PERSISTENT)
278 request->refcount = 1;
280 request->refcount = 0;
281 request->op = MPI_REPLACE;
284 if (flags & SEND) smpi_datatype_unuse(datatype);
290 void smpi_empty_status(MPI_Status * status)
292 if(status != MPI_STATUS_IGNORE) {
293 status->MPI_SOURCE = MPI_ANY_SOURCE;
294 status->MPI_TAG = MPI_ANY_TAG;
295 status->MPI_ERROR = MPI_SUCCESS;
300 static void smpi_mpi_request_free_voidp(void* request)
302 MPI_Request req = static_cast<MPI_Request>(request);
303 smpi_mpi_request_free(&req);
306 /* MPI Low level calls */
307 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
308 int dst, int tag, MPI_Comm comm)
310 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
311 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
312 comm, PERSISTENT | SEND | PREPARED);
316 MPI_Request smpi_mpi_ssend_init(void *buf, int count, MPI_Datatype datatype,
317 int dst, int tag, MPI_Comm comm)
319 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
320 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
321 comm, PERSISTENT | SSEND | SEND | PREPARED);
325 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
326 int src, int tag, MPI_Comm comm)
328 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
329 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,
330 comm, PERSISTENT | RECV | PREPARED);
334 void smpi_mpi_start(MPI_Request request)
338 xbt_assert(!request->action, "Cannot (re)start a non-finished communication");
339 request->flags &= ~PREPARED;
340 request->flags &= ~FINISHED;
343 if (request->flags & RECV) {
344 print_request("New recv", request);
346 int async_small_thresh = sg_cfg_get_int("smpi/async_small_thresh");
348 xbt_mutex_t mut = smpi_process_mailboxes_mutex();
349 if (async_small_thresh != 0 ||request->flags & RMA)
350 xbt_mutex_acquire(mut);
352 if (async_small_thresh == 0 && !(request->flags & RMA)) {
353 mailbox = smpi_process_mailbox();
355 else if (request->flags & RMA || static_cast<int>(request->size) < async_small_thresh){
356 //We have to check both mailboxes (because SSEND messages are sent to the large mbox). begin with the more appropriate one : the small one.
357 mailbox = smpi_process_mailbox_small();
358 XBT_DEBUG("Is there a corresponding send already posted in the small mailbox %p (in case of SSEND)?", mailbox);
359 smx_synchro_t action = simcall_comm_iprobe(mailbox, 0, request->src,request->tag, &match_recv, (void*)request);
362 mailbox = smpi_process_mailbox();
363 XBT_DEBUG("No, nothing in the small mailbox test the other one : %p", mailbox);
364 action = simcall_comm_iprobe(mailbox, 0, request->src,request->tag, &match_recv, (void*)request);
366 XBT_DEBUG("Still nothing, switch back to the small mailbox : %p", mailbox);
367 mailbox = smpi_process_mailbox_small();
370 XBT_DEBUG("yes there was something for us in the large mailbox");
373 mailbox = smpi_process_mailbox_small();
374 XBT_DEBUG("Is there a corresponding send already posted the small mailbox?");
375 smx_synchro_t action = simcall_comm_iprobe(mailbox, 0, request->src,request->tag, &match_recv, (void*)request);
378 XBT_DEBUG("No, nothing in the permanent receive mailbox");
379 mailbox = smpi_process_mailbox();
381 XBT_DEBUG("yes there was something for us in the small mailbox");
385 //integrate pseudo-timing for buffering of small messages, do not bother to execute the simcall if 0
386 double sleeptime = request->detached ? smpi_or(request->size) : 0.0;
388 simcall_process_sleep(sleeptime);
389 XBT_DEBUG("receiving size of %zu : sleep %f ", request->size, smpi_or(request->size));
392 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
393 request->real_size=request->size;
394 smpi_datatype_use(request->old_type);
395 smpi_comm_use(request->comm);
396 request->action = simcall_comm_irecv(SIMIX_process_self(), mailbox, request->buf,
397 &request->real_size, &match_recv,
398 !smpi_process_get_replaying()? &smpi_comm_copy_buffer_callback
399 : &smpi_comm_null_copy_buffer_callback,
401 XBT_DEBUG("recv simcall posted");
403 if (async_small_thresh != 0 || (request->flags & RMA))
404 xbt_mutex_release(mut);
408 int receiver = request->dst;
410 int rank = request->src;
411 if (TRACE_smpi_view_internals()) {
412 TRACE_smpi_send(rank, rank, receiver,request->size);
414 print_request("New send", request);
416 //if we are giving back the control to the user without waiting for completion, we have to inject timings
417 double sleeptime = 0.0;
418 if(request->detached || (request->flags & (ISEND|SSEND))){// issend should be treated as isend
419 //isend and send timings may be different
420 sleeptime = (request->flags & ISEND)? smpi_ois(request->size) : smpi_os(request->size);
423 if(sleeptime != 0.0){
424 simcall_process_sleep(sleeptime);
425 XBT_DEBUG("sending size of %zu : sleep %f ", request->size, smpi_os(request->size));
428 int async_small_thresh = sg_cfg_get_int("smpi/async_small_thresh");
430 xbt_mutex_t mut=smpi_process_remote_mailboxes_mutex(receiver);
432 if (async_small_thresh != 0 || (request->flags & RMA))
433 xbt_mutex_acquire(mut);
435 if (!(async_small_thresh != 0 || (request->flags & RMA))) {
436 mailbox = smpi_process_remote_mailbox(receiver);
438 else if (request->flags & RMA || static_cast<int>(request->size) < async_small_thresh) { // eager mode
439 mailbox = smpi_process_remote_mailbox(receiver);
440 XBT_DEBUG("Is there a corresponding recv already posted in the large mailbox %p?", mailbox);
441 smx_synchro_t action = simcall_comm_iprobe(mailbox, 1,request->dst, request->tag, &match_send, (void*)request);
443 if (! (request->flags & SSEND)){
444 mailbox = smpi_process_remote_mailbox_small(receiver);
445 XBT_DEBUG("No, nothing in the large mailbox, message is to be sent on the small one %p", mailbox);
447 mailbox = smpi_process_remote_mailbox_small(receiver);
448 XBT_DEBUG("SSEND : Is there a corresponding recv already posted in the small mailbox %p?", mailbox);
449 action = simcall_comm_iprobe(mailbox, 1,request->dst, request->tag, &match_send, (void*)request);
451 XBT_DEBUG("No, we are first, send to large mailbox");
452 mailbox = smpi_process_remote_mailbox(receiver);
456 XBT_DEBUG("Yes there was something for us in the large mailbox");
459 mailbox = smpi_process_remote_mailbox(receiver);
460 XBT_DEBUG("Send request %p is in the large mailbox %p (buf: %p)",mailbox, request,request->buf);
463 void* buf = request->buf;
464 if ( (! (request->flags & SSEND)) && (static_cast<int>(request->size) < sg_cfg_get_int("smpi/send_is_detached_thresh"))) {
466 request->detached = 1;
467 XBT_DEBUG("Send request %p is detached", request);
469 if(request->old_type->has_subtype == 0){
470 oldbuf = request->buf;
471 if (!smpi_process_get_replaying() && oldbuf && request->size!=0){
472 if((smpi_privatize_global_variables)
473 && ((char*) request->buf >= smpi_start_data_exe)
474 && ((char*)request->buf < smpi_start_data_exe + smpi_size_data_exe )){
475 XBT_DEBUG("Privatization : We are sending from a zone inside global memory. Switch data segment ");
476 smpi_switch_data_segment(request->src);
478 buf = xbt_malloc(request->size);
479 memcpy(buf,oldbuf,request->size);
480 XBT_DEBUG("buf %p copied into %p",oldbuf,buf);
485 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
486 request->real_size=request->size;
487 smpi_datatype_use(request->old_type);
488 smpi_comm_use(request->comm);
490 simcall_comm_isend(SIMIX_process_from_PID(request->src+1), mailbox, request->size, -1.0,
491 buf, request->real_size,
493 &xbt_free_f, // how to free the userdata if a detached send fails
494 !smpi_process_get_replaying()? &smpi_comm_copy_buffer_callback
495 : &smpi_comm_null_copy_buffer_callback,
497 // detach if msg size < eager/rdv switch limit
499 XBT_DEBUG("send simcall posted");
503 /* FIXME: detached sends are not traceable (request->action == NULL) */
505 simcall_set_category(request->action, TRACE_internal_smpi_get_category());
507 if (async_small_thresh != 0 || request->flags & RMA)
508 xbt_mutex_release(mut);
513 void smpi_mpi_startall(int count, MPI_Request * requests)
516 if(requests==NULL) return;
518 for(i = 0; i < count; i++) {
519 smpi_mpi_start(requests[i]);
523 void smpi_mpi_request_free(MPI_Request * request)
525 if((*request) != MPI_REQUEST_NULL){
526 (*request)->refcount--;
527 if((*request)->refcount<0) xbt_die("wrong refcount");
529 if((*request)->refcount==0){
530 print_request("Destroying", (*request));
532 *request = MPI_REQUEST_NULL;
534 print_request("Decrementing", (*request));
537 xbt_die("freeing an already free request");
542 MPI_Request smpi_rma_send_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 | ISEND | SEND | PREPARED);
550 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, src, dst, tag,
551 comm, RMA | NON_PERSISTENT | ISEND | SEND | PREPARED | ACCUMULATE);
557 MPI_Request smpi_rma_recv_init(void *buf, int count, MPI_Datatype datatype,
558 int src, int dst, int tag, MPI_Comm comm, MPI_Op op)
560 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, src, dst, tag,
563 comm, RMA | NON_PERSISTENT | RECV | PREPARED);
565 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, src, dst, tag,
566 comm, RMA | NON_PERSISTENT | RECV | PREPARED | ACCUMULATE);
573 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
574 int dst, int tag, MPI_Comm comm)
576 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
577 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf , count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
578 comm, PERSISTENT | ISEND | SEND | PREPARED);
582 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
583 int dst, int tag, MPI_Comm comm)
585 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
586 request = build_request(buf==MPI_BOTTOM?(void*)0:buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
587 comm, NON_PERSISTENT | ISEND | SEND);
588 smpi_mpi_start(request);
592 MPI_Request smpi_mpi_issend(void *buf, int count, MPI_Datatype datatype,
593 int dst, int tag, MPI_Comm comm)
595 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
596 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
597 comm, NON_PERSISTENT | ISEND | SSEND | SEND);
598 smpi_mpi_start(request);
602 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
603 int src, int tag, MPI_Comm comm)
605 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
606 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,
607 comm, PERSISTENT | RECV | PREPARED);
611 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
612 int src, int tag, MPI_Comm comm)
614 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
615 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,
616 comm, NON_PERSISTENT | RECV);
617 smpi_mpi_start(request);
621 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
622 int tag, MPI_Comm comm, MPI_Status * status)
624 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
625 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
626 smpi_mpi_wait(&request, status);
632 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
633 int tag, MPI_Comm comm)
635 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
636 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
637 comm, NON_PERSISTENT | SEND);
639 smpi_mpi_start(request);
640 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
644 void smpi_mpi_ssend(void *buf, int count, MPI_Datatype datatype,
645 int dst, int tag, MPI_Comm comm)
647 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
648 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
649 comm, NON_PERSISTENT | SSEND | SEND);
651 smpi_mpi_start(request);
652 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
656 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
657 int dst, int sendtag, void *recvbuf, int recvcount,
658 MPI_Datatype recvtype, int src, int recvtag,
659 MPI_Comm comm, MPI_Status * status)
661 MPI_Request requests[2];
663 int myid=smpi_process_index();
664 if ((smpi_group_index(smpi_comm_group(comm), dst) == myid) && (smpi_group_index(smpi_comm_group(comm), src) == myid)) {
665 smpi_datatype_copy(sendbuf, sendcount, sendtype,
666 recvbuf, recvcount, recvtype);
670 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
672 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
673 smpi_mpi_startall(2, requests);
674 smpi_mpi_waitall(2, requests, stats);
675 smpi_mpi_request_free(&requests[0]);
676 smpi_mpi_request_free(&requests[1]);
677 if(status != MPI_STATUS_IGNORE) {
678 // Copy receive status
683 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
685 return status->count / smpi_datatype_size(datatype);
688 static void finish_wait(MPI_Request * request, MPI_Status * status)
690 MPI_Request req = *request;
691 smpi_empty_status(status);
693 if(!(req->detached && req->flags & SEND)
694 && !(req->flags & PREPARED)){
695 if(status != MPI_STATUS_IGNORE) {
696 int src = req->src == MPI_ANY_SOURCE ? req->real_src : req->src;
697 status->MPI_SOURCE = smpi_group_rank(smpi_comm_group(req->comm), src);
698 status->MPI_TAG = req->tag == MPI_ANY_TAG ? req->real_tag : req->tag;
699 status->MPI_ERROR = req->truncated ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
700 // this handles the case were size in receive differs from size in send
701 // FIXME: really this should just contain the count of receive-type blocks,
703 status->count = req->real_size;
706 print_request("Finishing", req);
707 MPI_Datatype datatype = req->old_type;
709 if((req->flags & ACCUMULATE) || (datatype->has_subtype == 1)){
710 if (!smpi_process_get_replaying()){
711 if( smpi_privatize_global_variables
712 && ((char*)req->old_buf >= smpi_start_data_exe)
713 && ((char*)req->old_buf < smpi_start_data_exe + smpi_size_data_exe )
715 XBT_VERB("Privatization : We are unserializing to a zone in global memory - Switch data segment ");
716 smpi_switch_data_segment(smpi_process_index());
720 if(datatype->has_subtype == 1){
721 // This part handles the problem of non-contignous memory
722 // the unserialization at the reception
723 s_smpi_subtype_t *subtype = static_cast<s_smpi_subtype_t*>(datatype->substruct);
724 if(req->flags & RECV)
725 subtype->unserialize(req->buf, req->old_buf, req->real_size/smpi_datatype_size(datatype) , datatype->substruct, req->op);
726 if(req->detached == 0) free(req->buf);
727 }else if(req->flags & RECV){//apply op on contiguous buffer for accumulate
728 int n =req->real_size/smpi_datatype_size(datatype);
729 smpi_op_apply(req->op, req->buf, req->old_buf, &n, &datatype);
732 smpi_comm_unuse(req->comm);
733 smpi_datatype_unuse(datatype);
737 if (TRACE_smpi_view_internals()) {
738 if(req->flags & RECV){
739 int rank = smpi_process_index();
740 int src_traced = (req->src == MPI_ANY_SOURCE ? req->real_src : req->src);
741 TRACE_smpi_recv(rank, src_traced, rank);
745 if(req->detached_sender!=NULL){
746 smpi_mpi_request_free(&(req->detached_sender));
748 if(req->flags & PERSISTENT)
750 req->flags |= FINISHED;
752 smpi_mpi_request_free(request);
756 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
759 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or smpi_mpi_testall before)
761 // to avoid deadlocks if used as a break condition, such as
762 // while (MPI_Test(request, flag, status) && flag) {
764 // because the time will not normally advance when only calls to MPI_Test
765 // are made -> deadlock
766 // multiplier to the sleeptime, to increase speed of execution, each failed test will increase it
767 static int nsleeps = 1;
768 if(smpi_test_sleep > 0) simcall_process_sleep(nsleeps*smpi_test_sleep);
770 smpi_empty_status(status);
772 if (!((*request)->flags & PREPARED)) {
773 if ((*request)->action != NULL)
774 flag = simcall_comm_test((*request)->action);
776 finish_wait(request, status);
777 nsleeps=1;//reset the number of sleeps we will do next time
778 if (*request != MPI_REQUEST_NULL && !((*request)->flags & PERSISTENT))
779 *request = MPI_REQUEST_NULL;
787 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
794 *index = MPI_UNDEFINED;
796 comms = xbt_dynar_new(sizeof(smx_synchro_t), NULL);
797 map = xbt_new(int, count);
799 for(i = 0; i < count; i++) {
800 if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->action &&
801 !(requests[i]->flags & PREPARED)) {
802 xbt_dynar_push(comms, &requests[i]->action);
808 //multiplier to the sleeptime, to increase speed of execution, each failed testany will increase it
809 static int nsleeps = 1;
810 if(smpi_test_sleep > 0) simcall_process_sleep(nsleeps*smpi_test_sleep);
812 i = simcall_comm_testany(comms);
813 // not MPI_UNDEFINED, as this is a simix return code
816 finish_wait(&requests[*index], status);
817 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags & NON_PERSISTENT))
818 requests[*index] = MPI_REQUEST_NULL;
825 //all requests are null or inactive, return true
827 smpi_empty_status(status);
830 xbt_dynar_free(&comms);
836 int smpi_mpi_testall(int count, MPI_Request requests[],
840 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
843 for(i=0; i<count; i++){
844 if (requests[i] != MPI_REQUEST_NULL && !(requests[i]->flags & PREPARED)) {
845 if (smpi_mpi_test(&requests[i], pstat)!=1){
848 requests[i]=MPI_REQUEST_NULL;
851 smpi_empty_status(pstat);
853 if(status != MPI_STATUSES_IGNORE) {
860 void smpi_mpi_probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
862 //FIXME find another wait to avoid busy waiting ?
863 // the issue here is that we have to wait on a nonexistent comm
865 smpi_mpi_iprobe(source, tag, comm, &flag, status);
866 XBT_DEBUG("Busy Waiting on probing : %d", flag);
870 void smpi_mpi_iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
872 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,
873 comm, PERSISTENT | RECV);
875 // to avoid deadlock, we have to sleep some time here, or the timer won't advance and we will only do iprobe simcalls
876 // (especially when used as a break condition, such as while(MPI_Iprobe(...)) ... )
877 // multiplier to the sleeptime, to increase speed of execution, each failed iprobe will increase it
878 static int nsleeps = 1;
879 if(smpi_iprobe_sleep > 0) simcall_process_sleep(nsleeps*smpi_iprobe_sleep);
880 // behave like a receive, but don't do it
883 print_request("New iprobe", request);
884 // We have to test both mailboxes as we don't know if we will receive one one or another
885 if (sg_cfg_get_int("smpi/async_small_thresh")>0){
886 mailbox = smpi_process_mailbox_small();
887 XBT_DEBUG("trying to probe the perm recv mailbox");
888 request->action = simcall_comm_iprobe(mailbox, 0, request->src, request->tag, &match_recv, (void*)request);
890 if (request->action==NULL){
891 mailbox = smpi_process_mailbox();
892 XBT_DEBUG("trying to probe the other mailbox");
893 request->action = simcall_comm_iprobe(mailbox, 0, request->src,request->tag, &match_recv, (void*)request);
896 if (request->action){
897 MPI_Request req = (MPI_Request)SIMIX_comm_get_src_data(request->action);
899 if(status != MPI_STATUS_IGNORE && !(req->flags & PREPARED)) {
900 status->MPI_SOURCE = smpi_group_rank(smpi_comm_group(comm), req->src);
901 status->MPI_TAG = req->tag;
902 status->MPI_ERROR = MPI_SUCCESS;
903 status->count = req->real_size;
905 nsleeps=1;//reset the number of sleeps we will do next time
911 smpi_mpi_request_free(&request);
916 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
918 print_request("Waiting", *request);
919 if ((*request)->flags & PREPARED) {
920 smpi_empty_status(status);
924 if ((*request)->action != NULL) { // this is not a detached send
925 simcall_comm_wait((*request)->action, -1.0);
927 if((MC_is_active() || MC_record_replay_is_active()) && (*request)->action)
928 (*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
931 finish_wait(request, status);
932 if (*request != MPI_REQUEST_NULL && ((*request)->flags & NON_PERSISTENT))
933 *request = MPI_REQUEST_NULL;
934 // FIXME for a detached send, finish_wait is not called:
937 int smpi_mpi_waitany(int count, MPI_Request requests[],
944 index = MPI_UNDEFINED;
946 // Wait for a request to complete
947 comms = xbt_dynar_new(sizeof(smx_synchro_t), NULL);
948 map = xbt_new(int, count);
950 XBT_DEBUG("Wait for one of %d", count);
951 for(i = 0; i < count; i++) {
952 if (requests[i] != MPI_REQUEST_NULL
953 && !(requests[i]->flags & PREPARED)
954 && !(requests[i]->flags & FINISHED)) {
955 if (requests[i]->action != NULL) {
956 XBT_DEBUG("Waiting any %p ", requests[i]);
957 xbt_dynar_push(comms, &requests[i]->action);
961 //This is a finished detached request, let's return this one
962 size=0;//so we free the dynar but don't do the waitany call
964 finish_wait(&requests[i], status);//cleanup if refcount = 0
965 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags & NON_PERSISTENT))
966 requests[i]=MPI_REQUEST_NULL;//set to null
972 i = simcall_comm_waitany(comms);
974 // not MPI_UNDEFINED, as this is a simix return code
977 finish_wait(&requests[index], status);
978 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags & NON_PERSISTENT))
979 requests[index] = MPI_REQUEST_NULL;
983 xbt_dynar_free(&comms);
986 if (index==MPI_UNDEFINED)
987 smpi_empty_status(status);
992 int smpi_mpi_waitall(int count, MPI_Request requests[],
997 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
998 int retvalue = MPI_SUCCESS;
999 //tag invalid requests in the set
1000 if (status != MPI_STATUSES_IGNORE) {
1001 for (c = 0; c < count; c++) {
1002 if (requests[c] == MPI_REQUEST_NULL || requests[c]->dst == MPI_PROC_NULL ||
1003 (requests[c]->flags & PREPARED)) {
1004 smpi_empty_status(&status[c]);
1005 } else if (requests[c]->src == MPI_PROC_NULL) {
1006 smpi_empty_status(&status[c]);
1007 status[c].MPI_SOURCE = MPI_PROC_NULL;
1011 for(c = 0; c < count; c++) {
1013 if (MC_is_active() || MC_record_replay_is_active()) {
1014 smpi_mpi_wait(&requests[c], pstat);
1017 index = smpi_mpi_waitany(count, requests, pstat);
1018 if (index == MPI_UNDEFINED)
1020 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags & NON_PERSISTENT))
1021 requests[index]=MPI_REQUEST_NULL;
1023 if (status != MPI_STATUSES_IGNORE) {
1024 status[index] = *pstat;
1025 if (status[index].MPI_ERROR == MPI_ERR_TRUNCATE)
1026 retvalue = MPI_ERR_IN_STATUS;
1033 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
1034 MPI_Status status[])
1036 int i, count, index;
1038 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
1041 for(i = 0; i < incount; i++)
1043 index=smpi_mpi_waitany(incount, requests, pstat);
1044 if(index!=MPI_UNDEFINED){
1045 indices[count] = index;
1047 if(status != MPI_STATUSES_IGNORE) {
1048 status[index] = *pstat;
1050 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags & NON_PERSISTENT))
1051 requests[index]=MPI_REQUEST_NULL;
1053 return MPI_UNDEFINED;
1059 int smpi_mpi_testsome(int incount, MPI_Request requests[], int *indices,
1060 MPI_Status status[])
1062 int i, count, count_dead;
1064 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
1068 for(i = 0; i < incount; i++) {
1069 if((requests[i] != MPI_REQUEST_NULL)) {
1070 if(smpi_mpi_test(&requests[i], pstat)) {
1073 if(status != MPI_STATUSES_IGNORE) {
1076 if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->flags & NON_PERSISTENT)
1077 requests[i]=MPI_REQUEST_NULL;
1083 if(count_dead==incount)return MPI_UNDEFINED;
1087 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
1090 smpi_coll_tuned_bcast_binomial_tree(buf, count, datatype, root, comm);
1093 void smpi_mpi_barrier(MPI_Comm comm)
1095 smpi_coll_tuned_barrier_ompi_basic_linear(comm);
1098 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1099 void *recvbuf, int recvcount, MPI_Datatype recvtype,
1100 int root, MPI_Comm comm)
1102 int system_tag = COLL_TAG_GATHER;
1103 int rank, size, src, index;
1104 MPI_Aint lb = 0, recvext = 0;
1105 MPI_Request *requests;
1107 rank = smpi_comm_rank(comm);
1108 size = smpi_comm_size(comm);
1110 // Send buffer to root
1111 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
1113 // FIXME: check for errors
1114 smpi_datatype_extent(recvtype, &lb, &recvext);
1115 // Local copy from root
1116 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1117 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
1118 // Receive buffers from senders
1119 requests = xbt_new(MPI_Request, size - 1);
1121 for(src = 0; src < size; src++) {
1123 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
1124 recvcount, recvtype,
1125 src, system_tag, comm);
1129 // Wait for completion of irecv's.
1130 smpi_mpi_startall(size - 1, requests);
1131 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1132 for(src = 0; src < size-1; src++) {
1133 smpi_mpi_request_free(&requests[src]);
1140 void smpi_mpi_reduce_scatter(void *sendbuf, void *recvbuf, int *recvcounts,
1141 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1145 int rank = smpi_process_index();
1148 /* arbitrarily choose root as rank 0 */
1149 size = smpi_comm_size(comm);
1151 displs = xbt_new(int, size);
1152 for (i = 0; i < size; i++) {
1154 count += recvcounts[i];
1156 tmpbuf=(void*)smpi_get_tmp_sendbuffer(count*smpi_datatype_get_extent(datatype));
1158 mpi_coll_reduce_fun(sendbuf, tmpbuf, count, datatype, op, 0, comm);
1159 smpi_mpi_scatterv(tmpbuf, recvcounts, displs, datatype, recvbuf,
1160 recvcounts[rank], datatype, 0, comm);
1162 smpi_free_tmp_buffer(tmpbuf);
1165 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1166 void *recvbuf, int *recvcounts, int *displs,
1167 MPI_Datatype recvtype, int root, MPI_Comm comm)
1169 int system_tag = COLL_TAG_GATHERV;
1170 int rank, size, src, index;
1171 MPI_Aint lb = 0, recvext = 0;
1172 MPI_Request *requests;
1174 rank = smpi_comm_rank(comm);
1175 size = smpi_comm_size(comm);
1177 // Send buffer to root
1178 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
1180 // FIXME: check for errors
1181 smpi_datatype_extent(recvtype, &lb, &recvext);
1182 // Local copy from root
1183 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1184 (char *)recvbuf + displs[root] * recvext,
1185 recvcounts[root], recvtype);
1186 // Receive buffers from senders
1187 requests = xbt_new(MPI_Request, size - 1);
1189 for(src = 0; src < size; src++) {
1192 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
1193 recvcounts[src], recvtype, src, system_tag, comm);
1197 // Wait for completion of irecv's.
1198 smpi_mpi_startall(size - 1, requests);
1199 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1200 for(src = 0; src < size-1; src++) {
1201 smpi_mpi_request_free(&requests[src]);
1207 void smpi_mpi_allgather(void *sendbuf, int sendcount,
1208 MPI_Datatype sendtype, void *recvbuf,
1209 int recvcount, MPI_Datatype recvtype,
1212 int system_tag = COLL_TAG_ALLGATHER;
1213 int rank, size, other, index;
1214 MPI_Aint lb = 0, recvext = 0;
1215 MPI_Request *requests;
1217 rank = smpi_comm_rank(comm);
1218 size = smpi_comm_size(comm);
1219 // FIXME: check for errors
1220 smpi_datatype_extent(recvtype, &lb, &recvext);
1221 // Local copy from self
1222 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1223 (char *)recvbuf + rank * recvcount * recvext, recvcount,
1225 // Send/Recv buffers to/from others;
1226 requests = xbt_new(MPI_Request, 2 * (size - 1));
1228 for(other = 0; other < size; other++) {
1231 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
1234 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
1235 recvcount, recvtype, other,
1240 // Wait for completion of all comms.
1241 smpi_mpi_startall(2 * (size - 1), requests);
1242 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
1243 for(other = 0; other < 2*(size-1); other++) {
1244 smpi_mpi_request_free(&requests[other]);
1249 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
1250 MPI_Datatype sendtype, void *recvbuf,
1251 int *recvcounts, int *displs,
1252 MPI_Datatype recvtype, MPI_Comm comm)
1254 int system_tag = COLL_TAG_ALLGATHERV;
1255 int rank, size, other, index;
1256 MPI_Aint lb = 0, recvext = 0;
1257 MPI_Request *requests;
1259 rank = smpi_comm_rank(comm);
1260 size = smpi_comm_size(comm);
1261 // FIXME: check for errors
1262 smpi_datatype_extent(recvtype, &lb, &recvext);
1263 // Local copy from self
1264 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1265 (char *)recvbuf + displs[rank] * recvext,
1266 recvcounts[rank], recvtype);
1267 // Send buffers to others;
1268 requests = xbt_new(MPI_Request, 2 * (size - 1));
1270 for(other = 0; other < size; other++) {
1273 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
1277 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
1278 recvtype, other, system_tag, comm);
1282 // Wait for completion of all comms.
1283 smpi_mpi_startall(2 * (size - 1), requests);
1284 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
1285 for(other = 0; other < 2*(size-1); other++) {
1286 smpi_mpi_request_free(&requests[other]);
1291 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1292 void *recvbuf, int recvcount, MPI_Datatype recvtype,
1293 int root, MPI_Comm comm)
1295 int system_tag = COLL_TAG_SCATTER;
1296 int rank, size, dst, index;
1297 MPI_Aint lb = 0, sendext = 0;
1298 MPI_Request *requests;
1300 rank = smpi_comm_rank(comm);
1301 size = smpi_comm_size(comm);
1303 // Recv buffer from root
1304 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
1307 // FIXME: check for errors
1308 smpi_datatype_extent(sendtype, &lb, &sendext);
1309 // Local copy from root
1310 if(recvbuf!=MPI_IN_PLACE){
1311 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
1312 sendcount, sendtype, recvbuf, recvcount, recvtype);
1314 // Send buffers to receivers
1315 requests = xbt_new(MPI_Request, size - 1);
1317 for(dst = 0; dst < size; dst++) {
1319 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
1320 sendcount, sendtype, dst,
1325 // Wait for completion of isend's.
1326 smpi_mpi_startall(size - 1, requests);
1327 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1328 for(dst = 0; dst < size-1; dst++) {
1329 smpi_mpi_request_free(&requests[dst]);
1335 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
1336 MPI_Datatype sendtype, void *recvbuf, int recvcount,
1337 MPI_Datatype recvtype, int root, MPI_Comm comm)
1339 int system_tag = COLL_TAG_SCATTERV;
1340 int rank, size, dst, index;
1341 MPI_Aint lb = 0, sendext = 0;
1342 MPI_Request *requests;
1344 rank = smpi_comm_rank(comm);
1345 size = smpi_comm_size(comm);
1347 // Recv buffer from root
1348 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
1351 // FIXME: check for errors
1352 smpi_datatype_extent(sendtype, &lb, &sendext);
1353 // Local copy from root
1354 if(recvbuf!=MPI_IN_PLACE){
1355 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
1356 sendtype, recvbuf, recvcount, recvtype);
1358 // Send buffers to receivers
1359 requests = xbt_new(MPI_Request, size - 1);
1361 for(dst = 0; dst < size; dst++) {
1364 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
1365 sendtype, dst, system_tag, comm);
1369 // Wait for completion of isend's.
1370 smpi_mpi_startall(size - 1, requests);
1371 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1372 for(dst = 0; dst < size-1; dst++) {
1373 smpi_mpi_request_free(&requests[dst]);
1379 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
1380 MPI_Datatype datatype, MPI_Op op, int root,
1383 int system_tag = COLL_TAG_REDUCE;
1384 int rank, size, src, index;
1385 MPI_Aint lb = 0, dataext = 0;
1386 MPI_Request *requests;
1390 char* sendtmpbuf = (char*) sendbuf;
1391 if( sendbuf == MPI_IN_PLACE ) {
1392 sendtmpbuf = (char *)smpi_get_tmp_sendbuffer(count*smpi_datatype_get_extent(datatype));
1393 smpi_datatype_copy(recvbuf, count, datatype,sendtmpbuf, count, datatype);
1396 rank = smpi_comm_rank(comm);
1397 size = smpi_comm_size(comm);
1398 //non commutative case, use a working algo from openmpi
1399 if(!smpi_op_is_commute(op)){
1400 smpi_coll_tuned_reduce_ompi_basic_linear(sendtmpbuf, recvbuf, count,
1401 datatype, op, root, comm);
1406 // Send buffer to root
1407 smpi_mpi_send(sendtmpbuf, count, datatype, root, system_tag, comm);
1409 // FIXME: check for errors
1410 smpi_datatype_extent(datatype, &lb, &dataext);
1411 // Local copy from root
1412 if (sendtmpbuf && recvbuf)
1413 smpi_datatype_copy(sendtmpbuf, count, datatype, recvbuf, count, datatype);
1414 // Receive buffers from senders
1415 //TODO: make a MPI_barrier here ?
1416 requests = xbt_new(MPI_Request, size - 1);
1417 tmpbufs = xbt_new(void *, size - 1);
1419 for(src = 0; src < size; src++) {
1421 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1423 if (!smpi_process_get_replaying())
1424 tmpbufs[index] = xbt_malloc(count * dataext);
1426 tmpbufs[index] = smpi_get_tmp_sendbuffer(count * dataext);
1428 smpi_irecv_init(tmpbufs[index], count, datatype, src,
1433 // Wait for completion of irecv's.
1434 smpi_mpi_startall(size - 1, requests);
1435 for(src = 0; src < size - 1; src++) {
1436 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1437 XBT_DEBUG("finished waiting any request with index %d", index);
1438 if(index == MPI_UNDEFINED) {
1441 smpi_mpi_request_free(&requests[index]);
1443 if(op) /* op can be MPI_OP_NULL that does nothing */
1444 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1446 for(index = 0; index < size - 1; index++) {
1447 smpi_free_tmp_buffer(tmpbufs[index]);
1452 if( sendbuf == MPI_IN_PLACE ) {
1453 smpi_free_tmp_buffer(sendtmpbuf);
1458 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
1459 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1461 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
1462 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
1465 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
1466 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1468 int system_tag = -888;
1469 int rank, size, other, index;
1470 MPI_Aint lb = 0, dataext = 0;
1471 MPI_Request *requests;
1474 rank = smpi_comm_rank(comm);
1475 size = smpi_comm_size(comm);
1477 // FIXME: check for errors
1478 smpi_datatype_extent(datatype, &lb, &dataext);
1480 // Local copy from self
1481 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
1483 // Send/Recv buffers to/from others;
1484 requests = xbt_new(MPI_Request, size - 1);
1485 tmpbufs = xbt_new(void *, rank);
1487 for(other = 0; other < rank; other++) {
1488 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1490 tmpbufs[index] = smpi_get_tmp_sendbuffer(count * dataext);
1492 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
1496 for(other = rank + 1; other < size; other++) {
1498 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1501 // Wait for completion of all comms.
1502 smpi_mpi_startall(size - 1, requests);
1504 if(smpi_op_is_commute(op)){
1505 for(other = 0; other < size - 1; other++) {
1506 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1507 if(index == MPI_UNDEFINED) {
1511 // #Request is below rank: it's a irecv
1512 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1516 //non commutative case, wait in order
1517 for(other = 0; other < size - 1; other++) {
1518 smpi_mpi_wait(&(requests[other]), MPI_STATUS_IGNORE);
1520 smpi_op_apply(op, tmpbufs[other], recvbuf, &count, &datatype);
1524 for(index = 0; index < rank; index++) {
1525 smpi_free_tmp_buffer(tmpbufs[index]);
1527 for(index = 0; index < size-1; index++) {
1528 smpi_mpi_request_free(&requests[index]);
1534 void smpi_mpi_exscan(void *sendbuf, void *recvbuf, int count,
1535 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1537 int system_tag = -888;
1538 int rank, size, other, index;
1539 MPI_Aint lb = 0, dataext = 0;
1540 MPI_Request *requests;
1542 int recvbuf_is_empty=1;
1543 rank = smpi_comm_rank(comm);
1544 size = smpi_comm_size(comm);
1546 // FIXME: check for errors
1547 smpi_datatype_extent(datatype, &lb, &dataext);
1549 // Send/Recv buffers to/from others;
1550 requests = xbt_new(MPI_Request, size - 1);
1551 tmpbufs = xbt_new(void *, rank);
1553 for(other = 0; other < rank; other++) {
1554 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1556 tmpbufs[index] = smpi_get_tmp_sendbuffer(count * dataext);
1558 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
1562 for(other = rank + 1; other < size; other++) {
1564 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1567 // Wait for completion of all comms.
1568 smpi_mpi_startall(size - 1, requests);
1569 if(smpi_op_is_commute(op)){
1570 for(other = 0; other < size - 1; other++) {
1571 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1572 if(index == MPI_UNDEFINED) {
1576 if(recvbuf_is_empty){
1577 smpi_datatype_copy(tmpbufs[index], count, datatype, recvbuf, count, datatype);
1580 // #Request is below rank: it's a irecv
1581 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1585 //non commutative case, wait in order
1586 for(other = 0; other < size - 1; other++) {
1587 smpi_mpi_wait(&(requests[other]), MPI_STATUS_IGNORE);
1589 if(recvbuf_is_empty){
1590 smpi_datatype_copy(tmpbufs[other], count, datatype, recvbuf, count, datatype);
1592 }else smpi_op_apply(op, tmpbufs[other], recvbuf, &count, &datatype);
1596 for(index = 0; index < rank; index++) {
1597 smpi_free_tmp_buffer(tmpbufs[index]);
1599 for(index = 0; index < size-1; index++) {
1600 smpi_mpi_request_free(&requests[index]);