1 /* Copyright (c) 2007-2022. 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))
44 , size_(datatype->size() * count)
54 if(op != MPI_REPLACE && op != MPI_OP_NULL)
58 detached_sender_ = nullptr;
60 // get src_host if it's available (src is valid)
61 auto src_process = simgrid::s4u::Actor::by_pid(src);
63 src_host_ = src_process->get_host();
65 unmatched_types_ = false;
68 if (flags & MPI_REQ_PERSISTENT)
81 void Request::unref(MPI_Request* request)
83 xbt_assert(*request != MPI_REQUEST_NULL, "freeing an already free request");
85 (*request)->refcount_--;
86 if ((*request)->refcount_ < 0) {
87 (*request)->print_request("wrong refcount");
88 xbt_die("Whoops, wrong refcount");
90 if ((*request)->refcount_ == 0) {
91 if ((*request)->flags_ & MPI_REQ_GENERALIZED) {
92 ((*request)->generalized_funcs)->free_fn(((*request)->generalized_funcs)->extra_state);
94 Comm::unref((*request)->comm_);
95 Datatype::unref((*request)->type_);
97 if ((*request)->op_ != MPI_REPLACE && (*request)->op_ != MPI_OP_NULL)
98 Op::unref(&(*request)->op_);
100 (*request)->print_request("Destroying");
101 F2C::free_f((*request)->f2c_id());
103 *request = MPI_REQUEST_NULL;
105 (*request)->print_request("Decrementing");
109 bool Request::match_types(MPI_Datatype stype, MPI_Datatype rtype){
111 if ((stype == rtype) ||
112 //byte and packed always match with anything
113 (stype == MPI_PACKED || rtype == MPI_PACKED || stype == MPI_BYTE || rtype == MPI_BYTE) ||
114 //complex datatypes - we don't properly match these yet, as it would mean checking each subtype recursively.
115 (stype->flags() & DT_FLAG_DERIVED || rtype->flags() & DT_FLAG_DERIVED) ||
116 //duplicated datatypes, check if underlying is ok
117 (stype->duplicated_datatype()!=MPI_DATATYPE_NULL && match_types(stype->duplicated_datatype(), rtype)) ||
118 (rtype->duplicated_datatype()!=MPI_DATATYPE_NULL && match_types(stype, rtype->duplicated_datatype())))
121 XBT_WARN("Mismatched datatypes : sending %s and receiving %s", stype->name().c_str(), rtype->name().c_str());
126 bool Request::match_common(MPI_Request req, MPI_Request sender, MPI_Request receiver)
128 xbt_assert(sender, "Cannot match against null sender");
129 xbt_assert(receiver, "Cannot match against null receiver");
130 XBT_DEBUG("Trying to match %s of sender src %ld against %ld, tag %d against %d, id %d against %d",
131 (req == receiver ? "send" : "recv"), sender->src_, receiver->src_, sender->tag_, receiver->tag_,
132 sender->comm_->id(), receiver->comm_->id());
134 if ((receiver->comm_->id() == MPI_UNDEFINED || sender->comm_->id() == MPI_UNDEFINED ||
135 receiver->comm_->id() == sender->comm_->id()) &&
136 ((receiver->src_ == MPI_ANY_SOURCE && (receiver->comm_->group()->rank(sender->src_) != MPI_UNDEFINED)) ||
137 receiver->src_ == sender->src_) &&
138 ((receiver->tag_ == MPI_ANY_TAG && sender->tag_ >= 0) || receiver->tag_ == sender->tag_)) {
139 // we match, we can transfer some values
140 if (receiver->src_ == MPI_ANY_SOURCE) {
141 receiver->real_src_ = sender->src_;
142 receiver->src_host_ = sender->src_host_;
144 if (receiver->tag_ == MPI_ANY_TAG)
145 receiver->real_tag_ = sender->tag_;
146 if ((receiver->flags_ & MPI_REQ_PROBE) == 0 ){
147 if (receiver->real_size_ < sender->real_size_){
148 XBT_DEBUG("Truncating message - should not happen: receiver size : %zu < sender size : %zu", receiver->real_size_, sender->real_size_);
149 receiver->truncated_ = true;
152 //0-sized datatypes/counts should not interfere and match
153 if ( sender->real_size_ != 0 && receiver->real_size_ != 0 &&
154 !match_types(sender->type_, receiver->type_))
155 receiver->unmatched_types_ = true;
156 if (sender->detached_)
157 receiver->detached_sender_ = sender; // tie the sender to the receiver, as it is detached and has to be freed in
159 req->flags_ |= MPI_REQ_MATCHED; // mark as impossible to cancel anymore
160 XBT_DEBUG("match succeeded");
166 void Request::init_buffer(int count){
167 // FIXME Handle the case of a partial shared malloc.
168 // This part handles the problem of non-contiguous memory (for the unserialization at the reception)
169 if (not smpi_process()->replaying() &&
170 ((((flags_ & MPI_REQ_RECV) != 0) && ((flags_ & MPI_REQ_ACCUMULATE) != 0)) || (type_->flags() & DT_FLAG_DERIVED))) {
171 // This part handles the problem of non-contiguous memory
176 buf_ = xbt_malloc(count*type_->size());
177 if ((type_->flags() & DT_FLAG_DERIVED) && ((flags_ & MPI_REQ_SEND) != 0)) {
178 type_->serialize(old_buf_, buf_, count);
184 bool Request::match_recv(void* a, void* b, simgrid::kernel::activity::CommImpl*)
186 auto ref = static_cast<MPI_Request>(a);
187 auto req = static_cast<MPI_Request>(b);
188 bool match = match_common(req, req, ref);
189 if (match && (ref->comm_ != MPI_COMM_UNINITIALIZED) && !ref->comm_->is_smp_comm()){
190 if (ref->comm_->get_received_messages_count(ref->comm_->group()->rank(req->src_), ref->comm_->group()->rank(req->dst_), req->tag_) == req->message_id_ ){
191 if (((ref->flags_ & MPI_REQ_PROBE) == 0 ) && ((req->flags_ & MPI_REQ_PROBE) == 0)){
192 XBT_DEBUG("increasing count in comm %p, which was %u from pid %ld, to pid %ld with tag %d", ref->comm_, ref->comm_->get_received_messages_count(ref->comm_->group()->rank(req->src_), ref->comm_->group()->rank(req->dst_), req->tag_), req->src_, req->dst_, req->tag_);
193 ref->comm_->increment_received_messages_count(ref->comm_->group()->rank(req->src_), ref->comm_->group()->rank(req->dst_), req->tag_);
194 if (ref->real_size_ > req->real_size_){
195 ref->real_size_=req->real_size_;
200 req->flags_ &= ~MPI_REQ_MATCHED;
201 ref->detached_sender_=nullptr;
202 XBT_DEBUG("Refusing to match message, as its ID is not the one I expect. in comm %p, %u != %u, from pid %ld to pid %ld, with tag %d",ref->comm_, ref->comm_->get_received_messages_count(ref->comm_->group()->rank(req->src_), ref->comm_->group()->rank(req->dst_), req->tag_), req->message_id_ , req->src_, req->dst_, req->tag_);
208 bool Request::match_send(void* a, void* b, simgrid::kernel::activity::CommImpl*)
210 auto ref = static_cast<MPI_Request>(a);
211 auto req = static_cast<MPI_Request>(b);
212 return match_common(req, ref, req);
215 void Request::print_request(const char* message) const
217 XBT_VERB("%s request %p [buf = %p, size = %zu, src = %ld, dst = %ld, tag = %d, flags = %x]", message, this, buf_,
218 size_, src_, dst_, tag_, flags_);
221 /* factories, to hide the internal flags from the caller */
222 MPI_Request Request::bsend_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
224 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
225 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
226 MPI_REQ_PERSISTENT | MPI_REQ_SEND | MPI_REQ_PREPARED | MPI_REQ_BSEND);
229 MPI_Request Request::send_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
231 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
232 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
233 MPI_REQ_PERSISTENT | MPI_REQ_SEND | MPI_REQ_PREPARED);
236 MPI_Request Request::ssend_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
238 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
239 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
240 MPI_REQ_PERSISTENT | MPI_REQ_SSEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
243 MPI_Request Request::isend_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
245 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
246 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
247 MPI_REQ_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
250 MPI_Request Request::rma_send_init(const void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
255 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src),
256 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
257 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
259 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src),
260 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
261 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED |
268 MPI_Request Request::recv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
270 aid_t source = MPI_PROC_NULL;
271 if (src == MPI_ANY_SOURCE)
272 source = MPI_ANY_SOURCE;
273 else if (src != MPI_PROC_NULL)
274 source = comm->group()->actor(src);
275 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
277 simgrid::s4u::this_actor::get_pid(), tag, comm,
278 MPI_REQ_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
281 MPI_Request Request::rma_recv_init(void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
284 aid_t source = MPI_PROC_NULL;
285 if (src == MPI_ANY_SOURCE)
286 source = MPI_ANY_SOURCE;
287 else if (src != MPI_PROC_NULL)
288 source = comm->group()->actor(src);
291 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, source,
292 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
293 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
296 new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, source,
297 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
298 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED | MPI_REQ_ACCUMULATE, op);
303 MPI_Request Request::irecv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
305 aid_t 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);
310 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
311 source, simgrid::s4u::this_actor::get_pid(), tag, comm,
312 MPI_REQ_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
315 MPI_Request Request::ibsend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
317 auto request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
318 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
319 MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_BSEND);
320 if(dst != MPI_PROC_NULL)
325 MPI_Request Request::isend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
327 auto request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
328 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
329 MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND);
330 if(dst != MPI_PROC_NULL)
335 MPI_Request Request::issend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
337 auto request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
338 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
339 MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SSEND | MPI_REQ_SEND);
340 if(dst != MPI_PROC_NULL)
345 MPI_Request Request::irecv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
347 aid_t source = MPI_PROC_NULL;
348 if (src == MPI_ANY_SOURCE)
349 source = MPI_ANY_SOURCE;
350 else if (src != MPI_PROC_NULL)
351 source = comm->group()->actor(src);
352 auto request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, source,
353 simgrid::s4u::this_actor::get_pid(), tag, comm, MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV);
354 if(src != MPI_PROC_NULL)
359 int Request::recv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status * status)
361 MPI_Request request = irecv(buf, count, datatype, src, tag, comm);
362 int retval = wait(&request,status);
367 void Request::bsend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
369 auto request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
370 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
371 MPI_REQ_NON_PERSISTENT | MPI_REQ_SEND | MPI_REQ_BSEND);
373 if(dst != MPI_PROC_NULL)
375 wait(&request, MPI_STATUS_IGNORE);
379 void Request::send(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
381 auto request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
382 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
383 MPI_REQ_NON_PERSISTENT | MPI_REQ_SEND);
384 if(dst != MPI_PROC_NULL)
386 wait(&request, MPI_STATUS_IGNORE);
390 void Request::ssend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
392 auto request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
393 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
394 MPI_REQ_NON_PERSISTENT | MPI_REQ_SSEND | MPI_REQ_SEND);
396 if(dst != MPI_PROC_NULL)
398 wait(&request,MPI_STATUS_IGNORE);
402 void Request::sendrecv(const void *sendbuf, int sendcount, MPI_Datatype sendtype,int dst, int sendtag,
403 void *recvbuf, int recvcount, MPI_Datatype recvtype, int src, int recvtag,
404 MPI_Comm comm, MPI_Status * status)
406 aid_t source = MPI_PROC_NULL;
407 if (src == MPI_ANY_SOURCE)
408 source = MPI_ANY_SOURCE;
409 else if (src != MPI_PROC_NULL)
410 source = comm->group()->actor(src);
411 aid_t destination = dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL;
413 std::array<MPI_Request, 2> requests;
414 std::array<MPI_Status, 2> stats;
415 aid_t myid = simgrid::s4u::this_actor::get_pid();
416 if ((destination == myid) && (source == myid)) {
417 Datatype::copy(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
418 if (status != MPI_STATUS_IGNORE) {
419 status->MPI_SOURCE = source;
420 status->MPI_TAG = recvtag;
421 status->MPI_ERROR = MPI_SUCCESS;
422 status->count = sendcount * sendtype->size();
426 requests[0] = isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
427 requests[1] = irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
428 startall(2, requests.data());
429 waitall(2, requests.data(), stats.data());
432 if(status != MPI_STATUS_IGNORE) {
433 // Copy receive status
438 void Request::start()
440 s4u::Mailbox* mailbox;
442 xbt_assert(action_ == nullptr, "Cannot (re-)start unfinished communication");
443 //reinitialize temporary buffer for persistent requests
444 if(real_size_ > 0 && flags_ & MPI_REQ_FINISHED){
446 init_buffer(real_size_/type_->size());
448 flags_ &= ~MPI_REQ_PREPARED;
449 flags_ &= ~MPI_REQ_FINISHED;
452 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
454 if ((flags_ & MPI_REQ_RECV) != 0) {
455 this->print_request("New recv");
457 simgrid::smpi::ActorExt* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(dst_));
459 simgrid::s4u::MutexPtr mut = process->mailboxes_mutex();
460 if (smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)
463 bool is_probe = ((flags_ & MPI_REQ_PROBE) != 0);
464 flags_ |= MPI_REQ_PROBE;
466 if (smpi_cfg_async_small_thresh() == 0 && (flags_ & MPI_REQ_RMA) == 0) {
467 mailbox = process->mailbox();
468 } else if (((flags_ & MPI_REQ_RMA) != 0) || static_cast<int>(size_) < smpi_cfg_async_small_thresh()) {
469 //We have to check both mailboxes (because SSEND messages are sent to the large mbox).
470 //begin with the more appropriate one : the small one.
471 mailbox = process->mailbox_small();
472 XBT_DEBUG("Is there a corresponding send already posted in the small mailbox %s (in case of SSEND)?",
473 mailbox->get_cname());
474 simgrid::kernel::activity::ActivityImplPtr action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
476 if (action == nullptr) {
477 mailbox = process->mailbox();
478 XBT_DEBUG("No, nothing in the small mailbox test the other one : %s", mailbox->get_cname());
479 action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
480 if (action == nullptr) {
481 XBT_DEBUG("Still nothing, switch back to the small mailbox : %s", mailbox->get_cname());
482 mailbox = process->mailbox_small();
485 XBT_DEBUG("yes there was something for us in the small mailbox");
488 mailbox = process->mailbox_small();
489 XBT_DEBUG("Is there a corresponding send already posted the small mailbox?");
490 simgrid::kernel::activity::ActivityImplPtr action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
492 if (action == nullptr) {
493 XBT_DEBUG("No, nothing in the permanent receive mailbox");
494 mailbox = process->mailbox();
496 XBT_DEBUG("yes there was something for us in the small mailbox");
500 flags_ &= ~MPI_REQ_PROBE;
502 action_ = simcall_comm_irecv(
503 process->get_actor()->get_impl(), mailbox->get_impl(), buf_, &real_size_, &match_recv,
504 process->replaying() ? &smpi_comm_null_copy_buffer_callback : smpi_comm_copy_data_callback, this, -1.0);
505 XBT_DEBUG("recv simcall posted");
507 if (smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)
509 } else { /* the RECV flag was not set, so this is a send */
510 const simgrid::smpi::ActorExt* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(dst_));
511 xbt_assert(process, "Actor pid=%ld is gone??", dst_);
512 if (TRACE_smpi_view_internals())
513 TRACE_smpi_send(src_, src_, dst_, tag_, size_);
514 this->print_request("New send");
516 message_id_=comm_->get_sent_messages_count(comm_->group()->rank(src_), comm_->group()->rank(dst_), tag_);
517 comm_->increment_sent_messages_count(comm_->group()->rank(src_), comm_->group()->rank(dst_), tag_);
520 if ((flags_ & MPI_REQ_SSEND) == 0 &&
521 ((flags_ & MPI_REQ_RMA) != 0 || (flags_ & MPI_REQ_BSEND) != 0 ||
522 static_cast<int>(size_) < smpi_cfg_detached_send_thresh())) {
523 void *oldbuf = nullptr;
525 XBT_DEBUG("Send request %p is detached", this);
527 if (not(type_->flags() & DT_FLAG_DERIVED)) {
529 if (not process->replaying() && oldbuf != nullptr && size_ != 0) {
530 if (smpi_switch_data_segment(simgrid::s4u::Actor::by_pid(src_), buf_))
531 XBT_DEBUG("Privatization : We are sending from a zone inside global memory. Switch data segment ");
533 //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
534 //so actually ... don't use manually attached buffer space.
535 buf = xbt_malloc(size_);
536 memcpy(buf,oldbuf,size_);
537 XBT_DEBUG("buf %p copied into %p",oldbuf,buf);
542 //if we are giving back the control to the user without waiting for completion, we have to inject timings
543 double sleeptime = 0.0;
544 if (detached_ || ((flags_ & (MPI_REQ_ISEND | MPI_REQ_SSEND)) != 0)) { // issend should be treated as isend
545 // isend and send timings may be different
547 ((flags_ & MPI_REQ_ISEND) != 0)
548 ? simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->oisend(
549 size_, simgrid::s4u::Actor::by_pid(src_)->get_host(), simgrid::s4u::Actor::by_pid(dst_)->get_host())
550 : simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->osend(
551 size_, simgrid::s4u::Actor::by_pid(src_)->get_host(),
552 simgrid::s4u::Actor::by_pid(dst_)->get_host());
556 simgrid::s4u::this_actor::sleep_for(sleeptime);
557 XBT_DEBUG("sending size of %zu : sleep %f ", size_, sleeptime);
560 simgrid::s4u::MutexPtr mut = process->mailboxes_mutex();
562 if (smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)
565 if (not(smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)) {
566 mailbox = process->mailbox();
567 } else if (((flags_ & MPI_REQ_RMA) != 0) || static_cast<int>(size_) < smpi_cfg_async_small_thresh()) { // eager mode
568 bool is_probe = ((flags_ & MPI_REQ_PROBE) != 0);
569 flags_ |= MPI_REQ_PROBE;
571 mailbox = process->mailbox();
572 XBT_DEBUG("Is there a corresponding recv already posted in the large mailbox %s?", mailbox->get_cname());
573 simgrid::kernel::activity::ActivityImplPtr action = mailbox->iprobe(1, &match_send, static_cast<void*>(this));
574 if (action == nullptr) {
575 if ((flags_ & MPI_REQ_SSEND) == 0) {
576 mailbox = process->mailbox_small();
577 XBT_DEBUG("No, nothing in the large mailbox, message is to be sent on the small one %s",
578 mailbox->get_cname());
580 mailbox = process->mailbox_small();
581 XBT_DEBUG("SSEND : Is there a corresponding recv already posted in the small mailbox %s?",
582 mailbox->get_cname());
583 action = mailbox->iprobe(1, &match_send, static_cast<void*>(this));
584 if (action == nullptr) {
585 XBT_DEBUG("No, we are first, send to large mailbox");
586 mailbox = process->mailbox();
590 XBT_DEBUG("Yes there was something for us in the large mailbox");
593 flags_ &= ~MPI_REQ_PROBE;
595 mailbox = process->mailbox();
596 XBT_DEBUG("Send request %p is in the large mailbox %s (buf: %p)", this, mailbox->get_cname(), buf_);
599 size_t payload_size_ = size_ + 16;//MPI enveloppe size (tag+dest+communicator)
600 action_ = simcall_comm_isend(
601 simgrid::kernel::actor::ActorImpl::by_pid(src_), mailbox->get_impl(), payload_size_, -1.0, buf, real_size_,
603 &xbt_free_f, // how to free the userdata if a detached send fails
604 process->replaying() ? &smpi_comm_null_copy_buffer_callback : smpi_comm_copy_data_callback, this,
605 // detach if msg size < eager/rdv switch limit
607 XBT_DEBUG("send simcall posted");
609 /* FIXME: detached sends are not traceable (action_ == nullptr) */
610 if (action_ != nullptr) {
611 boost::static_pointer_cast<kernel::activity::CommImpl>(action_)->set_tracing_category(
612 smpi_process()->get_tracing_category());
615 if (smpi_cfg_async_small_thresh() != 0 || ((flags_ & MPI_REQ_RMA) != 0))
620 void Request::startall(int count, MPI_Request * requests)
622 if(requests== nullptr)
625 for(int i = 0; i < count; i++) {
626 if(requests[i]->src_ != MPI_PROC_NULL && requests[i]->dst_ != MPI_PROC_NULL)
627 requests[i]->start();
631 void Request::cancel()
633 this->flags_ |= MPI_REQ_CANCELLED;
634 if (this->action_ != nullptr)
635 (boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(this->action_))->cancel();
638 int Request::test(MPI_Request * request, MPI_Status * status, int* flag) {
639 // assume that *request is not MPI_REQUEST_NULL (filtered in PMPI_Test or testall before)
640 // to avoid deadlocks if used as a break condition, such as
641 // while (MPI_Test(request, flag, status) && flag) dostuff...
642 // because the time will not normally advance when only calls to MPI_Test are made -> deadlock
643 // multiplier to the sleeptime, to increase speed of execution, each failed test will increase it
644 xbt_assert(*request != MPI_REQUEST_NULL);
646 static int nsleeps = 1;
647 int ret = MPI_SUCCESS;
649 if(smpi_test_sleep > 0)
650 simgrid::s4u::this_actor::sleep_for(nsleeps * smpi_test_sleep);
652 Status::empty(status);
655 if ((*request)->flags_ & MPI_REQ_NBC){
656 *flag = finish_nbc_requests(request, 1);
659 if (((*request)->flags_ & (MPI_REQ_PREPARED | MPI_REQ_FINISHED)) == 0) {
660 if ((*request)->action_ != nullptr && ((*request)->flags_ & MPI_REQ_CANCELLED) == 0){
662 *flag = simcall_comm_test((*request)->action_.get());
663 } catch (const Exception&) {
668 if (((*request)->flags_ & MPI_REQ_GENERALIZED) && not((*request)->flags_ & MPI_REQ_COMPLETE))
671 finish_wait(request, status); // may invalidate *request
672 if (*request != MPI_REQUEST_NULL && ((*request)->flags_ & MPI_REQ_GENERALIZED)){
673 MPI_Status tmp_status;
674 MPI_Status* mystatus;
675 if (status == MPI_STATUS_IGNORE) {
676 mystatus = &tmp_status;
677 Status::empty(mystatus);
681 ret = ((*request)->generalized_funcs)->query_fn(((*request)->generalized_funcs)->extra_state, mystatus);
683 nsleeps=1;//reset the number of sleeps we will do next time
684 if (*request != MPI_REQUEST_NULL && ((*request)->flags_ & MPI_REQ_PERSISTENT) == 0)
685 *request = MPI_REQUEST_NULL;
686 } else if (smpi_cfg_grow_injected_times()) {
693 int Request::testsome(int incount, MPI_Request requests[], int *count, int *indices, MPI_Status status[])
699 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
702 for (int i = 0; i < incount; i++) {
703 if (requests[i] != MPI_REQUEST_NULL && not (requests[i]->flags_ & MPI_REQ_FINISHED)) {
704 int ret = test(&requests[i], pstat, &flag);
709 if (status != MPI_STATUSES_IGNORE)
710 status[*count] = *pstat;
712 if ((requests[i] != MPI_REQUEST_NULL) && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
713 requests[i] = MPI_REQUEST_NULL;
719 if(count_dead==incount)*count=MPI_UNDEFINED;
721 return MPI_ERR_IN_STATUS;
726 int Request::testany(int count, MPI_Request requests[], int *index, int* flag, MPI_Status * status)
728 std::vector<simgrid::kernel::activity::CommImpl*> comms;
729 comms.reserve(count);
732 int ret = MPI_SUCCESS;
733 *index = MPI_UNDEFINED;
735 std::vector<int> map; /** Maps all matching comms back to their location in requests **/
736 for (int i = 0; i < count; i++) {
737 if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->action_ && not(requests[i]->flags_ & MPI_REQ_PREPARED)) {
738 comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(requests[i]->action_.get()));
742 if (not map.empty()) {
743 //multiplier to the sleeptime, to increase speed of execution, each failed testany will increase it
744 static int nsleeps = 1;
745 if(smpi_test_sleep > 0)
746 simgrid::s4u::this_actor::sleep_for(nsleeps * smpi_test_sleep);
749 i = simcall_comm_testany(comms.data(), comms.size()); // The i-th element in comms matches!
750 } catch (const Exception&) {
751 XBT_DEBUG("Exception in testany");
755 if (i != -1) { // -1 is not MPI_UNDEFINED but a SIMIX return code. (nothing matches)
757 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags_ & MPI_REQ_GENERALIZED) &&
758 not(requests[*index]->flags_ & MPI_REQ_COMPLETE)) {
761 finish_wait(&requests[*index],status);
762 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags_ & MPI_REQ_GENERALIZED)){
763 MPI_Status tmp_status;
764 MPI_Status* mystatus;
765 if (status == MPI_STATUS_IGNORE) {
766 mystatus = &tmp_status;
767 Status::empty(mystatus);
771 ret=(requests[*index]->generalized_funcs)->query_fn((requests[*index]->generalized_funcs)->extra_state, mystatus);
774 if (requests[*index] != MPI_REQUEST_NULL && requests[*index]->flags_ & MPI_REQ_NBC){
775 *flag = finish_nbc_requests(&requests[*index] , 1);
778 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags_ & MPI_REQ_NON_PERSISTENT))
779 requests[*index] = MPI_REQUEST_NULL;
780 XBT_DEBUG("Testany - returning with index %d", *index);
788 XBT_DEBUG("Testany on inactive handles, returning flag=1 but empty status");
789 //all requests are null or inactive, return true
791 *index = MPI_UNDEFINED;
792 Status::empty(status);
798 int Request::testall(int count, MPI_Request requests[], int* outflag, MPI_Status status[])
801 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
805 for(int i=0; i<count; i++){
806 if (requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & MPI_REQ_PREPARED)) {
807 int ret = test(&requests[i], pstat, &flag);
813 if (ret != MPI_SUCCESS)
816 Status::empty(pstat);
818 if(status != MPI_STATUSES_IGNORE) {
823 return MPI_ERR_IN_STATUS;
828 void Request::probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
830 //FIXME find another way to avoid busy waiting ?
831 // the issue here is that we have to wait on a nonexistent comm
833 iprobe(source, tag, comm, &flag, status);
834 XBT_DEBUG("Busy Waiting on probing : %d", flag);
838 void Request::iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
839 // to avoid deadlock, we have to sleep some time here, or the timer won't advance and we will only do iprobe simcalls
840 // especially when used as a break condition, such as while (MPI_Iprobe(...)) dostuff...
841 // nsleeps is a multiplier to the sleeptime, to increase speed of execution, each failed iprobe will increase it
842 // This can speed up the execution of certain applications by an order of magnitude, such as HPL
843 static int nsleeps = 1;
844 double speed = s4u::this_actor::get_host()->get_speed();
845 double maxrate = smpi_cfg_iprobe_cpu_usage();
847 new Request(nullptr, 0, MPI_CHAR, source == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : comm->group()->actor(source),
848 simgrid::s4u::this_actor::get_pid(), tag, comm, MPI_REQ_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PROBE);
849 if (smpi_iprobe_sleep > 0) {
850 /** Compute the number of flops we will sleep **/
851 s4u::this_actor::exec_init(/*nsleeps: See comment above */ nsleeps *
852 /*(seconds * flop/s -> total flops)*/ smpi_iprobe_sleep * speed * maxrate)
854 /* Not the entire CPU can be used when iprobing: This is important for
855 * the energy consumption caused by polling with iprobes.
856 * Note also that the number of flops that was
857 * computed above contains a maxrate factor and is hence reduced (maxrate < 1)
859 ->set_bound(maxrate * speed)
863 // behave like a receive, but don't do it
864 s4u::Mailbox* mailbox;
866 request->print_request("New iprobe");
867 // We have to test both mailboxes as we don't know if we will receive one or another
868 if (smpi_cfg_async_small_thresh() > 0) {
869 mailbox = smpi_process()->mailbox_small();
870 XBT_DEBUG("Trying to probe the perm recv mailbox");
871 request->action_ = mailbox->iprobe(0, &match_recv, static_cast<void*>(request));
874 if (request->action_ == nullptr){
875 mailbox = smpi_process()->mailbox();
876 XBT_DEBUG("trying to probe the other mailbox");
877 request->action_ = mailbox->iprobe(0, &match_recv, static_cast<void*>(request));
880 if (request->action_ != nullptr){
881 kernel::activity::CommImplPtr sync_comm = boost::static_pointer_cast<kernel::activity::CommImpl>(request->action_);
882 const Request* req = static_cast<MPI_Request>(sync_comm->src_data_);
884 if (status != MPI_STATUS_IGNORE && (req->flags_ & MPI_REQ_PREPARED) == 0) {
885 status->MPI_SOURCE = comm->group()->rank(req->src_);
886 status->MPI_TAG = req->tag_;
887 status->MPI_ERROR = MPI_SUCCESS;
888 status->count = req->real_size_;
890 nsleeps = 1;//reset the number of sleeps we will do next time
894 if (smpi_cfg_grow_injected_times())
898 xbt_assert(request == MPI_REQUEST_NULL);
901 int Request::finish_nbc_requests(MPI_Request* request, int test){
905 ret = waitall((*request)->nbc_requests_.size(), (*request)->nbc_requests_.data(), MPI_STATUSES_IGNORE);
907 ret = testall((*request)->nbc_requests_.size(), (*request)->nbc_requests_.data(), &flag, MPI_STATUSES_IGNORE);
910 xbt_die("Failure when waiting on non blocking collective sub-requests");
912 XBT_DEBUG("Finishing non blocking collective request with %zu sub-requests", (*request)->nbc_requests_.size());
913 for(auto& req: (*request)->nbc_requests_){
914 if((*request)->buf_!=nullptr && req!=MPI_REQUEST_NULL){//reduce case
915 void * buf=req->buf_;
916 if((*request)->type_->flags() & DT_FLAG_DERIVED)
918 if(req->flags_ & MPI_REQ_RECV ){
919 if((*request)->op_!=MPI_OP_NULL){
920 int count=(*request)->size_/ (*request)->type_->size();
921 (*request)->op_->apply(buf, (*request)->buf_, &count, (*request)->type_);
923 smpi_free_tmp_buffer(static_cast<unsigned char*>(buf));
926 if(req!=MPI_REQUEST_NULL)
927 Request::unref(&req);
929 (*request)->nbc_requests_.clear();
934 void Request::finish_wait(MPI_Request* request, MPI_Status * status)
936 MPI_Request req = *request;
937 Status::empty(status);
938 if((req->flags_ & MPI_REQ_CANCELLED) != 0 && (req->flags_ & MPI_REQ_MATCHED) == 0) {
939 if (status!=MPI_STATUS_IGNORE)
941 if(req->detached_sender_ != nullptr)
942 unref(&(req->detached_sender_));
947 if ((req->flags_ & (MPI_REQ_PREPARED | MPI_REQ_GENERALIZED | MPI_REQ_FINISHED)) == 0) {
948 if (status != MPI_STATUS_IGNORE) {
949 if (req->src_== MPI_PROC_NULL || req->dst_== MPI_PROC_NULL){
950 Status::empty(status);
951 status->MPI_SOURCE = MPI_PROC_NULL;
953 aid_t src = req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_;
954 status->MPI_SOURCE = req->comm_->group()->rank(src);
955 status->MPI_TAG = req->tag_ == MPI_ANY_TAG ? req->real_tag_ : req->tag_;
956 status->MPI_ERROR = req->truncated_ ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
958 // this handles the case were size in receive differs from size in send
959 status->count = req->real_size_;
961 //detached send will be finished at the other end
962 if (not(req->detached_ && ((req->flags_ & MPI_REQ_SEND) != 0))) {
963 req->print_request("Finishing");
964 MPI_Datatype datatype = req->type_;
966 // FIXME Handle the case of a partial shared malloc.
967 if (not smpi_process()->replaying() &&
968 (((req->flags_ & MPI_REQ_ACCUMULATE) != 0) || (datatype->flags() & DT_FLAG_DERIVED))) {
969 if (smpi_switch_data_segment(simgrid::s4u::Actor::self(), req->old_buf_))
970 XBT_VERB("Privatization : We are unserializing to a zone in global memory Switch data segment ");
972 if(datatype->flags() & DT_FLAG_DERIVED){
973 // This part handles the problem of non-contiguous memory the unserialization at the reception
974 if ((req->flags_ & MPI_REQ_RECV) && datatype->size() != 0)
975 datatype->unserialize(req->buf_, req->old_buf_, req->real_size_/datatype->size() , req->op_);
978 } else if (req->flags_ & MPI_REQ_RECV) { // apply op on contiguous buffer for accumulate
979 if (datatype->size() != 0) {
980 int n = req->real_size_ / datatype->size();
981 req->op_->apply(req->buf_, req->old_buf_, &n, datatype);
990 if (TRACE_smpi_view_internals() && ((req->flags_ & MPI_REQ_RECV) != 0)) {
991 aid_t rank = simgrid::s4u::this_actor::get_pid();
992 aid_t src_traced = (req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_);
993 TRACE_smpi_recv(src_traced, rank,req->tag_);
995 if(req->detached_sender_ != nullptr){
996 //integrate pseudo-timing for buffering of small messages, do not bother to execute the simcall if 0
997 simgrid::s4u::Host* dst_host = simgrid::s4u::Actor::by_pid(req->dst_)->get_host();
998 double sleeptime = simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->orecv(
999 req->real_size(), req->src_host_, dst_host);
1000 if (sleeptime > 0.0) {
1001 simgrid::s4u::this_actor::sleep_for(sleeptime);
1002 XBT_DEBUG("receiving size of %zu : sleep %f ", req->real_size_, sleeptime);
1004 unref(&(req->detached_sender_));
1006 if (req->flags_ & MPI_REQ_PERSISTENT)
1007 req->action_ = nullptr;
1008 req->flags_ |= MPI_REQ_FINISHED;
1010 if (req->truncated_ || req->unmatched_types_) {
1011 char error_string[MPI_MAX_ERROR_STRING];
1014 if(req->truncated_ )
1015 errkind = MPI_ERR_TRUNCATE;
1017 errkind = MPI_ERR_TYPE;
1018 PMPI_Error_string(errkind, error_string, &error_size);
1019 MPI_Errhandler err = (req->comm_) ? (req->comm_)->errhandler() : MPI_ERRHANDLER_NULL;
1020 if (err == MPI_ERRHANDLER_NULL || err == MPI_ERRORS_RETURN)
1021 XBT_WARN("recv - returned %.*s instead of MPI_SUCCESS", error_size, error_string);
1022 else if (err == MPI_ERRORS_ARE_FATAL)
1023 xbt_die("recv - returned %.*s instead of MPI_SUCCESS", error_size, error_string);
1025 err->call((req->comm_), errkind);
1026 if (err != MPI_ERRHANDLER_NULL)
1027 simgrid::smpi::Errhandler::unref(err);
1028 MC_assert(not MC_is_active()); /* Only fail in MC mode */
1030 if(req->src_ != MPI_PROC_NULL && req->dst_ != MPI_PROC_NULL)
1034 int Request::wait(MPI_Request * request, MPI_Status * status)
1036 // assume that *request is not MPI_REQUEST_NULL (filtered in PMPI_Wait before)
1037 xbt_assert(*request != MPI_REQUEST_NULL);
1039 int ret=MPI_SUCCESS;
1041 if((*request)->src_ == MPI_PROC_NULL || (*request)->dst_ == MPI_PROC_NULL){
1042 if (status != MPI_STATUS_IGNORE) {
1043 Status::empty(status);
1044 status->MPI_SOURCE = MPI_PROC_NULL;
1046 (*request)=MPI_REQUEST_NULL;
1050 (*request)->print_request("Waiting");
1051 if ((*request)->flags_ & (MPI_REQ_PREPARED | MPI_REQ_FINISHED)) {
1052 Status::empty(status);
1056 if ((*request)->action_ != nullptr){
1058 // this is not a detached send
1059 simcall_comm_wait((*request)->action_.get(), -1.0);
1060 } catch (const CancelException&) {
1061 XBT_VERB("Request cancelled");
1065 if ((*request)->flags_ & MPI_REQ_GENERALIZED) {
1066 if (not((*request)->flags_ & MPI_REQ_COMPLETE)) {
1067 ((*request)->generalized_funcs)->mutex->lock();
1068 ((*request)->generalized_funcs)->cond->wait(((*request)->generalized_funcs)->mutex);
1069 ((*request)->generalized_funcs)->mutex->unlock();
1071 MPI_Status tmp_status;
1072 MPI_Status* mystatus;
1073 if (status == MPI_STATUS_IGNORE) {
1074 mystatus = &tmp_status;
1075 Status::empty(mystatus);
1079 ret = ((*request)->generalized_funcs)->query_fn(((*request)->generalized_funcs)->extra_state, mystatus);
1082 if ((*request)->truncated_)
1083 ret = MPI_ERR_TRUNCATE;
1085 if ((*request)->flags_ & MPI_REQ_NBC)
1086 finish_nbc_requests(request, 0);
1088 finish_wait(request, status); // may invalidate *request
1089 if (*request != MPI_REQUEST_NULL && (((*request)->flags_ & MPI_REQ_NON_PERSISTENT) != 0))
1090 *request = MPI_REQUEST_NULL;
1094 int Request::waitany(int count, MPI_Request requests[], MPI_Status * status)
1096 int index = MPI_UNDEFINED;
1099 // Wait for a request to complete
1100 std::vector<simgrid::kernel::activity::CommImpl*> comms;
1101 std::vector<int> map;
1102 XBT_DEBUG("Wait for one of %d", count);
1103 for(int i = 0; i < count; i++) {
1104 if (requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & MPI_REQ_PREPARED) &&
1105 not(requests[i]->flags_ & MPI_REQ_FINISHED)) {
1106 if (requests[i]->action_ != nullptr) {
1107 XBT_DEBUG("Waiting any %p ", requests[i]);
1108 comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(requests[i]->action_.get()));
1111 // This is a finished detached request, let's return this one
1112 comms.clear(); // don't do the waitany call afterwards
1114 if (requests[index]->flags_ & MPI_REQ_NBC)
1115 finish_nbc_requests(&requests[index], 0);
1116 finish_wait(&requests[i], status); // cleanup if refcount = 0
1117 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
1118 requests[i] = MPI_REQUEST_NULL; // set to null
1123 if (not comms.empty()) {
1124 XBT_DEBUG("Enter waitany for %zu comms", comms.size());
1127 i = simcall_comm_waitany(comms.data(), comms.size(), -1);
1128 } catch (const CancelException&) {
1129 XBT_INFO("request cancelled");
1133 // not MPI_UNDEFINED, as this is a simix return code
1136 //in case of an accumulate, we have to wait the end of all requests to apply the operation, ordered correctly.
1137 if ((requests[index] == MPI_REQUEST_NULL) ||
1138 (not((requests[index]->flags_ & MPI_REQ_ACCUMULATE) && (requests[index]->flags_ & MPI_REQ_RECV)))) {
1139 finish_wait(&requests[index],status);
1140 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_NON_PERSISTENT))
1141 requests[index] = MPI_REQUEST_NULL;
1148 if (index==MPI_UNDEFINED)
1149 Status::empty(status);
1154 static int sort_accumulates(const Request* a, const Request* b)
1156 return (a->tag() > b->tag());
1159 int Request::waitall(int count, MPI_Request requests[], MPI_Status status[])
1161 std::vector<MPI_Request> accumulates;
1164 MPI_Status *pstat = (status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat);
1165 int retvalue = MPI_SUCCESS;
1166 //tag invalid requests in the set
1167 if (status != MPI_STATUSES_IGNORE) {
1168 for (int c = 0; c < count; c++) {
1169 if (requests[c] == MPI_REQUEST_NULL || requests[c]->dst_ == MPI_PROC_NULL ||
1170 (requests[c]->flags_ & MPI_REQ_PREPARED)) {
1171 Status::empty(&status[c]);
1172 } else if (requests[c]->src_ == MPI_PROC_NULL) {
1173 Status::empty(&status[c]);
1174 status[c].MPI_SOURCE = MPI_PROC_NULL;
1178 for (int c = 0; c < count; c++) {
1179 if (MC_is_active() || MC_record_replay_is_active()) {
1180 wait(&requests[c],pstat);
1183 index = waitany(count, requests, pstat);
1185 if (index == MPI_UNDEFINED)
1188 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_RECV) &&
1189 (requests[index]->flags_ & MPI_REQ_ACCUMULATE))
1190 accumulates.push_back(requests[index]);
1191 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_NON_PERSISTENT))
1192 requests[index] = MPI_REQUEST_NULL;
1194 if (status != MPI_STATUSES_IGNORE) {
1195 status[index] = *pstat;
1196 if (status[index].MPI_ERROR == MPI_ERR_TRUNCATE)
1197 retvalue = MPI_ERR_IN_STATUS;
1201 std::sort(accumulates.begin(), accumulates.end(), sort_accumulates);
1202 for (auto& req : accumulates)
1203 finish_wait(&req, status);
1208 int Request::waitsome(int incount, MPI_Request requests[], int *indices, MPI_Status status[])
1214 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
1215 index = waitany(incount, requests, pstat);
1216 if(index==MPI_UNDEFINED) return MPI_UNDEFINED;
1217 if(status != MPI_STATUSES_IGNORE) {
1218 status[count] = *pstat;
1220 indices[count] = index;
1222 for (int i = 0; i < incount; i++) {
1223 if (i != index && requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & MPI_REQ_FINISHED)) {
1224 test(&requests[i], pstat,&flag);
1227 if(status != MPI_STATUSES_IGNORE) {
1228 status[count] = *pstat;
1230 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
1231 requests[i]=MPI_REQUEST_NULL;
1239 MPI_Request Request::f2c(int id)
1241 if(id==MPI_FORTRAN_REQUEST_NULL)
1242 return MPI_REQUEST_NULL;
1243 return static_cast<MPI_Request>(F2C::lookup()->at(id));
1246 void Request::free_f(int id)
1248 if (id != MPI_FORTRAN_REQUEST_NULL) {
1249 F2C::lookup()->erase(id);
1253 int Request::get_status(const Request* req, int* flag, MPI_Status* status)
1257 if(req != MPI_REQUEST_NULL && req->action_ != nullptr) {
1258 req->iprobe(req->comm_->group()->rank(req->src_), req->tag_, req->comm_, flag, status);
1262 if (req != MPI_REQUEST_NULL && (req->flags_ & MPI_REQ_GENERALIZED) && not(req->flags_ & MPI_REQ_COMPLETE)) {
1268 if(req != MPI_REQUEST_NULL &&
1269 status != MPI_STATUS_IGNORE) {
1270 aid_t src = req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_;
1271 status->MPI_SOURCE = req->comm_->group()->rank(src);
1272 status->MPI_TAG = req->tag_ == MPI_ANY_TAG ? req->real_tag_ : req->tag_;
1273 status->MPI_ERROR = req->truncated_ ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
1274 status->count = req->real_size_;
1279 int Request::grequest_start(MPI_Grequest_query_function* query_fn, MPI_Grequest_free_function* free_fn,
1280 MPI_Grequest_cancel_function* cancel_fn, void* extra_state, MPI_Request* request)
1282 *request = new Request();
1283 (*request)->flags_ |= MPI_REQ_GENERALIZED;
1284 (*request)->flags_ |= MPI_REQ_PERSISTENT;
1285 (*request)->refcount_ = 1;
1286 ((*request)->generalized_funcs) = std::make_unique<smpi_mpi_generalized_request_funcs_t>();
1287 ((*request)->generalized_funcs)->query_fn=query_fn;
1288 ((*request)->generalized_funcs)->free_fn=free_fn;
1289 ((*request)->generalized_funcs)->cancel_fn=cancel_fn;
1290 ((*request)->generalized_funcs)->extra_state=extra_state;
1291 ((*request)->generalized_funcs)->cond = simgrid::s4u::ConditionVariable::create();
1292 ((*request)->generalized_funcs)->mutex = simgrid::s4u::Mutex::create();
1296 int Request::grequest_complete(MPI_Request request)
1298 if ((not(request->flags_ & MPI_REQ_GENERALIZED)) || request->generalized_funcs->mutex == nullptr)
1299 return MPI_ERR_REQUEST;
1300 request->generalized_funcs->mutex->lock();
1301 request->flags_ |= MPI_REQ_COMPLETE; // in case wait would be called after complete
1302 request->generalized_funcs->cond->notify_one();
1303 request->generalized_funcs->mutex->unlock();
1307 void Request::start_nbc_requests(std::vector<MPI_Request> reqs){
1308 if (not reqs.empty()) {
1309 nbc_requests_ = reqs;
1310 Request::startall(reqs.size(), reqs.data());
1314 std::vector<MPI_Request> Request::get_nbc_requests() const
1316 return nbc_requests_;