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 "simgrid/s4u/Mutex.hpp"
13 #include "simgrid/s4u/ConditionVariable.hpp"
14 #include "smpi_comm.hpp"
15 #include "smpi_datatype.hpp"
16 #include "smpi_host.hpp"
17 #include "smpi_op.hpp"
18 #include "src/kernel/activity/CommImpl.hpp"
19 #include "src/mc/mc_replay.hpp"
20 #include "src/smpi/include/smpi_actor.hpp"
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_request, smpi, "Logging specific to SMPI (request)");
27 static simgrid::config::Flag<double> smpi_iprobe_sleep(
28 "smpi/iprobe", "Minimum time to inject inside a call to MPI_Iprobe", 1e-4);
29 static simgrid::config::Flag<double> smpi_test_sleep(
30 "smpi/test", "Minimum time to inject inside a call to MPI_Test", 1e-4);
32 std::vector<s_smpi_factor_t> smpi_ois_values;
34 extern void (*smpi_comm_copy_data_callback)(simgrid::kernel::activity::CommImpl*, void*, size_t);
39 Request::Request(const void* buf, int count, MPI_Datatype datatype, aid_t src, aid_t dst, int tag, MPI_Comm comm,
40 unsigned flags, MPI_Op op)
41 : buf_(const_cast<void*>(buf))
43 , size_(datatype->size() * count)
53 if(op != MPI_REPLACE && op != MPI_OP_NULL)
57 detached_sender_ = nullptr;
62 if (flags & MPI_REQ_PERSISTENT)
66 nbc_requests_=nullptr;
76 void Request::unref(MPI_Request* request)
78 xbt_assert(*request != MPI_REQUEST_NULL, "freeing an already free request");
80 (*request)->refcount_--;
81 if ((*request)->refcount_ < 0) {
82 (*request)->print_request("wrong refcount");
83 xbt_die("Whoops, wrong refcount");
85 if ((*request)->refcount_ == 0) {
86 if ((*request)->flags_ & MPI_REQ_GENERALIZED) {
87 ((*request)->generalized_funcs)->free_fn(((*request)->generalized_funcs)->extra_state);
89 Comm::unref((*request)->comm_);
90 Datatype::unref((*request)->old_type_);
92 if ((*request)->op_ != MPI_REPLACE && (*request)->op_ != MPI_OP_NULL)
93 Op::unref(&(*request)->op_);
95 (*request)->print_request("Destroying");
96 F2C::free_f((*request)->f2c_id());
98 *request = MPI_REQUEST_NULL;
100 (*request)->print_request("Decrementing");
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 %ld against %ld, 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->flags_ & MPI_REQ_PROBE) == 0 ){
123 if (receiver->real_size_ < sender->real_size_){
124 XBT_DEBUG("Truncating message - should not happen: receiver size : %zu < sender size : %zu", receiver->real_size_, sender->real_size_);
125 receiver->truncated_ = true;
126 } else if (receiver->real_size_ > sender->real_size_){
127 receiver->real_size_=sender->real_size_;
130 if (sender->detached_)
131 receiver->detached_sender_ = sender; // tie the sender to the receiver, as it is detached and has to be freed in
133 req->flags_ |= MPI_REQ_MATCHED; // mark as impossible to cancel anymore
134 XBT_DEBUG("match succeeded");
140 void Request::init_buffer(int count){
141 void *old_buf = nullptr;
142 // FIXME Handle the case of a partial shared malloc.
143 // This part handles the problem of non-contiguous memory (for the unserialization at the reception)
144 if ((((flags_ & MPI_REQ_RECV) != 0) && ((flags_ & MPI_REQ_ACCUMULATE) != 0)) || (old_type_->flags() & DT_FLAG_DERIVED)) {
145 // This part handles the problem of non-contiguous memory
150 buf_ = xbt_malloc(count*old_type_->size());
151 if ((old_type_->flags() & DT_FLAG_DERIVED) && ((flags_ & MPI_REQ_SEND) != 0)) {
152 old_type_->serialize(old_buf, buf_, count);
159 bool Request::match_recv(void* a, void* b, simgrid::kernel::activity::CommImpl*)
161 auto ref = static_cast<MPI_Request>(a);
162 auto req = static_cast<MPI_Request>(b);
163 return match_common(req, req, ref);
166 bool Request::match_send(void* a, void* b, simgrid::kernel::activity::CommImpl*)
168 auto ref = static_cast<MPI_Request>(a);
169 auto req = static_cast<MPI_Request>(b);
170 return match_common(req, ref, req);
173 void Request::print_request(const char* message) const
175 XBT_VERB("%s request %p [buf = %p, size = %zu, src = %ld, dst = %ld, tag = %d, flags = %x]", message, this, buf_,
176 size_, src_, dst_, tag_, flags_);
179 /* factories, to hide the internal flags from the caller */
180 MPI_Request Request::bsend_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
182 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
183 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
184 MPI_REQ_PERSISTENT | MPI_REQ_SEND | MPI_REQ_PREPARED | MPI_REQ_BSEND);
187 MPI_Request Request::send_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
189 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
190 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
191 MPI_REQ_PERSISTENT | MPI_REQ_SEND | MPI_REQ_PREPARED);
194 MPI_Request Request::ssend_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
196 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
197 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
198 MPI_REQ_PERSISTENT | MPI_REQ_SSEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
201 MPI_Request Request::isend_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
203 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
204 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
205 MPI_REQ_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
208 MPI_Request Request::rma_send_init(const void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
211 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
213 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src),
214 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
215 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
217 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src),
218 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
219 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED |
226 MPI_Request Request::recv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
228 aid_t source = MPI_PROC_NULL;
229 if (src == MPI_ANY_SOURCE)
230 source = MPI_ANY_SOURCE;
231 else if (src != MPI_PROC_NULL)
232 source = comm->group()->actor(src);
233 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
235 simgrid::s4u::this_actor::get_pid(), tag, comm,
236 MPI_REQ_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
239 MPI_Request Request::rma_recv_init(void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
242 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
243 aid_t source = MPI_PROC_NULL;
244 if (src == MPI_ANY_SOURCE)
245 source = MPI_ANY_SOURCE;
246 else if (src != MPI_PROC_NULL)
247 source = comm->group()->actor(src);
249 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, source,
250 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
251 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
254 new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, source,
255 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
256 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED | MPI_REQ_ACCUMULATE, op);
261 MPI_Request Request::irecv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
263 aid_t source = MPI_PROC_NULL;
264 if (src == MPI_ANY_SOURCE)
265 source = MPI_ANY_SOURCE;
266 else if (src != MPI_PROC_NULL)
267 source = comm->group()->actor(src);
268 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
269 source, simgrid::s4u::this_actor::get_pid(), tag, comm,
270 MPI_REQ_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
273 MPI_Request Request::ibsend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
275 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
276 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
277 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
278 MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_BSEND);
279 if(dst != MPI_PROC_NULL)
284 MPI_Request Request::isend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
286 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
287 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
288 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
289 MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND);
290 if(dst != MPI_PROC_NULL)
295 MPI_Request Request::issend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
297 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
298 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
299 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
300 MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SSEND | MPI_REQ_SEND);
301 if(dst != MPI_PROC_NULL)
306 MPI_Request Request::irecv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
308 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
309 aid_t source = MPI_PROC_NULL;
310 if (src == MPI_ANY_SOURCE)
311 source = MPI_ANY_SOURCE;
312 else if (src != MPI_PROC_NULL)
313 source = comm->group()->actor(src);
314 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
315 source, simgrid::s4u::this_actor::get_pid(), tag, comm,
316 MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV);
317 if(src != MPI_PROC_NULL)
322 int Request::recv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status * status)
324 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
325 request = irecv(buf, count, datatype, src, tag, comm);
326 int retval = wait(&request,status);
331 void Request::bsend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
333 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
334 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
335 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
336 MPI_REQ_NON_PERSISTENT | MPI_REQ_SEND | MPI_REQ_BSEND);
338 if(dst != MPI_PROC_NULL)
340 wait(&request, MPI_STATUS_IGNORE);
344 void Request::send(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
346 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
347 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
348 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
349 MPI_REQ_NON_PERSISTENT | MPI_REQ_SEND);
350 if(dst != MPI_PROC_NULL)
352 wait(&request, MPI_STATUS_IGNORE);
356 void Request::ssend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
358 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
359 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
360 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
361 MPI_REQ_NON_PERSISTENT | MPI_REQ_SSEND | MPI_REQ_SEND);
363 if(dst != MPI_PROC_NULL)
365 wait(&request,MPI_STATUS_IGNORE);
369 void Request::sendrecv(const void *sendbuf, int sendcount, MPI_Datatype sendtype,int dst, int sendtag,
370 void *recvbuf, int recvcount, MPI_Datatype recvtype, int src, int recvtag,
371 MPI_Comm comm, MPI_Status * status)
373 aid_t source = MPI_PROC_NULL;
374 if (src == MPI_ANY_SOURCE)
375 source = MPI_ANY_SOURCE;
376 else if (src != MPI_PROC_NULL)
377 source = comm->group()->actor(src);
378 aid_t destination = dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL;
380 std::array<MPI_Request, 2> requests;
381 std::array<MPI_Status, 2> stats;
382 aid_t myid = simgrid::s4u::this_actor::get_pid();
383 if ((destination == myid) && (source == myid)) {
384 Datatype::copy(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
385 if (status != MPI_STATUS_IGNORE) {
386 status->MPI_SOURCE = source;
387 status->MPI_TAG = recvtag;
388 status->MPI_ERROR = MPI_SUCCESS;
389 status->count = sendcount * sendtype->size();
393 requests[0] = isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
394 requests[1] = irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
395 startall(2, requests.data());
396 waitall(2, requests.data(), stats.data());
399 if(status != MPI_STATUS_IGNORE) {
400 // Copy receive status
405 void Request::start()
407 s4u::Mailbox* mailbox;
409 xbt_assert(action_ == nullptr, "Cannot (re-)start unfinished communication");
410 //reinitialize temporary buffer for persistent requests
411 if(real_size_ > 0 && flags_ & MPI_REQ_FINISHED){
413 init_buffer(real_size_/old_type_->size());
415 flags_ &= ~MPI_REQ_PREPARED;
416 flags_ &= ~MPI_REQ_FINISHED;
419 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
421 if ((flags_ & MPI_REQ_RECV) != 0) {
422 this->print_request("New recv");
424 simgrid::smpi::ActorExt* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(dst_));
426 simgrid::s4u::MutexPtr mut = process->mailboxes_mutex();
427 if (smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)
430 if (smpi_cfg_async_small_thresh() == 0 && (flags_ & MPI_REQ_RMA) == 0) {
431 mailbox = process->mailbox();
432 } else if (((flags_ & MPI_REQ_RMA) != 0) || static_cast<int>(size_) < smpi_cfg_async_small_thresh()) {
433 //We have to check both mailboxes (because SSEND messages are sent to the large mbox).
434 //begin with the more appropriate one : the small one.
435 mailbox = process->mailbox_small();
436 XBT_DEBUG("Is there a corresponding send already posted in the small mailbox %s (in case of SSEND)?",
437 mailbox->get_cname());
438 simgrid::kernel::activity::ActivityImplPtr action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
440 if (action == nullptr) {
441 mailbox = process->mailbox();
442 XBT_DEBUG("No, nothing in the small mailbox test the other one : %s", mailbox->get_cname());
443 action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
444 if (action == nullptr) {
445 XBT_DEBUG("Still nothing, switch back to the small mailbox : %s", mailbox->get_cname());
446 mailbox = process->mailbox_small();
449 XBT_DEBUG("yes there was something for us in the large mailbox");
452 mailbox = process->mailbox_small();
453 XBT_DEBUG("Is there a corresponding send already posted the small mailbox?");
454 simgrid::kernel::activity::ActivityImplPtr action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
456 if (action == nullptr) {
457 XBT_DEBUG("No, nothing in the permanent receive mailbox");
458 mailbox = process->mailbox();
460 XBT_DEBUG("yes there was something for us in the small mailbox");
464 action_ = simcall_comm_irecv(
465 process->get_actor()->get_impl(), mailbox->get_impl(), buf_, &real_size_, &match_recv,
466 process->replaying() ? &smpi_comm_null_copy_buffer_callback : smpi_comm_copy_data_callback, this, -1.0);
467 XBT_DEBUG("recv simcall posted");
469 if (smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)
471 } else { /* the RECV flag was not set, so this is a send */
472 const simgrid::smpi::ActorExt* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(dst_));
473 xbt_assert(process, "Actor pid=%ld is gone??", dst_);
474 if (TRACE_smpi_view_internals())
475 TRACE_smpi_send(src_, src_, dst_, tag_, size_);
476 this->print_request("New send");
479 if ((flags_ & MPI_REQ_SSEND) == 0 &&
480 ((flags_ & MPI_REQ_RMA) != 0 || (flags_ & MPI_REQ_BSEND) != 0 ||
481 static_cast<int>(size_) < smpi_cfg_detached_send_thresh())) {
482 void *oldbuf = nullptr;
484 XBT_DEBUG("Send request %p is detached", this);
486 if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
488 if (not process->replaying() && oldbuf != nullptr && size_ != 0) {
489 if ((smpi_cfg_privatization() != SmpiPrivStrategies::NONE) &&
490 (static_cast<char*>(buf_) >= smpi_data_exe_start) &&
491 (static_cast<char*>(buf_) < smpi_data_exe_start + smpi_data_exe_size)) {
492 XBT_DEBUG("Privatization : We are sending from a zone inside global memory. Switch data segment ");
493 smpi_switch_data_segment(simgrid::s4u::Actor::by_pid(src_));
495 //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
496 //so actually ... don't use manually attached buffer space.
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_ || ((flags_ & (MPI_REQ_ISEND | MPI_REQ_SSEND)) != 0)) { // issend should be treated as isend
507 // isend and send timings may be different
508 sleeptime = ((flags_ & MPI_REQ_ISEND) != 0)
509 ? simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->oisend(size_)
510 : simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->osend(size_);
514 simgrid::s4u::this_actor::sleep_for(sleeptime);
515 XBT_DEBUG("sending size of %zu : sleep %f ", size_, sleeptime);
518 simgrid::s4u::MutexPtr mut = process->mailboxes_mutex();
520 if (smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)
523 if (not(smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)) {
524 mailbox = process->mailbox();
525 } else if (((flags_ & MPI_REQ_RMA) != 0) || static_cast<int>(size_) < smpi_cfg_async_small_thresh()) { // eager mode
526 mailbox = process->mailbox();
527 XBT_DEBUG("Is there a corresponding recv already posted in the large mailbox %s?", mailbox->get_cname());
528 simgrid::kernel::activity::ActivityImplPtr action = mailbox->iprobe(1, &match_send, static_cast<void*>(this));
529 if (action == nullptr) {
530 if ((flags_ & MPI_REQ_SSEND) == 0) {
531 mailbox = process->mailbox_small();
532 XBT_DEBUG("No, nothing in the large mailbox, message is to be sent on the small one %s",
533 mailbox->get_cname());
535 mailbox = process->mailbox_small();
536 XBT_DEBUG("SSEND : Is there a corresponding recv already posted in the small mailbox %s?",
537 mailbox->get_cname());
538 action = mailbox->iprobe(1, &match_send, static_cast<void*>(this));
539 if (action == nullptr) {
540 XBT_DEBUG("No, we are first, send to large mailbox");
541 mailbox = process->mailbox();
545 XBT_DEBUG("Yes there was something for us in the large mailbox");
548 mailbox = process->mailbox();
549 XBT_DEBUG("Send request %p is in the large mailbox %s (buf: %p)", this, mailbox->get_cname(), buf_);
552 size_t payload_size_ = size_ + 16;//MPI enveloppe size (tag+dest+communicator)
553 action_ = simcall_comm_isend(
554 simgrid::kernel::actor::ActorImpl::by_pid(src_), mailbox->get_impl(), payload_size_, -1.0, buf, real_size_,
556 &xbt_free_f, // how to free the userdata if a detached send fails
557 process->replaying() ? &smpi_comm_null_copy_buffer_callback : smpi_comm_copy_data_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 boost::static_pointer_cast<kernel::activity::CommImpl>(action_)->set_tracing_category(
565 smpi_process()->get_tracing_category());
568 if (smpi_cfg_async_small_thresh() != 0 || ((flags_ & MPI_REQ_RMA) != 0))
573 void Request::startall(int count, MPI_Request * requests)
575 if(requests== nullptr)
578 for(int i = 0; i < count; i++) {
579 if(requests[i]->src_ != MPI_PROC_NULL && requests[i]->dst_ != MPI_PROC_NULL)
580 requests[i]->start();
584 void Request::cancel()
586 this->flags_ |= MPI_REQ_CANCELLED;
587 if (this->action_ != nullptr)
588 (boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(this->action_))->cancel();
591 int Request::test(MPI_Request * request, MPI_Status * status, int* flag) {
592 // assume that *request is not MPI_REQUEST_NULL (filtered in PMPI_Test or testall before)
593 // to avoid deadlocks if used as a break condition, such as
594 // while (MPI_Test(request, flag, status) && flag) dostuff...
595 // because the time will not normally advance when only calls to MPI_Test are made -> deadlock
596 // multiplier to the sleeptime, to increase speed of execution, each failed test will increase it
597 xbt_assert(*request != MPI_REQUEST_NULL);
599 static int nsleeps = 1;
600 int ret = MPI_SUCCESS;
602 if(smpi_test_sleep > 0)
603 simgrid::s4u::this_actor::sleep_for(nsleeps * smpi_test_sleep);
605 Status::empty(status);
608 if ((*request)->flags_ & MPI_REQ_NBC){
609 *flag = finish_nbc_requests(request, 1);
612 if (((*request)->flags_ & (MPI_REQ_PREPARED | MPI_REQ_FINISHED)) == 0) {
613 if ((*request)->action_ != nullptr && ((*request)->flags_ & MPI_REQ_CANCELLED) == 0){
615 *flag = simcall_comm_test((*request)->action_.get());
616 } catch (const Exception&) {
621 if (((*request)->flags_ & MPI_REQ_GENERALIZED) && !((*request)->flags_ & MPI_REQ_COMPLETE))
624 finish_wait(request, status); // may invalidate *request
625 if (*request != MPI_REQUEST_NULL && ((*request)->flags_ & MPI_REQ_GENERALIZED)){
626 MPI_Status tmp_status;
627 MPI_Status* mystatus;
628 if (status == MPI_STATUS_IGNORE) {
629 mystatus = &tmp_status;
630 Status::empty(mystatus);
634 ret = ((*request)->generalized_funcs)->query_fn(((*request)->generalized_funcs)->extra_state, mystatus);
636 nsleeps=1;//reset the number of sleeps we will do next time
637 if (*request != MPI_REQUEST_NULL && ((*request)->flags_ & MPI_REQ_PERSISTENT) == 0)
638 *request = MPI_REQUEST_NULL;
639 } else if (smpi_cfg_grow_injected_times()) {
646 int Request::testsome(int incount, MPI_Request requests[], int *count, int *indices, MPI_Status status[])
652 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
655 for (int i = 0; i < incount; i++) {
656 if (requests[i] != MPI_REQUEST_NULL && not (requests[i]->flags_ & MPI_REQ_FINISHED)) {
657 int ret = test(&requests[i], pstat, &flag);
662 if (status != MPI_STATUSES_IGNORE)
663 status[*count] = *pstat;
665 if ((requests[i] != MPI_REQUEST_NULL) && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
666 requests[i] = MPI_REQUEST_NULL;
672 if(count_dead==incount)*count=MPI_UNDEFINED;
674 return MPI_ERR_IN_STATUS;
679 int Request::testany(int count, MPI_Request requests[], int *index, int* flag, MPI_Status * status)
681 std::vector<simgrid::kernel::activity::CommImpl*> comms;
682 comms.reserve(count);
686 int ret = MPI_SUCCESS;
687 *index = MPI_UNDEFINED;
689 std::vector<int> map; /** Maps all matching comms back to their location in requests **/
690 for(i = 0; i < count; i++) {
691 if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->action_ && not(requests[i]->flags_ & MPI_REQ_PREPARED)) {
692 comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(requests[i]->action_.get()));
696 if (not map.empty()) {
697 //multiplier to the sleeptime, to increase speed of execution, each failed testany will increase it
698 static int nsleeps = 1;
699 if(smpi_test_sleep > 0)
700 simgrid::s4u::this_actor::sleep_for(nsleeps * smpi_test_sleep);
702 i = simcall_comm_testany(comms.data(), comms.size()); // The i-th element in comms matches!
703 } catch (const Exception&) {
704 XBT_DEBUG("Exception in testany");
708 if (i != -1) { // -1 is not MPI_UNDEFINED but a SIMIX return code. (nothing matches)
710 if (requests[*index] != MPI_REQUEST_NULL &&
711 (requests[*index]->flags_ & MPI_REQ_GENERALIZED)
712 && !(requests[*index]->flags_ & MPI_REQ_COMPLETE)) {
715 finish_wait(&requests[*index],status);
716 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags_ & MPI_REQ_GENERALIZED)){
717 MPI_Status tmp_status;
718 MPI_Status* mystatus;
719 if (status == MPI_STATUS_IGNORE) {
720 mystatus = &tmp_status;
721 Status::empty(mystatus);
725 ret=(requests[*index]->generalized_funcs)->query_fn((requests[*index]->generalized_funcs)->extra_state, mystatus);
728 if (requests[*index] != MPI_REQUEST_NULL && requests[*index]->flags_ & MPI_REQ_NBC){
729 *flag = finish_nbc_requests(&requests[*index] , 1);
732 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags_ & MPI_REQ_NON_PERSISTENT))
733 requests[*index] = MPI_REQUEST_NULL;
734 XBT_DEBUG("Testany - returning with index %d", *index);
742 XBT_DEBUG("Testany on inactive handles, returning flag=1 but empty status");
743 //all requests are null or inactive, return true
745 *index = MPI_UNDEFINED;
746 Status::empty(status);
752 int Request::testall(int count, MPI_Request requests[], int* outflag, MPI_Status status[])
755 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
759 for(int i=0; i<count; i++){
760 if (requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & MPI_REQ_PREPARED)) {
761 int ret = test(&requests[i], pstat, &flag);
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();
801 new Request(nullptr, 0, MPI_CHAR, source == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : comm->group()->actor(source),
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 int Request::finish_nbc_requests(MPI_Request* request, int test){
859 ret = waitall((*request)->nbc_requests_size_, (*request)->nbc_requests_, MPI_STATUSES_IGNORE);
861 ret = testall((*request)->nbc_requests_size_, (*request)->nbc_requests_, &flag, MPI_STATUSES_IGNORE);
864 xbt_die("Failure when waiting on non blocking collective sub-requests");
866 XBT_DEBUG("Finishing non blocking collective request with %d sub-requests", (*request)->nbc_requests_size_);
867 for (int i = 0; i < (*request)->nbc_requests_size_; i++) {
868 if((*request)->buf_!=nullptr && (*request)->nbc_requests_[i]!=MPI_REQUEST_NULL){//reduce case
869 void * buf=(*request)->nbc_requests_[i]->buf_;
870 if((*request)->old_type_->flags() & DT_FLAG_DERIVED)
871 buf=(*request)->nbc_requests_[i]->old_buf_;
872 if((*request)->nbc_requests_[i]->flags_ & MPI_REQ_RECV ){
873 if((*request)->op_!=MPI_OP_NULL){
874 int count=(*request)->size_/ (*request)->old_type_->size();
875 (*request)->op_->apply(buf, (*request)->buf_, &count, (*request)->old_type_);
877 smpi_free_tmp_buffer(static_cast<unsigned char*>(buf));
880 if((*request)->nbc_requests_[i]!=MPI_REQUEST_NULL)
881 Request::unref(&((*request)->nbc_requests_[i]));
883 delete[] (*request)->nbc_requests_;
884 (*request)->nbc_requests_size_=0;
889 void Request::finish_wait(MPI_Request* request, MPI_Status * status)
891 MPI_Request req = *request;
892 Status::empty(status);
893 if((req->flags_ & MPI_REQ_CANCELLED) != 0 && (req->flags_ & MPI_REQ_MATCHED) == 0) {
894 if (status!=MPI_STATUS_IGNORE)
896 if(req->detached_sender_ != nullptr)
897 unref(&(req->detached_sender_));
902 if ((req->flags_ & (MPI_REQ_PREPARED | MPI_REQ_GENERALIZED | MPI_REQ_FINISHED)) == 0) {
903 if (status != MPI_STATUS_IGNORE) {
904 if (req->src_== MPI_PROC_NULL || req->dst_== MPI_PROC_NULL){
905 Status::empty(status);
906 status->MPI_SOURCE = MPI_PROC_NULL;
908 aid_t src = req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_;
909 status->MPI_SOURCE = req->comm_->group()->rank(src);
910 status->MPI_TAG = req->tag_ == MPI_ANY_TAG ? req->real_tag_ : req->tag_;
911 status->MPI_ERROR = req->truncated_ ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
913 // this handles the case were size in receive differs from size in send
914 status->count = req->real_size_;
916 //detached send will be finished at the other end
917 if (not(req->detached_ && ((req->flags_ & MPI_REQ_SEND) != 0))) {
918 req->print_request("Finishing");
919 MPI_Datatype datatype = req->old_type_;
921 // FIXME Handle the case of a partial shared malloc.
922 if (((req->flags_ & MPI_REQ_ACCUMULATE) != 0) ||
923 (datatype->flags() & DT_FLAG_DERIVED)) { // && (not smpi_is_shared(req->old_buf_))){
924 if (not smpi_process()->replaying() && smpi_cfg_privatization() != SmpiPrivStrategies::NONE &&
925 static_cast<char*>(req->old_buf_) >= smpi_data_exe_start &&
926 static_cast<char*>(req->old_buf_) < smpi_data_exe_start + smpi_data_exe_size) {
927 XBT_VERB("Privatization : We are unserializing to a zone in global memory Switch data segment ");
928 smpi_switch_data_segment(simgrid::s4u::Actor::self());
931 if(datatype->flags() & DT_FLAG_DERIVED){
932 // This part handles the problem of non-contiguous memory the unserialization at the reception
933 if ((req->flags_ & MPI_REQ_RECV) && datatype->size() != 0)
934 datatype->unserialize(req->buf_, req->old_buf_, req->real_size_/datatype->size() , req->op_);
937 } else if (req->flags_ & MPI_REQ_RECV) { // apply op on contiguous buffer for accumulate
938 if (datatype->size() != 0) {
939 int n = req->real_size_ / datatype->size();
940 req->op_->apply(req->buf_, req->old_buf_, &n, datatype);
949 if (TRACE_smpi_view_internals() && ((req->flags_ & MPI_REQ_RECV) != 0)) {
950 aid_t rank = simgrid::s4u::this_actor::get_pid();
951 aid_t src_traced = (req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_);
952 TRACE_smpi_recv(src_traced, rank,req->tag_);
954 if(req->detached_sender_ != nullptr){
955 //integrate pseudo-timing for buffering of small messages, do not bother to execute the simcall if 0
957 simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->orecv(req->real_size());
958 if (sleeptime > 0.0) {
959 simgrid::s4u::this_actor::sleep_for(sleeptime);
960 XBT_DEBUG("receiving size of %zu : sleep %f ", req->real_size_, sleeptime);
962 unref(&(req->detached_sender_));
964 if (req->flags_ & MPI_REQ_PERSISTENT)
965 req->action_ = nullptr;
966 req->flags_ |= MPI_REQ_FINISHED;
968 if (req->truncated_) {
969 char error_string[MPI_MAX_ERROR_STRING];
971 PMPI_Error_string(MPI_ERR_TRUNCATE, error_string, &error_size);
972 MPI_Errhandler err = (req->comm_) ? (req->comm_)->errhandler() : MPI_ERRHANDLER_NULL;
973 if (err == MPI_ERRHANDLER_NULL || err == MPI_ERRORS_RETURN)
974 XBT_WARN("recv - returned %.*s instead of MPI_SUCCESS", error_size, error_string);
975 else if (err == MPI_ERRORS_ARE_FATAL)
976 xbt_die("recv - returned %.*s instead of MPI_SUCCESS", error_size, error_string);
978 err->call((req->comm_), MPI_ERR_TRUNCATE);
979 if (err != MPI_ERRHANDLER_NULL)
980 simgrid::smpi::Errhandler::unref(err);
981 MC_assert(not MC_is_active()); /* Only fail in MC mode */
983 if(req->src_ != MPI_PROC_NULL && req->dst_ != MPI_PROC_NULL)
987 int Request::wait(MPI_Request * request, MPI_Status * status)
989 // assume that *request is not MPI_REQUEST_NULL (filtered in PMPI_Wait before)
990 xbt_assert(*request != MPI_REQUEST_NULL);
994 if((*request)->src_ == MPI_PROC_NULL || (*request)->dst_ == MPI_PROC_NULL){
995 if (status != MPI_STATUS_IGNORE) {
996 Status::empty(status);
997 status->MPI_SOURCE = MPI_PROC_NULL;
999 (*request)=MPI_REQUEST_NULL;
1003 (*request)->print_request("Waiting");
1004 if ((*request)->flags_ & (MPI_REQ_PREPARED | MPI_REQ_FINISHED)) {
1005 Status::empty(status);
1009 if ((*request)->action_ != nullptr){
1011 // this is not a detached send
1012 simcall_comm_wait((*request)->action_.get(), -1.0);
1013 } catch (const Exception&) {
1014 XBT_VERB("Request cancelled");
1018 if ((*request)->flags_ & MPI_REQ_GENERALIZED) {
1019 if(!((*request)->flags_ & MPI_REQ_COMPLETE)){
1020 ((*request)->generalized_funcs)->mutex->lock();
1021 ((*request)->generalized_funcs)->cond->wait(((*request)->generalized_funcs)->mutex);
1022 ((*request)->generalized_funcs)->mutex->unlock();
1024 MPI_Status tmp_status;
1025 MPI_Status* mystatus;
1026 if (status == MPI_STATUS_IGNORE) {
1027 mystatus = &tmp_status;
1028 Status::empty(mystatus);
1032 ret = ((*request)->generalized_funcs)->query_fn(((*request)->generalized_funcs)->extra_state, mystatus);
1035 if ((*request)->truncated_)
1036 ret = MPI_ERR_TRUNCATE;
1038 if ((*request)->flags_ & MPI_REQ_NBC)
1039 finish_nbc_requests(request, 0);
1041 finish_wait(request, status); // may invalidate *request
1042 if (*request != MPI_REQUEST_NULL && (((*request)->flags_ & MPI_REQ_NON_PERSISTENT) != 0))
1043 *request = MPI_REQUEST_NULL;
1047 int Request::waitany(int count, MPI_Request requests[], MPI_Status * status)
1049 int index = MPI_UNDEFINED;
1052 // Wait for a request to complete
1053 std::vector<simgrid::kernel::activity::CommImpl*> comms;
1054 std::vector<int> map;
1055 XBT_DEBUG("Wait for one of %d", count);
1056 for(int i = 0; i < count; i++) {
1057 if (requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & MPI_REQ_PREPARED) &&
1058 not(requests[i]->flags_ & MPI_REQ_FINISHED)) {
1059 if (requests[i]->action_ != nullptr) {
1060 XBT_DEBUG("Waiting any %p ", requests[i]);
1061 comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(requests[i]->action_.get()));
1064 // This is a finished detached request, let's return this one
1065 comms.clear(); // don't do the waitany call afterwards
1067 if (requests[index] != MPI_REQUEST_NULL && (requests[index])->flags_ & MPI_REQ_NBC)
1068 finish_nbc_requests(&requests[index], 0);
1069 finish_wait(&requests[i], status); // cleanup if refcount = 0
1070 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
1071 requests[i] = MPI_REQUEST_NULL; // set to null
1076 if (not comms.empty()) {
1077 XBT_DEBUG("Enter waitany for %zu comms", comms.size());
1080 i = simcall_comm_waitany(comms.data(), comms.size(), -1);
1081 } catch (const Exception&) {
1082 XBT_INFO("request cancelled");
1086 // not MPI_UNDEFINED, as this is a simix return code
1089 //in case of an accumulate, we have to wait the end of all requests to apply the operation, ordered correctly.
1090 if ((requests[index] == MPI_REQUEST_NULL) ||
1091 (not((requests[index]->flags_ & MPI_REQ_ACCUMULATE) && (requests[index]->flags_ & MPI_REQ_RECV)))) {
1092 finish_wait(&requests[index],status);
1093 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_NON_PERSISTENT))
1094 requests[index] = MPI_REQUEST_NULL;
1101 if (index==MPI_UNDEFINED)
1102 Status::empty(status);
1107 static int sort_accumulates(const Request* a, const Request* b)
1109 return (a->tag() > b->tag());
1112 int Request::waitall(int count, MPI_Request requests[], MPI_Status status[])
1114 std::vector<MPI_Request> accumulates;
1117 MPI_Status *pstat = (status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat);
1118 int retvalue = MPI_SUCCESS;
1119 //tag invalid requests in the set
1120 if (status != MPI_STATUSES_IGNORE) {
1121 for (int c = 0; c < count; c++) {
1122 if (requests[c] == MPI_REQUEST_NULL || requests[c]->dst_ == MPI_PROC_NULL ||
1123 (requests[c]->flags_ & MPI_REQ_PREPARED)) {
1124 Status::empty(&status[c]);
1125 } else if (requests[c]->src_ == MPI_PROC_NULL) {
1126 Status::empty(&status[c]);
1127 status[c].MPI_SOURCE = MPI_PROC_NULL;
1131 for (int c = 0; c < count; c++) {
1132 if (MC_is_active() || MC_record_replay_is_active()) {
1133 wait(&requests[c],pstat);
1136 index = waitany(count, requests, pstat);
1138 if (index == MPI_UNDEFINED)
1141 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_RECV) &&
1142 (requests[index]->flags_ & MPI_REQ_ACCUMULATE))
1143 accumulates.push_back(requests[index]);
1144 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_NON_PERSISTENT))
1145 requests[index] = MPI_REQUEST_NULL;
1147 if (status != MPI_STATUSES_IGNORE) {
1148 status[index] = *pstat;
1149 if (status[index].MPI_ERROR == MPI_ERR_TRUNCATE)
1150 retvalue = MPI_ERR_IN_STATUS;
1154 if (not accumulates.empty()) {
1155 std::sort(accumulates.begin(), accumulates.end(), sort_accumulates);
1156 for (auto& req : accumulates) {
1157 finish_wait(&req, status);
1164 int Request::waitsome(int incount, MPI_Request requests[], int *indices, MPI_Status status[])
1170 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
1171 index = waitany(incount, requests, pstat);
1172 if(index==MPI_UNDEFINED) return MPI_UNDEFINED;
1173 if(status != MPI_STATUSES_IGNORE) {
1174 status[count] = *pstat;
1176 indices[count] = index;
1178 for (int i = 0; i < incount; i++) {
1179 if (i!=index && requests[i] != MPI_REQUEST_NULL
1180 && not(requests[i]->flags_ & MPI_REQ_FINISHED)) {
1181 test(&requests[i], pstat,&flag);
1184 if(status != MPI_STATUSES_IGNORE) {
1185 status[count] = *pstat;
1187 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
1188 requests[i]=MPI_REQUEST_NULL;
1196 MPI_Request Request::f2c(int id)
1198 if(id==MPI_FORTRAN_REQUEST_NULL)
1199 return MPI_REQUEST_NULL;
1200 return static_cast<MPI_Request>(F2C::lookup()->at(id));
1203 void Request::free_f(int id)
1205 if (id != MPI_FORTRAN_REQUEST_NULL) {
1206 F2C::lookup()->erase(id);
1210 int Request::get_status(const Request* req, int* flag, MPI_Status* status)
1214 if(req != MPI_REQUEST_NULL && req->action_ != nullptr) {
1215 req->iprobe(req->comm_->group()->rank(req->src_), req->tag_, req->comm_, flag, status);
1219 if (req != MPI_REQUEST_NULL &&
1220 (req->flags_ & MPI_REQ_GENERALIZED)
1221 && !(req->flags_ & MPI_REQ_COMPLETE)) {
1227 if(req != MPI_REQUEST_NULL &&
1228 status != MPI_STATUS_IGNORE) {
1229 aid_t src = req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_;
1230 status->MPI_SOURCE = req->comm_->group()->rank(src);
1231 status->MPI_TAG = req->tag_ == MPI_ANY_TAG ? req->real_tag_ : req->tag_;
1232 status->MPI_ERROR = req->truncated_ ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
1233 status->count = req->real_size_;
1238 int Request::grequest_start(MPI_Grequest_query_function* query_fn, MPI_Grequest_free_function* free_fn,
1239 MPI_Grequest_cancel_function* cancel_fn, void* extra_state, MPI_Request* request)
1241 *request = new Request();
1242 (*request)->flags_ |= MPI_REQ_GENERALIZED;
1243 (*request)->flags_ |= MPI_REQ_PERSISTENT;
1244 (*request)->refcount_ = 1;
1245 ((*request)->generalized_funcs) = std::make_unique<smpi_mpi_generalized_request_funcs_t>();
1246 ((*request)->generalized_funcs)->query_fn=query_fn;
1247 ((*request)->generalized_funcs)->free_fn=free_fn;
1248 ((*request)->generalized_funcs)->cancel_fn=cancel_fn;
1249 ((*request)->generalized_funcs)->extra_state=extra_state;
1250 ((*request)->generalized_funcs)->cond = simgrid::s4u::ConditionVariable::create();
1251 ((*request)->generalized_funcs)->mutex = simgrid::s4u::Mutex::create();
1255 int Request::grequest_complete(MPI_Request request)
1257 if ((!(request->flags_ & MPI_REQ_GENERALIZED)) || request->generalized_funcs->mutex == nullptr)
1258 return MPI_ERR_REQUEST;
1259 request->generalized_funcs->mutex->lock();
1260 request->flags_ |= MPI_REQ_COMPLETE; // in case wait would be called after complete
1261 request->generalized_funcs->cond->notify_one();
1262 request->generalized_funcs->mutex->unlock();
1266 void Request::set_nbc_requests(MPI_Request* reqs, int size){
1267 nbc_requests_size_ = size;
1269 nbc_requests_ = reqs;
1272 nbc_requests_ = nullptr;
1276 int Request::get_nbc_requests_size() const
1278 return nbc_requests_size_;
1281 MPI_Request* Request::get_nbc_requests() const
1283 return nbc_requests_;