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>
11 #include "xbt/replay.h"
13 #include "src/mc/mc_replay.h"
14 #include "src/simix/smx_private.h"
15 #include "simgrid/sg_config.h"
16 #include "smpi/smpi_utils.hpp"
17 #include <simgrid/s4u/host.hpp>
19 #include "src/kernel/activity/SynchroComm.hpp"
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_request, smpi, "Logging specific to SMPI (reques)");
23 static simgrid::config::Flag<double> smpi_iprobe_sleep(
24 "smpi/iprobe", "Minimum time to inject inside a call to MPI_Iprobe", 1e-4);
25 static simgrid::config::Flag<double> smpi_test_sleep(
26 "smpi/test", "Minimum time to inject inside a call to MPI_Test", 1e-4);
28 std::vector<s_smpi_factor_t> smpi_os_values;
29 std::vector<s_smpi_factor_t> smpi_or_values;
30 std::vector<s_smpi_factor_t> smpi_ois_values;
32 extern void (*smpi_comm_copy_data_callback) (smx_activity_t, void*, size_t);
34 static double smpi_os(size_t size)
36 if (smpi_os_values.empty()) {
37 smpi_os_values = parse_factor(xbt_cfg_get_string("smpi/os"));
39 double current=smpi_os_values.empty()?0.0:smpi_os_values[0].values[0]+smpi_os_values[0].values[1]*size;
40 // Iterate over all the sections that were specified and find the right
41 // value. (fact.factor represents the interval sizes; we want to find the
42 // section that has fact.factor <= size and no other such fact.factor <= size)
43 // Note: parse_factor() (used before) already sorts the vector we iterate over!
44 for (auto& fact : smpi_os_values) {
45 if (size <= fact.factor) { // Values already too large, use the previously computed value of current!
46 XBT_DEBUG("os : %zu <= %zu return %.10f", size, fact.factor, current);
49 // If the next section is too large, the current section must be used.
50 // Hence, save the cost, as we might have to use it.
51 current = fact.values[0]+fact.values[1]*size;
54 XBT_DEBUG("Searching for smpi/os: %zu is larger than the largest boundary, return %.10f", size, current);
59 static double smpi_ois(size_t size)
61 if (smpi_ois_values.empty()) {
62 smpi_ois_values = parse_factor(xbt_cfg_get_string("smpi/ois"));
64 double current=smpi_ois_values.empty()?0.0:smpi_ois_values[0].values[0]+smpi_ois_values[0].values[1]*size;
65 // Iterate over all the sections that were specified and find the right value. (fact.factor represents the interval
66 // sizes; we want to find the section that has fact.factor <= size and no other such fact.factor <= size)
67 // Note: parse_factor() (used before) already sorts the vector we iterate over!
68 for (auto& fact : smpi_ois_values) {
69 if (size <= fact.factor) { // Values already too large, use the previously computed value of current!
70 XBT_DEBUG("ois : %zu <= %zu return %.10f", size, fact.factor, current);
73 // If the next section is too large, the current section must be used.
74 // Hence, save the cost, as we might have to use it.
75 current = fact.values[0]+fact.values[1]*size;
78 XBT_DEBUG("Searching for smpi/ois: %zu is larger than the largest boundary, return %.10f", size, current);
83 static double smpi_or(size_t size)
85 if (smpi_or_values.empty()) {
86 smpi_or_values = parse_factor(xbt_cfg_get_string("smpi/or"));
89 double current=smpi_or_values.empty()?0.0:smpi_or_values.front().values[0]+smpi_or_values.front().values[1]*size;
91 // Iterate over all the sections that were specified and find the right value. (fact.factor represents the interval
92 // sizes; we want to find the section that has fact.factor <= size and no other such fact.factor <= size)
93 // Note: parse_factor() (used before) already sorts the vector we iterate over!
94 for (auto fact : smpi_or_values) {
95 if (size <= fact.factor) { // Values already too large, use the previously computed value of current!
96 XBT_DEBUG("or : %zu <= %zu return %.10f", size, fact.factor, current);
99 // If the next section is too large, the current section must be used.
100 // Hence, save the cost, as we might have to use it.
101 current=fact.values[0]+fact.values[1]*size;
104 XBT_DEBUG("smpi_or: %zu is larger than largest boundary, return %.10f", size, current);
113 Request::Request(void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm, unsigned flags) : buf_(buf), old_type_(datatype), src_(src), dst_(dst), tag_(tag), comm_(comm), flags_(flags)
115 void *old_buf = nullptr;
117 if((((flags & RECV) != 0) && ((flags & ACCUMULATE) !=0)) || (datatype->flags() & DT_FLAG_DERIVED)){
118 // This part handles the problem of non-contiguous memory
120 buf_ = count==0 ? nullptr : xbt_malloc(count*datatype->size());
121 if ((datatype->flags() & DT_FLAG_DERIVED) && ((flags & SEND) != 0)) {
122 datatype->serialize(old_buf, buf_, count);
125 // This part handles the problem of non-contiguous memory (for the unserialisation at the reception)
127 size_ = datatype->size() * count;
132 detached_sender_ = nullptr;
137 if (flags & PERSISTENT)
145 //Request::destroy(void* request)
147 // MPI_Request req = static_cast<MPI_Request>(request);
151 MPI_Comm Request::comm(){
167 int Request::flags(){
171 int Request::detached(){
176 void Request::unuse(MPI_Request* request)
178 if((*request) != MPI_REQUEST_NULL){
179 (*request)->refcount_--;
180 if((*request)->refcount_<0) xbt_die("wrong refcount");
182 if((*request)->refcount_==0){
183 (*request)->old_type_->unuse();
184 (*request)->comm_->unuse();
185 (*request)->print_request("Destroying");
187 *request = MPI_REQUEST_NULL;
189 (*request)->print_request("Decrementing");
192 xbt_die("freeing an already free request");
197 int Request::match_recv(void* a, void* b, smx_activity_t ignored) {
198 MPI_Request ref = static_cast<MPI_Request>(a);
199 MPI_Request req = static_cast<MPI_Request>(b);
200 XBT_DEBUG("Trying to match a recv of src %d against %d, tag %d against %d",ref->src_,req->src_, ref->tag_, req->tag_);
202 xbt_assert(ref, "Cannot match recv against null reference");
203 xbt_assert(req, "Cannot match recv against null request");
204 if((ref->src_ == MPI_ANY_SOURCE || req->src_ == ref->src_)
205 && ((ref->tag_ == MPI_ANY_TAG && req->tag_ >=0) || req->tag_ == ref->tag_)){
206 //we match, we can transfer some values
207 if(ref->src_ == MPI_ANY_SOURCE)
208 ref->real_src_ = req->src_;
209 if(ref->tag_ == MPI_ANY_TAG)
210 ref->real_tag_ = req->tag_;
211 if(ref->real_size_ < req->real_size_)
213 if(req->detached_==1)
214 ref->detached_sender_=req; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
215 XBT_DEBUG("match succeeded");
220 int Request::match_send(void* a, void* b,smx_activity_t ignored) {
221 MPI_Request ref = static_cast<MPI_Request>(a);
222 MPI_Request req = static_cast<MPI_Request>(b);
223 XBT_DEBUG("Trying to match a send of src %d against %d, tag %d against %d",ref->src_,req->src_, ref->tag_, req->tag_);
224 xbt_assert(ref, "Cannot match send against null reference");
225 xbt_assert(req, "Cannot match send against null request");
227 if((req->src_ == MPI_ANY_SOURCE || req->src_ == ref->src_)
228 && ((req->tag_ == MPI_ANY_TAG && ref->tag_ >=0)|| req->tag_ == ref->tag_)){
229 if(req->src_ == MPI_ANY_SOURCE)
230 req->real_src_ = ref->src_;
231 if(req->tag_ == MPI_ANY_TAG)
232 req->real_tag_ = ref->tag_;
233 if(req->real_size_ < ref->real_size_)
235 if(ref->detached_==1)
236 req->detached_sender_=ref; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
237 XBT_DEBUG("match succeeded");
243 void Request::print_request(const char *message)
245 XBT_VERB("%s request %p [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
246 message, this, buf_, size_, src_, dst_, tag_, flags_);
250 /* factories, to hide the internal flags from the caller */
251 MPI_Request Request::send_init(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
253 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
254 request = new Request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype, smpi_process_index(),
255 comm->group()->index(dst), tag, comm, PERSISTENT | SEND | PREPARED);
259 MPI_Request Request::ssend_init(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
261 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
262 request = new Request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype, smpi_process_index(),
263 comm->group()->index(dst), tag, comm, PERSISTENT | SSEND | SEND | PREPARED);
267 MPI_Request Request::isend_init(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
269 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
270 request = new Request(buf==MPI_BOTTOM ? nullptr : buf , count, datatype, smpi_process_index(),
271 comm->group()->index(dst), tag,comm, PERSISTENT | ISEND | SEND | PREPARED);
276 MPI_Request Request::rma_send_init(void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
279 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
281 request = new Request(buf==MPI_BOTTOM ? nullptr : buf , count, datatype, src, dst, tag,
282 comm, RMA | NON_PERSISTENT | ISEND | SEND | PREPARED);
284 request = new Request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype, src, dst, tag,
285 comm, RMA | NON_PERSISTENT | ISEND | SEND | PREPARED | ACCUMULATE);
291 MPI_Request Request::recv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
293 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
294 request = new Request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype,
295 src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : comm->group()->index(src),
296 smpi_process_index(), tag, comm, PERSISTENT | RECV | PREPARED);
300 MPI_Request Request::rma_recv_init(void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
303 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
305 request = new Request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype, src, dst, tag,
306 comm, RMA | NON_PERSISTENT | RECV | PREPARED);
308 request = new Request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype, src, dst, tag,
309 comm, RMA | NON_PERSISTENT | RECV | PREPARED | ACCUMULATE);
315 MPI_Request Request::irecv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
317 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
318 request = new Request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype, src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE :
319 comm->group()->index(src), smpi_process_index(), tag,
320 comm, PERSISTENT | RECV | PREPARED);
324 MPI_Request Request::isend(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
326 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
327 request = new Request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype, smpi_process_index(),
328 comm->group()->index(dst), tag, comm, NON_PERSISTENT | ISEND | SEND);
333 MPI_Request Request::issend(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
335 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
336 request = new Request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype, smpi_process_index(),
337 comm->group()->index(dst), tag,comm, NON_PERSISTENT | ISEND | SSEND | SEND);
343 MPI_Request Request::irecv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
345 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
346 request = new Request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype, src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE :
347 comm->group()->index(src), smpi_process_index(), tag, comm,
348 NON_PERSISTENT | RECV);
353 void Request::recv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status * status)
355 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
356 request = irecv(buf, count, datatype, src, tag, comm);
357 wait(&request,status);
361 void Request::send(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
363 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
364 request = new Request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype, smpi_process_index(),
365 comm->group()->index(dst), tag, comm, NON_PERSISTENT | SEND);
368 wait(&request, MPI_STATUS_IGNORE);
372 void Request::ssend(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
374 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
375 request = new Request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype, smpi_process_index(),
376 comm->group()->index(dst), tag, comm, NON_PERSISTENT | SSEND | SEND);
379 wait(&request,MPI_STATUS_IGNORE);
385 void Request::sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,int dst, int sendtag,
386 void *recvbuf, int recvcount, MPI_Datatype recvtype, int src, int recvtag,
387 MPI_Comm comm, MPI_Status * status)
389 MPI_Request requests[2];
391 int myid=smpi_process_index();
392 if ((comm->group()->index(dst) == myid) && (comm->group()->index(src) == myid)){
393 Datatype::copy(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
396 requests[0] = isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
397 requests[1] = irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
398 startall(2, requests);
399 waitall(2, requests, stats);
402 if(status != MPI_STATUS_IGNORE) {
403 // Copy receive status
410 void Request::start()
412 smx_mailbox_t mailbox;
414 xbt_assert(action_ == nullptr, "Cannot (re-)start unfinished communication");
419 if ((flags_ & RECV) != 0) {
420 this->print_request("New recv");
422 int async_small_thresh = xbt_cfg_get_int("smpi/async-small-thresh");
424 xbt_mutex_t mut = smpi_process_mailboxes_mutex();
425 if (async_small_thresh != 0 || (flags_ & RMA) != 0)
426 xbt_mutex_acquire(mut);
428 if (async_small_thresh == 0 && (flags_ & RMA) == 0 ) {
429 mailbox = smpi_process_mailbox();
431 else if (((flags_ & RMA) != 0) || static_cast<int>(size_) < async_small_thresh) {
432 //We have to check both mailboxes (because SSEND messages are sent to the large mbox).
433 //begin with the more appropriate one : the small one.
434 mailbox = smpi_process_mailbox_small();
435 XBT_DEBUG("Is there a corresponding send already posted in the small mailbox %p (in case of SSEND)?", mailbox);
436 smx_activity_t action = simcall_comm_iprobe(mailbox, 0, src_,tag_, &match_recv,
437 static_cast<void*>(this));
439 if (action == nullptr) {
440 mailbox = smpi_process_mailbox();
441 XBT_DEBUG("No, nothing in the small mailbox test the other one : %p", mailbox);
442 action = simcall_comm_iprobe(mailbox, 0, src_,tag_, &match_recv, static_cast<void*>(this));
443 if (action == nullptr) {
444 XBT_DEBUG("Still nothing, switch back to the small mailbox : %p", mailbox);
445 mailbox = smpi_process_mailbox_small();
448 XBT_DEBUG("yes there was something for us in the large mailbox");
451 mailbox = smpi_process_mailbox_small();
452 XBT_DEBUG("Is there a corresponding send already posted the small mailbox?");
453 smx_activity_t action = simcall_comm_iprobe(mailbox, 0, src_,tag_, &match_recv, static_cast<void*>(this));
455 if (action == nullptr) {
456 XBT_DEBUG("No, nothing in the permanent receive mailbox");
457 mailbox = smpi_process_mailbox();
459 XBT_DEBUG("yes there was something for us in the small mailbox");
463 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
465 action_ = simcall_comm_irecv(SIMIX_process_self(), mailbox, buf_, &real_size_, &match_recv,
466 ! smpi_process_get_replaying()? smpi_comm_copy_data_callback
467 : &smpi_comm_null_copy_buffer_callback, this, -1.0);
468 XBT_DEBUG("recv simcall posted");
470 if (async_small_thresh != 0 || (flags_ & RMA) != 0 )
471 xbt_mutex_release(mut);
472 } else { /* the RECV flag was not set, so this is a send */
476 if (TRACE_smpi_view_internals()) {
477 TRACE_smpi_send(rank, rank, receiver, tag_, size_);
479 this->print_request("New send");
482 if ((flags_ & SSEND) == 0 && ( (flags_ & RMA) != 0
483 || static_cast<int>(size_) < xbt_cfg_get_int("smpi/send-is-detached-thresh") ) ) {
484 void *oldbuf = nullptr;
486 XBT_DEBUG("Send request %p is detached", this);
488 if(!(old_type_->flags() & DT_FLAG_DERIVED)){
490 if (!smpi_process_get_replaying() && oldbuf != nullptr && size_!=0){
491 if((smpi_privatize_global_variables != 0)
492 && (static_cast<char*>(buf_) >= smpi_start_data_exe)
493 && (static_cast<char*>(buf_) < smpi_start_data_exe + smpi_size_data_exe )){
494 XBT_DEBUG("Privatization : We are sending from a zone inside global memory. Switch data segment ");
495 smpi_switch_data_segment(src_);
497 buf = xbt_malloc(size_);
498 memcpy(buf,oldbuf,size_);
499 XBT_DEBUG("buf %p copied into %p",oldbuf,buf);
504 //if we are giving back the control to the user without waiting for completion, we have to inject timings
505 double sleeptime = 0.0;
506 if(detached_ != 0 || ((flags_ & (ISEND|SSEND)) != 0)){// issend should be treated as isend
507 //isend and send timings may be different
508 sleeptime = ((flags_ & ISEND) != 0) ? smpi_ois(size_) : smpi_os(size_);
512 simcall_process_sleep(sleeptime);
513 XBT_DEBUG("sending size of %zu : sleep %f ", size_, sleeptime);
516 int async_small_thresh = xbt_cfg_get_int("smpi/async-small-thresh");
518 xbt_mutex_t mut=smpi_process_remote_mailboxes_mutex(receiver);
520 if (async_small_thresh != 0 || (flags_ & RMA) != 0)
521 xbt_mutex_acquire(mut);
523 if (!(async_small_thresh != 0 || (flags_ & RMA) !=0)) {
524 mailbox = smpi_process_remote_mailbox(receiver);
525 } else if (((flags_ & RMA) != 0) || static_cast<int>(size_) < async_small_thresh) { // eager mode
526 mailbox = smpi_process_remote_mailbox(receiver);
527 XBT_DEBUG("Is there a corresponding recv already posted in the large mailbox %p?", mailbox);
528 smx_activity_t action = simcall_comm_iprobe(mailbox, 1,dst_, tag_, &match_send,
529 static_cast<void*>(this));
530 if (action == nullptr) {
531 if ((flags_ & SSEND) == 0){
532 mailbox = smpi_process_remote_mailbox_small(receiver);
533 XBT_DEBUG("No, nothing in the large mailbox, message is to be sent on the small one %p", mailbox);
535 mailbox = smpi_process_remote_mailbox_small(receiver);
536 XBT_DEBUG("SSEND : Is there a corresponding recv already posted in the small mailbox %p?", mailbox);
537 action = simcall_comm_iprobe(mailbox, 1,dst_, tag_, &match_send, static_cast<void*>(this));
538 if (action == nullptr) {
539 XBT_DEBUG("No, we are first, send to large mailbox");
540 mailbox = smpi_process_remote_mailbox(receiver);
544 XBT_DEBUG("Yes there was something for us in the large mailbox");
547 mailbox = smpi_process_remote_mailbox(receiver);
548 XBT_DEBUG("Send request %p is in the large mailbox %p (buf: %p)",mailbox, this,buf_);
551 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
553 action_ = simcall_comm_isend(SIMIX_process_from_PID(src_+1), mailbox, size_, -1.0,
554 buf, real_size_, &match_send,
555 &xbt_free_f, // how to free the userdata if a detached send fails
556 !smpi_process_get_replaying() ? smpi_comm_copy_data_callback
557 : &smpi_comm_null_copy_buffer_callback, this,
558 // detach if msg size < eager/rdv switch limit
560 XBT_DEBUG("send simcall posted");
562 /* FIXME: detached sends are not traceable (action_ == nullptr) */
563 if (action_ != nullptr)
564 simcall_set_category(action_, TRACE_internal_smpi_get_category());
566 if (async_small_thresh != 0 || ((flags_ & RMA)!=0))
567 xbt_mutex_release(mut);
572 void Request::startall(int count, MPI_Request * requests)
574 if(requests== nullptr)
577 for(int i = 0; i < count; i++) {
578 requests[i]->start();
582 int Request::test(MPI_Request * request, MPI_Status * status) {
583 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or testall before)
585 // to avoid deadlocks if used as a break condition, such as
586 // while (MPI_Test(request, flag, status) && flag) {
588 // because the time will not normally advance when only calls to MPI_Test are made -> deadlock
589 // multiplier to the sleeptime, to increase speed of execution, each failed test will increase it
590 static int nsleeps = 1;
591 if(smpi_test_sleep > 0)
592 simcall_process_sleep(nsleeps*smpi_test_sleep);
594 smpi_empty_status(status);
596 if (((*request)->flags_ & PREPARED) == 0) {
597 if ((*request)->action_ != nullptr)
598 flag = simcall_comm_test((*request)->action_);
600 finish_wait(request,status);
601 nsleeps=1;//reset the number of sleeps we will do next time
602 if (*request != MPI_REQUEST_NULL && ((*request)->flags_ & PERSISTENT)==0)
603 *request = MPI_REQUEST_NULL;
604 } else if (xbt_cfg_get_boolean("smpi/grow-injected-times")){
612 int Request::testsome(int incount, MPI_Request requests[], int *indices, MPI_Status status[])
618 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
620 for(i = 0; i < incount; i++) {
621 if((requests[i] != MPI_REQUEST_NULL)) {
622 if(test(&requests[i], pstat)) {
625 if(status != MPI_STATUSES_IGNORE) {
628 if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->flags_ & NON_PERSISTENT)
629 requests[i]=MPI_REQUEST_NULL;
635 if(count_dead==incount)
636 return MPI_UNDEFINED;
641 int Request::testany(int count, MPI_Request requests[], int *index, MPI_Status * status)
643 std::vector<simgrid::kernel::activity::ActivityImpl*> comms;
644 comms.reserve(count);
649 *index = MPI_UNDEFINED;
651 std::vector<int> map; /** Maps all matching comms back to their location in requests **/
652 for(i = 0; i < count; i++) {
653 if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->action_ && !(requests[i]->flags_ & PREPARED)) {
654 comms.push_back(requests[i]->action_);
659 //multiplier to the sleeptime, to increase speed of execution, each failed testany will increase it
660 static int nsleeps = 1;
661 if(smpi_test_sleep > 0)
662 simcall_process_sleep(nsleeps*smpi_test_sleep);
664 i = simcall_comm_testany(comms.data(), comms.size()); // The i-th element in comms matches!
665 if (i != -1) { // -1 is not MPI_UNDEFINED but a SIMIX return code. (nothing matches)
667 finish_wait(&requests[*index],status);
670 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags_ & NON_PERSISTENT)) {
671 requests[*index] = MPI_REQUEST_NULL;
677 //all requests are null or inactive, return true
679 smpi_empty_status(status);
686 int Request::testall(int count, MPI_Request requests[], MPI_Status status[])
689 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
691 for(int i=0; i<count; i++){
692 if (requests[i] != MPI_REQUEST_NULL && !(requests[i]->flags_ & PREPARED)) {
693 if (test(&requests[i], pstat)!=1){
696 requests[i]=MPI_REQUEST_NULL;
699 smpi_empty_status(pstat);
701 if(status != MPI_STATUSES_IGNORE) {
711 void Request::probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
713 //FIXME find another way to avoid busy waiting ?
714 // the issue here is that we have to wait on a nonexistent comm
716 iprobe(source, tag, comm, &flag, status);
717 XBT_DEBUG("Busy Waiting on probing : %d", flag);
721 void Request::iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
722 MPI_Request request = new Request(nullptr, 0, MPI_CHAR, source == MPI_ANY_SOURCE ? MPI_ANY_SOURCE :
723 comm->group()->index(source), comm->rank(), tag, comm, PERSISTENT | RECV);
725 // to avoid deadlock, we have to sleep some time here, or the timer won't advance and we will only do iprobe simcalls
726 // (especially when used as a break condition, such as while(MPI_Iprobe(...)) ... )
727 // nsleeps is a multiplier to the sleeptime, to increase speed of execution, each failed iprobe will increase it
728 // (This can speed up the execution of certain applications by an order of magnitude, such as HPL)
729 static int nsleeps = 1;
730 double speed = simgrid::s4u::Actor::self()->host()->speed();
731 double maxrate = xbt_cfg_get_double("smpi/iprobe-cpu-usage");
732 if (smpi_iprobe_sleep > 0) {
733 smx_activity_t iprobe_sleep = simcall_execution_start("iprobe", /* flops to executek*/nsleeps*smpi_iprobe_sleep*speed*maxrate, /* priority */1.0, /* performance bound */maxrate*speed);
734 simcall_execution_wait(iprobe_sleep);
736 // behave like a receive, but don't do it
737 smx_mailbox_t mailbox;
739 request->print_request("New iprobe");
740 // We have to test both mailboxes as we don't know if we will receive one one or another
741 if (xbt_cfg_get_int("smpi/async-small-thresh") > 0){
742 mailbox = smpi_process_mailbox_small();
743 XBT_DEBUG("Trying to probe the perm recv mailbox");
744 request->action_ = simcall_comm_iprobe(mailbox, 0, request->src_, request->tag_, &match_recv,
745 static_cast<void*>(request));
748 if (request->action_ == nullptr){
749 mailbox = smpi_process_mailbox();
750 XBT_DEBUG("trying to probe the other mailbox");
751 request->action_ = simcall_comm_iprobe(mailbox, 0, request->src_,request->tag_, &match_recv,
752 static_cast<void*>(request));
755 if (request->action_ != nullptr){
756 simgrid::kernel::activity::Comm *sync_comm = static_cast<simgrid::kernel::activity::Comm*>(request->action_);
757 MPI_Request req = static_cast<MPI_Request>(sync_comm->src_data);
759 if(status != MPI_STATUS_IGNORE && (req->flags_ & PREPARED) == 0) {
760 status->MPI_SOURCE = comm->group()->rank(req->src_);
761 status->MPI_TAG = req->tag_;
762 status->MPI_ERROR = MPI_SUCCESS;
763 status->count = req->real_size_;
765 nsleeps = 1;//reset the number of sleeps we will do next time
769 if (xbt_cfg_get_boolean("smpi/grow-injected-times"))
776 void Request::finish_wait(MPI_Request* request, MPI_Status * status)
778 MPI_Request req = *request;
779 smpi_empty_status(status);
781 if(!((req->detached_ != 0) && ((req->flags_ & SEND) != 0)) && ((req->flags_ & PREPARED) == 0)){
782 if(status != MPI_STATUS_IGNORE) {
783 int src = req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_;
784 status->MPI_SOURCE = req->comm_->group()->rank(src);
785 status->MPI_TAG = req->tag_ == MPI_ANY_TAG ? req->real_tag_ : req->tag_;
786 status->MPI_ERROR = req->truncated_ != 0 ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
787 // this handles the case were size in receive differs from size in send
788 status->count = req->real_size_;
791 req->print_request("Finishing");
792 MPI_Datatype datatype = req->old_type_;
794 if(((req->flags_ & ACCUMULATE) != 0) || (datatype->flags() & DT_FLAG_DERIVED)){
795 if (!smpi_process_get_replaying()){
796 if( smpi_privatize_global_variables != 0 && (static_cast<char*>(req->old_buf_) >= smpi_start_data_exe)
797 && ((char*)req->old_buf_ < smpi_start_data_exe + smpi_size_data_exe )){
798 XBT_VERB("Privatization : We are unserializing to a zone in global memory Switch data segment ");
799 smpi_switch_data_segment(smpi_process_index());
803 if(datatype->flags() & DT_FLAG_DERIVED){
804 // This part handles the problem of non-contignous memory the unserialization at the reception
805 if((req->flags_ & RECV) && datatype->size()!=0)
806 datatype->unserialize(req->buf_, req->old_buf_, req->real_size_/datatype->size() , req->op_);
808 }else if(req->flags_ & RECV){//apply op on contiguous buffer for accumulate
809 int n =req->real_size_/datatype->size();
810 req->op_->apply(req->buf_, req->old_buf_, &n, datatype);
816 if (TRACE_smpi_view_internals() && ((req->flags_ & RECV) != 0)){
817 int rank = smpi_process_index();
818 int src_traced = (req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_);
819 TRACE_smpi_recv(rank, src_traced, rank,req->tag_);
821 if(req->detached_sender_ != nullptr){
822 //integrate pseudo-timing for buffering of small messages, do not bother to execute the simcall if 0
823 double sleeptime = smpi_or(req->real_size_);
825 simcall_process_sleep(sleeptime);
826 XBT_DEBUG("receiving size of %zu : sleep %f ", req->real_size_, sleeptime);
828 unuse(&(req->detached_sender_));
830 if(req->flags_ & PERSISTENT)
831 req->action_ = nullptr;
832 req->flags_ |= FINISHED;
837 void Request::wait(MPI_Request * request, MPI_Status * status)
839 (*request)->print_request("Waiting");
840 if ((*request)->flags_ & PREPARED) {
841 smpi_empty_status(status);
845 if ((*request)->action_ != nullptr)
846 // this is not a detached send
847 simcall_comm_wait((*request)->action_, -1.0);
849 finish_wait(request,status);
850 if (*request != MPI_REQUEST_NULL && (((*request)->flags_ & NON_PERSISTENT)!=0))
851 *request = MPI_REQUEST_NULL;
854 int Request::waitany(int count, MPI_Request requests[], MPI_Status * status)
856 s_xbt_dynar_t comms; // Keep it on stack to save some extra mallocs
859 int index = MPI_UNDEFINED;
863 // Wait for a request to complete
864 xbt_dynar_init(&comms, sizeof(smx_activity_t), nullptr);
865 map = xbt_new(int, count);
866 XBT_DEBUG("Wait for one of %d", count);
867 for(i = 0; i < count; i++) {
868 if (requests[i] != MPI_REQUEST_NULL && !(requests[i]->flags_ & PREPARED) && !(requests[i]->flags_ & FINISHED)) {
869 if (requests[i]->action_ != nullptr) {
870 XBT_DEBUG("Waiting any %p ", requests[i]);
871 xbt_dynar_push(&comms, &requests[i]->action_);
875 // This is a finished detached request, let's return this one
876 size = 0; // so we free the dynar but don't do the waitany call
878 finish_wait(&requests[i], status); // cleanup if refcount = 0
879 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags_ & NON_PERSISTENT))
880 requests[i] = MPI_REQUEST_NULL; // set to null
886 i = simcall_comm_waitany(&comms, -1);
888 // not MPI_UNDEFINED, as this is a simix return code
891 //in case of an accumulate, we have to wait the end of all requests to apply the operation, ordered correctly.
892 if ((requests[index] == MPI_REQUEST_NULL)
893 || (!((requests[index]->flags_ & ACCUMULATE) && (requests[index]->flags_ & RECV)))){
894 finish_wait(&requests[index],status);
895 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags_ & NON_PERSISTENT))
896 requests[index] = MPI_REQUEST_NULL;
903 xbt_dynar_free_data(&comms);
907 if (index==MPI_UNDEFINED)
908 smpi_empty_status(status);
913 static int sort_accumulates(MPI_Request a, MPI_Request b)
915 return (a->tag() < b->tag());
918 int Request::waitall(int count, MPI_Request requests[], MPI_Status status[])
920 std::vector<MPI_Request> accumulates;
923 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
924 int retvalue = MPI_SUCCESS;
925 //tag invalid requests in the set
926 if (status != MPI_STATUSES_IGNORE) {
927 for (int c = 0; c < count; c++) {
928 if (requests[c] == MPI_REQUEST_NULL || requests[c]->dst_ == MPI_PROC_NULL || (requests[c]->flags_ & PREPARED)) {
929 smpi_empty_status(&status[c]);
930 } else if (requests[c]->src_ == MPI_PROC_NULL) {
931 smpi_empty_status(&status[c]);
932 status[c].MPI_SOURCE = MPI_PROC_NULL;
936 for (int c = 0; c < count; c++) {
937 if (MC_is_active() || MC_record_replay_is_active()) {
938 wait(&requests[c],pstat);
941 index = waitany(count, requests, pstat);
942 if (index == MPI_UNDEFINED)
945 if (requests[index] != MPI_REQUEST_NULL
946 && (requests[index]->flags_ & RECV)
947 && (requests[index]->flags_ & ACCUMULATE))
948 accumulates.push_back(requests[index]);
949 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & NON_PERSISTENT))
950 requests[index] = MPI_REQUEST_NULL;
952 if (status != MPI_STATUSES_IGNORE) {
953 status[index] = *pstat;
954 if (status[index].MPI_ERROR == MPI_ERR_TRUNCATE)
955 retvalue = MPI_ERR_IN_STATUS;
959 if (!accumulates.empty()) {
960 std::sort(accumulates.begin(), accumulates.end(), sort_accumulates);
961 for (auto req : accumulates) {
962 finish_wait(&req, status);
969 int Request::waitsome(int incount, MPI_Request requests[], int *indices, MPI_Status status[])
975 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
977 for(i = 0; i < incount; i++)
979 index=waitany(incount, requests, pstat);
980 if(index!=MPI_UNDEFINED){
981 indices[count] = index;
983 if(status != MPI_STATUSES_IGNORE) {
984 status[index] = *pstat;
986 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & NON_PERSISTENT))
987 requests[index]=MPI_REQUEST_NULL;
989 return MPI_UNDEFINED;