1 /* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "smpi_request.hpp"
10 #include "simgrid/Exception.hpp"
11 #include "simgrid/s4u/Exec.hpp"
12 #include "smpi_comm.hpp"
13 #include "smpi_datatype.hpp"
14 #include "smpi_host.hpp"
15 #include "smpi_op.hpp"
16 #include "src/kernel/activity/CommImpl.hpp"
17 #include "src/mc/mc_replay.hpp"
18 #include "src/smpi/include/smpi_actor.hpp"
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_request, smpi, "Logging specific to SMPI (request)");
25 static simgrid::config::Flag<double> smpi_iprobe_sleep(
26 "smpi/iprobe", "Minimum time to inject inside a call to MPI_Iprobe", 1e-4);
27 static simgrid::config::Flag<double> smpi_test_sleep(
28 "smpi/test", "Minimum time to inject inside a call to MPI_Test", 1e-4);
30 std::vector<s_smpi_factor_t> smpi_ois_values;
32 extern void (*smpi_comm_copy_data_callback)(simgrid::kernel::activity::CommImpl*, void*, size_t);
37 Request::Request(const void* buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
38 unsigned flags, MPI_Op op)
39 : buf_(const_cast<void*>(buf))
41 , size_(datatype->size() * count)
51 if(op != MPI_REPLACE && op != MPI_OP_NULL)
55 detached_sender_ = nullptr;
60 if (flags & MPI_REQ_PERSISTENT)
64 nbc_requests_=nullptr;
74 void Request::unref(MPI_Request* request)
76 if((*request) != MPI_REQUEST_NULL){
77 (*request)->refcount_--;
78 if((*request)->refcount_ < 0) {
79 (*request)->print_request("wrong refcount");
80 xbt_die("Whoops, wrong refcount");
82 if((*request)->refcount_==0){
83 if ((*request)->flags_ & MPI_REQ_GENERALIZED){
84 ((*request)->generalized_funcs)->free_fn(((*request)->generalized_funcs)->extra_state);
86 Comm::unref((*request)->comm_);
87 Datatype::unref((*request)->old_type_);
89 if ((*request)->op_!=MPI_REPLACE && (*request)->op_!=MPI_OP_NULL)
90 Op::unref(&(*request)->op_);
92 (*request)->print_request("Destroying");
93 F2C::free_f((*request)->c2f());
95 *request = MPI_REQUEST_NULL;
97 (*request)->print_request("Decrementing");
100 xbt_die("freeing an already free request");
104 bool Request::match_common(MPI_Request req, MPI_Request sender, MPI_Request receiver)
106 xbt_assert(sender, "Cannot match against null sender");
107 xbt_assert(receiver, "Cannot match against null receiver");
108 XBT_DEBUG("Trying to match %s of sender src %d against %d, tag %d against %d, id %d against %d",
109 (req == receiver ? "send" : "recv"), sender->src_, receiver->src_, sender->tag_, receiver->tag_,
110 sender->comm_->id(), receiver->comm_->id());
112 if ((receiver->comm_->id() == MPI_UNDEFINED || sender->comm_->id() == MPI_UNDEFINED ||
113 receiver->comm_->id() == sender->comm_->id()) &&
114 ((receiver->src_ == MPI_ANY_SOURCE && (receiver->comm_->group()->rank(sender->src_) != MPI_UNDEFINED)) ||
115 receiver->src_ == sender->src_) &&
116 ((receiver->tag_ == MPI_ANY_TAG && sender->tag_ >= 0) || receiver->tag_ == sender->tag_)) {
117 // we match, we can transfer some values
118 if (receiver->src_ == MPI_ANY_SOURCE)
119 receiver->real_src_ = sender->src_;
120 if (receiver->tag_ == MPI_ANY_TAG)
121 receiver->real_tag_ = sender->tag_;
122 if (receiver->real_size_ < sender->real_size_ && ((receiver->flags_ & MPI_REQ_PROBE) == 0 )){
123 XBT_DEBUG("Truncating message - should not happen: receiver size : %zu < sender size : %zu", receiver->real_size_, sender->real_size_);
124 receiver->truncated_ = true;
126 if (sender->detached_)
127 receiver->detached_sender_ = sender; // tie the sender to the receiver, as it is detached and has to be freed in
129 req->flags_ |= MPI_REQ_MATCHED; // mark as impossible to cancel anymore
130 XBT_DEBUG("match succeeded");
136 void Request::init_buffer(int count){
137 void *old_buf = nullptr;
138 // FIXME Handle the case of a partial shared malloc.
139 // This part handles the problem of non-contiguous memory (for the unserialization at the reception)
140 if ((((flags_ & MPI_REQ_RECV) != 0) && ((flags_ & MPI_REQ_ACCUMULATE) != 0)) || (old_type_->flags() & DT_FLAG_DERIVED)) {
141 // This part handles the problem of non-contiguous memory
146 buf_ = xbt_malloc(count*old_type_->size());
147 if ((old_type_->flags() & DT_FLAG_DERIVED) && ((flags_ & MPI_REQ_SEND) != 0)) {
148 old_type_->serialize(old_buf, buf_, count);
155 bool Request::match_recv(void* a, void* b, simgrid::kernel::activity::CommImpl*)
157 auto ref = static_cast<MPI_Request>(a);
158 auto req = static_cast<MPI_Request>(b);
159 return match_common(req, req, ref);
162 bool Request::match_send(void* a, void* b, simgrid::kernel::activity::CommImpl*)
164 auto ref = static_cast<MPI_Request>(a);
165 auto req = static_cast<MPI_Request>(b);
166 return match_common(req, ref, req);
169 void Request::print_request(const char* message) const
171 XBT_VERB("%s request %p [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
172 message, this, buf_, size_, src_, dst_, tag_, flags_);
175 /* factories, to hide the internal flags from the caller */
176 MPI_Request Request::bsend_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
178 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
179 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL, tag, comm,
180 MPI_REQ_PERSISTENT | MPI_REQ_SEND | MPI_REQ_PREPARED | MPI_REQ_BSEND);
183 MPI_Request Request::send_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
185 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
186 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL, tag, comm,
187 MPI_REQ_PERSISTENT | MPI_REQ_SEND | MPI_REQ_PREPARED);
190 MPI_Request Request::ssend_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
192 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
193 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL, tag, comm,
194 MPI_REQ_PERSISTENT | MPI_REQ_SSEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
197 MPI_Request Request::isend_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
199 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
200 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL, tag, comm,
201 MPI_REQ_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
205 MPI_Request Request::rma_send_init(const void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
208 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
210 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src)->get_pid(),
211 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL, tag, comm,
212 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
214 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src)->get_pid(),
215 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL, tag, comm,
216 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED |
217 MPI_REQ_ACCUMULATE, op);
222 MPI_Request Request::recv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
224 int source = MPI_PROC_NULL;
225 if (src == MPI_ANY_SOURCE)
226 source = MPI_ANY_SOURCE;
227 else if (src != MPI_PROC_NULL)
228 source = comm->group()->actor(src)->get_pid();
229 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
231 simgrid::s4u::this_actor::get_pid(), tag, comm,
232 MPI_REQ_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
235 MPI_Request Request::rma_recv_init(void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
238 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
239 int source = MPI_PROC_NULL;
240 if (src == MPI_ANY_SOURCE)
241 source = MPI_ANY_SOURCE;
242 else if (src != MPI_PROC_NULL)
243 source = comm->group()->actor(src)->get_pid();
245 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, source,
246 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL, tag, comm,
247 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
249 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, source,
250 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL, tag, comm,
251 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED | MPI_REQ_ACCUMULATE, op);
256 MPI_Request Request::irecv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
258 int source = MPI_PROC_NULL;
259 if (src == MPI_ANY_SOURCE)
260 source = MPI_ANY_SOURCE;
261 else if (src != MPI_PROC_NULL)
262 source = comm->group()->actor(src)->get_pid();
263 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
264 source, simgrid::s4u::this_actor::get_pid(), tag, comm,
265 MPI_REQ_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
268 MPI_Request Request::ibsend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
270 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
271 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
272 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL, tag, comm,
273 MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_BSEND);
274 if(dst != MPI_PROC_NULL)
279 MPI_Request Request::isend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
281 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
282 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
283 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL, tag, comm,
284 MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND);
285 if(dst != MPI_PROC_NULL)
290 MPI_Request Request::issend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
292 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
293 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
294 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL, tag, comm,
295 MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SSEND | MPI_REQ_SEND);
296 if(dst != MPI_PROC_NULL)
302 MPI_Request Request::irecv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
304 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
305 int source = MPI_PROC_NULL;
306 if (src == MPI_ANY_SOURCE)
307 source = MPI_ANY_SOURCE;
308 else if (src != MPI_PROC_NULL)
309 source = comm->group()->actor(src)->get_pid();
310 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
311 source, simgrid::s4u::this_actor::get_pid(), tag, comm,
312 MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV);
313 if(src != MPI_PROC_NULL)
318 void Request::recv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status * status)
320 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
321 request = irecv(buf, count, datatype, src, tag, comm);
322 wait(&request,status);
326 void Request::bsend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
328 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
329 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
330 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL,
331 tag, comm, MPI_REQ_NON_PERSISTENT | MPI_REQ_SEND | MPI_REQ_BSEND);
333 if(dst != MPI_PROC_NULL)
335 wait(&request, MPI_STATUS_IGNORE);
339 void Request::send(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
341 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
342 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
343 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL,
344 tag, comm, MPI_REQ_NON_PERSISTENT | MPI_REQ_SEND);
345 if(dst != MPI_PROC_NULL)
347 wait(&request, MPI_STATUS_IGNORE);
351 void Request::ssend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
353 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
354 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
355 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL, tag, comm,
356 MPI_REQ_NON_PERSISTENT | MPI_REQ_SSEND | MPI_REQ_SEND);
358 if(dst != MPI_PROC_NULL)
360 wait(&request,MPI_STATUS_IGNORE);
364 void Request::sendrecv(const void *sendbuf, int sendcount, MPI_Datatype sendtype,int dst, int sendtag,
365 void *recvbuf, int recvcount, MPI_Datatype recvtype, int src, int recvtag,
366 MPI_Comm comm, MPI_Status * status)
368 int source = MPI_PROC_NULL;
369 if (src == MPI_ANY_SOURCE)
370 source = MPI_ANY_SOURCE;
371 else if (src != MPI_PROC_NULL)
372 source = comm->group()->actor(src)->get_pid();
373 int destination = dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL;
375 std::array<MPI_Request, 2> requests;
376 std::array<MPI_Status, 2> stats;
377 int myid = simgrid::s4u::this_actor::get_pid();
378 if ((destination == myid) && (source == myid)) {
379 Datatype::copy(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
380 if (status != MPI_STATUS_IGNORE) {
381 status->MPI_SOURCE = source;
382 status->MPI_TAG = recvtag;
383 status->MPI_ERROR = MPI_SUCCESS;
384 status->count = sendcount * sendtype->size();
388 requests[0] = isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
389 requests[1] = irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
390 startall(2, requests.data());
391 waitall(2, requests.data(), stats.data());
394 if(status != MPI_STATUS_IGNORE) {
395 // Copy receive status
400 void Request::start()
402 s4u::Mailbox* mailbox;
404 xbt_assert(action_ == nullptr, "Cannot (re-)start unfinished communication");
405 //reinitialize temporary buffer for persistent requests
406 if(real_size_ > 0 && flags_ & MPI_REQ_FINISHED){
408 init_buffer(real_size_/old_type_->size());
410 flags_ &= ~MPI_REQ_PREPARED;
411 flags_ &= ~MPI_REQ_FINISHED;
414 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
416 if ((flags_ & MPI_REQ_RECV) != 0) {
417 this->print_request("New recv");
419 simgrid::smpi::ActorExt* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(dst_));
421 simgrid::s4u::MutexPtr mut = process->mailboxes_mutex();
422 if (smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)
425 if (smpi_cfg_async_small_thresh() == 0 && (flags_ & MPI_REQ_RMA) == 0) {
426 mailbox = process->mailbox();
427 } else if (((flags_ & MPI_REQ_RMA) != 0) || static_cast<int>(size_) < smpi_cfg_async_small_thresh()) {
428 //We have to check both mailboxes (because SSEND messages are sent to the large mbox).
429 //begin with the more appropriate one : the small one.
430 mailbox = process->mailbox_small();
431 XBT_DEBUG("Is there a corresponding send already posted in the small mailbox %s (in case of SSEND)?",
432 mailbox->get_cname());
433 simgrid::kernel::activity::ActivityImplPtr action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
435 if (action == nullptr) {
436 mailbox = process->mailbox();
437 XBT_DEBUG("No, nothing in the small mailbox test the other one : %s", mailbox->get_cname());
438 action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
439 if (action == nullptr) {
440 XBT_DEBUG("Still nothing, switch back to the small mailbox : %s", mailbox->get_cname());
441 mailbox = process->mailbox_small();
444 XBT_DEBUG("yes there was something for us in the large mailbox");
447 mailbox = process->mailbox_small();
448 XBT_DEBUG("Is there a corresponding send already posted the small mailbox?");
449 simgrid::kernel::activity::ActivityImplPtr action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
451 if (action == nullptr) {
452 XBT_DEBUG("No, nothing in the permanent receive mailbox");
453 mailbox = process->mailbox();
455 XBT_DEBUG("yes there was something for us in the small mailbox");
459 action_ = simcall_comm_irecv(
460 process->get_actor()->get_impl(), mailbox->get_impl(), buf_, &real_size_, &match_recv,
461 process->replaying() ? &smpi_comm_null_copy_buffer_callback : smpi_comm_copy_data_callback, this, -1.0);
462 XBT_DEBUG("recv simcall posted");
464 if (smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)
466 } else { /* the RECV flag was not set, so this is a send */
467 const simgrid::smpi::ActorExt* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(dst_));
468 xbt_assert(process, "Actor pid=%d is gone??", dst_);
470 if (TRACE_smpi_view_internals()) {
471 TRACE_smpi_send(rank, rank, dst_, tag_, size_);
473 this->print_request("New send");
476 if ((flags_ & MPI_REQ_SSEND) == 0 &&
477 ((flags_ & MPI_REQ_RMA) != 0 || (flags_ & MPI_REQ_BSEND) != 0 ||
478 static_cast<int>(size_) < smpi_cfg_detached_send_thresh())) {
479 void *oldbuf = nullptr;
481 XBT_DEBUG("Send request %p is detached", this);
483 if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
485 if (not process->replaying() && oldbuf != nullptr && size_ != 0) {
486 if ((smpi_cfg_privatization() != SmpiPrivStrategies::NONE) &&
487 (static_cast<char*>(buf_) >= smpi_data_exe_start) &&
488 (static_cast<char*>(buf_) < smpi_data_exe_start + smpi_data_exe_size)) {
489 XBT_DEBUG("Privatization : We are sending from a zone inside global memory. Switch data segment ");
490 smpi_switch_data_segment(simgrid::s4u::Actor::by_pid(src_));
492 //we need this temporary buffer even for bsend, as it will be released in the copy callback and we don't have a way to differentiate it
493 //so actually ... don't use manually attached buffer space.
494 buf = xbt_malloc(size_);
495 memcpy(buf,oldbuf,size_);
496 XBT_DEBUG("buf %p copied into %p",oldbuf,buf);
501 //if we are giving back the control to the user without waiting for completion, we have to inject timings
502 double sleeptime = 0.0;
503 if (detached_ || ((flags_ & (MPI_REQ_ISEND | MPI_REQ_SSEND)) != 0)) { // issend should be treated as isend
504 // isend and send timings may be different
505 sleeptime = ((flags_ & MPI_REQ_ISEND) != 0)
506 ? simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->oisend(size_)
507 : simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->osend(size_);
511 simgrid::s4u::this_actor::sleep_for(sleeptime);
512 XBT_DEBUG("sending size of %zu : sleep %f ", size_, sleeptime);
515 simgrid::s4u::MutexPtr mut = process->mailboxes_mutex();
517 if (smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)
520 if (not(smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)) {
521 mailbox = process->mailbox();
522 } else if (((flags_ & MPI_REQ_RMA) != 0) || static_cast<int>(size_) < smpi_cfg_async_small_thresh()) { // eager mode
523 mailbox = process->mailbox();
524 XBT_DEBUG("Is there a corresponding recv already posted in the large mailbox %s?", mailbox->get_cname());
525 simgrid::kernel::activity::ActivityImplPtr action = mailbox->iprobe(1, &match_send, static_cast<void*>(this));
526 if (action == nullptr) {
527 if ((flags_ & MPI_REQ_SSEND) == 0) {
528 mailbox = process->mailbox_small();
529 XBT_DEBUG("No, nothing in the large mailbox, message is to be sent on the small one %s",
530 mailbox->get_cname());
532 mailbox = process->mailbox_small();
533 XBT_DEBUG("SSEND : Is there a corresponding recv already posted in the small mailbox %s?",
534 mailbox->get_cname());
535 action = mailbox->iprobe(1, &match_send, static_cast<void*>(this));
536 if (action == nullptr) {
537 XBT_DEBUG("No, we are first, send to large mailbox");
538 mailbox = process->mailbox();
542 XBT_DEBUG("Yes there was something for us in the large mailbox");
545 mailbox = process->mailbox();
546 XBT_DEBUG("Send request %p is in the large mailbox %s (buf: %p)", this, mailbox->get_cname(), buf_);
549 size_t payload_size_ = size_ + 16;//MPI enveloppe size (tag+dest+communicator)
550 action_ = simcall_comm_isend(
551 simgrid::s4u::Actor::by_pid(src_)->get_impl(), mailbox->get_impl(), payload_size_, -1.0, buf, real_size_, &match_send,
552 &xbt_free_f, // how to free the userdata if a detached send fails
553 process->replaying() ? &smpi_comm_null_copy_buffer_callback : smpi_comm_copy_data_callback, this,
554 // detach if msg size < eager/rdv switch limit
556 XBT_DEBUG("send simcall posted");
558 /* FIXME: detached sends are not traceable (action_ == nullptr) */
559 if (action_ != nullptr) {
560 boost::static_pointer_cast<kernel::activity::CommImpl>(action_)->set_tracing_category(
561 smpi_process()->get_tracing_category());
564 if (smpi_cfg_async_small_thresh() != 0 || ((flags_ & MPI_REQ_RMA) != 0))
569 void Request::startall(int count, MPI_Request * requests)
571 if(requests== nullptr)
574 for(int i = 0; i < count; i++) {
575 if(requests[i]->src_ != MPI_PROC_NULL && requests[i]->dst_ != MPI_PROC_NULL)
576 requests[i]->start();
580 void Request::cancel()
582 this->flags_ |= MPI_REQ_CANCELLED;
583 if (this->action_ != nullptr)
584 (boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(this->action_))->cancel();
587 int Request::test(MPI_Request * request, MPI_Status * status, int* flag) {
588 // assume that *request is not MPI_REQUEST_NULL (filtered in PMPI_Test or testall before)
589 // to avoid deadlocks if used as a break condition, such as
590 // while (MPI_Test(request, flag, status) && flag) dostuff...
591 // because the time will not normally advance when only calls to MPI_Test are made -> deadlock
592 // multiplier to the sleeptime, to increase speed of execution, each failed test will increase it
593 xbt_assert(*request != MPI_REQUEST_NULL);
595 static int nsleeps = 1;
596 int ret = MPI_SUCCESS;
598 // Are we testing a request meant for non blocking collectives ?
599 // If so, test all the subrequests.
600 if ((*request)->nbc_requests_size_>0){
601 ret = testall((*request)->nbc_requests_size_, (*request)->nbc_requests_, flag, MPI_STATUSES_IGNORE);
603 delete[] (*request)->nbc_requests_;
604 (*request)->nbc_requests_size_=0;
610 if(smpi_test_sleep > 0)
611 simgrid::s4u::this_actor::sleep_for(nsleeps * smpi_test_sleep);
613 Status::empty(status);
615 if (((*request)->flags_ & (MPI_REQ_PREPARED | MPI_REQ_FINISHED)) == 0) {
616 if ((*request)->action_ != nullptr && ((*request)->flags_ & MPI_REQ_CANCELLED) == 0){
618 *flag = simcall_comm_test((*request)->action_.get());
619 } catch (const Exception&) {
624 if (((*request)->flags_ & MPI_REQ_GENERALIZED) && !((*request)->flags_ & MPI_REQ_COMPLETE))
627 finish_wait(request, status); // may invalidate *request
628 if (*request != MPI_REQUEST_NULL && ((*request)->flags_ & MPI_REQ_GENERALIZED)){
629 MPI_Status tmp_status;
630 MPI_Status* mystatus;
631 if (status == MPI_STATUS_IGNORE) {
632 mystatus = &tmp_status;
633 Status::empty(mystatus);
637 ret = ((*request)->generalized_funcs)->query_fn(((*request)->generalized_funcs)->extra_state, mystatus);
639 nsleeps=1;//reset the number of sleeps we will do next time
640 if (*request != MPI_REQUEST_NULL && ((*request)->flags_ & MPI_REQ_PERSISTENT) == 0)
641 *request = MPI_REQUEST_NULL;
642 } else if (smpi_cfg_grow_injected_times()) {
649 int Request::testsome(int incount, MPI_Request requests[], int *count, int *indices, MPI_Status status[])
655 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
658 for (int i = 0; i < incount; i++) {
659 if (requests[i] != MPI_REQUEST_NULL && not (requests[i]->flags_ & MPI_REQ_FINISHED)) {
660 int ret = test(&requests[i], pstat, &flag);
665 if (status != MPI_STATUSES_IGNORE)
666 status[*count] = *pstat;
668 if ((requests[i] != MPI_REQUEST_NULL) && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
669 requests[i] = MPI_REQUEST_NULL;
675 if(count_dead==incount)*count=MPI_UNDEFINED;
677 return MPI_ERR_IN_STATUS;
682 int Request::testany(int count, MPI_Request requests[], int *index, int* flag, MPI_Status * status)
684 std::vector<simgrid::kernel::activity::CommImpl*> comms;
685 comms.reserve(count);
689 int ret = MPI_SUCCESS;
690 *index = MPI_UNDEFINED;
692 std::vector<int> map; /** Maps all matching comms back to their location in requests **/
693 for(i = 0; i < count; i++) {
694 if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->action_ && not(requests[i]->flags_ & MPI_REQ_PREPARED)) {
695 comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(requests[i]->action_.get()));
699 if (not map.empty()) {
700 //multiplier to the sleeptime, to increase speed of execution, each failed testany will increase it
701 static int nsleeps = 1;
702 if(smpi_test_sleep > 0)
703 simgrid::s4u::this_actor::sleep_for(nsleeps * smpi_test_sleep);
705 i = simcall_comm_testany(comms.data(), comms.size()); // The i-th element in comms matches!
706 } catch (const Exception&) {
707 XBT_DEBUG("Exception in testany");
711 if (i != -1) { // -1 is not MPI_UNDEFINED but a SIMIX return code. (nothing matches)
713 if (requests[*index] != MPI_REQUEST_NULL &&
714 (requests[*index]->flags_ & MPI_REQ_GENERALIZED)
715 && !(requests[*index]->flags_ & MPI_REQ_COMPLETE)) {
718 finish_wait(&requests[*index],status);
719 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags_ & MPI_REQ_GENERALIZED)){
720 MPI_Status tmp_status;
721 MPI_Status* mystatus;
722 if (status == MPI_STATUS_IGNORE) {
723 mystatus = &tmp_status;
724 Status::empty(mystatus);
728 ret=(requests[*index]->generalized_funcs)->query_fn((requests[*index]->generalized_funcs)->extra_state, mystatus);
731 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags_ & MPI_REQ_NON_PERSISTENT))
732 requests[*index] = MPI_REQUEST_NULL;
733 XBT_DEBUG("Testany - returning with index %d", *index);
741 XBT_DEBUG("Testany on inactive handles, returning flag=1 but empty status");
742 //all requests are null or inactive, return true
744 *index = MPI_UNDEFINED;
745 Status::empty(status);
751 int Request::testall(int count, MPI_Request requests[], int* outflag, MPI_Status status[])
754 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
758 for(int i=0; i<count; i++){
759 if (requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & MPI_REQ_PREPARED)) {
760 int ret = test(&requests[i], pstat, &flag);
763 requests[i]=MPI_REQUEST_NULL;
767 if (ret != MPI_SUCCESS)
770 Status::empty(pstat);
772 if(status != MPI_STATUSES_IGNORE) {
777 return MPI_ERR_IN_STATUS;
782 void Request::probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
784 //FIXME find another way to avoid busy waiting ?
785 // the issue here is that we have to wait on a nonexistent comm
787 iprobe(source, tag, comm, &flag, status);
788 XBT_DEBUG("Busy Waiting on probing : %d", flag);
792 void Request::iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
793 // to avoid deadlock, we have to sleep some time here, or the timer won't advance and we will only do iprobe simcalls
794 // especially when used as a break condition, such as while (MPI_Iprobe(...)) dostuff...
795 // nsleeps is a multiplier to the sleeptime, to increase speed of execution, each failed iprobe will increase it
796 // This can speed up the execution of certain applications by an order of magnitude, such as HPL
797 static int nsleeps = 1;
798 double speed = s4u::this_actor::get_host()->get_speed();
799 double maxrate = smpi_cfg_iprobe_cpu_usage();
800 auto request = new Request(nullptr, 0, MPI_CHAR,
801 source == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : comm->group()->actor(source)->get_pid(),
802 simgrid::s4u::this_actor::get_pid(), tag, comm, MPI_REQ_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PROBE);
803 if (smpi_iprobe_sleep > 0) {
804 /** Compute the number of flops we will sleep **/
805 s4u::this_actor::exec_init(/*nsleeps: See comment above */ nsleeps *
806 /*(seconds * flop/s -> total flops)*/ smpi_iprobe_sleep * speed * maxrate)
808 /* Not the entire CPU can be used when iprobing: This is important for
809 * the energy consumption caused by polling with iprobes.
810 * Note also that the number of flops that was
811 * computed above contains a maxrate factor and is hence reduced (maxrate < 1)
813 ->set_bound(maxrate*speed)
817 // behave like a receive, but don't do it
818 s4u::Mailbox* mailbox;
820 request->print_request("New iprobe");
821 // We have to test both mailboxes as we don't know if we will receive one or another
822 if (smpi_cfg_async_small_thresh() > 0) {
823 mailbox = smpi_process()->mailbox_small();
824 XBT_DEBUG("Trying to probe the perm recv mailbox");
825 request->action_ = mailbox->iprobe(0, &match_recv, static_cast<void*>(request));
828 if (request->action_ == nullptr){
829 mailbox = smpi_process()->mailbox();
830 XBT_DEBUG("trying to probe the other mailbox");
831 request->action_ = mailbox->iprobe(0, &match_recv, static_cast<void*>(request));
834 if (request->action_ != nullptr){
835 kernel::activity::CommImplPtr sync_comm = boost::static_pointer_cast<kernel::activity::CommImpl>(request->action_);
836 const Request* req = static_cast<MPI_Request>(sync_comm->src_data_);
838 if (status != MPI_STATUS_IGNORE && (req->flags_ & MPI_REQ_PREPARED) == 0) {
839 status->MPI_SOURCE = comm->group()->rank(req->src_);
840 status->MPI_TAG = req->tag_;
841 status->MPI_ERROR = MPI_SUCCESS;
842 status->count = req->real_size_;
844 nsleeps = 1;//reset the number of sleeps we will do next time
848 if (smpi_cfg_grow_injected_times())
852 xbt_assert(request == MPI_REQUEST_NULL);
855 void Request::finish_wait(MPI_Request* request, MPI_Status * status)
857 MPI_Request req = *request;
858 Status::empty(status);
859 if((req->flags_ & MPI_REQ_CANCELLED) != 0 && (req->flags_ & MPI_REQ_MATCHED) == 0) {
860 if (status!=MPI_STATUS_IGNORE)
862 if(req->detached_sender_ != nullptr)
863 unref(&(req->detached_sender_));
866 } else if ((req->flags_ & MPI_REQ_CANCELLED) != 0){
867 XBT_WARN("tatatata");
870 if ((req->flags_ & (MPI_REQ_PREPARED | MPI_REQ_GENERALIZED | MPI_REQ_FINISHED)) == 0) {
871 if (status != MPI_STATUS_IGNORE) {
872 if (req->src_== MPI_PROC_NULL || req->dst_== MPI_PROC_NULL){
873 Status::empty(status);
874 status->MPI_SOURCE = MPI_PROC_NULL;
876 int src = req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_;
877 status->MPI_SOURCE = req->comm_->group()->rank(src);
878 status->MPI_TAG = req->tag_ == MPI_ANY_TAG ? req->real_tag_ : req->tag_;
879 status->MPI_ERROR = req->truncated_ ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
881 // this handles the case were size in receive differs from size in send
882 status->count = req->real_size_;
884 //detached send will be finished at the other end
885 if (not(req->detached_ && ((req->flags_ & MPI_REQ_SEND) != 0))) {
886 req->print_request("Finishing");
887 MPI_Datatype datatype = req->old_type_;
889 // FIXME Handle the case of a partial shared malloc.
890 if (((req->flags_ & MPI_REQ_ACCUMULATE) != 0) ||
891 (datatype->flags() & DT_FLAG_DERIVED)) { // && (not smpi_is_shared(req->old_buf_))){
892 if (not smpi_process()->replaying() && smpi_cfg_privatization() != SmpiPrivStrategies::NONE &&
893 static_cast<char*>(req->old_buf_) >= smpi_data_exe_start &&
894 static_cast<char*>(req->old_buf_) < smpi_data_exe_start + smpi_data_exe_size) {
895 XBT_VERB("Privatization : We are unserializing to a zone in global memory Switch data segment ");
896 smpi_switch_data_segment(simgrid::s4u::Actor::self());
899 if(datatype->flags() & DT_FLAG_DERIVED){
900 // This part handles the problem of non-contiguous memory the unserialization at the reception
901 if ((req->flags_ & MPI_REQ_RECV) && datatype->size() != 0)
902 datatype->unserialize(req->buf_, req->old_buf_, req->real_size_/datatype->size() , req->op_);
905 } else if (req->flags_ & MPI_REQ_RECV) { // apply op on contiguous buffer for accumulate
906 if (datatype->size() != 0) {
907 int n = req->real_size_ / datatype->size();
908 req->op_->apply(req->buf_, req->old_buf_, &n, datatype);
917 if (TRACE_smpi_view_internals() && ((req->flags_ & MPI_REQ_RECV) != 0)) {
918 int rank = simgrid::s4u::this_actor::get_pid();
919 int src_traced = (req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_);
920 TRACE_smpi_recv(src_traced, rank,req->tag_);
922 if(req->detached_sender_ != nullptr){
923 //integrate pseudo-timing for buffering of small messages, do not bother to execute the simcall if 0
925 simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->orecv(req->real_size());
926 if (sleeptime > 0.0) {
927 simgrid::s4u::this_actor::sleep_for(sleeptime);
928 XBT_DEBUG("receiving size of %zu : sleep %f ", req->real_size_, sleeptime);
930 unref(&(req->detached_sender_));
932 if (req->flags_ & MPI_REQ_PERSISTENT)
933 req->action_ = nullptr;
934 req->flags_ |= MPI_REQ_FINISHED;
936 if (req->truncated_) {
937 char error_string[MPI_MAX_ERROR_STRING];
939 PMPI_Error_string(MPI_ERR_TRUNCATE, error_string, &error_size);
940 MPI_Errhandler err = (req->comm_) ? (req->comm_)->errhandler() : MPI_ERRHANDLER_NULL;
941 if (err == MPI_ERRHANDLER_NULL || err == MPI_ERRORS_RETURN)
942 XBT_WARN("recv - returned %.*s instead of MPI_SUCCESS", error_size, error_string);
943 else if (err == MPI_ERRORS_ARE_FATAL)
944 xbt_die("recv - returned %.*s instead of MPI_SUCCESS", error_size, error_string);
946 err->call((req->comm_), MPI_ERR_TRUNCATE);
947 if (err != MPI_ERRHANDLER_NULL)
948 simgrid::smpi::Errhandler::unref(err);
949 MC_assert(not MC_is_active()); /* Only fail in MC mode */
951 if(req->src_ != MPI_PROC_NULL && req->dst_ != MPI_PROC_NULL)
955 int Request::wait(MPI_Request * request, MPI_Status * status)
957 // assume that *request is not MPI_REQUEST_NULL (filtered in PMPI_Wait before)
958 xbt_assert(*request != MPI_REQUEST_NULL);
962 if((*request)->src_ == MPI_PROC_NULL || (*request)->dst_ == MPI_PROC_NULL){
963 if (status != MPI_STATUS_IGNORE) {
964 Status::empty(status);
965 status->MPI_SOURCE = MPI_PROC_NULL;
967 (*request)=MPI_REQUEST_NULL;
970 // Are we waiting on a request meant for non blocking collectives ?
971 // If so, wait for all the subrequests.
972 if ((*request)->nbc_requests_size_>0){
973 ret = waitall((*request)->nbc_requests_size_, (*request)->nbc_requests_, MPI_STATUSES_IGNORE);
974 for (int i = 0; i < (*request)->nbc_requests_size_; i++) {
975 if((*request)->buf_!=nullptr && (*request)->nbc_requests_[i]!=MPI_REQUEST_NULL){//reduce case
976 void * buf=(*request)->nbc_requests_[i]->buf_;
977 if((*request)->old_type_->flags() & DT_FLAG_DERIVED)
978 buf=(*request)->nbc_requests_[i]->old_buf_;
979 if((*request)->nbc_requests_[i]->flags_ & MPI_REQ_RECV ){
980 if((*request)->op_!=MPI_OP_NULL){
981 int count=(*request)->size_/ (*request)->old_type_->size();
982 (*request)->op_->apply(buf, (*request)->buf_, &count, (*request)->old_type_);
984 smpi_free_tmp_buffer(static_cast<unsigned char*>(buf));
987 if((*request)->nbc_requests_[i]!=MPI_REQUEST_NULL)
988 Request::unref(&((*request)->nbc_requests_[i]));
990 delete[] (*request)->nbc_requests_;
991 (*request)->nbc_requests_size_=0;
993 (*request)=MPI_REQUEST_NULL;
997 (*request)->print_request("Waiting");
998 if ((*request)->flags_ & (MPI_REQ_PREPARED | MPI_REQ_FINISHED)) {
999 Status::empty(status);
1003 if ((*request)->action_ != nullptr){
1005 // this is not a detached send
1006 simcall_comm_wait((*request)->action_.get(), -1.0);
1007 } catch (const Exception&) {
1008 XBT_VERB("Request cancelled");
1012 if ((*request)->flags_ & MPI_REQ_GENERALIZED) {
1013 if(!((*request)->flags_ & MPI_REQ_COMPLETE)){
1014 ((*request)->generalized_funcs)->mutex->lock();
1015 ((*request)->generalized_funcs)->cond->wait(((*request)->generalized_funcs)->mutex);
1016 ((*request)->generalized_funcs)->mutex->unlock();
1018 MPI_Status tmp_status;
1019 MPI_Status* mystatus;
1020 if (status == MPI_STATUS_IGNORE) {
1021 mystatus = &tmp_status;
1022 Status::empty(mystatus);
1026 ret = ((*request)->generalized_funcs)->query_fn(((*request)->generalized_funcs)->extra_state, mystatus);
1029 finish_wait(request, status); // may invalidate *request
1030 if (*request != MPI_REQUEST_NULL && (((*request)->flags_ & MPI_REQ_NON_PERSISTENT) != 0))
1031 *request = MPI_REQUEST_NULL;
1035 int Request::waitany(int count, MPI_Request requests[], MPI_Status * status)
1037 int index = MPI_UNDEFINED;
1040 // Wait for a request to complete
1041 std::vector<simgrid::kernel::activity::CommImpl*> comms;
1042 std::vector<int> map;
1043 XBT_DEBUG("Wait for one of %d", count);
1044 for(int i = 0; i < count; i++) {
1045 if (requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & MPI_REQ_PREPARED) &&
1046 not(requests[i]->flags_ & MPI_REQ_FINISHED)) {
1047 if (requests[i]->action_ != nullptr) {
1048 XBT_DEBUG("Waiting any %p ", requests[i]);
1049 comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(requests[i]->action_.get()));
1052 // This is a finished detached request, let's return this one
1053 comms.clear(); // don't do the waitany call afterwards
1055 finish_wait(&requests[i], status); // cleanup if refcount = 0
1056 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
1057 requests[i] = MPI_REQUEST_NULL; // set to null
1062 if (not comms.empty()) {
1063 XBT_DEBUG("Enter waitany for %zu comms", comms.size());
1066 i = simcall_comm_waitany(comms.data(), comms.size(), -1);
1067 } catch (const Exception&) {
1068 XBT_INFO("request cancelled");
1072 // not MPI_UNDEFINED, as this is a simix return code
1075 //in case of an accumulate, we have to wait the end of all requests to apply the operation, ordered correctly.
1076 if ((requests[index] == MPI_REQUEST_NULL) ||
1077 (not((requests[index]->flags_ & MPI_REQ_ACCUMULATE) && (requests[index]->flags_ & MPI_REQ_RECV)))) {
1078 finish_wait(&requests[index],status);
1079 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_NON_PERSISTENT))
1080 requests[index] = MPI_REQUEST_NULL;
1086 if (index==MPI_UNDEFINED)
1087 Status::empty(status);
1092 static int sort_accumulates(const Request* a, const Request* b)
1094 return (a->tag() > b->tag());
1097 int Request::waitall(int count, MPI_Request requests[], MPI_Status status[])
1099 std::vector<MPI_Request> accumulates;
1102 MPI_Status *pstat = (status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat);
1103 int retvalue = MPI_SUCCESS;
1104 //tag invalid requests in the set
1105 if (status != MPI_STATUSES_IGNORE) {
1106 for (int c = 0; c < count; c++) {
1107 if (requests[c] == MPI_REQUEST_NULL || requests[c]->dst_ == MPI_PROC_NULL ||
1108 (requests[c]->flags_ & MPI_REQ_PREPARED)) {
1109 Status::empty(&status[c]);
1110 } else if (requests[c]->src_ == MPI_PROC_NULL) {
1111 Status::empty(&status[c]);
1112 status[c].MPI_SOURCE = MPI_PROC_NULL;
1116 for (int c = 0; c < count; c++) {
1117 if (MC_is_active() || MC_record_replay_is_active()) {
1118 wait(&requests[c],pstat);
1121 index = waitany(count, requests, pstat);
1123 if (index == MPI_UNDEFINED)
1126 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_RECV) &&
1127 (requests[index]->flags_ & MPI_REQ_ACCUMULATE))
1128 accumulates.push_back(requests[index]);
1129 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_NON_PERSISTENT))
1130 requests[index] = MPI_REQUEST_NULL;
1132 if (status != MPI_STATUSES_IGNORE) {
1133 status[index] = *pstat;
1134 if (status[index].MPI_ERROR == MPI_ERR_TRUNCATE)
1135 retvalue = MPI_ERR_IN_STATUS;
1139 if (not accumulates.empty()) {
1140 std::sort(accumulates.begin(), accumulates.end(), sort_accumulates);
1141 for (auto& req : accumulates) {
1142 finish_wait(&req, status);
1149 int Request::waitsome(int incount, MPI_Request requests[], int *indices, MPI_Status status[])
1155 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
1156 index = waitany(incount, requests, pstat);
1157 if(index==MPI_UNDEFINED) return MPI_UNDEFINED;
1158 if(status != MPI_STATUSES_IGNORE) {
1159 status[count] = *pstat;
1161 indices[count] = index;
1163 for (int i = 0; i < incount; i++) {
1164 if (i!=index && requests[i] != MPI_REQUEST_NULL
1165 && not(requests[i]->flags_ & MPI_REQ_FINISHED)) {
1166 test(&requests[i], pstat,&flag);
1169 if(status != MPI_STATUSES_IGNORE) {
1170 status[count] = *pstat;
1172 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
1173 requests[i]=MPI_REQUEST_NULL;
1181 MPI_Request Request::f2c(int id)
1183 if(id==MPI_FORTRAN_REQUEST_NULL)
1184 return MPI_REQUEST_NULL;
1185 return static_cast<MPI_Request>(F2C::lookup()->at(id));
1188 void Request::free_f(int id)
1190 if (id != MPI_FORTRAN_REQUEST_NULL) {
1191 F2C::lookup()->erase(id);
1195 int Request::get_status(const Request* req, int* flag, MPI_Status* status)
1199 if(req != MPI_REQUEST_NULL && req->action_ != nullptr) {
1200 req->iprobe(req->src_, req->tag_, req->comm_, flag, status);
1204 if (req != MPI_REQUEST_NULL &&
1205 (req->flags_ & MPI_REQ_GENERALIZED)
1206 && !(req->flags_ & MPI_REQ_COMPLETE)) {
1212 if(req != MPI_REQUEST_NULL &&
1213 status != MPI_STATUS_IGNORE) {
1214 int src = req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_;
1215 status->MPI_SOURCE = req->comm_->group()->rank(src);
1216 status->MPI_TAG = req->tag_ == MPI_ANY_TAG ? req->real_tag_ : req->tag_;
1217 status->MPI_ERROR = req->truncated_ ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
1218 status->count = req->real_size_;
1223 int Request::grequest_start(MPI_Grequest_query_function* query_fn, MPI_Grequest_free_function* free_fn,
1224 MPI_Grequest_cancel_function* cancel_fn, void* extra_state, MPI_Request* request)
1226 *request = new Request();
1227 (*request)->flags_ |= MPI_REQ_GENERALIZED;
1228 (*request)->flags_ |= MPI_REQ_PERSISTENT;
1229 (*request)->refcount_ = 1;
1230 ((*request)->generalized_funcs) = std::make_unique<smpi_mpi_generalized_request_funcs_t>();
1231 ((*request)->generalized_funcs)->query_fn=query_fn;
1232 ((*request)->generalized_funcs)->free_fn=free_fn;
1233 ((*request)->generalized_funcs)->cancel_fn=cancel_fn;
1234 ((*request)->generalized_funcs)->extra_state=extra_state;
1235 ((*request)->generalized_funcs)->cond = simgrid::s4u::ConditionVariable::create();
1236 ((*request)->generalized_funcs)->mutex = simgrid::s4u::Mutex::create();
1240 int Request::grequest_complete(MPI_Request request)
1242 if ((!(request->flags_ & MPI_REQ_GENERALIZED)) || request->generalized_funcs->mutex == nullptr)
1243 return MPI_ERR_REQUEST;
1244 request->generalized_funcs->mutex->lock();
1245 request->flags_ |= MPI_REQ_COMPLETE; // in case wait would be called after complete
1246 request->generalized_funcs->cond->notify_one();
1247 request->generalized_funcs->mutex->unlock();
1251 void Request::set_nbc_requests(MPI_Request* reqs, int size){
1252 nbc_requests_size_ = size;
1254 nbc_requests_ = reqs;
1257 nbc_requests_ = nullptr;
1261 int Request::get_nbc_requests_size() const
1263 return nbc_requests_size_;
1266 MPI_Request* Request::get_nbc_requests() const
1268 return nbc_requests_;