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 xbt_assert(*request != MPI_REQUEST_NULL, "freeing an already free request");
78 (*request)->refcount_--;
79 if ((*request)->refcount_ < 0) {
80 (*request)->print_request("wrong refcount");
81 xbt_die("Whoops, wrong refcount");
83 if ((*request)->refcount_ == 0) {
84 if ((*request)->flags_ & MPI_REQ_GENERALIZED) {
85 ((*request)->generalized_funcs)->free_fn(((*request)->generalized_funcs)->extra_state);
87 Comm::unref((*request)->comm_);
88 Datatype::unref((*request)->old_type_);
90 if ((*request)->op_ != MPI_REPLACE && (*request)->op_ != MPI_OP_NULL)
91 Op::unref(&(*request)->op_);
93 (*request)->print_request("Destroying");
94 F2C::free_f((*request)->c2f());
96 *request = MPI_REQUEST_NULL;
98 (*request)->print_request("Decrementing");
102 bool Request::match_common(MPI_Request req, MPI_Request sender, MPI_Request receiver)
104 xbt_assert(sender, "Cannot match against null sender");
105 xbt_assert(receiver, "Cannot match against null receiver");
106 XBT_DEBUG("Trying to match %s of sender src %d against %d, tag %d against %d, id %d against %d",
107 (req == receiver ? "send" : "recv"), sender->src_, receiver->src_, sender->tag_, receiver->tag_,
108 sender->comm_->id(), receiver->comm_->id());
110 if ((receiver->comm_->id() == MPI_UNDEFINED || sender->comm_->id() == MPI_UNDEFINED ||
111 receiver->comm_->id() == sender->comm_->id()) &&
112 ((receiver->src_ == MPI_ANY_SOURCE && (receiver->comm_->group()->rank(sender->src_) != MPI_UNDEFINED)) ||
113 receiver->src_ == sender->src_) &&
114 ((receiver->tag_ == MPI_ANY_TAG && sender->tag_ >= 0) || receiver->tag_ == sender->tag_)) {
115 // we match, we can transfer some values
116 if (receiver->src_ == MPI_ANY_SOURCE)
117 receiver->real_src_ = sender->src_;
118 if (receiver->tag_ == MPI_ANY_TAG)
119 receiver->real_tag_ = sender->tag_;
120 if (receiver->real_size_ < sender->real_size_ && ((receiver->flags_ & MPI_REQ_PROBE) == 0 )){
121 XBT_DEBUG("Truncating message - should not happen: receiver size : %zu < sender size : %zu", receiver->real_size_, sender->real_size_);
122 receiver->truncated_ = true;
124 if (sender->detached_)
125 receiver->detached_sender_ = sender; // tie the sender to the receiver, as it is detached and has to be freed in
127 req->flags_ |= MPI_REQ_MATCHED; // mark as impossible to cancel anymore
128 XBT_DEBUG("match succeeded");
134 void Request::init_buffer(int count){
135 void *old_buf = nullptr;
136 // FIXME Handle the case of a partial shared malloc.
137 // This part handles the problem of non-contiguous memory (for the unserialization at the reception)
138 if ((((flags_ & MPI_REQ_RECV) != 0) && ((flags_ & MPI_REQ_ACCUMULATE) != 0)) || (old_type_->flags() & DT_FLAG_DERIVED)) {
139 // This part handles the problem of non-contiguous memory
144 buf_ = xbt_malloc(count*old_type_->size());
145 if ((old_type_->flags() & DT_FLAG_DERIVED) && ((flags_ & MPI_REQ_SEND) != 0)) {
146 old_type_->serialize(old_buf, buf_, count);
153 bool Request::match_recv(void* a, void* b, simgrid::kernel::activity::CommImpl*)
155 auto ref = static_cast<MPI_Request>(a);
156 auto req = static_cast<MPI_Request>(b);
157 return match_common(req, req, ref);
160 bool Request::match_send(void* a, void* b, simgrid::kernel::activity::CommImpl*)
162 auto ref = static_cast<MPI_Request>(a);
163 auto req = static_cast<MPI_Request>(b);
164 return match_common(req, ref, req);
167 void Request::print_request(const char* message) const
169 XBT_VERB("%s request %p [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
170 message, this, buf_, size_, src_, dst_, tag_, flags_);
173 /* factories, to hide the internal flags from the caller */
174 MPI_Request Request::bsend_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
176 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
177 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL, tag, comm,
178 MPI_REQ_PERSISTENT | MPI_REQ_SEND | MPI_REQ_PREPARED | MPI_REQ_BSEND);
181 MPI_Request Request::send_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
183 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
184 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL, tag, comm,
185 MPI_REQ_PERSISTENT | MPI_REQ_SEND | MPI_REQ_PREPARED);
188 MPI_Request Request::ssend_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
190 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
191 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL, tag, comm,
192 MPI_REQ_PERSISTENT | MPI_REQ_SSEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
195 MPI_Request Request::isend_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
197 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
198 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL, tag, comm,
199 MPI_REQ_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
203 MPI_Request Request::rma_send_init(const void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
206 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
208 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src)->get_pid(),
209 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL, tag, comm,
210 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
212 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src)->get_pid(),
213 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL, tag, comm,
214 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED |
215 MPI_REQ_ACCUMULATE, op);
220 MPI_Request Request::recv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
222 int source = MPI_PROC_NULL;
223 if (src == MPI_ANY_SOURCE)
224 source = MPI_ANY_SOURCE;
225 else if (src != MPI_PROC_NULL)
226 source = comm->group()->actor(src)->get_pid();
227 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
229 simgrid::s4u::this_actor::get_pid(), tag, comm,
230 MPI_REQ_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
233 MPI_Request Request::rma_recv_init(void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
236 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
237 int source = MPI_PROC_NULL;
238 if (src == MPI_ANY_SOURCE)
239 source = MPI_ANY_SOURCE;
240 else if (src != MPI_PROC_NULL)
241 source = comm->group()->actor(src)->get_pid();
243 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, source,
244 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL, tag, comm,
245 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
247 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, source,
248 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL, tag, comm,
249 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED | MPI_REQ_ACCUMULATE, op);
254 MPI_Request Request::irecv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
256 int source = MPI_PROC_NULL;
257 if (src == MPI_ANY_SOURCE)
258 source = MPI_ANY_SOURCE;
259 else if (src != MPI_PROC_NULL)
260 source = comm->group()->actor(src)->get_pid();
261 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
262 source, simgrid::s4u::this_actor::get_pid(), tag, comm,
263 MPI_REQ_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
266 MPI_Request Request::ibsend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
268 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
269 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
270 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL, tag, comm,
271 MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_BSEND);
272 if(dst != MPI_PROC_NULL)
277 MPI_Request Request::isend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
279 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
280 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
281 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL, tag, comm,
282 MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND);
283 if(dst != MPI_PROC_NULL)
288 MPI_Request Request::issend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
290 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
291 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
292 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL, tag, comm,
293 MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SSEND | MPI_REQ_SEND);
294 if(dst != MPI_PROC_NULL)
300 MPI_Request Request::irecv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
302 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
303 int source = MPI_PROC_NULL;
304 if (src == MPI_ANY_SOURCE)
305 source = MPI_ANY_SOURCE;
306 else if (src != MPI_PROC_NULL)
307 source = comm->group()->actor(src)->get_pid();
308 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
309 source, simgrid::s4u::this_actor::get_pid(), tag, comm,
310 MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV);
311 if(src != MPI_PROC_NULL)
316 int Request::recv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status * status)
318 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
319 request = irecv(buf, count, datatype, src, tag, comm);
320 int retval = wait(&request,status);
325 void Request::bsend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
327 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
328 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
329 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL,
330 tag, comm, MPI_REQ_NON_PERSISTENT | MPI_REQ_SEND | MPI_REQ_BSEND);
332 if(dst != MPI_PROC_NULL)
334 wait(&request, MPI_STATUS_IGNORE);
338 void Request::send(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
340 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
341 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
342 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL,
343 tag, comm, MPI_REQ_NON_PERSISTENT | MPI_REQ_SEND);
344 if(dst != MPI_PROC_NULL)
346 wait(&request, MPI_STATUS_IGNORE);
350 void Request::ssend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
352 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
353 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
354 dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL, tag, comm,
355 MPI_REQ_NON_PERSISTENT | MPI_REQ_SSEND | MPI_REQ_SEND);
357 if(dst != MPI_PROC_NULL)
359 wait(&request,MPI_STATUS_IGNORE);
363 void Request::sendrecv(const void *sendbuf, int sendcount, MPI_Datatype sendtype,int dst, int sendtag,
364 void *recvbuf, int recvcount, MPI_Datatype recvtype, int src, int recvtag,
365 MPI_Comm comm, MPI_Status * status)
367 int source = MPI_PROC_NULL;
368 if (src == MPI_ANY_SOURCE)
369 source = MPI_ANY_SOURCE;
370 else if (src != MPI_PROC_NULL)
371 source = comm->group()->actor(src)->get_pid();
372 int destination = dst != MPI_PROC_NULL ? comm->group()->actor(dst)->get_pid() : MPI_PROC_NULL;
374 std::array<MPI_Request, 2> requests;
375 std::array<MPI_Status, 2> stats;
376 int myid = simgrid::s4u::this_actor::get_pid();
377 if ((destination == myid) && (source == myid)) {
378 Datatype::copy(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
379 if (status != MPI_STATUS_IGNORE) {
380 status->MPI_SOURCE = source;
381 status->MPI_TAG = recvtag;
382 status->MPI_ERROR = MPI_SUCCESS;
383 status->count = sendcount * sendtype->size();
387 requests[0] = isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
388 requests[1] = irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
389 startall(2, requests.data());
390 waitall(2, requests.data(), stats.data());
393 if(status != MPI_STATUS_IGNORE) {
394 // Copy receive status
399 void Request::start()
401 s4u::Mailbox* mailbox;
403 xbt_assert(action_ == nullptr, "Cannot (re-)start unfinished communication");
404 //reinitialize temporary buffer for persistent requests
405 if(real_size_ > 0 && flags_ & MPI_REQ_FINISHED){
407 init_buffer(real_size_/old_type_->size());
409 flags_ &= ~MPI_REQ_PREPARED;
410 flags_ &= ~MPI_REQ_FINISHED;
413 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
415 if ((flags_ & MPI_REQ_RECV) != 0) {
416 this->print_request("New recv");
418 simgrid::smpi::ActorExt* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(dst_));
420 simgrid::s4u::MutexPtr mut = process->mailboxes_mutex();
421 if (smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)
424 if (smpi_cfg_async_small_thresh() == 0 && (flags_ & MPI_REQ_RMA) == 0) {
425 mailbox = process->mailbox();
426 } else if (((flags_ & MPI_REQ_RMA) != 0) || static_cast<int>(size_) < smpi_cfg_async_small_thresh()) {
427 //We have to check both mailboxes (because SSEND messages are sent to the large mbox).
428 //begin with the more appropriate one : the small one.
429 mailbox = process->mailbox_small();
430 XBT_DEBUG("Is there a corresponding send already posted in the small mailbox %s (in case of SSEND)?",
431 mailbox->get_cname());
432 simgrid::kernel::activity::ActivityImplPtr action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
434 if (action == nullptr) {
435 mailbox = process->mailbox();
436 XBT_DEBUG("No, nothing in the small mailbox test the other one : %s", mailbox->get_cname());
437 action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
438 if (action == nullptr) {
439 XBT_DEBUG("Still nothing, switch back to the small mailbox : %s", mailbox->get_cname());
440 mailbox = process->mailbox_small();
443 XBT_DEBUG("yes there was something for us in the large mailbox");
446 mailbox = process->mailbox_small();
447 XBT_DEBUG("Is there a corresponding send already posted the small mailbox?");
448 simgrid::kernel::activity::ActivityImplPtr action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
450 if (action == nullptr) {
451 XBT_DEBUG("No, nothing in the permanent receive mailbox");
452 mailbox = process->mailbox();
454 XBT_DEBUG("yes there was something for us in the small mailbox");
458 action_ = simcall_comm_irecv(
459 process->get_actor()->get_impl(), mailbox->get_impl(), buf_, &real_size_, &match_recv,
460 process->replaying() ? &smpi_comm_null_copy_buffer_callback : smpi_comm_copy_data_callback, this, -1.0);
461 XBT_DEBUG("recv simcall posted");
463 if (smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)
465 } else { /* the RECV flag was not set, so this is a send */
466 const simgrid::smpi::ActorExt* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(dst_));
467 xbt_assert(process, "Actor pid=%d is gone??", dst_);
469 if (TRACE_smpi_view_internals()) {
470 TRACE_smpi_send(rank, rank, dst_, tag_, size_);
472 this->print_request("New send");
475 if ((flags_ & MPI_REQ_SSEND) == 0 &&
476 ((flags_ & MPI_REQ_RMA) != 0 || (flags_ & MPI_REQ_BSEND) != 0 ||
477 static_cast<int>(size_) < smpi_cfg_detached_send_thresh())) {
478 void *oldbuf = nullptr;
480 XBT_DEBUG("Send request %p is detached", this);
482 if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
484 if (not process->replaying() && oldbuf != nullptr && size_ != 0) {
485 if ((smpi_cfg_privatization() != SmpiPrivStrategies::NONE) &&
486 (static_cast<char*>(buf_) >= smpi_data_exe_start) &&
487 (static_cast<char*>(buf_) < smpi_data_exe_start + smpi_data_exe_size)) {
488 XBT_DEBUG("Privatization : We are sending from a zone inside global memory. Switch data segment ");
489 smpi_switch_data_segment(simgrid::s4u::Actor::by_pid(src_));
491 //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
492 //so actually ... don't use manually attached buffer space.
493 buf = xbt_malloc(size_);
494 memcpy(buf,oldbuf,size_);
495 XBT_DEBUG("buf %p copied into %p",oldbuf,buf);
500 //if we are giving back the control to the user without waiting for completion, we have to inject timings
501 double sleeptime = 0.0;
502 if (detached_ || ((flags_ & (MPI_REQ_ISEND | MPI_REQ_SSEND)) != 0)) { // issend should be treated as isend
503 // isend and send timings may be different
504 sleeptime = ((flags_ & MPI_REQ_ISEND) != 0)
505 ? simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->oisend(size_)
506 : simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->osend(size_);
510 simgrid::s4u::this_actor::sleep_for(sleeptime);
511 XBT_DEBUG("sending size of %zu : sleep %f ", size_, sleeptime);
514 simgrid::s4u::MutexPtr mut = process->mailboxes_mutex();
516 if (smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)
519 if (not(smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)) {
520 mailbox = process->mailbox();
521 } else if (((flags_ & MPI_REQ_RMA) != 0) || static_cast<int>(size_) < smpi_cfg_async_small_thresh()) { // eager mode
522 mailbox = process->mailbox();
523 XBT_DEBUG("Is there a corresponding recv already posted in the large mailbox %s?", mailbox->get_cname());
524 simgrid::kernel::activity::ActivityImplPtr action = mailbox->iprobe(1, &match_send, static_cast<void*>(this));
525 if (action == nullptr) {
526 if ((flags_ & MPI_REQ_SSEND) == 0) {
527 mailbox = process->mailbox_small();
528 XBT_DEBUG("No, nothing in the large mailbox, message is to be sent on the small one %s",
529 mailbox->get_cname());
531 mailbox = process->mailbox_small();
532 XBT_DEBUG("SSEND : Is there a corresponding recv already posted in the small mailbox %s?",
533 mailbox->get_cname());
534 action = mailbox->iprobe(1, &match_send, static_cast<void*>(this));
535 if (action == nullptr) {
536 XBT_DEBUG("No, we are first, send to large mailbox");
537 mailbox = process->mailbox();
541 XBT_DEBUG("Yes there was something for us in the large mailbox");
544 mailbox = process->mailbox();
545 XBT_DEBUG("Send request %p is in the large mailbox %s (buf: %p)", this, mailbox->get_cname(), buf_);
548 size_t payload_size_ = size_ + 16;//MPI enveloppe size (tag+dest+communicator)
549 action_ = simcall_comm_isend(
550 simgrid::kernel::actor::ActorImpl::by_pid(src_), mailbox->get_impl(), payload_size_, -1.0, buf, real_size_,
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_));
868 if ((req->flags_ & (MPI_REQ_PREPARED | MPI_REQ_GENERALIZED | MPI_REQ_FINISHED)) == 0) {
869 if (status != MPI_STATUS_IGNORE) {
870 if (req->src_== MPI_PROC_NULL || req->dst_== MPI_PROC_NULL){
871 Status::empty(status);
872 status->MPI_SOURCE = MPI_PROC_NULL;
874 int src = req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_;
875 status->MPI_SOURCE = req->comm_->group()->rank(src);
876 status->MPI_TAG = req->tag_ == MPI_ANY_TAG ? req->real_tag_ : req->tag_;
877 status->MPI_ERROR = req->truncated_ ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
879 // this handles the case were size in receive differs from size in send
880 status->count = req->real_size_;
882 //detached send will be finished at the other end
883 if (not(req->detached_ && ((req->flags_ & MPI_REQ_SEND) != 0))) {
884 req->print_request("Finishing");
885 MPI_Datatype datatype = req->old_type_;
887 // FIXME Handle the case of a partial shared malloc.
888 if (((req->flags_ & MPI_REQ_ACCUMULATE) != 0) ||
889 (datatype->flags() & DT_FLAG_DERIVED)) { // && (not smpi_is_shared(req->old_buf_))){
890 if (not smpi_process()->replaying() && smpi_cfg_privatization() != SmpiPrivStrategies::NONE &&
891 static_cast<char*>(req->old_buf_) >= smpi_data_exe_start &&
892 static_cast<char*>(req->old_buf_) < smpi_data_exe_start + smpi_data_exe_size) {
893 XBT_VERB("Privatization : We are unserializing to a zone in global memory Switch data segment ");
894 smpi_switch_data_segment(simgrid::s4u::Actor::self());
897 if(datatype->flags() & DT_FLAG_DERIVED){
898 // This part handles the problem of non-contiguous memory the unserialization at the reception
899 if ((req->flags_ & MPI_REQ_RECV) && datatype->size() != 0)
900 datatype->unserialize(req->buf_, req->old_buf_, req->real_size_/datatype->size() , req->op_);
903 } else if (req->flags_ & MPI_REQ_RECV) { // apply op on contiguous buffer for accumulate
904 if (datatype->size() != 0) {
905 int n = req->real_size_ / datatype->size();
906 req->op_->apply(req->buf_, req->old_buf_, &n, datatype);
915 if (TRACE_smpi_view_internals() && ((req->flags_ & MPI_REQ_RECV) != 0)) {
916 int rank = simgrid::s4u::this_actor::get_pid();
917 int src_traced = (req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_);
918 TRACE_smpi_recv(src_traced, rank,req->tag_);
920 if(req->detached_sender_ != nullptr){
921 //integrate pseudo-timing for buffering of small messages, do not bother to execute the simcall if 0
923 simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->orecv(req->real_size());
924 if (sleeptime > 0.0) {
925 simgrid::s4u::this_actor::sleep_for(sleeptime);
926 XBT_DEBUG("receiving size of %zu : sleep %f ", req->real_size_, sleeptime);
928 unref(&(req->detached_sender_));
930 if (req->flags_ & MPI_REQ_PERSISTENT)
931 req->action_ = nullptr;
932 req->flags_ |= MPI_REQ_FINISHED;
934 if (req->truncated_) {
935 char error_string[MPI_MAX_ERROR_STRING];
937 PMPI_Error_string(MPI_ERR_TRUNCATE, error_string, &error_size);
938 MPI_Errhandler err = (req->comm_) ? (req->comm_)->errhandler() : MPI_ERRHANDLER_NULL;
939 if (err == MPI_ERRHANDLER_NULL || err == MPI_ERRORS_RETURN)
940 XBT_WARN("recv - returned %.*s instead of MPI_SUCCESS", error_size, error_string);
941 else if (err == MPI_ERRORS_ARE_FATAL)
942 xbt_die("recv - returned %.*s instead of MPI_SUCCESS", error_size, error_string);
944 err->call((req->comm_), MPI_ERR_TRUNCATE);
945 if (err != MPI_ERRHANDLER_NULL)
946 simgrid::smpi::Errhandler::unref(err);
947 MC_assert(not MC_is_active()); /* Only fail in MC mode */
949 if(req->src_ != MPI_PROC_NULL && req->dst_ != MPI_PROC_NULL)
953 int Request::wait(MPI_Request * request, MPI_Status * status)
955 // assume that *request is not MPI_REQUEST_NULL (filtered in PMPI_Wait before)
956 xbt_assert(*request != MPI_REQUEST_NULL);
960 if((*request)->src_ == MPI_PROC_NULL || (*request)->dst_ == MPI_PROC_NULL){
961 if (status != MPI_STATUS_IGNORE) {
962 Status::empty(status);
963 status->MPI_SOURCE = MPI_PROC_NULL;
965 (*request)=MPI_REQUEST_NULL;
968 // Are we waiting on a request meant for non blocking collectives ?
969 // If so, wait for all the subrequests.
970 if ((*request)->nbc_requests_size_>0){
971 ret = waitall((*request)->nbc_requests_size_, (*request)->nbc_requests_, MPI_STATUSES_IGNORE);
972 for (int i = 0; i < (*request)->nbc_requests_size_; i++) {
973 if((*request)->buf_!=nullptr && (*request)->nbc_requests_[i]!=MPI_REQUEST_NULL){//reduce case
974 void * buf=(*request)->nbc_requests_[i]->buf_;
975 if((*request)->old_type_->flags() & DT_FLAG_DERIVED)
976 buf=(*request)->nbc_requests_[i]->old_buf_;
977 if((*request)->nbc_requests_[i]->flags_ & MPI_REQ_RECV ){
978 if((*request)->op_!=MPI_OP_NULL){
979 int count=(*request)->size_/ (*request)->old_type_->size();
980 (*request)->op_->apply(buf, (*request)->buf_, &count, (*request)->old_type_);
982 smpi_free_tmp_buffer(static_cast<unsigned char*>(buf));
985 if((*request)->nbc_requests_[i]!=MPI_REQUEST_NULL)
986 Request::unref(&((*request)->nbc_requests_[i]));
988 delete[] (*request)->nbc_requests_;
989 (*request)->nbc_requests_size_=0;
991 (*request)=MPI_REQUEST_NULL;
995 (*request)->print_request("Waiting");
996 if ((*request)->flags_ & (MPI_REQ_PREPARED | MPI_REQ_FINISHED)) {
997 Status::empty(status);
1001 if ((*request)->action_ != nullptr){
1003 // this is not a detached send
1004 simcall_comm_wait((*request)->action_.get(), -1.0);
1005 } catch (const Exception&) {
1006 XBT_VERB("Request cancelled");
1010 if ((*request)->flags_ & MPI_REQ_GENERALIZED) {
1011 if(!((*request)->flags_ & MPI_REQ_COMPLETE)){
1012 ((*request)->generalized_funcs)->mutex->lock();
1013 ((*request)->generalized_funcs)->cond->wait(((*request)->generalized_funcs)->mutex);
1014 ((*request)->generalized_funcs)->mutex->unlock();
1016 MPI_Status tmp_status;
1017 MPI_Status* mystatus;
1018 if (status == MPI_STATUS_IGNORE) {
1019 mystatus = &tmp_status;
1020 Status::empty(mystatus);
1024 ret = ((*request)->generalized_funcs)->query_fn(((*request)->generalized_funcs)->extra_state, mystatus);
1027 if ((*request)->truncated_)
1028 ret = MPI_ERR_TRUNCATE;
1030 finish_wait(request, status); // may invalidate *request
1031 if (*request != MPI_REQUEST_NULL && (((*request)->flags_ & MPI_REQ_NON_PERSISTENT) != 0))
1032 *request = MPI_REQUEST_NULL;
1036 int Request::waitany(int count, MPI_Request requests[], MPI_Status * status)
1038 int index = MPI_UNDEFINED;
1041 // Wait for a request to complete
1042 std::vector<simgrid::kernel::activity::CommImpl*> comms;
1043 std::vector<int> map;
1044 XBT_DEBUG("Wait for one of %d", count);
1045 for(int i = 0; i < count; i++) {
1046 if (requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & MPI_REQ_PREPARED) &&
1047 not(requests[i]->flags_ & MPI_REQ_FINISHED)) {
1048 if (requests[i]->action_ != nullptr) {
1049 XBT_DEBUG("Waiting any %p ", requests[i]);
1050 comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(requests[i]->action_.get()));
1053 // This is a finished detached request, let's return this one
1054 comms.clear(); // don't do the waitany call afterwards
1056 finish_wait(&requests[i], status); // cleanup if refcount = 0
1057 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
1058 requests[i] = MPI_REQUEST_NULL; // set to null
1063 if (not comms.empty()) {
1064 XBT_DEBUG("Enter waitany for %zu comms", comms.size());
1067 i = simcall_comm_waitany(comms.data(), comms.size(), -1);
1068 } catch (const Exception&) {
1069 XBT_INFO("request cancelled");
1073 // not MPI_UNDEFINED, as this is a simix return code
1076 //in case of an accumulate, we have to wait the end of all requests to apply the operation, ordered correctly.
1077 if ((requests[index] == MPI_REQUEST_NULL) ||
1078 (not((requests[index]->flags_ & MPI_REQ_ACCUMULATE) && (requests[index]->flags_ & MPI_REQ_RECV)))) {
1079 finish_wait(&requests[index],status);
1080 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_NON_PERSISTENT))
1081 requests[index] = MPI_REQUEST_NULL;
1087 if (index==MPI_UNDEFINED)
1088 Status::empty(status);
1093 static int sort_accumulates(const Request* a, const Request* b)
1095 return (a->tag() > b->tag());
1098 int Request::waitall(int count, MPI_Request requests[], MPI_Status status[])
1100 std::vector<MPI_Request> accumulates;
1103 MPI_Status *pstat = (status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat);
1104 int retvalue = MPI_SUCCESS;
1105 //tag invalid requests in the set
1106 if (status != MPI_STATUSES_IGNORE) {
1107 for (int c = 0; c < count; c++) {
1108 if (requests[c] == MPI_REQUEST_NULL || requests[c]->dst_ == MPI_PROC_NULL ||
1109 (requests[c]->flags_ & MPI_REQ_PREPARED)) {
1110 Status::empty(&status[c]);
1111 } else if (requests[c]->src_ == MPI_PROC_NULL) {
1112 Status::empty(&status[c]);
1113 status[c].MPI_SOURCE = MPI_PROC_NULL;
1117 for (int c = 0; c < count; c++) {
1118 if (MC_is_active() || MC_record_replay_is_active()) {
1119 wait(&requests[c],pstat);
1122 index = waitany(count, requests, pstat);
1124 if (index == MPI_UNDEFINED)
1127 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_RECV) &&
1128 (requests[index]->flags_ & MPI_REQ_ACCUMULATE))
1129 accumulates.push_back(requests[index]);
1130 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_NON_PERSISTENT))
1131 requests[index] = MPI_REQUEST_NULL;
1133 if (status != MPI_STATUSES_IGNORE) {
1134 status[index] = *pstat;
1135 if (status[index].MPI_ERROR == MPI_ERR_TRUNCATE)
1136 retvalue = MPI_ERR_IN_STATUS;
1140 if (not accumulates.empty()) {
1141 std::sort(accumulates.begin(), accumulates.end(), sort_accumulates);
1142 for (auto& req : accumulates) {
1143 finish_wait(&req, status);
1150 int Request::waitsome(int incount, MPI_Request requests[], int *indices, MPI_Status status[])
1156 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
1157 index = waitany(incount, requests, pstat);
1158 if(index==MPI_UNDEFINED) return MPI_UNDEFINED;
1159 if(status != MPI_STATUSES_IGNORE) {
1160 status[count] = *pstat;
1162 indices[count] = index;
1164 for (int i = 0; i < incount; i++) {
1165 if (i!=index && requests[i] != MPI_REQUEST_NULL
1166 && not(requests[i]->flags_ & MPI_REQ_FINISHED)) {
1167 test(&requests[i], pstat,&flag);
1170 if(status != MPI_STATUSES_IGNORE) {
1171 status[count] = *pstat;
1173 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
1174 requests[i]=MPI_REQUEST_NULL;
1182 MPI_Request Request::f2c(int id)
1184 if(id==MPI_FORTRAN_REQUEST_NULL)
1185 return MPI_REQUEST_NULL;
1186 return static_cast<MPI_Request>(F2C::lookup()->at(id));
1189 void Request::free_f(int id)
1191 if (id != MPI_FORTRAN_REQUEST_NULL) {
1192 F2C::lookup()->erase(id);
1196 int Request::get_status(const Request* req, int* flag, MPI_Status* status)
1200 if(req != MPI_REQUEST_NULL && req->action_ != nullptr) {
1201 req->iprobe(req->comm_->group()->rank(req->src_), req->tag_, req->comm_, flag, status);
1205 if (req != MPI_REQUEST_NULL &&
1206 (req->flags_ & MPI_REQ_GENERALIZED)
1207 && !(req->flags_ & MPI_REQ_COMPLETE)) {
1213 if(req != MPI_REQUEST_NULL &&
1214 status != MPI_STATUS_IGNORE) {
1215 int src = req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_;
1216 status->MPI_SOURCE = req->comm_->group()->rank(src);
1217 status->MPI_TAG = req->tag_ == MPI_ANY_TAG ? req->real_tag_ : req->tag_;
1218 status->MPI_ERROR = req->truncated_ ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
1219 status->count = req->real_size_;
1224 int Request::grequest_start(MPI_Grequest_query_function* query_fn, MPI_Grequest_free_function* free_fn,
1225 MPI_Grequest_cancel_function* cancel_fn, void* extra_state, MPI_Request* request)
1227 *request = new Request();
1228 (*request)->flags_ |= MPI_REQ_GENERALIZED;
1229 (*request)->flags_ |= MPI_REQ_PERSISTENT;
1230 (*request)->refcount_ = 1;
1231 ((*request)->generalized_funcs) = std::make_unique<smpi_mpi_generalized_request_funcs_t>();
1232 ((*request)->generalized_funcs)->query_fn=query_fn;
1233 ((*request)->generalized_funcs)->free_fn=free_fn;
1234 ((*request)->generalized_funcs)->cancel_fn=cancel_fn;
1235 ((*request)->generalized_funcs)->extra_state=extra_state;
1236 ((*request)->generalized_funcs)->cond = simgrid::s4u::ConditionVariable::create();
1237 ((*request)->generalized_funcs)->mutex = simgrid::s4u::Mutex::create();
1241 int Request::grequest_complete(MPI_Request request)
1243 if ((!(request->flags_ & MPI_REQ_GENERALIZED)) || request->generalized_funcs->mutex == nullptr)
1244 return MPI_ERR_REQUEST;
1245 request->generalized_funcs->mutex->lock();
1246 request->flags_ |= MPI_REQ_COMPLETE; // in case wait would be called after complete
1247 request->generalized_funcs->cond->notify_one();
1248 request->generalized_funcs->mutex->unlock();
1252 void Request::set_nbc_requests(MPI_Request* reqs, int size){
1253 nbc_requests_size_ = size;
1255 nbc_requests_ = reqs;
1258 nbc_requests_ = nullptr;
1262 int Request::get_nbc_requests_size() const
1264 return nbc_requests_size_;
1267 MPI_Request* Request::get_nbc_requests() const
1269 return nbc_requests_;