1 /* Copyright (c) 2007-2023. 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"
9 #include "simgrid/Exception.hpp"
10 #include "simgrid/s4u/ConditionVariable.hpp"
11 #include "simgrid/s4u/Exec.hpp"
12 #include "simgrid/s4u/Mutex.hpp"
13 #include "smpi_comm.hpp"
14 #include "smpi_datatype.hpp"
15 #include "smpi_host.hpp"
16 #include "smpi_op.hpp"
17 #include "src/kernel/EngineImpl.hpp"
18 #include "src/kernel/activity/CommImpl.hpp"
19 #include "src/kernel/actor/ActorImpl.hpp"
20 #include "src/kernel/actor/SimcallObserver.hpp"
21 #include "src/mc/mc.h"
22 #include "src/mc/mc_replay.hpp"
23 #include "src/smpi/include/smpi_actor.hpp"
27 #include <mutex> // std::scoped_lock and std::unique_lock
29 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_request, smpi, "Logging specific to SMPI (request)");
31 static simgrid::config::Flag<double> smpi_iprobe_sleep(
32 "smpi/iprobe", "Minimum time to inject inside a call to MPI_Iprobe", 1e-4);
33 static simgrid::config::Flag<double> smpi_test_sleep(
34 "smpi/test", "Minimum time to inject inside a call to MPI_Test", 1e-4);
36 extern std::function<void(simgrid::kernel::activity::CommImpl*, void*, size_t)> smpi_comm_copy_data_callback;
38 namespace simgrid::smpi {
40 Request::Request(const void* buf, int count, MPI_Datatype datatype, aid_t src, aid_t dst, int tag, MPI_Comm comm,
41 unsigned flags, MPI_Op op)
42 : buf_(const_cast<void*>(buf))
45 , size_(datatype->size() * count)
55 if(op != MPI_REPLACE && op != MPI_OP_NULL)
59 detached_sender_ = nullptr;
61 // get src_host if it's available (src is valid)
62 if (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)
80 void Request::unref(MPI_Request* request)
82 xbt_assert(*request != MPI_REQUEST_NULL, "freeing an already free request");
84 (*request)->refcount_--;
85 if ((*request)->refcount_ < 0) {
86 (*request)->print_request("wrong refcount");
87 xbt_die("Whoops, wrong refcount");
89 if ((*request)->refcount_ == 0) {
90 if ((*request)->flags_ & MPI_REQ_GENERALIZED) {
91 ((*request)->generalized_funcs)->free_fn(((*request)->generalized_funcs)->extra_state);
93 Comm::unref((*request)->comm_);
94 Datatype::unref((*request)->type_);
96 if ((*request)->op_ != MPI_REPLACE && (*request)->op_ != MPI_OP_NULL)
97 Op::unref(&(*request)->op_);
99 (*request)->print_request("Destroying");
100 F2C::free_f((*request)->f2c_id());
102 *request = MPI_REQUEST_NULL;
104 (*request)->print_request("Decrementing");
108 bool Request::match_types(MPI_Datatype stype, MPI_Datatype rtype){
110 if ((stype == rtype) ||
111 //byte and packed always match with anything
112 (stype == MPI_PACKED || rtype == MPI_PACKED || stype == MPI_BYTE || rtype == MPI_BYTE) ||
113 //complex datatypes - we don't properly match these yet, as it would mean checking each subtype recursively.
114 (stype->flags() & DT_FLAG_DERIVED || rtype->flags() & DT_FLAG_DERIVED) ||
115 //duplicated datatypes, check if underlying is ok
116 (stype->duplicated_datatype()!=MPI_DATATYPE_NULL && match_types(stype->duplicated_datatype(), rtype)) ||
117 (rtype->duplicated_datatype()!=MPI_DATATYPE_NULL && match_types(stype, rtype->duplicated_datatype())))
120 XBT_WARN("Mismatched datatypes : sending %s and receiving %s", stype->name().c_str(), rtype->name().c_str());
125 bool Request::match_common(MPI_Request req, MPI_Request sender, MPI_Request receiver)
127 xbt_assert(sender, "Cannot match against null sender");
128 xbt_assert(receiver, "Cannot match against null receiver");
129 XBT_DEBUG("Trying to match %s of sender src %ld against %ld, tag %d against %d, id %d against %d",
130 (req == receiver ? "send" : "recv"), sender->src_, receiver->src_, sender->tag_, receiver->tag_,
131 sender->comm_->id(), receiver->comm_->id());
133 if ((receiver->comm_->id() == MPI_UNDEFINED || sender->comm_->id() == MPI_UNDEFINED ||
134 receiver->comm_->id() == sender->comm_->id()) &&
135 ((receiver->src_ == MPI_ANY_SOURCE && (receiver->comm_->group()->rank(sender->src_) != MPI_UNDEFINED)) ||
136 receiver->src_ == sender->src_) &&
137 ((receiver->tag_ == MPI_ANY_TAG && sender->tag_ >= 0) || receiver->tag_ == sender->tag_)) {
138 // we match, we can transfer some values
139 if (receiver->src_ == MPI_ANY_SOURCE) {
140 receiver->real_src_ = sender->src_;
141 receiver->src_host_ = sender->src_host_;
143 if (receiver->tag_ == MPI_ANY_TAG)
144 receiver->real_tag_ = sender->tag_;
145 if ((receiver->flags_ & MPI_REQ_PROBE) == 0 && receiver->real_size_ < sender->real_size_) {
146 XBT_DEBUG("Truncating message - should not happen: receiver size : %zu < sender size : %zu", receiver->real_size_,
148 receiver->truncated_ = true;
150 //0-sized datatypes/counts should not interfere and match
151 if (sender->real_size_ != 0 && receiver->real_size_ != 0 && not match_types(sender->type_, receiver->type_))
152 receiver->unmatched_types_ = true;
153 if (sender->detached_)
154 receiver->detached_sender_ = sender; // tie the sender to the receiver, as it is detached and has to be freed in
156 req->flags_ |= MPI_REQ_MATCHED; // mark as impossible to cancel anymore
157 XBT_DEBUG("match succeeded");
163 void Request::init_buffer(int count){
164 // FIXME Handle the case of a partial shared malloc.
165 // This part handles the problem of non-contiguous memory (for the unserialization at the reception)
166 if (not smpi_process()->replaying() &&
167 ((((flags_ & MPI_REQ_RECV) != 0) && ((flags_ & MPI_REQ_ACCUMULATE) != 0)) || (type_->flags() & DT_FLAG_DERIVED))) {
168 // This part handles the problem of non-contiguous memory
173 buf_ = xbt_malloc(count*type_->size());
174 if ((type_->flags() & DT_FLAG_DERIVED) && ((flags_ & MPI_REQ_SEND) != 0)) {
175 type_->serialize(old_buf_, buf_, count);
181 bool Request::match_recv(void* a, void* b, simgrid::kernel::activity::CommImpl*)
183 auto* ref = static_cast<MPI_Request>(a);
184 auto* req = static_cast<MPI_Request>(b);
185 bool match = match_common(req, req, ref);
186 if (not match || ref->comm_ == MPI_COMM_UNINITIALIZED || ref->comm_->is_smp_comm())
188 auto it = std::find(req->message_id_.begin(), req->message_id_.end(), ref->comm_->get_received_messages_count(ref->comm_->group()->rank(req->src_),
189 ref->comm_->group()->rank(req->dst_), req->tag_));
190 if (it != req->message_id_.end()) {
191 if (((ref->flags_ & MPI_REQ_PROBE) == 0) && ((req->flags_ & MPI_REQ_PROBE) == 0)) {
192 req->message_id_.erase(it);
193 XBT_DEBUG("increasing count in comm %p, which was %u from pid %ld, to pid %ld with tag %d", ref->comm_,
194 ref->comm_->get_received_messages_count(ref->comm_->group()->rank(req->src_),
195 ref->comm_->group()->rank(req->dst_), req->tag_),
196 req->src_, req->dst_, req->tag_);
197 ref->comm_->increment_received_messages_count(ref->comm_->group()->rank(req->src_),
198 ref->comm_->group()->rank(req->dst_), req->tag_);
199 if (ref->real_size_ > req->real_size_) {
200 ref->real_size_ = req->real_size_;
205 req->flags_ &= ~MPI_REQ_MATCHED;
206 ref->detached_sender_ = nullptr;
207 XBT_DEBUG("Refusing to match message, as its ID is not the one I expect. in comm %p, %u, "
208 "from pid %ld to pid %ld, with tag %d",
210 ref->comm_->get_received_messages_count(ref->comm_->group()->rank(req->src_),
211 ref->comm_->group()->rank(req->dst_), req->tag_),
212 req->src_, req->dst_, req->tag_);
217 bool Request::match_send(void* a, void* b, simgrid::kernel::activity::CommImpl*)
219 auto* ref = static_cast<MPI_Request>(a);
220 auto* req = static_cast<MPI_Request>(b);
221 return match_common(req, ref, req);
224 void Request::print_request(const char* message) const
226 XBT_VERB("%s request %p [buf = %p, size = %zu, src = %ld, dst = %ld, tag = %d, flags = %x]", message, this, buf_,
227 size_, src_, dst_, tag_, flags_);
230 /* factories, to hide the internal flags from the caller */
231 MPI_Request Request::bsend_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
233 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
234 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
235 MPI_REQ_PERSISTENT | MPI_REQ_SEND | MPI_REQ_PREPARED | MPI_REQ_BSEND);
238 MPI_Request Request::send_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
240 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
241 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
242 MPI_REQ_PERSISTENT | MPI_REQ_SEND | MPI_REQ_PREPARED);
245 MPI_Request Request::ssend_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
247 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
248 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
249 MPI_REQ_PERSISTENT | MPI_REQ_SSEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
252 MPI_Request Request::isend_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
254 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
255 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
256 MPI_REQ_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
259 MPI_Request Request::rma_send_init(const void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
264 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src),
265 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
266 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
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 |
277 MPI_Request Request::recv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
279 aid_t source = MPI_PROC_NULL;
280 if (src == MPI_ANY_SOURCE)
281 source = MPI_ANY_SOURCE;
282 else if (src != MPI_PROC_NULL)
283 source = comm->group()->actor(src);
284 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
286 simgrid::s4u::this_actor::get_pid(), tag, comm,
287 MPI_REQ_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
290 MPI_Request Request::rma_recv_init(void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
293 aid_t source = MPI_PROC_NULL;
294 if (src == MPI_ANY_SOURCE)
295 source = MPI_ANY_SOURCE;
296 else if (src != MPI_PROC_NULL)
297 source = comm->group()->actor(src);
300 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, source,
301 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
302 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
305 new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, source,
306 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
307 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED | MPI_REQ_ACCUMULATE, op);
312 MPI_Request Request::irecv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
314 aid_t source = MPI_PROC_NULL;
315 if (src == MPI_ANY_SOURCE)
316 source = MPI_ANY_SOURCE;
317 else if (src != MPI_PROC_NULL)
318 source = comm->group()->actor(src);
319 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
320 source, simgrid::s4u::this_actor::get_pid(), tag, comm,
321 MPI_REQ_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
324 MPI_Request Request::ibsend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
326 auto* request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
327 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
328 MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_BSEND);
329 if(dst != MPI_PROC_NULL)
334 MPI_Request Request::isend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
336 auto* request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
337 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
338 MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND);
339 if(dst != MPI_PROC_NULL)
344 MPI_Request Request::issend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
346 auto* request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
347 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
348 MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SSEND | MPI_REQ_SEND);
349 if(dst != MPI_PROC_NULL)
354 MPI_Request Request::irecv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
356 aid_t source = MPI_PROC_NULL;
357 if (src == MPI_ANY_SOURCE)
358 source = MPI_ANY_SOURCE;
359 else if (src != MPI_PROC_NULL)
360 source = comm->group()->actor(src);
361 auto* request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, source,
362 simgrid::s4u::this_actor::get_pid(), tag, comm, MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV);
363 if(src != MPI_PROC_NULL)
368 int Request::recv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status * status)
370 MPI_Request request = irecv(buf, count, datatype, src, tag, comm);
371 int retval = wait(&request,status);
375 void Request::bsend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
377 auto* request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
378 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
379 MPI_REQ_NON_PERSISTENT | MPI_REQ_SEND | MPI_REQ_BSEND);
381 if(dst != MPI_PROC_NULL)
383 wait(&request, MPI_STATUS_IGNORE);
386 void Request::send(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
388 auto* request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
389 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
390 MPI_REQ_NON_PERSISTENT | MPI_REQ_SEND);
391 if(dst != MPI_PROC_NULL)
393 wait(&request, MPI_STATUS_IGNORE);
396 void Request::ssend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
398 auto* request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
399 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
400 MPI_REQ_NON_PERSISTENT | MPI_REQ_SSEND | MPI_REQ_SEND);
402 if(dst != MPI_PROC_NULL)
404 wait(&request,MPI_STATUS_IGNORE);
407 void Request::sendrecv(const void *sendbuf, int sendcount, MPI_Datatype sendtype,int dst, int sendtag,
408 void *recvbuf, int recvcount, MPI_Datatype recvtype, int src, int recvtag,
409 MPI_Comm comm, MPI_Status * status)
411 aid_t source = MPI_PROC_NULL;
412 if (src == MPI_ANY_SOURCE)
413 source = MPI_ANY_SOURCE;
414 else if (src != MPI_PROC_NULL)
415 source = comm->group()->actor(src);
416 aid_t destination = dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL;
418 std::array<MPI_Request, 2> requests;
419 std::array<MPI_Status, 2> stats;
420 if (aid_t myid = simgrid::s4u::this_actor::get_pid(); (destination == myid) && (source == myid)) {
421 Datatype::copy(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
422 if (status != MPI_STATUS_IGNORE) {
423 status->MPI_SOURCE = source;
424 status->MPI_TAG = recvtag;
425 status->MPI_ERROR = MPI_SUCCESS;
426 status->count = sendcount * sendtype->size();
430 requests[0] = isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
431 requests[1] = irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
432 startall(2, requests.data());
433 waitall(2, requests.data(), stats.data());
436 if(status != MPI_STATUS_IGNORE) {
437 // Copy receive status
442 void Request::isendrecv(const void *sendbuf, int sendcount, MPI_Datatype sendtype,int dst, int sendtag,
443 void *recvbuf, int recvcount, MPI_Datatype recvtype, int src, int recvtag,
444 MPI_Comm comm, MPI_Request* request)
446 aid_t source = MPI_PROC_NULL;
447 if (src == MPI_ANY_SOURCE)
448 source = MPI_ANY_SOURCE;
449 else if (src != MPI_PROC_NULL)
450 source = comm->group()->actor(src);
451 aid_t destination = dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL;
453 (*request) = new Request( nullptr, 0, MPI_BYTE,
454 src,dst, sendtag, comm, MPI_REQ_PERSISTENT|MPI_REQ_NBC);
455 std::vector<MPI_Request> requests;
456 if (aid_t myid = simgrid::s4u::this_actor::get_pid(); (destination == myid) && (source == myid)) {
457 Datatype::copy(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
460 requests.push_back(isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm));
461 requests.push_back(irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm));
462 (*request)->start_nbc_requests(requests);
465 void Request::start()
467 s4u::Mailbox* mailbox;
469 xbt_assert(action_ == nullptr, "Cannot (re-)start unfinished communication");
470 //reinitialize temporary buffer for persistent requests
471 if(real_size_ > 0 && flags_ & MPI_REQ_FINISHED){
473 init_buffer(real_size_/type_->size());
475 flags_ &= ~MPI_REQ_PREPARED;
476 flags_ &= ~MPI_REQ_FINISHED;
479 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
481 if ((flags_ & MPI_REQ_RECV) != 0) {
482 this->print_request("New recv");
484 simgrid::smpi::ActorExt* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(dst_));
486 std::unique_lock<s4u::Mutex> mut_lock;
487 if (smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)
488 mut_lock = std::unique_lock(*process->mailboxes_mutex());
490 bool is_probe = ((flags_ & MPI_REQ_PROBE) != 0);
491 flags_ |= MPI_REQ_PROBE;
493 if (smpi_cfg_async_small_thresh() == 0 && (flags_ & MPI_REQ_RMA) == 0) {
494 mailbox = process->mailbox();
495 } else if (((flags_ & MPI_REQ_RMA) != 0) || static_cast<int>(size_) < smpi_cfg_async_small_thresh()) {
496 //We have to check both mailboxes (because SSEND messages are sent to the large mbox).
497 //begin with the more appropriate one : the small one.
498 mailbox = process->mailbox_small();
499 XBT_DEBUG("Is there a corresponding send already posted in the small mailbox %s (in case of SSEND)?",
500 mailbox->get_cname());
501 simgrid::kernel::activity::ActivityImplPtr action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
503 if (action == nullptr) {
504 mailbox = process->mailbox();
505 XBT_DEBUG("No, nothing in the small mailbox test the other one : %s", mailbox->get_cname());
506 action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
507 if (action == nullptr) {
508 XBT_DEBUG("Still nothing, switch back to the small mailbox : %s", mailbox->get_cname());
509 mailbox = process->mailbox_small();
512 XBT_DEBUG("yes there was something for us in the small mailbox");
515 mailbox = process->mailbox_small();
516 XBT_DEBUG("Is there a corresponding send already posted the small mailbox?");
517 simgrid::kernel::activity::ActivityImplPtr action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
519 if (action == nullptr) {
520 XBT_DEBUG("No, nothing in the permanent receive mailbox");
521 mailbox = process->mailbox();
523 XBT_DEBUG("yes there was something for us in the small mailbox");
527 flags_ &= ~MPI_REQ_PROBE;
528 kernel::actor::CommIrecvSimcall observer{process->get_actor()->get_impl(),
530 static_cast<unsigned char*>(buf_),
533 process->replaying() ? &smpi_comm_null_copy_buffer_callback
534 : smpi_comm_copy_data_callback,
537 process->call_location()->get_call_location()};
538 observer.set_tag(tag_);
540 action_ = kernel::actor::simcall_answered([&observer] { return kernel::activity::CommImpl::irecv(&observer); },
543 XBT_DEBUG("recv simcall posted");
544 } else { /* the RECV flag was not set, so this is a send */
545 simgrid::smpi::ActorExt* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(dst_));
546 xbt_assert(process, "Actor pid=%ld is gone??", dst_);
547 if (TRACE_smpi_view_internals())
548 TRACE_smpi_send(src_, src_, dst_, tag_, size_);
549 this->print_request("New send");
551 message_id_.push_back(comm_->get_sent_messages_count(comm_->group()->rank(src_), comm_->group()->rank(dst_), tag_));
552 comm_->increment_sent_messages_count(comm_->group()->rank(src_), comm_->group()->rank(dst_), tag_);
555 if ((flags_ & MPI_REQ_SSEND) == 0 && ((flags_ & MPI_REQ_RMA) != 0 || (flags_ & MPI_REQ_BSEND) != 0 ||
556 static_cast<int>(size_) < smpi_cfg_detached_send_thresh())) {
558 XBT_DEBUG("Send request %p is detached", this);
560 if (not(type_->flags() & DT_FLAG_DERIVED)) {
562 if (not process->replaying() && oldbuf != nullptr && size_ != 0) {
563 if (smpi_switch_data_segment(simgrid::s4u::Actor::by_pid(src_), buf_))
564 XBT_DEBUG("Privatization : We are sending from a zone inside global memory. Switch data segment ");
566 //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
567 //so actually ... don't use manually attached buffer space.
568 buf = xbt_malloc(size_);
569 memcpy(buf,oldbuf,size_);
570 XBT_DEBUG("buf %p copied into %p",oldbuf,buf);
575 //if we are giving back the control to the user without waiting for completion, we have to inject timings
576 double sleeptime = 0.0;
577 if (detached_ || ((flags_ & (MPI_REQ_ISEND | MPI_REQ_SSEND)) != 0)) { // issend should be treated as isend
578 // isend and send timings may be different
580 ((flags_ & MPI_REQ_ISEND) != 0)
581 ? simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->oisend(
582 size_, simgrid::s4u::Actor::by_pid(src_)->get_host(), simgrid::s4u::Actor::by_pid(dst_)->get_host())
583 : simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->osend(
584 size_, simgrid::s4u::Actor::by_pid(src_)->get_host(),
585 simgrid::s4u::Actor::by_pid(dst_)->get_host());
589 simgrid::s4u::this_actor::sleep_for(sleeptime);
590 XBT_DEBUG("sending size of %zu : sleep %f ", size_, sleeptime);
593 std::unique_lock<s4u::Mutex> mut_lock;
594 if (smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)
595 mut_lock = std::unique_lock(*process->mailboxes_mutex());
597 if (not(smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)) {
598 mailbox = process->mailbox();
599 } else if (((flags_ & MPI_REQ_RMA) != 0) || static_cast<int>(size_) < smpi_cfg_async_small_thresh()) { // eager mode
600 bool is_probe = ((flags_ & MPI_REQ_PROBE) != 0);
601 flags_ |= MPI_REQ_PROBE;
603 mailbox = process->mailbox();
604 XBT_DEBUG("Is there a corresponding recv already posted in the large mailbox %s?", mailbox->get_cname());
605 if (not mailbox->iprobe(1, &match_send, static_cast<void*>(this))) {
606 if ((flags_ & MPI_REQ_SSEND) == 0) {
607 mailbox = process->mailbox_small();
608 XBT_DEBUG("No, nothing in the large mailbox, message is to be sent on the small one %s",
609 mailbox->get_cname());
611 mailbox = process->mailbox_small();
612 XBT_DEBUG("SSEND : Is there a corresponding recv already posted in the small mailbox %s?",
613 mailbox->get_cname());
614 if (not mailbox->iprobe(1, &match_send, static_cast<void*>(this))) {
615 XBT_DEBUG("No, we are first, send to large mailbox");
616 mailbox = process->mailbox();
620 XBT_DEBUG("Yes there was something for us in the large mailbox");
623 flags_ &= ~MPI_REQ_PROBE;
625 mailbox = process->mailbox();
626 XBT_DEBUG("Send request %p is in the large mailbox %s (buf: %p)", this, mailbox->get_cname(), buf_);
629 size_t payload_size_ = size_ + 16;//MPI enveloppe size (tag+dest+communicator)
630 kernel::actor::CommIsendSimcall observer{
631 simgrid::kernel::EngineImpl::get_instance()->get_actor_by_pid(src_), mailbox->get_impl(),
632 static_cast<double>(payload_size_), -1, static_cast<unsigned char*>(buf), real_size_, &match_send,
633 &xbt_free_f, // how to free the userdata if a detached send fails
634 process->replaying() ? &smpi_comm_null_copy_buffer_callback : smpi_comm_copy_data_callback, this,
635 // detach if msg size < eager/rdv switch limit
636 detached_, process->call_location()->get_call_location()};
637 observer.set_tag(tag_);
638 action_ = kernel::actor::simcall_answered([&observer] { return kernel::activity::CommImpl::isend(&observer); },
640 XBT_DEBUG("send simcall posted");
642 /* FIXME: detached sends are not traceable (action_ == nullptr) */
643 if (action_ != nullptr) {
644 boost::static_pointer_cast<kernel::activity::CommImpl>(action_)->set_tracing_category(
645 smpi_process()->get_tracing_category());
650 void Request::startall(int count, MPI_Request * requests)
652 if(requests== nullptr)
655 for(int i = 0; i < count; i++) {
656 if(requests[i]->src_ != MPI_PROC_NULL && requests[i]->dst_ != MPI_PROC_NULL)
657 requests[i]->start();
661 void Request::cancel()
663 this->flags_ |= MPI_REQ_CANCELLED;
664 if (this->action_ != nullptr)
665 (boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(this->action_))->cancel();
668 int Request::test(MPI_Request * request, MPI_Status * status, int* flag) {
669 // assume that *request is not MPI_REQUEST_NULL (filtered in PMPI_Test or testall before)
670 // to avoid deadlocks if used as a break condition, such as
671 // while (MPI_Test(request, flag, status) && flag) dostuff...
672 // because the time will not normally advance when only calls to MPI_Test are made -> deadlock
673 // multiplier to the sleeptime, to increase speed of execution, each failed test will increase it
674 xbt_assert(*request != MPI_REQUEST_NULL);
676 static int nsleeps = 1;
677 int ret = MPI_SUCCESS;
679 if(smpi_test_sleep > 0)
680 simgrid::s4u::this_actor::sleep_for(nsleeps * smpi_test_sleep);
682 Status::empty(status);
685 if ((*request)->flags_ & MPI_REQ_NBC){
686 *flag = finish_nbc_requests(request, 1);
689 if (((*request)->flags_ & (MPI_REQ_PREPARED | MPI_REQ_FINISHED)) == 0) {
690 if ((*request)->action_ != nullptr && ((*request)->flags_ & MPI_REQ_CANCELLED) == 0){
692 kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
693 simgrid::smpi::ActorExt* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(issuer->get_pid()));
694 kernel::actor::ActivityTestSimcall observer{issuer, (*request)->action_.get(),
695 process->call_location()->get_call_location()};
696 *flag = kernel::actor::simcall_answered(
697 [&observer] { return observer.get_activity()->test(observer.get_issuer()); }, &observer);
698 } catch (const Exception&) {
703 if (((*request)->flags_ & MPI_REQ_GENERALIZED) && not((*request)->flags_ & MPI_REQ_COMPLETE))
706 finish_wait(request, status); // may invalidate *request
707 if (*request != MPI_REQUEST_NULL && ((*request)->flags_ & MPI_REQ_GENERALIZED)){
708 MPI_Status tmp_status;
709 MPI_Status* mystatus;
710 if (status == MPI_STATUS_IGNORE) {
711 mystatus = &tmp_status;
712 Status::empty(mystatus);
716 ret = ((*request)->generalized_funcs)->query_fn(((*request)->generalized_funcs)->extra_state, mystatus);
718 nsleeps=1;//reset the number of sleeps we will do next time
719 if (*request != MPI_REQUEST_NULL && ((*request)->flags_ & MPI_REQ_PERSISTENT) == 0)
720 *request = MPI_REQUEST_NULL;
721 } else if (smpi_cfg_grow_injected_times()) {
728 int Request::testsome(int incount, MPI_Request requests[], int *count, int *indices, MPI_Status status[])
734 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
737 for (int i = 0; i < incount; i++) {
738 if (requests[i] != MPI_REQUEST_NULL && not (requests[i]->flags_ & MPI_REQ_FINISHED)) {
739 if (test(&requests[i], pstat, &flag) != MPI_SUCCESS)
743 if (status != MPI_STATUSES_IGNORE)
744 status[*count] = *pstat;
746 if ((requests[i] != MPI_REQUEST_NULL) && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
747 requests[i] = MPI_REQUEST_NULL;
753 if(count_dead==incount)*count=MPI_UNDEFINED;
755 return MPI_ERR_IN_STATUS;
760 int Request::testany(int count, MPI_Request requests[], int *index, int* flag, MPI_Status * status)
762 std::vector<simgrid::kernel::activity::ActivityImpl*> comms;
763 comms.reserve(count);
766 int ret = MPI_SUCCESS;
767 *index = MPI_UNDEFINED;
769 std::vector<int> map; /** Maps all matching comms back to their location in requests **/
770 for (int i = 0; i < count; i++) {
771 if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->action_ && not(requests[i]->flags_ & MPI_REQ_PREPARED)) {
772 comms.push_back(requests[i]->action_.get());
776 if (not map.empty()) {
777 //multiplier to the sleeptime, to increase speed of execution, each failed testany will increase it
778 static int nsleeps = 1;
779 if(smpi_test_sleep > 0)
780 simgrid::s4u::this_actor::sleep_for(nsleeps * smpi_test_sleep);
783 kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
784 simgrid::smpi::ActorExt* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(issuer->get_pid()));
785 kernel::actor::ActivityTestanySimcall observer{issuer, comms, process->call_location()->get_call_location()};
786 i = kernel::actor::simcall_answered(
788 return kernel::activity::ActivityImpl::test_any(observer.get_issuer(), observer.get_activities());
791 } catch (const Exception&) {
792 XBT_DEBUG("Exception in testany");
796 if (i != -1) { // -1 is not MPI_UNDEFINED but a SIMIX return code. (nothing matches)
798 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags_ & MPI_REQ_GENERALIZED) &&
799 not(requests[*index]->flags_ & MPI_REQ_COMPLETE)) {
802 finish_wait(&requests[*index],status);
803 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags_ & MPI_REQ_GENERALIZED)){
804 MPI_Status tmp_status;
805 MPI_Status* mystatus;
806 if (status == MPI_STATUS_IGNORE) {
807 mystatus = &tmp_status;
808 Status::empty(mystatus);
812 ret=(requests[*index]->generalized_funcs)->query_fn((requests[*index]->generalized_funcs)->extra_state, mystatus);
815 if (requests[*index] != MPI_REQUEST_NULL && requests[*index]->flags_ & MPI_REQ_NBC){
816 *flag = finish_nbc_requests(&requests[*index] , 1);
819 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags_ & MPI_REQ_NON_PERSISTENT))
820 requests[*index] = MPI_REQUEST_NULL;
821 XBT_DEBUG("Testany - returning with index %d", *index);
829 XBT_DEBUG("Testany on inactive handles, returning flag=1 but empty status");
830 //all requests are null or inactive, return true
832 *index = MPI_UNDEFINED;
833 Status::empty(status);
839 int Request::testall(int count, MPI_Request requests[], int* outflag, MPI_Status status[])
842 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
846 for(int i=0; i<count; i++){
847 if (requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & MPI_REQ_PREPARED)) {
848 int ret = test(&requests[i], pstat, &flag);
854 if (ret != MPI_SUCCESS)
857 Status::empty(pstat);
859 if(status != MPI_STATUSES_IGNORE) {
864 return MPI_ERR_IN_STATUS;
869 void Request::probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
871 //FIXME find another way to avoid busy waiting ?
872 // the issue here is that we have to wait on a nonexistent comm
874 iprobe(source, tag, comm, &flag, status);
875 XBT_DEBUG("Busy Waiting on probing : %d", flag);
879 void Request::iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
880 // to avoid deadlock, we have to sleep some time here, or the timer won't advance and we will only do iprobe simcalls
881 // especially when used as a break condition, such as while (MPI_Iprobe(...)) dostuff...
882 // nsleeps is a multiplier to the sleeptime, to increase speed of execution, each failed iprobe will increase it
883 // This can speed up the execution of certain applications by an order of magnitude, such as HPL
884 static int nsleeps = 1;
885 double speed = s4u::this_actor::get_host()->get_speed();
886 double maxrate = smpi_cfg_iprobe_cpu_usage();
888 new Request(nullptr, 0, MPI_CHAR, source == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : comm->group()->actor(source),
889 simgrid::s4u::this_actor::get_pid(), tag, comm, MPI_REQ_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PROBE);
890 if (smpi_iprobe_sleep > 0) {
891 /** Compute the number of flops we will sleep **/
892 s4u::this_actor::exec_init(/*nsleeps: See comment above */ nsleeps *
893 /*(seconds * flop/s -> total flops)*/ smpi_iprobe_sleep * speed * maxrate)
895 /* Not the entire CPU can be used when iprobing: This is important for
896 * the energy consumption caused by polling with iprobes.
897 * Note also that the number of flops that was
898 * computed above contains a maxrate factor and is hence reduced (maxrate < 1)
900 ->set_bound(maxrate * speed)
904 // behave like a receive, but don't do it
905 s4u::Mailbox* mailbox;
907 request->print_request("New iprobe");
908 // We have to test both mailboxes as we don't know if we will receive one or another
909 if (smpi_cfg_async_small_thresh() > 0) {
910 mailbox = smpi_process()->mailbox_small();
911 XBT_DEBUG("Trying to probe the perm recv mailbox");
912 request->action_ = mailbox->iprobe(0, &match_recv, static_cast<void*>(request));
915 if (request->action_ == nullptr){
916 mailbox = smpi_process()->mailbox();
917 XBT_DEBUG("trying to probe the other mailbox");
918 request->action_ = mailbox->iprobe(0, &match_recv, static_cast<void*>(request));
921 if (request->action_ != nullptr){
922 kernel::activity::CommImplPtr sync_comm = boost::static_pointer_cast<kernel::activity::CommImpl>(request->action_);
923 const Request* req = static_cast<MPI_Request>(sync_comm->src_data_);
925 if (status != MPI_STATUS_IGNORE && (req->flags_ & MPI_REQ_PREPARED) == 0) {
926 status->MPI_SOURCE = comm->group()->rank(req->src_);
927 status->MPI_TAG = req->tag_;
928 status->MPI_ERROR = MPI_SUCCESS;
929 status->count = req->real_size_;
931 nsleeps = 1;//reset the number of sleeps we will do next time
935 if (smpi_cfg_grow_injected_times())
939 xbt_assert(request == MPI_REQUEST_NULL);
942 int Request::finish_nbc_requests(MPI_Request* request, int test){
946 ret = waitall((*request)->nbc_requests_.size(), (*request)->nbc_requests_.data(), MPI_STATUSES_IGNORE);
948 ret = testall((*request)->nbc_requests_.size(), (*request)->nbc_requests_.data(), &flag, MPI_STATUSES_IGNORE);
951 xbt_die("Failure when waiting on non blocking collective sub-requests");
953 XBT_DEBUG("Finishing non blocking collective request with %zu sub-requests", (*request)->nbc_requests_.size());
954 for(auto& req: (*request)->nbc_requests_){
955 if((*request)->buf_!=nullptr && req!=MPI_REQUEST_NULL){//reduce case
956 void * buf=req->buf_;
957 if((*request)->type_->flags() & DT_FLAG_DERIVED)
959 if(req->flags_ & MPI_REQ_RECV ){
960 if((*request)->op_!=MPI_OP_NULL){
961 int count=(*request)->size_/ (*request)->type_->size();
962 (*request)->op_->apply(buf, (*request)->buf_, &count, (*request)->type_);
964 smpi_free_tmp_buffer(static_cast<unsigned char*>(buf));
967 if(req!=MPI_REQUEST_NULL)
968 Request::unref(&req);
970 (*request)->nbc_requests_.clear();
975 void Request::finish_wait(MPI_Request* request, MPI_Status * status)
977 MPI_Request req = *request;
978 Status::empty(status);
979 if((req->flags_ & MPI_REQ_CANCELLED) != 0 && (req->flags_ & MPI_REQ_MATCHED) == 0) {
980 if (status!=MPI_STATUS_IGNORE)
982 if(req->detached_sender_ != nullptr)
983 unref(&(req->detached_sender_));
988 if ((req->flags_ & (MPI_REQ_PREPARED | MPI_REQ_GENERALIZED | MPI_REQ_FINISHED)) == 0) {
989 if (status != MPI_STATUS_IGNORE) {
990 if (req->src_== MPI_PROC_NULL || req->dst_== MPI_PROC_NULL){
991 Status::empty(status);
992 status->MPI_SOURCE = MPI_PROC_NULL;
994 aid_t src = req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_;
995 status->MPI_SOURCE = req->comm_->group()->rank(src);
996 status->MPI_TAG = req->tag_ == MPI_ANY_TAG ? req->real_tag_ : req->tag_;
997 status->MPI_ERROR = req->truncated_ ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
999 // this handles the case were size in receive differs from size in send
1000 status->count = req->real_size_;
1002 //detached send will be finished at the other end
1003 if (not(req->detached_ && ((req->flags_ & MPI_REQ_SEND) != 0))) {
1004 req->print_request("Finishing");
1005 MPI_Datatype datatype = req->type_;
1007 // FIXME Handle the case of a partial shared malloc.
1008 if (not smpi_process()->replaying() &&
1009 (((req->flags_ & MPI_REQ_ACCUMULATE) != 0) || (datatype->flags() & DT_FLAG_DERIVED))) {
1010 if (smpi_switch_data_segment(simgrid::s4u::Actor::self(), req->old_buf_))
1011 XBT_VERB("Privatization : We are unserializing to a zone in global memory Switch data segment ");
1013 if(datatype->flags() & DT_FLAG_DERIVED){
1014 // This part handles the problem of non-contiguous memory the unserialization at the reception
1015 if ((req->flags_ & MPI_REQ_RECV) && datatype->size() != 0)
1016 datatype->unserialize(req->buf_, req->old_buf_, req->real_size_/datatype->size() , req->op_);
1017 xbt_free(req->buf_);
1019 } else if (req->flags_ & MPI_REQ_RECV) { // apply op on contiguous buffer for accumulate
1020 if (datatype->size() != 0) {
1021 int n = req->real_size_ / datatype->size();
1022 req->op_->apply(req->buf_, req->old_buf_, &n, datatype);
1024 xbt_free(req->buf_);
1031 if (TRACE_smpi_view_internals() && ((req->flags_ & MPI_REQ_RECV) != 0)) {
1032 aid_t rank = simgrid::s4u::this_actor::get_pid();
1033 aid_t src_traced = (req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_);
1034 TRACE_smpi_recv(src_traced, rank,req->tag_);
1036 if(req->detached_sender_ != nullptr){
1037 //integrate pseudo-timing for buffering of small messages, do not bother to execute the simcall if 0
1038 simgrid::s4u::Host* dst_host = simgrid::s4u::Actor::by_pid(req->dst_)->get_host();
1039 if (double sleeptime = simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->orecv(
1040 req->real_size(), req->src_host_, dst_host);
1042 simgrid::s4u::this_actor::sleep_for(sleeptime);
1043 XBT_DEBUG("receiving size of %zu : sleep %f ", req->real_size_, sleeptime);
1045 unref(&(req->detached_sender_));
1047 if (req->flags_ & MPI_REQ_PERSISTENT)
1048 req->action_ = nullptr;
1049 req->flags_ |= MPI_REQ_FINISHED;
1051 if (req->truncated_ || req->unmatched_types_) {
1052 char error_string[MPI_MAX_ERROR_STRING];
1055 if(req->truncated_ )
1056 errkind = MPI_ERR_TRUNCATE;
1058 errkind = MPI_ERR_TYPE;
1059 PMPI_Error_string(errkind, error_string, &error_size);
1060 MPI_Errhandler err = (req->comm_) ? (req->comm_)->errhandler() : MPI_ERRHANDLER_NULL;
1061 if (err == MPI_ERRHANDLER_NULL || err == MPI_ERRORS_RETURN)
1062 XBT_WARN("recv - returned %.*s instead of MPI_SUCCESS", error_size, error_string);
1063 else if (err == MPI_ERRORS_ARE_FATAL)
1064 xbt_die("recv - returned %.*s instead of MPI_SUCCESS", error_size, error_string);
1066 err->call((req->comm_), errkind);
1067 if (err != MPI_ERRHANDLER_NULL)
1068 simgrid::smpi::Errhandler::unref(err);
1069 MC_assert(not MC_is_active()); /* Only fail in MC mode */
1071 if(req->src_ != MPI_PROC_NULL && req->dst_ != MPI_PROC_NULL)
1075 int Request::wait(MPI_Request * request, MPI_Status * status)
1077 // assume that *request is not MPI_REQUEST_NULL (filtered in PMPI_Wait before)
1078 xbt_assert(*request != MPI_REQUEST_NULL);
1080 int ret=MPI_SUCCESS;
1082 if((*request)->src_ == MPI_PROC_NULL || (*request)->dst_ == MPI_PROC_NULL){
1083 if (status != MPI_STATUS_IGNORE) {
1084 Status::empty(status);
1085 status->MPI_SOURCE = MPI_PROC_NULL;
1087 (*request)=MPI_REQUEST_NULL;
1091 (*request)->print_request("Waiting");
1092 if ((*request)->flags_ & (MPI_REQ_PREPARED | MPI_REQ_FINISHED)) {
1093 Status::empty(status);
1097 if ((*request)->action_ != nullptr){
1099 // this is not a detached send
1100 kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
1101 simgrid::smpi::ActorExt* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(issuer->get_pid()));
1102 kernel::actor::ActivityWaitSimcall observer{issuer, (*request)->action_.get(), -1,
1103 process->call_location()->get_call_location()};
1104 kernel::actor::simcall_blocking([issuer, &observer] { observer.get_activity()->wait_for(issuer, -1); },
1106 } catch (const CancelException&) {
1107 XBT_VERB("Request cancelled");
1111 if ((*request)->flags_ & MPI_REQ_GENERALIZED) {
1112 if (not((*request)->flags_ & MPI_REQ_COMPLETE)) {
1113 const std::scoped_lock lock(*(*request)->generalized_funcs->mutex);
1114 (*request)->generalized_funcs->cond->wait((*request)->generalized_funcs->mutex);
1116 MPI_Status tmp_status;
1117 MPI_Status* mystatus;
1118 if (status == MPI_STATUS_IGNORE) {
1119 mystatus = &tmp_status;
1120 Status::empty(mystatus);
1124 ret = ((*request)->generalized_funcs)->query_fn(((*request)->generalized_funcs)->extra_state, mystatus);
1127 if ((*request)->truncated_)
1128 ret = MPI_ERR_TRUNCATE;
1130 if ((*request)->flags_ & MPI_REQ_NBC)
1131 finish_nbc_requests(request, 0);
1133 finish_wait(request, status); // may invalidate *request
1134 if (*request != MPI_REQUEST_NULL && (((*request)->flags_ & MPI_REQ_NON_PERSISTENT) != 0))
1135 *request = MPI_REQUEST_NULL;
1139 int Request::waitany(int count, MPI_Request requests[], MPI_Status * status)
1141 int index = MPI_UNDEFINED;
1144 // Wait for a request to complete
1145 std::vector<simgrid::kernel::activity::ActivityImpl*> comms;
1146 std::vector<int> map;
1147 XBT_DEBUG("Wait for one of %d", count);
1148 for(int i = 0; i < count; i++) {
1149 if (requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & MPI_REQ_PREPARED) &&
1150 not(requests[i]->flags_ & MPI_REQ_FINISHED)) {
1151 if (requests[i]->action_ != nullptr) {
1152 XBT_DEBUG("Waiting any %p ", requests[i]);
1153 comms.push_back(requests[i]->action_.get());
1156 // This is a finished detached request, let's return this one
1157 comms.clear(); // don't do the waitany call afterwards
1159 if (requests[index]->flags_ & MPI_REQ_NBC)
1160 finish_nbc_requests(&requests[index], 0);
1161 finish_wait(&requests[i], status); // cleanup if refcount = 0
1162 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
1163 requests[i] = MPI_REQUEST_NULL; // set to null
1168 if (not comms.empty()) {
1169 XBT_DEBUG("Enter waitany for %zu comms", comms.size());
1172 kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
1173 simgrid::smpi::ActorExt* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(issuer->get_pid()));
1174 kernel::actor::ActivityWaitanySimcall observer{issuer, comms, -1,
1175 process->call_location()->get_call_location()};
1176 i = kernel::actor::simcall_blocking(
1178 kernel::activity::ActivityImpl::wait_any_for(observer.get_issuer(), observer.get_activities(),
1179 observer.get_timeout());
1182 } catch (const CancelException&) {
1183 XBT_INFO("request cancelled");
1187 // not MPI_UNDEFINED, as this is a simix return code
1190 //in case of an accumulate, we have to wait the end of all requests to apply the operation, ordered correctly.
1191 if ((requests[index] == MPI_REQUEST_NULL) ||
1192 (not((requests[index]->flags_ & MPI_REQ_ACCUMULATE) && (requests[index]->flags_ & MPI_REQ_RECV)))) {
1193 finish_wait(&requests[index],status);
1194 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_NON_PERSISTENT))
1195 requests[index] = MPI_REQUEST_NULL;
1202 if (index==MPI_UNDEFINED)
1203 Status::empty(status);
1208 static int sort_accumulates(const Request* a, const Request* b)
1210 return (a->tag() > b->tag());
1213 int Request::waitall(int count, MPI_Request requests[], MPI_Status status[])
1215 std::vector<MPI_Request> accumulates;
1218 MPI_Status *pstat = (status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat);
1219 int retvalue = MPI_SUCCESS;
1220 //tag invalid requests in the set
1221 if (status != MPI_STATUSES_IGNORE) {
1222 for (int c = 0; c < count; c++) {
1223 if (requests[c] == MPI_REQUEST_NULL || requests[c]->dst_ == MPI_PROC_NULL ||
1224 (requests[c]->flags_ & MPI_REQ_PREPARED)) {
1225 Status::empty(&status[c]);
1226 } else if (requests[c]->src_ == MPI_PROC_NULL) {
1227 Status::empty(&status[c]);
1228 status[c].MPI_SOURCE = MPI_PROC_NULL;
1232 for (int c = 0; c < count; c++) {
1233 if (MC_is_active() || MC_record_replay_is_active()) {
1234 wait(&requests[c],pstat);
1237 index = waitany(count, requests, pstat);
1239 if (index == MPI_UNDEFINED)
1242 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_RECV) &&
1243 (requests[index]->flags_ & MPI_REQ_ACCUMULATE))
1244 accumulates.push_back(requests[index]);
1245 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_NON_PERSISTENT))
1246 requests[index] = MPI_REQUEST_NULL;
1248 if (status != MPI_STATUSES_IGNORE) {
1249 status[index] = *pstat;
1250 if (status[index].MPI_ERROR == MPI_ERR_TRUNCATE)
1251 retvalue = MPI_ERR_IN_STATUS;
1255 std::sort(accumulates.begin(), accumulates.end(), sort_accumulates);
1256 for (auto& req : accumulates)
1257 finish_wait(&req, status);
1262 int Request::waitsome(int incount, MPI_Request requests[], int *indices, MPI_Status status[])
1268 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
1269 index = waitany(incount, requests, pstat);
1270 if(index==MPI_UNDEFINED) return MPI_UNDEFINED;
1271 if(status != MPI_STATUSES_IGNORE) {
1272 status[count] = *pstat;
1274 indices[count] = index;
1276 for (int i = 0; i < incount; i++) {
1277 if (i != index && requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & MPI_REQ_FINISHED)) {
1278 test(&requests[i], pstat,&flag);
1281 if(status != MPI_STATUSES_IGNORE) {
1282 status[count] = *pstat;
1284 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
1285 requests[i]=MPI_REQUEST_NULL;
1293 MPI_Request Request::f2c(int id)
1295 if(id==MPI_FORTRAN_REQUEST_NULL)
1296 return MPI_REQUEST_NULL;
1297 return static_cast<MPI_Request>(F2C::lookup()->at(id));
1300 void Request::free_f(int id)
1302 if (id != MPI_FORTRAN_REQUEST_NULL) {
1303 F2C::lookup()->erase(id);
1307 int Request::get_status(const Request* req, int* flag, MPI_Status* status)
1309 if(req != MPI_REQUEST_NULL && req->action_ != nullptr) {
1310 req->iprobe(req->comm_->group()->rank(req->src_), req->tag_, req->comm_, flag, status);
1314 if (req != MPI_REQUEST_NULL && (req->flags_ & MPI_REQ_GENERALIZED) && not(req->flags_ & MPI_REQ_COMPLETE)) {
1320 if(req != MPI_REQUEST_NULL &&
1321 status != MPI_STATUS_IGNORE) {
1322 aid_t src = req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_;
1323 status->MPI_SOURCE = req->comm_->group()->rank(src);
1324 status->MPI_TAG = req->tag_ == MPI_ANY_TAG ? req->real_tag_ : req->tag_;
1325 status->MPI_ERROR = req->truncated_ ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
1326 status->count = req->real_size_;
1331 int Request::grequest_start(MPI_Grequest_query_function* query_fn, MPI_Grequest_free_function* free_fn,
1332 MPI_Grequest_cancel_function* cancel_fn, void* extra_state, MPI_Request* request)
1334 *request = new Request();
1335 (*request)->flags_ |= MPI_REQ_GENERALIZED;
1336 (*request)->flags_ |= MPI_REQ_PERSISTENT;
1337 (*request)->refcount_ = 1;
1338 ((*request)->generalized_funcs) = std::make_unique<smpi_mpi_generalized_request_funcs_t>();
1339 ((*request)->generalized_funcs)->query_fn=query_fn;
1340 ((*request)->generalized_funcs)->free_fn=free_fn;
1341 ((*request)->generalized_funcs)->cancel_fn=cancel_fn;
1342 ((*request)->generalized_funcs)->extra_state=extra_state;
1343 ((*request)->generalized_funcs)->cond = simgrid::s4u::ConditionVariable::create();
1344 ((*request)->generalized_funcs)->mutex = simgrid::s4u::Mutex::create();
1348 int Request::grequest_complete(MPI_Request request)
1350 if ((not(request->flags_ & MPI_REQ_GENERALIZED)) || request->generalized_funcs->mutex == nullptr)
1351 return MPI_ERR_REQUEST;
1352 const std::scoped_lock lock(*request->generalized_funcs->mutex);
1353 request->flags_ |= MPI_REQ_COMPLETE; // in case wait would be called after complete
1354 request->generalized_funcs->cond->notify_one();
1358 void Request::start_nbc_requests(std::vector<MPI_Request> reqs){
1359 if (not reqs.empty()) {
1360 nbc_requests_ = reqs;
1361 Request::startall(reqs.size(), reqs.data());
1365 std::vector<MPI_Request> Request::get_nbc_requests() const
1367 return nbc_requests_;
1369 } // namespace simgrid::smpi