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. */
7 #include <xbt/config.hpp>
8 #include <boost/tokenizer.hpp>
12 #include "xbt/virtu.h"
14 #include "src/mc/mc_replay.h"
15 #include "xbt/replay.h"
17 #include "src/simix/smx_private.h"
18 #include "surf/surf.h"
19 #include "simgrid/sg_config.h"
20 #include "colls/colls.h"
22 #include "src/kernel/activity/SynchroComm.hpp"
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi, "Logging specific to SMPI (base)");
26 static int match_recv(void* a, void* b, smx_activity_t ignored) {
27 MPI_Request ref = static_cast<MPI_Request>(a);
28 MPI_Request req = static_cast<MPI_Request>(b);
29 XBT_DEBUG("Trying to match a recv of src %d against %d, tag %d against %d",ref->src,req->src, ref->tag, req->tag);
31 xbt_assert(ref, "Cannot match recv against null reference");
32 xbt_assert(req, "Cannot match recv against null request");
33 if((ref->src == MPI_ANY_SOURCE || req->src == ref->src)
34 && ((ref->tag == MPI_ANY_TAG && req->tag >=0) || req->tag == ref->tag)){
35 //we match, we can transfer some values
36 if(ref->src == MPI_ANY_SOURCE)
37 ref->real_src = req->src;
38 if(ref->tag == MPI_ANY_TAG)
39 ref->real_tag = req->tag;
40 if(ref->real_size < req->real_size)
43 ref->detached_sender=req; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
44 XBT_DEBUG("match succeeded");
49 static int match_send(void* a, void* b,smx_activity_t ignored) {
50 MPI_Request ref = static_cast<MPI_Request>(a);
51 MPI_Request req = static_cast<MPI_Request>(b);
52 XBT_DEBUG("Trying to match a send of src %d against %d, tag %d against %d",ref->src,req->src, ref->tag, req->tag);
53 xbt_assert(ref, "Cannot match send against null reference");
54 xbt_assert(req, "Cannot match send against null request");
56 if((req->src == MPI_ANY_SOURCE || req->src == ref->src)
57 && ((req->tag == MPI_ANY_TAG && ref->tag >=0)|| req->tag == ref->tag))
59 if(req->src == MPI_ANY_SOURCE)
60 req->real_src = ref->src;
61 if(req->tag == MPI_ANY_TAG)
62 req->real_tag = ref->tag;
63 if(req->real_size < ref->real_size)
66 req->detached_sender=ref; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
67 XBT_DEBUG("match succeeded");
72 // Methods used to parse and store the values for timing injections in smpi
73 // These are taken from surf/network.c and generalized to have more values for each factor
74 typedef struct s_smpi_factor_multival *smpi_os_factor_multival_t;
75 typedef struct s_smpi_factor_multival { // FIXME: this should be merged (deduplicated) with s_smpi_factor defined in network_smpi.c
77 std::vector<double> values;
78 } s_smpi_factor_multival_t;
80 std::vector<s_smpi_factor_multival_t> smpi_os_values;
81 std::vector<s_smpi_factor_multival_t> smpi_or_values;
82 std::vector<s_smpi_factor_multival_t> smpi_ois_values;
84 static simgrid::config::Flag<double> smpi_wtime_sleep(
85 "smpi/wtime", "Minimum time to inject inside a call to MPI_Wtime", 0.0);
86 static simgrid::config::Flag<double> smpi_init_sleep(
87 "smpi/init", "Time to inject inside a call to MPI_Init", 0.0);
88 static simgrid::config::Flag<double> smpi_iprobe_sleep(
89 "smpi/iprobe", "Minimum time to inject inside a call to MPI_Iprobe", 1e-4);
90 static simgrid::config::Flag<double> smpi_test_sleep(
91 "smpi/test", "Minimum time to inject inside a call to MPI_Test", 1e-4);
93 static std::vector<s_smpi_factor_multival_t> parse_factor(const char *smpi_coef_string)
95 std::vector<s_smpi_factor_multival_t> smpi_factor;
97 /** Setup the tokenizer that parses the string **/
98 typedef boost::tokenizer<boost::char_separator<char>> Tokenizer;
99 boost::char_separator<char> sep(";");
100 boost::char_separator<char> factor_separator(":");
101 std::string tmp_string(smpi_coef_string);
102 Tokenizer tokens(tmp_string, sep);
105 * Iterate over patterns like A:B:C:D;E:F;G:H
106 * These will be broken down into:
111 for (Tokenizer::iterator token_iter = tokens.begin();
112 token_iter != tokens.end(); token_iter++) {
113 XBT_DEBUG("token : %s", token_iter->c_str());
114 Tokenizer factor_values(*token_iter, factor_separator);
115 s_smpi_factor_multival_t fact;
116 if (factor_values.begin() == factor_values.end()) {
117 xbt_die("Malformed radical for smpi factor: '%s'", smpi_coef_string);
119 unsigned int iteration = 0;
120 for (Tokenizer::iterator factor_iter = factor_values.begin();
121 factor_iter != factor_values.end(); factor_iter++, iteration++) {
124 if (factor_iter == factor_values.begin()) { /* first element */
125 errmsg = bprintf("Invalid factor in chunk #%zu: %%s", smpi_factor.size()+1);
126 fact.factor = xbt_str_parse_int(factor_iter->c_str(), errmsg);
129 errmsg = bprintf("Invalid factor value %d in chunk #%zu: %%s", iteration, smpi_factor.size()+1);
130 fact.values.push_back(xbt_str_parse_double(factor_iter->c_str(), errmsg));
135 smpi_factor.push_back(fact);
136 XBT_DEBUG("smpi_factor:\t%zu : %zu values, first: %f", fact.factor, smpi_factor.size(), fact.values[0]);
138 std::sort(smpi_factor.begin(), smpi_factor.end(),
139 [](const s_smpi_factor_multival_t &pa,
140 const s_smpi_factor_multival_t &pb) {
141 return (pa.factor < pb.factor);
143 for (auto& fact : smpi_factor) {
144 XBT_DEBUG("smpi_factor:\t%zu : %zu values, first: %f", fact.factor, smpi_factor.size() ,fact.values[0]);
150 static double smpi_os(size_t size)
152 if (smpi_os_values.empty()) {
153 smpi_os_values = parse_factor(xbt_cfg_get_string("smpi/os"));
155 double current=smpi_os_values.empty()?0.0:smpi_os_values[0].values[0]+smpi_os_values[0].values[1]*size;
156 // Iterate over all the sections that were specified and find the right
157 // value. (fact.factor represents the interval sizes; we want to find the
158 // section that has fact.factor <= size and no other such fact.factor <= size)
159 // Note: parse_factor() (used before) already sorts the vector we iterate over!
160 for (auto& fact : smpi_os_values) {
161 if (size <= fact.factor) { // Values already too large, use the previously
162 // computed value of current!
163 XBT_DEBUG("os : %zu <= %zu return %.10f", size, fact.factor, current);
166 // If the next section is too large, the current section must be used.
167 // Hence, save the cost, as we might have to use it.
168 current = fact.values[0]+fact.values[1]*size;
171 XBT_DEBUG("Searching for smpi/os: %zu is larger than the largest boundary, return %.10f", size, current);
176 static double smpi_ois(size_t size)
178 if (smpi_ois_values.empty()) {
179 smpi_ois_values = parse_factor(xbt_cfg_get_string("smpi/ois"));
181 double current=smpi_ois_values.empty()?0.0:smpi_ois_values[0].values[0]+smpi_ois_values[0].values[1]*size;
182 // Iterate over all the sections that were specified and find the right value. (fact.factor represents the interval
183 // sizes; we want to find the section that has fact.factor <= size and no other such fact.factor <= size)
184 // Note: parse_factor() (used before) already sorts the vector we iterate over!
185 for (auto& fact : smpi_ois_values) {
186 if (size <= fact.factor) { // Values already too large, use the previously computed value of current!
187 XBT_DEBUG("ois : %zu <= %zu return %.10f", size, fact.factor, current);
190 // If the next section is too large, the current section must be used.
191 // Hence, save the cost, as we might have to use it.
192 current = fact.values[0]+fact.values[1]*size;
195 XBT_DEBUG("Searching for smpi/ois: %zu is larger than the largest boundary, return %.10f", size, current);
200 static double smpi_or(size_t size)
202 if (smpi_or_values.empty()) {
203 smpi_or_values = parse_factor(xbt_cfg_get_string("smpi/or"));
206 double current=smpi_or_values.empty()?0.0:smpi_or_values.front().values[0]+smpi_or_values.front().values[1]*size;
208 // Iterate over all the sections that were specified and find the right value. (fact.factor represents the interval
209 // sizes; we want to find the section that has fact.factor <= size and no other such fact.factor <= size)
210 // Note: parse_factor() (used before) already sorts the vector we iterate over!
211 for (auto fact : smpi_or_values) {
212 if (size <= fact.factor) { // Values already too large, use the previously computed value of current!
213 XBT_DEBUG("or : %zu <= %zu return %.10f", size, fact.factor, current);
216 // If the next section is too large, the current section must be used.
217 // Hence, save the cost, as we might have to use it.
218 current=fact.values[0]+fact.values[1]*size;
221 XBT_DEBUG("smpi_or: %zu is larger than largest boundary, return %.10f", size, current);
226 void smpi_mpi_init() {
227 if(smpi_init_sleep > 0)
228 simcall_process_sleep(smpi_init_sleep);
231 double smpi_mpi_wtime(){
233 if (smpi_process_initialized() != 0 && smpi_process_finalized() == 0 && smpi_process_get_sampling() == 0) {
235 time = SIMIX_get_clock();
236 // to avoid deadlocks if used as a break condition, such as
237 // while (MPI_Wtime(...) < time_limit) {
240 // because the time will not normally advance when only calls to MPI_Wtime
241 // are made -> deadlock (MPI_Wtime never reaches the time limit)
242 if(smpi_wtime_sleep > 0)
243 simcall_process_sleep(smpi_wtime_sleep);
246 time = SIMIX_get_clock();
251 static MPI_Request build_request(void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
254 MPI_Request request = nullptr;
256 void *old_buf = nullptr;
258 request = xbt_new(s_smpi_mpi_request_t, 1);
260 s_smpi_subtype_t *subtype = static_cast<s_smpi_subtype_t*>(datatype->substruct);
262 if((((flags & RECV) != 0) && ((flags & ACCUMULATE) !=0)) || (datatype->sizeof_substruct != 0)){
263 // This part handles the problem of non-contiguous memory
265 buf = count==0 ? nullptr : xbt_malloc(count*smpi_datatype_size(datatype));
266 if ((datatype->sizeof_substruct != 0) && ((flags & SEND) != 0)) {
267 subtype->serialize(old_buf, buf, count, datatype->substruct);
272 // This part handles the problem of non-contiguous memory (for the unserialisation at the reception)
273 request->old_buf = old_buf;
274 request->old_type = datatype;
276 request->size = smpi_datatype_size(datatype) * count;
277 smpi_datatype_use(datatype);
281 request->comm = comm;
282 smpi_comm_use(request->comm);
283 request->action = nullptr;
284 request->flags = flags;
285 request->detached = 0;
286 request->detached_sender = nullptr;
287 request->real_src = 0;
288 request->truncated = 0;
289 request->real_size = 0;
290 request->real_tag = 0;
291 if (flags & PERSISTENT)
292 request->refcount = 1;
294 request->refcount = 0;
295 request->op = MPI_REPLACE;
302 void smpi_empty_status(MPI_Status * status)
304 if(status != MPI_STATUS_IGNORE) {
305 status->MPI_SOURCE = MPI_ANY_SOURCE;
306 status->MPI_TAG = MPI_ANY_TAG;
307 status->MPI_ERROR = MPI_SUCCESS;
312 static void smpi_mpi_request_free_voidp(void* request)
314 MPI_Request req = static_cast<MPI_Request>(request);
315 smpi_mpi_request_free(&req);
318 /* MPI Low level calls */
319 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
320 int dst, int tag, MPI_Comm comm)
322 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
323 request = build_request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype, smpi_process_index(),
324 smpi_group_index(smpi_comm_group(comm), dst), tag, comm, PERSISTENT | SEND | PREPARED);
328 MPI_Request smpi_mpi_ssend_init(void *buf, int count, MPI_Datatype datatype,
329 int dst, int tag, MPI_Comm comm)
331 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
332 request = build_request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype, smpi_process_index(),
333 smpi_group_index(smpi_comm_group(comm), dst), tag, comm, PERSISTENT | SSEND | SEND | PREPARED);
337 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
338 int src, int tag, MPI_Comm comm)
340 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
341 request = build_request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype,
342 src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : smpi_group_index(smpi_comm_group(comm), src),
343 smpi_process_index(), tag, comm, PERSISTENT | RECV | PREPARED);
347 void smpi_mpi_start(MPI_Request request)
349 smx_mailbox_t mailbox;
351 xbt_assert(request->action == nullptr, "Cannot (re-)start unfinished communication");
352 request->flags &= ~PREPARED;
353 request->flags &= ~FINISHED;
356 if ((request->flags & RECV) != 0) {
357 print_request("New recv", request);
359 int async_small_thresh = xbt_cfg_get_int("smpi/async-small-thresh");
361 xbt_mutex_t mut = smpi_process_mailboxes_mutex();
362 if (async_small_thresh != 0 || (request->flags & RMA) != 0)
363 xbt_mutex_acquire(mut);
365 if (async_small_thresh == 0 && (request->flags & RMA) == 0 ) {
366 mailbox = smpi_process_mailbox();
368 else if (((request->flags & RMA) != 0) || static_cast<int>(request->size) < async_small_thresh) {
369 //We have to check both mailboxes (because SSEND messages are sent to the large mbox).
370 //begin with the more appropriate one : the small one.
371 mailbox = smpi_process_mailbox_small();
372 XBT_DEBUG("Is there a corresponding send already posted in the small mailbox %p (in case of SSEND)?", mailbox);
373 smx_activity_t action = simcall_comm_iprobe(mailbox, 0, request->src,request->tag, &match_recv, static_cast<void*>(request));
375 if (action == nullptr) {
376 mailbox = smpi_process_mailbox();
377 XBT_DEBUG("No, nothing in the small mailbox test the other one : %p", mailbox);
378 action = simcall_comm_iprobe(mailbox, 0, request->src,request->tag, &match_recv, static_cast<void*>(request));
379 if (action == nullptr) {
380 XBT_DEBUG("Still nothing, switch back to the small mailbox : %p", mailbox);
381 mailbox = smpi_process_mailbox_small();
385 XBT_DEBUG("yes there was something for us in the large mailbox");
389 mailbox = smpi_process_mailbox_small();
390 XBT_DEBUG("Is there a corresponding send already posted the small mailbox?");
391 smx_activity_t action = simcall_comm_iprobe(mailbox, 0, request->src,request->tag, &match_recv, (void*)request);
393 if (action == nullptr) {
394 XBT_DEBUG("No, nothing in the permanent receive mailbox");
395 mailbox = smpi_process_mailbox();
398 XBT_DEBUG("yes there was something for us in the small mailbox");
402 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
403 request->real_size=request->size;
404 request->action = simcall_comm_irecv(SIMIX_process_self(), mailbox, request->buf, &request->real_size, &match_recv,
405 ! smpi_process_get_replaying()? &smpi_comm_copy_buffer_callback
406 : &smpi_comm_null_copy_buffer_callback, request, -1.0);
407 XBT_DEBUG("recv simcall posted");
409 if (async_small_thresh != 0 || (request->flags & RMA) != 0 )
410 xbt_mutex_release(mut);
412 else { /* the RECV flag was not set, so this is a send */
413 int receiver = request->dst;
415 int rank = request->src;
416 if (TRACE_smpi_view_internals()) {
417 TRACE_smpi_send(rank, rank, receiver,request->size);
419 print_request("New send", request);
421 void* buf = request->buf;
422 if ( (request->flags & SSEND) == 0
423 && ( (request->flags & RMA) != 0 || static_cast<int>(request->size) < xbt_cfg_get_int("smpi/send-is-detached-thresh") ) ) {
424 void *oldbuf = nullptr;
425 request->detached = 1;
426 XBT_DEBUG("Send request %p is detached", request);
428 if(request->old_type->sizeof_substruct == 0){
429 oldbuf = request->buf;
430 if (!smpi_process_get_replaying() && oldbuf != nullptr && request->size!=0){
431 if((smpi_privatize_global_variables != 0)
432 && (static_cast<char*>(request->buf) >= smpi_start_data_exe)
433 && (static_cast<char*>(request->buf) < smpi_start_data_exe + smpi_size_data_exe )){
434 XBT_DEBUG("Privatization : We are sending from a zone inside global memory. Switch data segment ");
435 smpi_switch_data_segment(request->src);
437 buf = xbt_malloc(request->size);
438 memcpy(buf,oldbuf,request->size);
439 XBT_DEBUG("buf %p copied into %p",oldbuf,buf);
444 //if we are giving back the control to the user without waiting for completion, we have to inject timings
445 double sleeptime = 0.0;
446 if(request->detached != 0 || ((request->flags & (ISEND|SSEND)) != 0)){// issend should be treated as isend
447 //isend and send timings may be different
448 sleeptime = ((request->flags & ISEND) != 0) ? smpi_ois(request->size) : smpi_os(request->size);
452 simcall_process_sleep(sleeptime);
453 XBT_DEBUG("sending size of %zu : sleep %f ", request->size, sleeptime);
456 int async_small_thresh = xbt_cfg_get_int("smpi/async-small-thresh");
458 xbt_mutex_t mut=smpi_process_remote_mailboxes_mutex(receiver);
460 if (async_small_thresh != 0 || (request->flags & RMA) != 0)
461 xbt_mutex_acquire(mut);
463 if (!(async_small_thresh != 0 || (request->flags & RMA) !=0)) {
464 mailbox = smpi_process_remote_mailbox(receiver);
466 else if (((request->flags & RMA) != 0) || static_cast<int>(request->size) < async_small_thresh) { // eager mode
467 mailbox = smpi_process_remote_mailbox(receiver);
468 XBT_DEBUG("Is there a corresponding recv already posted in the large mailbox %p?", mailbox);
469 smx_activity_t action = simcall_comm_iprobe(mailbox, 1,request->dst, request->tag, &match_send, static_cast<void*>(request));
470 if (action == nullptr) {
471 if ((request->flags & SSEND) == 0){
472 mailbox = smpi_process_remote_mailbox_small(receiver);
473 XBT_DEBUG("No, nothing in the large mailbox, message is to be sent on the small one %p", mailbox);
476 mailbox = smpi_process_remote_mailbox_small(receiver);
477 XBT_DEBUG("SSEND : Is there a corresponding recv already posted in the small mailbox %p?", mailbox);
478 action = simcall_comm_iprobe(mailbox, 1,request->dst, request->tag, &match_send, static_cast<void*>(request));
479 if (action == nullptr) {
480 XBT_DEBUG("No, we are first, send to large mailbox");
481 mailbox = smpi_process_remote_mailbox(receiver);
486 XBT_DEBUG("Yes there was something for us in the large mailbox");
490 mailbox = smpi_process_remote_mailbox(receiver);
491 XBT_DEBUG("Send request %p is in the large mailbox %p (buf: %p)",mailbox, request,request->buf);
494 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
495 request->real_size=request->size;
496 request->action = simcall_comm_isend(SIMIX_process_from_PID(request->src+1), mailbox, request->size, -1.0,
497 buf, request->real_size, &match_send,
498 &xbt_free_f, // how to free the userdata if a detached send fails
499 !smpi_process_get_replaying() ? &smpi_comm_copy_buffer_callback
500 : &smpi_comm_null_copy_buffer_callback, request,
501 // detach if msg size < eager/rdv switch limit
503 XBT_DEBUG("send simcall posted");
505 /* FIXME: detached sends are not traceable (request->action == nullptr) */
506 if (request->action != nullptr)
507 simcall_set_category(request->action, TRACE_internal_smpi_get_category());
509 if (async_small_thresh != 0 || ((request->flags & RMA)!=0))
510 xbt_mutex_release(mut);
514 void smpi_mpi_startall(int count, MPI_Request * requests)
516 if(requests== nullptr)
519 for(int i = 0; i < count; i++) {
520 smpi_mpi_start(requests[i]);
524 void smpi_mpi_request_free(MPI_Request * request)
526 if((*request) != MPI_REQUEST_NULL){
527 (*request)->refcount--;
528 if((*request)->refcount<0) xbt_die("wrong refcount");
530 if((*request)->refcount==0){
531 smpi_datatype_unuse((*request)->old_type);
532 smpi_comm_unuse((*request)->comm);
533 print_request("Destroying", (*request));
535 *request = MPI_REQUEST_NULL;
537 print_request("Decrementing", (*request));
540 xbt_die("freeing an already free request");
544 MPI_Request smpi_rma_send_init(void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
547 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
549 request = build_request(buf==MPI_BOTTOM ? nullptr : buf , count, datatype, src, dst, tag,
550 comm, RMA | NON_PERSISTENT | ISEND | SEND | PREPARED);
552 request = build_request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype, src, dst, tag,
553 comm, RMA | NON_PERSISTENT | ISEND | SEND | PREPARED | ACCUMULATE);
559 MPI_Request smpi_rma_recv_init(void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
562 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
564 request = build_request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype, src, dst, tag,
565 comm, RMA | NON_PERSISTENT | RECV | PREPARED);
567 request = build_request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype, src, dst, tag,
568 comm, RMA | NON_PERSISTENT | RECV | PREPARED | ACCUMULATE);
574 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
576 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
577 request = build_request(buf==MPI_BOTTOM ? nullptr : buf , count, datatype, smpi_process_index(),
578 smpi_group_index(smpi_comm_group(comm), dst), tag,comm, PERSISTENT | ISEND | SEND | PREPARED);
582 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
584 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
585 request = build_request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype, smpi_process_index(),
586 smpi_group_index(smpi_comm_group(comm), dst), tag, comm, NON_PERSISTENT | ISEND | SEND);
587 smpi_mpi_start(request);
591 MPI_Request smpi_mpi_issend(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
593 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
594 request = build_request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype, smpi_process_index(),
595 smpi_group_index(smpi_comm_group(comm), dst), tag,comm, NON_PERSISTENT | ISEND | SSEND | SEND);
596 smpi_mpi_start(request);
600 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
602 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
603 request = build_request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype, src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE :
604 smpi_group_index(smpi_comm_group(comm), src), smpi_process_index(), tag,
605 comm, PERSISTENT | RECV | PREPARED);
609 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
611 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
612 request = build_request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype, src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE :
613 smpi_group_index(smpi_comm_group(comm), src), smpi_process_index(), tag, comm,
614 NON_PERSISTENT | RECV);
615 smpi_mpi_start(request);
619 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status * status)
621 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
622 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
623 smpi_mpi_wait(&request, status);
627 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
629 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
630 request = build_request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype, smpi_process_index(),
631 smpi_group_index(smpi_comm_group(comm), dst), tag, comm, NON_PERSISTENT | SEND);
633 smpi_mpi_start(request);
634 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
638 void smpi_mpi_ssend(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
640 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
641 request = build_request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype, smpi_process_index(),
642 smpi_group_index(smpi_comm_group(comm), dst), tag, comm, NON_PERSISTENT | SSEND | SEND);
644 smpi_mpi_start(request);
645 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
649 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,int dst, int sendtag,
650 void *recvbuf, int recvcount, MPI_Datatype recvtype, int src, int recvtag,
651 MPI_Comm comm, MPI_Status * status)
653 MPI_Request requests[2];
655 int myid=smpi_process_index();
656 if ((smpi_group_index(smpi_comm_group(comm), dst) == myid) && (smpi_group_index(smpi_comm_group(comm), src) == myid)){
657 smpi_datatype_copy(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
660 requests[0] = smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
661 requests[1] = smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
662 smpi_mpi_startall(2, requests);
663 smpi_mpi_waitall(2, requests, stats);
664 smpi_mpi_request_free(&requests[0]);
665 smpi_mpi_request_free(&requests[1]);
666 if(status != MPI_STATUS_IGNORE) {
667 // Copy receive status
672 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
674 return status->count / smpi_datatype_size(datatype);
677 static void finish_wait(MPI_Request * request, MPI_Status * status)
679 MPI_Request req = *request;
680 smpi_empty_status(status);
682 if(!((req->detached != 0) && ((req->flags & SEND) != 0)) && ((req->flags & PREPARED) == 0)){
683 if(status != MPI_STATUS_IGNORE) {
684 int src = req->src == MPI_ANY_SOURCE ? req->real_src : req->src;
685 status->MPI_SOURCE = smpi_group_rank(smpi_comm_group(req->comm), src);
686 status->MPI_TAG = req->tag == MPI_ANY_TAG ? req->real_tag : req->tag;
687 status->MPI_ERROR = req->truncated != 0 ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
688 // this handles the case were size in receive differs from size in send
689 status->count = req->real_size;
692 print_request("Finishing", req);
693 MPI_Datatype datatype = req->old_type;
695 if(((req->flags & ACCUMULATE) != 0) || (datatype->sizeof_substruct != 0)){
696 if (!smpi_process_get_replaying()){
697 if( smpi_privatize_global_variables != 0 && (static_cast<char*>(req->old_buf) >= smpi_start_data_exe)
698 && ((char*)req->old_buf < smpi_start_data_exe + smpi_size_data_exe )){
699 XBT_VERB("Privatization : We are unserializing to a zone in global memory - Switch data segment ");
700 smpi_switch_data_segment(smpi_process_index());
704 if(datatype->sizeof_substruct != 0){
705 // This part handles the problem of non-contignous memory the unserialization at the reception
706 s_smpi_subtype_t *subtype = static_cast<s_smpi_subtype_t*>(datatype->substruct);
707 if(req->flags & RECV)
708 subtype->unserialize(req->buf, req->old_buf, req->real_size/smpi_datatype_size(datatype) ,
709 datatype->substruct, req->op);
711 }else if(req->flags & RECV){//apply op on contiguous buffer for accumulate
712 int n =req->real_size/smpi_datatype_size(datatype);
713 smpi_op_apply(req->op, req->buf, req->old_buf, &n, &datatype);
719 if (TRACE_smpi_view_internals() && ((req->flags & RECV) != 0)){
720 int rank = smpi_process_index();
721 int src_traced = (req->src == MPI_ANY_SOURCE ? req->real_src : req->src);
722 TRACE_smpi_recv(rank, src_traced, rank);
725 if(req->detached_sender != nullptr){
727 //integrate pseudo-timing for buffering of small messages, do not bother to execute the simcall if 0
728 double sleeptime = smpi_or(req->real_size);
730 simcall_process_sleep(sleeptime);
731 XBT_DEBUG("receiving size of %zu : sleep %f ", req->real_size, sleeptime);
733 smpi_mpi_request_free(&(req->detached_sender));
735 if(req->flags & PERSISTENT)
736 req->action = nullptr;
737 req->flags |= FINISHED;
739 smpi_mpi_request_free(request);
742 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
743 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or smpi_mpi_testall before)
745 // to avoid deadlocks if used as a break condition, such as
746 // while (MPI_Test(request, flag, status) && flag) {
748 // because the time will not normally advance when only calls to MPI_Test are made -> deadlock
749 // multiplier to the sleeptime, to increase speed of execution, each failed test will increase it
750 static int nsleeps = 1;
751 if(smpi_test_sleep > 0)
752 simcall_process_sleep(nsleeps*smpi_test_sleep);
754 smpi_empty_status(status);
756 if (((*request)->flags & PREPARED) == 0) {
757 if ((*request)->action != nullptr)
758 flag = simcall_comm_test((*request)->action);
760 finish_wait(request, status);
761 nsleeps=1;//reset the number of sleeps we will do next time
762 if (*request != MPI_REQUEST_NULL && ((*request)->flags & PERSISTENT)==0)
763 *request = MPI_REQUEST_NULL;
771 int smpi_mpi_testany(int count, MPI_Request requests[], int *index, MPI_Status * status)
773 std::vector<simgrid::kernel::activity::ActivityImpl*> comms;
774 comms.reserve(count);
779 *index = MPI_UNDEFINED;
781 std::vector<int> map; /** Maps all matching comms back to their location in requests **/
782 for(i = 0; i < count; i++) {
783 if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->action && !(requests[i]->flags & PREPARED)) {
784 comms.push_back(requests[i]->action);
789 //multiplier to the sleeptime, to increase speed of execution, each failed testany will increase it
790 static int nsleeps = 1;
791 if(smpi_test_sleep > 0)
792 simcall_process_sleep(nsleeps*smpi_test_sleep);
794 i = simcall_comm_testany(comms.data(), comms.size()); // The i-th element in comms matches!
795 if (i != -1) { // -1 is not MPI_UNDEFINED but a SIMIX return code. (nothing matches)
797 finish_wait(&requests[*index], status);
800 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags & NON_PERSISTENT)) {
801 requests[*index] = MPI_REQUEST_NULL;
807 //all requests are null or inactive, return true
809 smpi_empty_status(status);
815 int smpi_mpi_testall(int count, MPI_Request requests[], MPI_Status status[])
818 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
820 for(int i=0; i<count; i++){
821 if (requests[i] != MPI_REQUEST_NULL && !(requests[i]->flags & PREPARED)) {
822 if (smpi_mpi_test(&requests[i], pstat)!=1){
825 requests[i]=MPI_REQUEST_NULL;
828 smpi_empty_status(pstat);
830 if(status != MPI_STATUSES_IGNORE) {
837 void smpi_mpi_probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
839 //FIXME find another way to avoid busy waiting ?
840 // the issue here is that we have to wait on a nonexistent comm
842 smpi_mpi_iprobe(source, tag, comm, &flag, status);
843 XBT_DEBUG("Busy Waiting on probing : %d", flag);
847 void smpi_mpi_iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
849 MPI_Request request = build_request(nullptr, 0, MPI_CHAR, source == MPI_ANY_SOURCE ? MPI_ANY_SOURCE :
850 smpi_group_index(smpi_comm_group(comm), source), smpi_comm_rank(comm), tag, comm, PERSISTENT | RECV);
852 // to avoid deadlock, we have to sleep some time here, or the timer won't advance and we will only do iprobe simcalls
853 // (especially when used as a break condition, such as while(MPI_Iprobe(...)) ... )
854 // multiplier to the sleeptime, to increase speed of execution, each failed iprobe will increase it
855 static int nsleeps = 1;
856 if(smpi_iprobe_sleep > 0)
857 simcall_process_sleep(nsleeps*smpi_iprobe_sleep);
858 // behave like a receive, but don't do it
859 smx_mailbox_t mailbox;
861 print_request("New iprobe", request);
862 // We have to test both mailboxes as we don't know if we will receive one one or another
863 if (xbt_cfg_get_int("smpi/async-small-thresh") > 0){
864 mailbox = smpi_process_mailbox_small();
865 XBT_DEBUG("Trying to probe the perm recv mailbox");
866 request->action = simcall_comm_iprobe(mailbox, 0, request->src, request->tag, &match_recv, static_cast<void*>(request));
869 if (request->action == nullptr){
870 mailbox = smpi_process_mailbox();
871 XBT_DEBUG("trying to probe the other mailbox");
872 request->action = simcall_comm_iprobe(mailbox, 0, request->src,request->tag, &match_recv, static_cast<void*>(request));
875 if (request->action != nullptr){
876 simgrid::kernel::activity::Comm *sync_comm = static_cast<simgrid::kernel::activity::Comm*>(request->action);
877 MPI_Request req = static_cast<MPI_Request>(sync_comm->src_data);
879 if(status != MPI_STATUS_IGNORE && (req->flags & PREPARED) == 0) {
880 status->MPI_SOURCE = smpi_group_rank(smpi_comm_group(comm), req->src);
881 status->MPI_TAG = req->tag;
882 status->MPI_ERROR = MPI_SUCCESS;
883 status->count = req->real_size;
885 nsleeps = 1;//reset the number of sleeps we will do next time
891 smpi_mpi_request_free(&request);
896 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
898 print_request("Waiting", *request);
899 if ((*request)->flags & PREPARED) {
900 smpi_empty_status(status);
904 if ((*request)->action != nullptr)
905 // this is not a detached send
906 simcall_comm_wait((*request)->action, -1.0);
908 finish_wait(request, status);
909 if (*request != MPI_REQUEST_NULL && (((*request)->flags & NON_PERSISTENT)!=0))
910 *request = MPI_REQUEST_NULL;
913 int smpi_mpi_waitany(int count, MPI_Request requests[], MPI_Status * status)
918 int index = MPI_UNDEFINED;
922 // Wait for a request to complete
923 comms = xbt_dynar_new(sizeof(smx_activity_t), nullptr);
924 map = xbt_new(int, count);
925 XBT_DEBUG("Wait for one of %d", count);
926 for(i = 0; i < count; i++) {
927 if (requests[i] != MPI_REQUEST_NULL && !(requests[i]->flags & PREPARED) && !(requests[i]->flags & FINISHED)) {
928 if (requests[i]->action != nullptr) {
929 XBT_DEBUG("Waiting any %p ", requests[i]);
930 xbt_dynar_push(comms, &requests[i]->action);
934 //This is a finished detached request, let's return this one
935 size=0;//so we free the dynar but don't do the waitany call
937 finish_wait(&requests[i], status);//cleanup if refcount = 0
938 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags & NON_PERSISTENT))
939 requests[i]=MPI_REQUEST_NULL;//set to null
945 i = simcall_comm_waitany(comms, -1);
947 // not MPI_UNDEFINED, as this is a simix return code
950 finish_wait(&requests[index], status);
951 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags & NON_PERSISTENT))
952 requests[index] = MPI_REQUEST_NULL;
956 xbt_dynar_free(&comms);
959 if (index==MPI_UNDEFINED)
960 smpi_empty_status(status);
965 int smpi_mpi_waitall(int count, MPI_Request requests[], MPI_Status status[])
969 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
970 int retvalue = MPI_SUCCESS;
971 //tag invalid requests in the set
972 if (status != MPI_STATUSES_IGNORE) {
973 for (c = 0; c < count; c++) {
974 if (requests[c] == MPI_REQUEST_NULL || requests[c]->dst == MPI_PROC_NULL || (requests[c]->flags & PREPARED)) {
975 smpi_empty_status(&status[c]);
976 } else if (requests[c]->src == MPI_PROC_NULL) {
977 smpi_empty_status(&status[c]);
978 status[c].MPI_SOURCE = MPI_PROC_NULL;
982 for(c = 0; c < count; c++) {
984 if (MC_is_active() || MC_record_replay_is_active()) {
985 smpi_mpi_wait(&requests[c], pstat);
988 index = smpi_mpi_waitany(count, requests, pstat);
989 if (index == MPI_UNDEFINED)
991 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags & NON_PERSISTENT))
992 requests[index]=MPI_REQUEST_NULL;
994 if (status != MPI_STATUSES_IGNORE) {
995 status[index] = *pstat;
996 if (status[index].MPI_ERROR == MPI_ERR_TRUNCATE)
997 retvalue = MPI_ERR_IN_STATUS;
1004 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices, MPI_Status status[])
1010 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
1012 for(i = 0; i < incount; i++)
1014 index=smpi_mpi_waitany(incount, requests, pstat);
1015 if(index!=MPI_UNDEFINED){
1016 indices[count] = index;
1018 if(status != MPI_STATUSES_IGNORE) {
1019 status[index] = *pstat;
1021 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags & NON_PERSISTENT))
1022 requests[index]=MPI_REQUEST_NULL;
1024 return MPI_UNDEFINED;
1030 int smpi_mpi_testsome(int incount, MPI_Request requests[], int *indices, MPI_Status status[])
1036 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
1038 for(i = 0; i < incount; i++) {
1039 if((requests[i] != MPI_REQUEST_NULL)) {
1040 if(smpi_mpi_test(&requests[i], pstat)) {
1043 if(status != MPI_STATUSES_IGNORE) {
1046 if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->flags & NON_PERSISTENT)
1047 requests[i]=MPI_REQUEST_NULL;
1053 if(count_dead==incount)
1054 return MPI_UNDEFINED;
1058 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm)
1060 smpi_coll_tuned_bcast_binomial_tree(buf, count, datatype, root, comm);
1063 void smpi_mpi_barrier(MPI_Comm comm)
1065 smpi_coll_tuned_barrier_ompi_basic_linear(comm);
1068 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1069 void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
1071 int system_tag = COLL_TAG_GATHER;
1072 int rank, size, src, index;
1073 MPI_Aint lb = 0, recvext = 0;
1074 MPI_Request *requests;
1076 rank = smpi_comm_rank(comm);
1077 size = smpi_comm_size(comm);
1079 // Send buffer to root
1080 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
1082 smpi_datatype_extent(recvtype, &lb, &recvext);
1083 // Local copy from root
1084 smpi_datatype_copy(sendbuf, sendcount, sendtype, static_cast<char*>(recvbuf) + root * recvcount * recvext, recvcount, recvtype);
1085 // Receive buffers from senders
1086 requests = xbt_new(MPI_Request, size - 1);
1088 for(src = 0; src < size; src++) {
1090 requests[index] = smpi_irecv_init(static_cast<char*>(recvbuf) + src * recvcount * recvext, recvcount, recvtype,
1091 src, system_tag, comm);
1095 // Wait for completion of irecv's.
1096 smpi_mpi_startall(size - 1, requests);
1097 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1098 for(src = 0; src < size-1; src++) {
1099 smpi_mpi_request_free(&requests[src]);
1105 void smpi_mpi_reduce_scatter(void *sendbuf, void *recvbuf, int *recvcounts, MPI_Datatype datatype, MPI_Op op,
1110 int rank = smpi_comm_rank(comm);
1113 /* arbitrarily choose root as rank 0 */
1114 size = smpi_comm_size(comm);
1116 displs = xbt_new(int, size);
1117 for (i = 0; i < size; i++) {
1119 count += recvcounts[i];
1121 tmpbuf=static_cast<void*>(smpi_get_tmp_sendbuffer(count*smpi_datatype_get_extent(datatype)));
1123 mpi_coll_reduce_fun(sendbuf, tmpbuf, count, datatype, op, 0, comm);
1124 smpi_mpi_scatterv(tmpbuf, recvcounts, displs, datatype, recvbuf, recvcounts[rank], datatype, 0, comm);
1126 smpi_free_tmp_buffer(tmpbuf);
1129 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, int *recvcounts, int *displs,
1130 MPI_Datatype recvtype, int root, MPI_Comm comm)
1132 int system_tag = COLL_TAG_GATHERV;
1133 int rank, size, src, index;
1134 MPI_Aint lb = 0, recvext = 0;
1135 MPI_Request *requests;
1137 rank = smpi_comm_rank(comm);
1138 size = smpi_comm_size(comm);
1140 // Send buffer to root
1141 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
1143 smpi_datatype_extent(recvtype, &lb, &recvext);
1144 // Local copy from root
1145 smpi_datatype_copy(sendbuf, sendcount, sendtype, static_cast<char*>(recvbuf) + displs[root] * recvext,
1146 recvcounts[root], recvtype);
1147 // Receive buffers from senders
1148 requests = xbt_new(MPI_Request, size - 1);
1150 for(src = 0; src < size; src++) {
1152 requests[index] = smpi_irecv_init(static_cast<char*>(recvbuf) + displs[src] * recvext,
1153 recvcounts[src], recvtype, src, system_tag, comm);
1157 // Wait for completion of irecv's.
1158 smpi_mpi_startall(size - 1, requests);
1159 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1160 for(src = 0; src < size-1; src++) {
1161 smpi_mpi_request_free(&requests[src]);
1167 void smpi_mpi_allgather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1168 void *recvbuf,int recvcount, MPI_Datatype recvtype, MPI_Comm comm)
1170 int system_tag = COLL_TAG_ALLGATHER;
1171 int rank, size, other, index;
1172 MPI_Aint lb = 0, recvext = 0;
1173 MPI_Request *requests;
1175 rank = smpi_comm_rank(comm);
1176 size = smpi_comm_size(comm);
1177 // FIXME: check for errors
1178 smpi_datatype_extent(recvtype, &lb, &recvext);
1179 // Local copy from self
1180 smpi_datatype_copy(sendbuf, sendcount, sendtype, static_cast<char *>(recvbuf) + rank * recvcount * recvext, recvcount, recvtype);
1181 // Send/Recv buffers to/from others;
1182 requests = xbt_new(MPI_Request, 2 * (size - 1));
1184 for(other = 0; other < size; other++) {
1186 requests[index] = smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,comm);
1188 requests[index] = smpi_irecv_init(static_cast<char *>(recvbuf) + other * recvcount * recvext, recvcount, recvtype, other,
1193 // Wait for completion of all comms.
1194 smpi_mpi_startall(2 * (size - 1), requests);
1195 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
1196 for(other = 0; other < 2*(size-1); other++) {
1197 smpi_mpi_request_free(&requests[other]);
1202 void smpi_mpi_allgatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf,
1203 int *recvcounts, int *displs, MPI_Datatype recvtype, MPI_Comm comm)
1205 int system_tag = COLL_TAG_ALLGATHERV;
1206 int rank, size, other, index;
1207 MPI_Aint lb = 0, recvext = 0;
1208 MPI_Request *requests;
1210 rank = smpi_comm_rank(comm);
1211 size = smpi_comm_size(comm);
1212 smpi_datatype_extent(recvtype, &lb, &recvext);
1213 // Local copy from self
1214 smpi_datatype_copy(sendbuf, sendcount, sendtype, static_cast<char *>(recvbuf) + displs[rank] * recvext,recvcounts[rank], recvtype);
1215 // Send buffers to others;
1216 requests = xbt_new(MPI_Request, 2 * (size - 1));
1218 for(other = 0; other < size; other++) {
1221 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag, comm);
1223 requests[index] = smpi_irecv_init(static_cast<char *>(recvbuf) + displs[other] * recvext, recvcounts[other],
1224 recvtype, other, system_tag, comm);
1228 // Wait for completion of all comms.
1229 smpi_mpi_startall(2 * (size - 1), requests);
1230 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
1231 for(other = 0; other < 2*(size-1); other++) {
1232 smpi_mpi_request_free(&requests[other]);
1237 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1238 void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
1240 int system_tag = COLL_TAG_SCATTER;
1243 MPI_Aint sendext = 0;
1244 MPI_Request *requests;
1246 int rank = smpi_comm_rank(comm);
1247 int size = smpi_comm_size(comm);
1249 // Recv buffer from root
1250 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm, MPI_STATUS_IGNORE);
1252 smpi_datatype_extent(sendtype, &lb, &sendext);
1253 // Local copy from root
1254 if(recvbuf!=MPI_IN_PLACE){
1255 smpi_datatype_copy(static_cast<char *>(sendbuf) + root * sendcount * sendext,
1256 sendcount, sendtype, recvbuf, recvcount, recvtype);
1258 // Send buffers to receivers
1259 requests = xbt_new(MPI_Request, size - 1);
1261 for(dst = 0; dst < size; dst++) {
1263 requests[index] = smpi_isend_init(static_cast<char *>(sendbuf) + dst * sendcount * sendext, sendcount, sendtype, dst,
1268 // Wait for completion of isend's.
1269 smpi_mpi_startall(size - 1, requests);
1270 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1271 for(dst = 0; dst < size-1; dst++) {
1272 smpi_mpi_request_free(&requests[dst]);
1278 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs, MPI_Datatype sendtype, void *recvbuf, int recvcount,
1279 MPI_Datatype recvtype, int root, MPI_Comm comm)
1281 int system_tag = COLL_TAG_SCATTERV;
1284 MPI_Aint sendext = 0;
1285 MPI_Request *requests;
1287 int rank = smpi_comm_rank(comm);
1288 int size = smpi_comm_size(comm);
1290 // Recv buffer from root
1291 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm, MPI_STATUS_IGNORE);
1293 smpi_datatype_extent(sendtype, &lb, &sendext);
1294 // Local copy from root
1295 if(recvbuf!=MPI_IN_PLACE){
1296 smpi_datatype_copy(static_cast<char *>(sendbuf) + displs[root] * sendext, sendcounts[root],
1297 sendtype, recvbuf, recvcount, recvtype);
1299 // Send buffers to receivers
1300 requests = xbt_new(MPI_Request, size - 1);
1302 for(dst = 0; dst < size; dst++) {
1304 requests[index] = smpi_isend_init(static_cast<char *>(sendbuf) + displs[dst] * sendext, sendcounts[dst],
1305 sendtype, dst, system_tag, comm);
1309 // Wait for completion of isend's.
1310 smpi_mpi_startall(size - 1, requests);
1311 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1312 for(dst = 0; dst < size-1; dst++) {
1313 smpi_mpi_request_free(&requests[dst]);
1319 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root,
1322 int system_tag = COLL_TAG_REDUCE;
1325 MPI_Aint dataext = 0;
1326 MPI_Request *requests;
1329 char* sendtmpbuf = static_cast<char *>(sendbuf);
1331 int rank = smpi_comm_rank(comm);
1332 int size = smpi_comm_size(comm);
1333 //non commutative case, use a working algo from openmpi
1334 if(!smpi_op_is_commute(op)){
1335 smpi_coll_tuned_reduce_ompi_basic_linear(sendtmpbuf, recvbuf, count, datatype, op, root, comm);
1339 if( sendbuf == MPI_IN_PLACE ) {
1340 sendtmpbuf = static_cast<char *>(smpi_get_tmp_sendbuffer(count*smpi_datatype_get_extent(datatype)));
1341 smpi_datatype_copy(recvbuf, count, datatype,sendtmpbuf, count, datatype);
1345 // Send buffer to root
1346 smpi_mpi_send(sendtmpbuf, count, datatype, root, system_tag, comm);
1348 smpi_datatype_extent(datatype, &lb, &dataext);
1349 // Local copy from root
1350 if (sendtmpbuf != nullptr && recvbuf != nullptr)
1351 smpi_datatype_copy(sendtmpbuf, count, datatype, recvbuf, count, datatype);
1352 // Receive buffers from senders
1353 requests = xbt_new(MPI_Request, size - 1);
1354 tmpbufs = xbt_new(void *, size - 1);
1356 for(src = 0; src < size; src++) {
1358 if (!smpi_process_get_replaying())
1359 tmpbufs[index] = xbt_malloc(count * dataext);
1361 tmpbufs[index] = smpi_get_tmp_sendbuffer(count * dataext);
1363 smpi_irecv_init(tmpbufs[index], count, datatype, src, system_tag, comm);
1367 // Wait for completion of irecv's.
1368 smpi_mpi_startall(size - 1, requests);
1369 for(src = 0; src < size - 1; src++) {
1370 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1371 XBT_DEBUG("finished waiting any request with index %d", index);
1372 if(index == MPI_UNDEFINED) {
1375 smpi_mpi_request_free(&requests[index]);
1377 if(op) /* op can be MPI_OP_NULL that does nothing */
1378 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1380 for(index = 0; index < size - 1; index++) {
1381 smpi_free_tmp_buffer(tmpbufs[index]);
1387 if( sendbuf == MPI_IN_PLACE ) {
1388 smpi_free_tmp_buffer(sendtmpbuf);
1392 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1394 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
1395 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
1398 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1400 int system_tag = -888;
1402 MPI_Aint lb = 0, dataext = 0;
1403 MPI_Request *requests;
1406 int rank = smpi_comm_rank(comm);
1407 int size = smpi_comm_size(comm);
1409 smpi_datatype_extent(datatype, &lb, &dataext);
1411 // Local copy from self
1412 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
1414 // Send/Recv buffers to/from others;
1415 requests = xbt_new(MPI_Request, size - 1);
1416 tmpbufs = xbt_new(void *, rank);
1418 for(other = 0; other < rank; other++) {
1419 tmpbufs[index] = smpi_get_tmp_sendbuffer(count * dataext);
1420 requests[index] = smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag, comm);
1423 for(other = rank + 1; other < size; other++) {
1424 requests[index] = smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1427 // Wait for completion of all comms.
1428 smpi_mpi_startall(size - 1, requests);
1430 if(smpi_op_is_commute(op)){
1431 for(other = 0; other < size - 1; other++) {
1432 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1433 if(index == MPI_UNDEFINED) {
1437 // #Request is below rank: it's a irecv
1438 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1442 //non commutative case, wait in order
1443 for(other = 0; other < size - 1; other++) {
1444 smpi_mpi_wait(&(requests[other]), MPI_STATUS_IGNORE);
1446 smpi_op_apply(op, tmpbufs[other], recvbuf, &count, &datatype);
1450 for(index = 0; index < rank; index++) {
1451 smpi_free_tmp_buffer(tmpbufs[index]);
1453 for(index = 0; index < size-1; index++) {
1454 smpi_mpi_request_free(&requests[index]);
1460 void smpi_mpi_exscan(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1462 int system_tag = -888;
1464 MPI_Aint lb = 0, dataext = 0;
1465 MPI_Request *requests;
1467 int recvbuf_is_empty=1;
1468 int rank = smpi_comm_rank(comm);
1469 int size = smpi_comm_size(comm);
1471 smpi_datatype_extent(datatype, &lb, &dataext);
1473 // Send/Recv buffers to/from others;
1474 requests = xbt_new(MPI_Request, size - 1);
1475 tmpbufs = xbt_new(void *, rank);
1477 for(other = 0; other < rank; other++) {
1478 tmpbufs[index] = smpi_get_tmp_sendbuffer(count * dataext);
1479 requests[index] = smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag, comm);
1482 for(other = rank + 1; other < size; other++) {
1483 requests[index] = smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1486 // Wait for completion of all comms.
1487 smpi_mpi_startall(size - 1, requests);
1488 if(smpi_op_is_commute(op)){
1489 for(other = 0; other < size - 1; other++) {
1490 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1491 if(index == MPI_UNDEFINED) {
1495 if(recvbuf_is_empty){
1496 smpi_datatype_copy(tmpbufs[index], count, datatype, recvbuf, count, datatype);
1499 // #Request is below rank: it's a irecv
1500 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1504 //non commutative case, wait in order
1505 for(other = 0; other < size - 1; other++) {
1506 smpi_mpi_wait(&(requests[other]), MPI_STATUS_IGNORE);
1508 if(recvbuf_is_empty){
1509 smpi_datatype_copy(tmpbufs[other], count, datatype, recvbuf, count, datatype);
1512 smpi_op_apply(op, tmpbufs[other], recvbuf, &count, &datatype);
1516 for(index = 0; index < rank; index++) {
1517 smpi_free_tmp_buffer(tmpbufs[index]);
1519 for(index = 0; index < size-1; index++) {
1520 smpi_mpi_request_free(&requests[index]);