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/ConditionVariable.hpp"
12 #include "simgrid/s4u/Exec.hpp"
13 #include "simgrid/s4u/Mutex.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/EngineImpl.hpp"
19 #include "src/kernel/activity/CommImpl.hpp"
20 #include "src/kernel/actor/ActorImpl.hpp"
21 #include "src/kernel/actor/SimcallObserver.hpp"
22 #include "src/mc/mc_replay.hpp"
23 #include "src/smpi/include/smpi_actor.hpp"
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_request, smpi, "Logging specific to SMPI (request)");
30 static simgrid::config::Flag<double> smpi_iprobe_sleep(
31 "smpi/iprobe", "Minimum time to inject inside a call to MPI_Iprobe", 1e-4);
32 static simgrid::config::Flag<double> smpi_test_sleep(
33 "smpi/test", "Minimum time to inject inside a call to MPI_Test", 1e-4);
35 std::vector<s_smpi_factor_t> smpi_ois_values;
37 extern std::function<void(simgrid::kernel::activity::CommImpl*, void*, size_t)> smpi_comm_copy_data_callback;
42 Request::Request(const void* buf, int count, MPI_Datatype datatype, aid_t src, aid_t dst, int tag, MPI_Comm comm,
43 unsigned flags, MPI_Op op)
44 : buf_(const_cast<void*>(buf))
47 , size_(datatype->size() * count)
57 if(op != MPI_REPLACE && op != MPI_OP_NULL)
61 detached_sender_ = nullptr;
63 // get src_host if it's available (src is valid)
64 auto src_process = simgrid::s4u::Actor::by_pid(src);
66 src_host_ = src_process->get_host();
68 unmatched_types_ = false;
71 if (flags & MPI_REQ_PERSISTENT)
84 void Request::unref(MPI_Request* request)
86 xbt_assert(*request != MPI_REQUEST_NULL, "freeing an already free request");
88 (*request)->refcount_--;
89 if ((*request)->refcount_ < 0) {
90 (*request)->print_request("wrong refcount");
91 xbt_die("Whoops, wrong refcount");
93 if ((*request)->refcount_ == 0) {
94 if ((*request)->flags_ & MPI_REQ_GENERALIZED) {
95 ((*request)->generalized_funcs)->free_fn(((*request)->generalized_funcs)->extra_state);
97 Comm::unref((*request)->comm_);
98 Datatype::unref((*request)->type_);
100 if ((*request)->op_ != MPI_REPLACE && (*request)->op_ != MPI_OP_NULL)
101 Op::unref(&(*request)->op_);
103 (*request)->print_request("Destroying");
104 F2C::free_f((*request)->f2c_id());
106 *request = MPI_REQUEST_NULL;
108 (*request)->print_request("Decrementing");
112 bool Request::match_types(MPI_Datatype stype, MPI_Datatype rtype){
114 if ((stype == rtype) ||
115 //byte and packed always match with anything
116 (stype == MPI_PACKED || rtype == MPI_PACKED || stype == MPI_BYTE || rtype == MPI_BYTE) ||
117 //complex datatypes - we don't properly match these yet, as it would mean checking each subtype recursively.
118 (stype->flags() & DT_FLAG_DERIVED || rtype->flags() & DT_FLAG_DERIVED) ||
119 //duplicated datatypes, check if underlying is ok
120 (stype->duplicated_datatype()!=MPI_DATATYPE_NULL && match_types(stype->duplicated_datatype(), rtype)) ||
121 (rtype->duplicated_datatype()!=MPI_DATATYPE_NULL && match_types(stype, rtype->duplicated_datatype())))
124 XBT_WARN("Mismatched datatypes : sending %s and receiving %s", stype->name().c_str(), rtype->name().c_str());
129 bool Request::match_common(MPI_Request req, MPI_Request sender, MPI_Request receiver)
131 xbt_assert(sender, "Cannot match against null sender");
132 xbt_assert(receiver, "Cannot match against null receiver");
133 XBT_DEBUG("Trying to match %s of sender src %ld against %ld, tag %d against %d, id %d against %d",
134 (req == receiver ? "send" : "recv"), sender->src_, receiver->src_, sender->tag_, receiver->tag_,
135 sender->comm_->id(), receiver->comm_->id());
137 if ((receiver->comm_->id() == MPI_UNDEFINED || sender->comm_->id() == MPI_UNDEFINED ||
138 receiver->comm_->id() == sender->comm_->id()) &&
139 ((receiver->src_ == MPI_ANY_SOURCE && (receiver->comm_->group()->rank(sender->src_) != MPI_UNDEFINED)) ||
140 receiver->src_ == sender->src_) &&
141 ((receiver->tag_ == MPI_ANY_TAG && sender->tag_ >= 0) || receiver->tag_ == sender->tag_)) {
142 // we match, we can transfer some values
143 if (receiver->src_ == MPI_ANY_SOURCE) {
144 receiver->real_src_ = sender->src_;
145 receiver->src_host_ = sender->src_host_;
147 if (receiver->tag_ == MPI_ANY_TAG)
148 receiver->real_tag_ = sender->tag_;
149 if ((receiver->flags_ & MPI_REQ_PROBE) == 0 && receiver->real_size_ < sender->real_size_) {
150 XBT_DEBUG("Truncating message - should not happen: receiver size : %zu < sender size : %zu", receiver->real_size_,
152 receiver->truncated_ = true;
154 //0-sized datatypes/counts should not interfere and match
155 if ( sender->real_size_ != 0 && receiver->real_size_ != 0 &&
156 !match_types(sender->type_, receiver->type_))
157 receiver->unmatched_types_ = true;
158 if (sender->detached_)
159 receiver->detached_sender_ = sender; // tie the sender to the receiver, as it is detached and has to be freed in
161 req->flags_ |= MPI_REQ_MATCHED; // mark as impossible to cancel anymore
162 XBT_DEBUG("match succeeded");
168 void Request::init_buffer(int count){
169 // FIXME Handle the case of a partial shared malloc.
170 // This part handles the problem of non-contiguous memory (for the unserialization at the reception)
171 if (not smpi_process()->replaying() &&
172 ((((flags_ & MPI_REQ_RECV) != 0) && ((flags_ & MPI_REQ_ACCUMULATE) != 0)) || (type_->flags() & DT_FLAG_DERIVED))) {
173 // This part handles the problem of non-contiguous memory
178 buf_ = xbt_malloc(count*type_->size());
179 if ((type_->flags() & DT_FLAG_DERIVED) && ((flags_ & MPI_REQ_SEND) != 0)) {
180 type_->serialize(old_buf_, buf_, count);
186 bool Request::match_recv(void* a, void* b, simgrid::kernel::activity::CommImpl*)
188 auto ref = static_cast<MPI_Request>(a);
189 auto req = static_cast<MPI_Request>(b);
190 bool match = match_common(req, req, ref);
191 if (not match || ref->comm_ == MPI_COMM_UNINITIALIZED || ref->comm_->is_smp_comm())
194 if (ref->comm_->get_received_messages_count(ref->comm_->group()->rank(req->src_),
195 ref->comm_->group()->rank(req->dst_), req->tag_) == req->message_id_) {
196 if (((ref->flags_ & MPI_REQ_PROBE) == 0) && ((req->flags_ & MPI_REQ_PROBE) == 0)) {
197 XBT_DEBUG("increasing count in comm %p, which was %u from pid %ld, to pid %ld with tag %d", ref->comm_,
198 ref->comm_->get_received_messages_count(ref->comm_->group()->rank(req->src_),
199 ref->comm_->group()->rank(req->dst_), req->tag_),
200 req->src_, req->dst_, req->tag_);
201 ref->comm_->increment_received_messages_count(ref->comm_->group()->rank(req->src_),
202 ref->comm_->group()->rank(req->dst_), req->tag_);
203 if (ref->real_size_ > req->real_size_) {
204 ref->real_size_ = req->real_size_;
209 req->flags_ &= ~MPI_REQ_MATCHED;
210 ref->detached_sender_ = nullptr;
211 XBT_DEBUG("Refusing to match message, as its ID is not the one I expect. in comm %p, %u != %u, "
212 "from pid %ld to pid %ld, with tag %d",
214 ref->comm_->get_received_messages_count(ref->comm_->group()->rank(req->src_),
215 ref->comm_->group()->rank(req->dst_), req->tag_),
216 req->message_id_, req->src_, req->dst_, req->tag_);
221 bool Request::match_send(void* a, void* b, simgrid::kernel::activity::CommImpl*)
223 auto ref = static_cast<MPI_Request>(a);
224 auto req = static_cast<MPI_Request>(b);
225 return match_common(req, ref, req);
228 void Request::print_request(const char* message) const
230 XBT_VERB("%s request %p [buf = %p, size = %zu, src = %ld, dst = %ld, tag = %d, flags = %x]", message, this, buf_,
231 size_, src_, dst_, tag_, flags_);
234 /* factories, to hide the internal flags from the caller */
235 MPI_Request Request::bsend_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
237 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
238 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
239 MPI_REQ_PERSISTENT | MPI_REQ_SEND | MPI_REQ_PREPARED | MPI_REQ_BSEND);
242 MPI_Request Request::send_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
244 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
245 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
246 MPI_REQ_PERSISTENT | MPI_REQ_SEND | MPI_REQ_PREPARED);
249 MPI_Request Request::ssend_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
251 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
252 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
253 MPI_REQ_PERSISTENT | MPI_REQ_SSEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
256 MPI_Request Request::isend_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
258 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
259 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
260 MPI_REQ_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
263 MPI_Request Request::rma_send_init(const void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
268 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src),
269 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
270 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
272 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src),
273 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
274 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED |
281 MPI_Request Request::recv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
283 aid_t source = MPI_PROC_NULL;
284 if (src == MPI_ANY_SOURCE)
285 source = MPI_ANY_SOURCE;
286 else if (src != MPI_PROC_NULL)
287 source = comm->group()->actor(src);
288 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
290 simgrid::s4u::this_actor::get_pid(), tag, comm,
291 MPI_REQ_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
294 MPI_Request Request::rma_recv_init(void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
297 aid_t source = MPI_PROC_NULL;
298 if (src == MPI_ANY_SOURCE)
299 source = MPI_ANY_SOURCE;
300 else if (src != MPI_PROC_NULL)
301 source = comm->group()->actor(src);
304 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, source,
305 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
306 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
309 new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, source,
310 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
311 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED | MPI_REQ_ACCUMULATE, op);
316 MPI_Request Request::irecv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
318 aid_t source = MPI_PROC_NULL;
319 if (src == MPI_ANY_SOURCE)
320 source = MPI_ANY_SOURCE;
321 else if (src != MPI_PROC_NULL)
322 source = comm->group()->actor(src);
323 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
324 source, simgrid::s4u::this_actor::get_pid(), tag, comm,
325 MPI_REQ_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
328 MPI_Request Request::ibsend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
330 auto request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
331 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
332 MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_BSEND);
333 if(dst != MPI_PROC_NULL)
338 MPI_Request Request::isend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
340 auto request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
341 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
342 MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND);
343 if(dst != MPI_PROC_NULL)
348 MPI_Request Request::issend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
350 auto request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
351 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
352 MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SSEND | MPI_REQ_SEND);
353 if(dst != MPI_PROC_NULL)
358 MPI_Request Request::irecv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
360 aid_t source = MPI_PROC_NULL;
361 if (src == MPI_ANY_SOURCE)
362 source = MPI_ANY_SOURCE;
363 else if (src != MPI_PROC_NULL)
364 source = comm->group()->actor(src);
365 auto request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, source,
366 simgrid::s4u::this_actor::get_pid(), tag, comm, MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV);
367 if(src != MPI_PROC_NULL)
372 int Request::recv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status * status)
374 MPI_Request request = irecv(buf, count, datatype, src, tag, comm);
375 int retval = wait(&request,status);
380 void Request::bsend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
382 auto request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
383 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
384 MPI_REQ_NON_PERSISTENT | MPI_REQ_SEND | MPI_REQ_BSEND);
386 if(dst != MPI_PROC_NULL)
388 wait(&request, MPI_STATUS_IGNORE);
392 void Request::send(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
394 auto request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
395 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
396 MPI_REQ_NON_PERSISTENT | MPI_REQ_SEND);
397 if(dst != MPI_PROC_NULL)
399 wait(&request, MPI_STATUS_IGNORE);
403 void Request::ssend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
405 auto request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
406 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
407 MPI_REQ_NON_PERSISTENT | MPI_REQ_SSEND | MPI_REQ_SEND);
409 if(dst != MPI_PROC_NULL)
411 wait(&request,MPI_STATUS_IGNORE);
415 void Request::sendrecv(const void *sendbuf, int sendcount, MPI_Datatype sendtype,int dst, int sendtag,
416 void *recvbuf, int recvcount, MPI_Datatype recvtype, int src, int recvtag,
417 MPI_Comm comm, MPI_Status * status)
419 aid_t source = MPI_PROC_NULL;
420 if (src == MPI_ANY_SOURCE)
421 source = MPI_ANY_SOURCE;
422 else if (src != MPI_PROC_NULL)
423 source = comm->group()->actor(src);
424 aid_t destination = dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL;
426 std::array<MPI_Request, 2> requests;
427 std::array<MPI_Status, 2> stats;
428 aid_t myid = simgrid::s4u::this_actor::get_pid();
429 if ((destination == myid) && (source == myid)) {
430 Datatype::copy(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
431 if (status != MPI_STATUS_IGNORE) {
432 status->MPI_SOURCE = source;
433 status->MPI_TAG = recvtag;
434 status->MPI_ERROR = MPI_SUCCESS;
435 status->count = sendcount * sendtype->size();
439 requests[0] = isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
440 requests[1] = irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
441 startall(2, requests.data());
442 waitall(2, requests.data(), stats.data());
445 if(status != MPI_STATUS_IGNORE) {
446 // Copy receive status
451 void Request::start()
453 s4u::Mailbox* mailbox;
455 xbt_assert(action_ == nullptr, "Cannot (re-)start unfinished communication");
456 //reinitialize temporary buffer for persistent requests
457 if(real_size_ > 0 && flags_ & MPI_REQ_FINISHED){
459 init_buffer(real_size_/type_->size());
461 flags_ &= ~MPI_REQ_PREPARED;
462 flags_ &= ~MPI_REQ_FINISHED;
465 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
467 if ((flags_ & MPI_REQ_RECV) != 0) {
468 this->print_request("New recv");
470 simgrid::smpi::ActorExt* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(dst_));
472 simgrid::s4u::MutexPtr mut = process->mailboxes_mutex();
473 if (smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)
476 bool is_probe = ((flags_ & MPI_REQ_PROBE) != 0);
477 flags_ |= MPI_REQ_PROBE;
479 if (smpi_cfg_async_small_thresh() == 0 && (flags_ & MPI_REQ_RMA) == 0) {
480 mailbox = process->mailbox();
481 } else if (((flags_ & MPI_REQ_RMA) != 0) || static_cast<int>(size_) < smpi_cfg_async_small_thresh()) {
482 //We have to check both mailboxes (because SSEND messages are sent to the large mbox).
483 //begin with the more appropriate one : the small one.
484 mailbox = process->mailbox_small();
485 XBT_DEBUG("Is there a corresponding send already posted in the small mailbox %s (in case of SSEND)?",
486 mailbox->get_cname());
487 simgrid::kernel::activity::ActivityImplPtr action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
489 if (action == nullptr) {
490 mailbox = process->mailbox();
491 XBT_DEBUG("No, nothing in the small mailbox test the other one : %s", mailbox->get_cname());
492 action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
493 if (action == nullptr) {
494 XBT_DEBUG("Still nothing, switch back to the small mailbox : %s", mailbox->get_cname());
495 mailbox = process->mailbox_small();
498 XBT_DEBUG("yes there was something for us in the small mailbox");
501 mailbox = process->mailbox_small();
502 XBT_DEBUG("Is there a corresponding send already posted the small mailbox?");
503 simgrid::kernel::activity::ActivityImplPtr action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
505 if (action == nullptr) {
506 XBT_DEBUG("No, nothing in the permanent receive mailbox");
507 mailbox = process->mailbox();
509 XBT_DEBUG("yes there was something for us in the small mailbox");
513 flags_ &= ~MPI_REQ_PROBE;
514 kernel::actor::CommIrecvSimcall observer{process->get_actor()->get_impl(),
516 static_cast<unsigned char*>(buf_),
519 process->replaying() ? &smpi_comm_null_copy_buffer_callback
520 : smpi_comm_copy_data_callback,
523 observer.set_tag(tag_);
525 action_ = kernel::actor::simcall_answered([&observer] { return kernel::activity::CommImpl::irecv(&observer); },
528 XBT_DEBUG("recv simcall posted");
530 if (smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)
532 } else { /* the RECV flag was not set, so this is a send */
533 const simgrid::smpi::ActorExt* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(dst_));
534 xbt_assert(process, "Actor pid=%ld is gone??", dst_);
535 if (TRACE_smpi_view_internals())
536 TRACE_smpi_send(src_, src_, dst_, tag_, size_);
537 this->print_request("New send");
539 message_id_=comm_->get_sent_messages_count(comm_->group()->rank(src_), comm_->group()->rank(dst_), tag_);
540 comm_->increment_sent_messages_count(comm_->group()->rank(src_), comm_->group()->rank(dst_), tag_);
543 if ((flags_ & MPI_REQ_SSEND) == 0 &&
544 ((flags_ & MPI_REQ_RMA) != 0 || (flags_ & MPI_REQ_BSEND) != 0 ||
545 static_cast<int>(size_) < smpi_cfg_detached_send_thresh())) {
546 void *oldbuf = nullptr;
548 XBT_DEBUG("Send request %p is detached", this);
550 if (not(type_->flags() & DT_FLAG_DERIVED)) {
552 if (not process->replaying() && oldbuf != nullptr && size_ != 0) {
553 if (smpi_switch_data_segment(simgrid::s4u::Actor::by_pid(src_), buf_))
554 XBT_DEBUG("Privatization : We are sending from a zone inside global memory. Switch data segment ");
556 //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
557 //so actually ... don't use manually attached buffer space.
558 buf = xbt_malloc(size_);
559 memcpy(buf,oldbuf,size_);
560 XBT_DEBUG("buf %p copied into %p",oldbuf,buf);
565 //if we are giving back the control to the user without waiting for completion, we have to inject timings
566 double sleeptime = 0.0;
567 if (detached_ || ((flags_ & (MPI_REQ_ISEND | MPI_REQ_SSEND)) != 0)) { // issend should be treated as isend
568 // isend and send timings may be different
570 ((flags_ & MPI_REQ_ISEND) != 0)
571 ? simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->oisend(
572 size_, simgrid::s4u::Actor::by_pid(src_)->get_host(), simgrid::s4u::Actor::by_pid(dst_)->get_host())
573 : simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->osend(
574 size_, simgrid::s4u::Actor::by_pid(src_)->get_host(),
575 simgrid::s4u::Actor::by_pid(dst_)->get_host());
579 simgrid::s4u::this_actor::sleep_for(sleeptime);
580 XBT_DEBUG("sending size of %zu : sleep %f ", size_, sleeptime);
583 simgrid::s4u::MutexPtr mut = process->mailboxes_mutex();
585 if (smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)
588 if (not(smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)) {
589 mailbox = process->mailbox();
590 } else if (((flags_ & MPI_REQ_RMA) != 0) || static_cast<int>(size_) < smpi_cfg_async_small_thresh()) { // eager mode
591 bool is_probe = ((flags_ & MPI_REQ_PROBE) != 0);
592 flags_ |= MPI_REQ_PROBE;
594 mailbox = process->mailbox();
595 XBT_DEBUG("Is there a corresponding recv already posted in the large mailbox %s?", mailbox->get_cname());
596 simgrid::kernel::activity::ActivityImplPtr action = mailbox->iprobe(1, &match_send, static_cast<void*>(this));
597 if (action == nullptr) {
598 if ((flags_ & MPI_REQ_SSEND) == 0) {
599 mailbox = process->mailbox_small();
600 XBT_DEBUG("No, nothing in the large mailbox, message is to be sent on the small one %s",
601 mailbox->get_cname());
603 mailbox = process->mailbox_small();
604 XBT_DEBUG("SSEND : Is there a corresponding recv already posted in the small mailbox %s?",
605 mailbox->get_cname());
606 action = mailbox->iprobe(1, &match_send, static_cast<void*>(this));
607 if (action == nullptr) {
608 XBT_DEBUG("No, we are first, send to large mailbox");
609 mailbox = process->mailbox();
613 XBT_DEBUG("Yes there was something for us in the large mailbox");
616 flags_ &= ~MPI_REQ_PROBE;
618 mailbox = process->mailbox();
619 XBT_DEBUG("Send request %p is in the large mailbox %s (buf: %p)", this, mailbox->get_cname(), buf_);
622 size_t payload_size_ = size_ + 16;//MPI enveloppe size (tag+dest+communicator)
623 kernel::actor::CommIsendSimcall observer{
624 simgrid::kernel::EngineImpl::get_instance()->get_actor_by_pid(src_), mailbox->get_impl(),
625 static_cast<double>(payload_size_), -1, static_cast<unsigned char*>(buf), real_size_, &match_send,
626 &xbt_free_f, // how to free the userdata if a detached send fails
627 process->replaying() ? &smpi_comm_null_copy_buffer_callback : smpi_comm_copy_data_callback, this,
628 // detach if msg size < eager/rdv switch limit
630 observer.set_tag(tag_);
631 action_ = kernel::actor::simcall_answered([&observer] { return kernel::activity::CommImpl::isend(&observer); },
633 XBT_DEBUG("send simcall posted");
635 /* FIXME: detached sends are not traceable (action_ == nullptr) */
636 if (action_ != nullptr) {
637 boost::static_pointer_cast<kernel::activity::CommImpl>(action_)->set_tracing_category(
638 smpi_process()->get_tracing_category());
641 if (smpi_cfg_async_small_thresh() != 0 || ((flags_ & MPI_REQ_RMA) != 0))
646 void Request::startall(int count, MPI_Request * requests)
648 if(requests== nullptr)
651 for(int i = 0; i < count; i++) {
652 if(requests[i]->src_ != MPI_PROC_NULL && requests[i]->dst_ != MPI_PROC_NULL)
653 requests[i]->start();
657 void Request::cancel()
659 this->flags_ |= MPI_REQ_CANCELLED;
660 if (this->action_ != nullptr)
661 (boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(this->action_))->cancel();
664 int Request::test(MPI_Request * request, MPI_Status * status, int* flag) {
665 // assume that *request is not MPI_REQUEST_NULL (filtered in PMPI_Test or testall before)
666 // to avoid deadlocks if used as a break condition, such as
667 // while (MPI_Test(request, flag, status) && flag) dostuff...
668 // because the time will not normally advance when only calls to MPI_Test are made -> deadlock
669 // multiplier to the sleeptime, to increase speed of execution, each failed test will increase it
670 xbt_assert(*request != MPI_REQUEST_NULL);
672 static int nsleeps = 1;
673 int ret = MPI_SUCCESS;
675 if(smpi_test_sleep > 0)
676 simgrid::s4u::this_actor::sleep_for(nsleeps * smpi_test_sleep);
678 Status::empty(status);
681 if ((*request)->flags_ & MPI_REQ_NBC){
682 *flag = finish_nbc_requests(request, 1);
685 if (((*request)->flags_ & (MPI_REQ_PREPARED | MPI_REQ_FINISHED)) == 0) {
686 if ((*request)->action_ != nullptr && ((*request)->flags_ & MPI_REQ_CANCELLED) == 0){
688 kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
689 kernel::actor::ActivityTestSimcall observer{issuer, (*request)->action_.get()};
690 *flag = kernel::actor::simcall_answered(
691 [&observer] { return observer.get_activity()->test(observer.get_issuer()); }, &observer);
692 } catch (const Exception&) {
697 if (((*request)->flags_ & MPI_REQ_GENERALIZED) && not((*request)->flags_ & MPI_REQ_COMPLETE))
700 finish_wait(request, status); // may invalidate *request
701 if (*request != MPI_REQUEST_NULL && ((*request)->flags_ & MPI_REQ_GENERALIZED)){
702 MPI_Status tmp_status;
703 MPI_Status* mystatus;
704 if (status == MPI_STATUS_IGNORE) {
705 mystatus = &tmp_status;
706 Status::empty(mystatus);
710 ret = ((*request)->generalized_funcs)->query_fn(((*request)->generalized_funcs)->extra_state, mystatus);
712 nsleeps=1;//reset the number of sleeps we will do next time
713 if (*request != MPI_REQUEST_NULL && ((*request)->flags_ & MPI_REQ_PERSISTENT) == 0)
714 *request = MPI_REQUEST_NULL;
715 } else if (smpi_cfg_grow_injected_times()) {
722 int Request::testsome(int incount, MPI_Request requests[], int *count, int *indices, MPI_Status status[])
728 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
731 for (int i = 0; i < incount; i++) {
732 if (requests[i] != MPI_REQUEST_NULL && not (requests[i]->flags_ & MPI_REQ_FINISHED)) {
733 int ret = test(&requests[i], pstat, &flag);
738 if (status != MPI_STATUSES_IGNORE)
739 status[*count] = *pstat;
741 if ((requests[i] != MPI_REQUEST_NULL) && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
742 requests[i] = MPI_REQUEST_NULL;
748 if(count_dead==incount)*count=MPI_UNDEFINED;
750 return MPI_ERR_IN_STATUS;
755 int Request::testany(int count, MPI_Request requests[], int *index, int* flag, MPI_Status * status)
757 std::vector<simgrid::kernel::activity::ActivityImpl*> comms;
758 comms.reserve(count);
761 int ret = MPI_SUCCESS;
762 *index = MPI_UNDEFINED;
764 std::vector<int> map; /** Maps all matching comms back to their location in requests **/
765 for (int i = 0; i < count; i++) {
766 if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->action_ && not(requests[i]->flags_ & MPI_REQ_PREPARED)) {
767 comms.push_back(requests[i]->action_.get());
771 if (not map.empty()) {
772 //multiplier to the sleeptime, to increase speed of execution, each failed testany will increase it
773 static int nsleeps = 1;
774 if(smpi_test_sleep > 0)
775 simgrid::s4u::this_actor::sleep_for(nsleeps * smpi_test_sleep);
778 kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
779 kernel::actor::ActivityTestanySimcall observer{issuer, comms};
780 i = kernel::actor::simcall_answered(
782 return kernel::activity::ActivityImpl::test_any(observer.get_issuer(), observer.get_activities());
785 } catch (const Exception&) {
786 XBT_DEBUG("Exception in testany");
790 if (i != -1) { // -1 is not MPI_UNDEFINED but a SIMIX return code. (nothing matches)
792 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags_ & MPI_REQ_GENERALIZED) &&
793 not(requests[*index]->flags_ & MPI_REQ_COMPLETE)) {
796 finish_wait(&requests[*index],status);
797 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags_ & MPI_REQ_GENERALIZED)){
798 MPI_Status tmp_status;
799 MPI_Status* mystatus;
800 if (status == MPI_STATUS_IGNORE) {
801 mystatus = &tmp_status;
802 Status::empty(mystatus);
806 ret=(requests[*index]->generalized_funcs)->query_fn((requests[*index]->generalized_funcs)->extra_state, mystatus);
809 if (requests[*index] != MPI_REQUEST_NULL && requests[*index]->flags_ & MPI_REQ_NBC){
810 *flag = finish_nbc_requests(&requests[*index] , 1);
813 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags_ & MPI_REQ_NON_PERSISTENT))
814 requests[*index] = MPI_REQUEST_NULL;
815 XBT_DEBUG("Testany - returning with index %d", *index);
823 XBT_DEBUG("Testany on inactive handles, returning flag=1 but empty status");
824 //all requests are null or inactive, return true
826 *index = MPI_UNDEFINED;
827 Status::empty(status);
833 int Request::testall(int count, MPI_Request requests[], int* outflag, MPI_Status status[])
836 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
840 for(int i=0; i<count; i++){
841 if (requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & MPI_REQ_PREPARED)) {
842 int ret = test(&requests[i], pstat, &flag);
848 if (ret != MPI_SUCCESS)
851 Status::empty(pstat);
853 if(status != MPI_STATUSES_IGNORE) {
858 return MPI_ERR_IN_STATUS;
863 void Request::probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
865 //FIXME find another way to avoid busy waiting ?
866 // the issue here is that we have to wait on a nonexistent comm
868 iprobe(source, tag, comm, &flag, status);
869 XBT_DEBUG("Busy Waiting on probing : %d", flag);
873 void Request::iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
874 // to avoid deadlock, we have to sleep some time here, or the timer won't advance and we will only do iprobe simcalls
875 // especially when used as a break condition, such as while (MPI_Iprobe(...)) dostuff...
876 // nsleeps is a multiplier to the sleeptime, to increase speed of execution, each failed iprobe will increase it
877 // This can speed up the execution of certain applications by an order of magnitude, such as HPL
878 static int nsleeps = 1;
879 double speed = s4u::this_actor::get_host()->get_speed();
880 double maxrate = smpi_cfg_iprobe_cpu_usage();
882 new Request(nullptr, 0, MPI_CHAR, source == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : comm->group()->actor(source),
883 simgrid::s4u::this_actor::get_pid(), tag, comm, MPI_REQ_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PROBE);
884 if (smpi_iprobe_sleep > 0) {
885 /** Compute the number of flops we will sleep **/
886 s4u::this_actor::exec_init(/*nsleeps: See comment above */ nsleeps *
887 /*(seconds * flop/s -> total flops)*/ smpi_iprobe_sleep * speed * maxrate)
889 /* Not the entire CPU can be used when iprobing: This is important for
890 * the energy consumption caused by polling with iprobes.
891 * Note also that the number of flops that was
892 * computed above contains a maxrate factor and is hence reduced (maxrate < 1)
894 ->set_bound(maxrate * speed)
898 // behave like a receive, but don't do it
899 s4u::Mailbox* mailbox;
901 request->print_request("New iprobe");
902 // We have to test both mailboxes as we don't know if we will receive one or another
903 if (smpi_cfg_async_small_thresh() > 0) {
904 mailbox = smpi_process()->mailbox_small();
905 XBT_DEBUG("Trying to probe the perm recv mailbox");
906 request->action_ = mailbox->iprobe(0, &match_recv, static_cast<void*>(request));
909 if (request->action_ == nullptr){
910 mailbox = smpi_process()->mailbox();
911 XBT_DEBUG("trying to probe the other mailbox");
912 request->action_ = mailbox->iprobe(0, &match_recv, static_cast<void*>(request));
915 if (request->action_ != nullptr){
916 kernel::activity::CommImplPtr sync_comm = boost::static_pointer_cast<kernel::activity::CommImpl>(request->action_);
917 const Request* req = static_cast<MPI_Request>(sync_comm->src_data_);
919 if (status != MPI_STATUS_IGNORE && (req->flags_ & MPI_REQ_PREPARED) == 0) {
920 status->MPI_SOURCE = comm->group()->rank(req->src_);
921 status->MPI_TAG = req->tag_;
922 status->MPI_ERROR = MPI_SUCCESS;
923 status->count = req->real_size_;
925 nsleeps = 1;//reset the number of sleeps we will do next time
929 if (smpi_cfg_grow_injected_times())
933 xbt_assert(request == MPI_REQUEST_NULL);
936 int Request::finish_nbc_requests(MPI_Request* request, int test){
940 ret = waitall((*request)->nbc_requests_.size(), (*request)->nbc_requests_.data(), MPI_STATUSES_IGNORE);
942 ret = testall((*request)->nbc_requests_.size(), (*request)->nbc_requests_.data(), &flag, MPI_STATUSES_IGNORE);
945 xbt_die("Failure when waiting on non blocking collective sub-requests");
947 XBT_DEBUG("Finishing non blocking collective request with %zu sub-requests", (*request)->nbc_requests_.size());
948 for(auto& req: (*request)->nbc_requests_){
949 if((*request)->buf_!=nullptr && req!=MPI_REQUEST_NULL){//reduce case
950 void * buf=req->buf_;
951 if((*request)->type_->flags() & DT_FLAG_DERIVED)
953 if(req->flags_ & MPI_REQ_RECV ){
954 if((*request)->op_!=MPI_OP_NULL){
955 int count=(*request)->size_/ (*request)->type_->size();
956 (*request)->op_->apply(buf, (*request)->buf_, &count, (*request)->type_);
958 smpi_free_tmp_buffer(static_cast<unsigned char*>(buf));
961 if(req!=MPI_REQUEST_NULL)
962 Request::unref(&req);
964 (*request)->nbc_requests_.clear();
969 void Request::finish_wait(MPI_Request* request, MPI_Status * status)
971 MPI_Request req = *request;
972 Status::empty(status);
973 if((req->flags_ & MPI_REQ_CANCELLED) != 0 && (req->flags_ & MPI_REQ_MATCHED) == 0) {
974 if (status!=MPI_STATUS_IGNORE)
976 if(req->detached_sender_ != nullptr)
977 unref(&(req->detached_sender_));
982 if ((req->flags_ & (MPI_REQ_PREPARED | MPI_REQ_GENERALIZED | MPI_REQ_FINISHED)) == 0) {
983 if (status != MPI_STATUS_IGNORE) {
984 if (req->src_== MPI_PROC_NULL || req->dst_== MPI_PROC_NULL){
985 Status::empty(status);
986 status->MPI_SOURCE = MPI_PROC_NULL;
988 aid_t src = req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_;
989 status->MPI_SOURCE = req->comm_->group()->rank(src);
990 status->MPI_TAG = req->tag_ == MPI_ANY_TAG ? req->real_tag_ : req->tag_;
991 status->MPI_ERROR = req->truncated_ ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
993 // this handles the case were size in receive differs from size in send
994 status->count = req->real_size_;
996 //detached send will be finished at the other end
997 if (not(req->detached_ && ((req->flags_ & MPI_REQ_SEND) != 0))) {
998 req->print_request("Finishing");
999 MPI_Datatype datatype = req->type_;
1001 // FIXME Handle the case of a partial shared malloc.
1002 if (not smpi_process()->replaying() &&
1003 (((req->flags_ & MPI_REQ_ACCUMULATE) != 0) || (datatype->flags() & DT_FLAG_DERIVED))) {
1004 if (smpi_switch_data_segment(simgrid::s4u::Actor::self(), req->old_buf_))
1005 XBT_VERB("Privatization : We are unserializing to a zone in global memory Switch data segment ");
1007 if(datatype->flags() & DT_FLAG_DERIVED){
1008 // This part handles the problem of non-contiguous memory the unserialization at the reception
1009 if ((req->flags_ & MPI_REQ_RECV) && datatype->size() != 0)
1010 datatype->unserialize(req->buf_, req->old_buf_, req->real_size_/datatype->size() , req->op_);
1011 xbt_free(req->buf_);
1013 } else if (req->flags_ & MPI_REQ_RECV) { // apply op on contiguous buffer for accumulate
1014 if (datatype->size() != 0) {
1015 int n = req->real_size_ / datatype->size();
1016 req->op_->apply(req->buf_, req->old_buf_, &n, datatype);
1018 xbt_free(req->buf_);
1025 if (TRACE_smpi_view_internals() && ((req->flags_ & MPI_REQ_RECV) != 0)) {
1026 aid_t rank = simgrid::s4u::this_actor::get_pid();
1027 aid_t src_traced = (req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_);
1028 TRACE_smpi_recv(src_traced, rank,req->tag_);
1030 if(req->detached_sender_ != nullptr){
1031 //integrate pseudo-timing for buffering of small messages, do not bother to execute the simcall if 0
1032 simgrid::s4u::Host* dst_host = simgrid::s4u::Actor::by_pid(req->dst_)->get_host();
1033 double sleeptime = simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->orecv(
1034 req->real_size(), req->src_host_, dst_host);
1035 if (sleeptime > 0.0) {
1036 simgrid::s4u::this_actor::sleep_for(sleeptime);
1037 XBT_DEBUG("receiving size of %zu : sleep %f ", req->real_size_, sleeptime);
1039 unref(&(req->detached_sender_));
1041 if (req->flags_ & MPI_REQ_PERSISTENT)
1042 req->action_ = nullptr;
1043 req->flags_ |= MPI_REQ_FINISHED;
1045 if (req->truncated_ || req->unmatched_types_) {
1046 char error_string[MPI_MAX_ERROR_STRING];
1049 if(req->truncated_ )
1050 errkind = MPI_ERR_TRUNCATE;
1052 errkind = MPI_ERR_TYPE;
1053 PMPI_Error_string(errkind, error_string, &error_size);
1054 MPI_Errhandler err = (req->comm_) ? (req->comm_)->errhandler() : MPI_ERRHANDLER_NULL;
1055 if (err == MPI_ERRHANDLER_NULL || err == MPI_ERRORS_RETURN)
1056 XBT_WARN("recv - returned %.*s instead of MPI_SUCCESS", error_size, error_string);
1057 else if (err == MPI_ERRORS_ARE_FATAL)
1058 xbt_die("recv - returned %.*s instead of MPI_SUCCESS", error_size, error_string);
1060 err->call((req->comm_), errkind);
1061 if (err != MPI_ERRHANDLER_NULL)
1062 simgrid::smpi::Errhandler::unref(err);
1063 MC_assert(not MC_is_active()); /* Only fail in MC mode */
1065 if(req->src_ != MPI_PROC_NULL && req->dst_ != MPI_PROC_NULL)
1069 int Request::wait(MPI_Request * request, MPI_Status * status)
1071 // assume that *request is not MPI_REQUEST_NULL (filtered in PMPI_Wait before)
1072 xbt_assert(*request != MPI_REQUEST_NULL);
1074 int ret=MPI_SUCCESS;
1076 if((*request)->src_ == MPI_PROC_NULL || (*request)->dst_ == MPI_PROC_NULL){
1077 if (status != MPI_STATUS_IGNORE) {
1078 Status::empty(status);
1079 status->MPI_SOURCE = MPI_PROC_NULL;
1081 (*request)=MPI_REQUEST_NULL;
1085 (*request)->print_request("Waiting");
1086 if ((*request)->flags_ & (MPI_REQ_PREPARED | MPI_REQ_FINISHED)) {
1087 Status::empty(status);
1091 if ((*request)->action_ != nullptr){
1093 // this is not a detached send
1094 kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
1095 kernel::actor::ActivityWaitSimcall observer{issuer, (*request)->action_.get(), -1};
1096 kernel::actor::simcall_blocking(
1097 [&observer] { observer.get_activity()->wait_for(observer.get_issuer(), observer.get_timeout()); },
1099 } catch (const CancelException&) {
1100 XBT_VERB("Request cancelled");
1104 if ((*request)->flags_ & MPI_REQ_GENERALIZED) {
1105 if (not((*request)->flags_ & MPI_REQ_COMPLETE)) {
1106 ((*request)->generalized_funcs)->mutex->lock();
1107 ((*request)->generalized_funcs)->cond->wait(((*request)->generalized_funcs)->mutex);
1108 ((*request)->generalized_funcs)->mutex->unlock();
1110 MPI_Status tmp_status;
1111 MPI_Status* mystatus;
1112 if (status == MPI_STATUS_IGNORE) {
1113 mystatus = &tmp_status;
1114 Status::empty(mystatus);
1118 ret = ((*request)->generalized_funcs)->query_fn(((*request)->generalized_funcs)->extra_state, mystatus);
1121 if ((*request)->truncated_)
1122 ret = MPI_ERR_TRUNCATE;
1124 if ((*request)->flags_ & MPI_REQ_NBC)
1125 finish_nbc_requests(request, 0);
1127 finish_wait(request, status); // may invalidate *request
1128 if (*request != MPI_REQUEST_NULL && (((*request)->flags_ & MPI_REQ_NON_PERSISTENT) != 0))
1129 *request = MPI_REQUEST_NULL;
1133 int Request::waitany(int count, MPI_Request requests[], MPI_Status * status)
1135 int index = MPI_UNDEFINED;
1138 // Wait for a request to complete
1139 std::vector<simgrid::kernel::activity::ActivityImpl*> comms;
1140 std::vector<int> map;
1141 XBT_DEBUG("Wait for one of %d", count);
1142 for(int i = 0; i < count; i++) {
1143 if (requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & MPI_REQ_PREPARED) &&
1144 not(requests[i]->flags_ & MPI_REQ_FINISHED)) {
1145 if (requests[i]->action_ != nullptr) {
1146 XBT_DEBUG("Waiting any %p ", requests[i]);
1147 comms.push_back(requests[i]->action_.get());
1150 // This is a finished detached request, let's return this one
1151 comms.clear(); // don't do the waitany call afterwards
1153 if (requests[index]->flags_ & MPI_REQ_NBC)
1154 finish_nbc_requests(&requests[index], 0);
1155 finish_wait(&requests[i], status); // cleanup if refcount = 0
1156 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
1157 requests[i] = MPI_REQUEST_NULL; // set to null
1162 if (not comms.empty()) {
1163 XBT_DEBUG("Enter waitany for %zu comms", comms.size());
1166 kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
1167 kernel::actor::ActivityWaitanySimcall observer{issuer, comms, -1};
1168 i = kernel::actor::simcall_blocking(
1170 kernel::activity::ActivityImpl::wait_any_for(observer.get_issuer(), observer.get_activities(),
1171 observer.get_timeout());
1174 } catch (const CancelException&) {
1175 XBT_INFO("request cancelled");
1179 // not MPI_UNDEFINED, as this is a simix return code
1182 //in case of an accumulate, we have to wait the end of all requests to apply the operation, ordered correctly.
1183 if ((requests[index] == MPI_REQUEST_NULL) ||
1184 (not((requests[index]->flags_ & MPI_REQ_ACCUMULATE) && (requests[index]->flags_ & MPI_REQ_RECV)))) {
1185 finish_wait(&requests[index],status);
1186 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_NON_PERSISTENT))
1187 requests[index] = MPI_REQUEST_NULL;
1194 if (index==MPI_UNDEFINED)
1195 Status::empty(status);
1200 static int sort_accumulates(const Request* a, const Request* b)
1202 return (a->tag() > b->tag());
1205 int Request::waitall(int count, MPI_Request requests[], MPI_Status status[])
1207 std::vector<MPI_Request> accumulates;
1210 MPI_Status *pstat = (status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat);
1211 int retvalue = MPI_SUCCESS;
1212 //tag invalid requests in the set
1213 if (status != MPI_STATUSES_IGNORE) {
1214 for (int c = 0; c < count; c++) {
1215 if (requests[c] == MPI_REQUEST_NULL || requests[c]->dst_ == MPI_PROC_NULL ||
1216 (requests[c]->flags_ & MPI_REQ_PREPARED)) {
1217 Status::empty(&status[c]);
1218 } else if (requests[c]->src_ == MPI_PROC_NULL) {
1219 Status::empty(&status[c]);
1220 status[c].MPI_SOURCE = MPI_PROC_NULL;
1224 for (int c = 0; c < count; c++) {
1225 if (MC_is_active() || MC_record_replay_is_active()) {
1226 wait(&requests[c],pstat);
1229 index = waitany(count, requests, pstat);
1231 if (index == MPI_UNDEFINED)
1234 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_RECV) &&
1235 (requests[index]->flags_ & MPI_REQ_ACCUMULATE))
1236 accumulates.push_back(requests[index]);
1237 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_NON_PERSISTENT))
1238 requests[index] = MPI_REQUEST_NULL;
1240 if (status != MPI_STATUSES_IGNORE) {
1241 status[index] = *pstat;
1242 if (status[index].MPI_ERROR == MPI_ERR_TRUNCATE)
1243 retvalue = MPI_ERR_IN_STATUS;
1247 std::sort(accumulates.begin(), accumulates.end(), sort_accumulates);
1248 for (auto& req : accumulates)
1249 finish_wait(&req, status);
1254 int Request::waitsome(int incount, MPI_Request requests[], int *indices, MPI_Status status[])
1260 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
1261 index = waitany(incount, requests, pstat);
1262 if(index==MPI_UNDEFINED) return MPI_UNDEFINED;
1263 if(status != MPI_STATUSES_IGNORE) {
1264 status[count] = *pstat;
1266 indices[count] = index;
1268 for (int i = 0; i < incount; i++) {
1269 if (i != index && requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & MPI_REQ_FINISHED)) {
1270 test(&requests[i], pstat,&flag);
1273 if(status != MPI_STATUSES_IGNORE) {
1274 status[count] = *pstat;
1276 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
1277 requests[i]=MPI_REQUEST_NULL;
1285 MPI_Request Request::f2c(int id)
1287 if(id==MPI_FORTRAN_REQUEST_NULL)
1288 return MPI_REQUEST_NULL;
1289 return static_cast<MPI_Request>(F2C::lookup()->at(id));
1292 void Request::free_f(int id)
1294 if (id != MPI_FORTRAN_REQUEST_NULL) {
1295 F2C::lookup()->erase(id);
1299 int Request::get_status(const Request* req, int* flag, MPI_Status* status)
1303 if(req != MPI_REQUEST_NULL && req->action_ != nullptr) {
1304 req->iprobe(req->comm_->group()->rank(req->src_), req->tag_, req->comm_, flag, status);
1308 if (req != MPI_REQUEST_NULL && (req->flags_ & MPI_REQ_GENERALIZED) && not(req->flags_ & MPI_REQ_COMPLETE)) {
1314 if(req != MPI_REQUEST_NULL &&
1315 status != MPI_STATUS_IGNORE) {
1316 aid_t src = req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_;
1317 status->MPI_SOURCE = req->comm_->group()->rank(src);
1318 status->MPI_TAG = req->tag_ == MPI_ANY_TAG ? req->real_tag_ : req->tag_;
1319 status->MPI_ERROR = req->truncated_ ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
1320 status->count = req->real_size_;
1325 int Request::grequest_start(MPI_Grequest_query_function* query_fn, MPI_Grequest_free_function* free_fn,
1326 MPI_Grequest_cancel_function* cancel_fn, void* extra_state, MPI_Request* request)
1328 *request = new Request();
1329 (*request)->flags_ |= MPI_REQ_GENERALIZED;
1330 (*request)->flags_ |= MPI_REQ_PERSISTENT;
1331 (*request)->refcount_ = 1;
1332 ((*request)->generalized_funcs) = std::make_unique<smpi_mpi_generalized_request_funcs_t>();
1333 ((*request)->generalized_funcs)->query_fn=query_fn;
1334 ((*request)->generalized_funcs)->free_fn=free_fn;
1335 ((*request)->generalized_funcs)->cancel_fn=cancel_fn;
1336 ((*request)->generalized_funcs)->extra_state=extra_state;
1337 ((*request)->generalized_funcs)->cond = simgrid::s4u::ConditionVariable::create();
1338 ((*request)->generalized_funcs)->mutex = simgrid::s4u::Mutex::create();
1342 int Request::grequest_complete(MPI_Request request)
1344 if ((not(request->flags_ & MPI_REQ_GENERALIZED)) || request->generalized_funcs->mutex == nullptr)
1345 return MPI_ERR_REQUEST;
1346 request->generalized_funcs->mutex->lock();
1347 request->flags_ |= MPI_REQ_COMPLETE; // in case wait would be called after complete
1348 request->generalized_funcs->cond->notify_one();
1349 request->generalized_funcs->mutex->unlock();
1353 void Request::start_nbc_requests(std::vector<MPI_Request> reqs){
1354 if (not reqs.empty()) {
1355 nbc_requests_ = reqs;
1356 Request::startall(reqs.size(), reqs.data());
1360 std::vector<MPI_Request> Request::get_nbc_requests() const
1362 return nbc_requests_;