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/activity/CommImpl.hpp"
19 #include "src/kernel/actor/ActorImpl.hpp"
20 #include "src/kernel/actor/SimcallObserver.hpp"
21 #include "src/mc/mc_replay.hpp"
22 #include "src/smpi/include/smpi_actor.hpp"
27 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_request, smpi, "Logging specific to SMPI (request)");
29 static simgrid::config::Flag<double> smpi_iprobe_sleep(
30 "smpi/iprobe", "Minimum time to inject inside a call to MPI_Iprobe", 1e-4);
31 static simgrid::config::Flag<double> smpi_test_sleep(
32 "smpi/test", "Minimum time to inject inside a call to MPI_Test", 1e-4);
34 std::vector<s_smpi_factor_t> smpi_ois_values;
36 extern void (*smpi_comm_copy_data_callback)(simgrid::kernel::activity::CommImpl*, void*, size_t);
41 Request::Request(const void* buf, int count, MPI_Datatype datatype, aid_t src, aid_t dst, int tag, MPI_Comm comm,
42 unsigned flags, MPI_Op op)
43 : buf_(const_cast<void*>(buf))
46 , size_(datatype->size() * count)
56 if(op != MPI_REPLACE && op != MPI_OP_NULL)
60 detached_sender_ = nullptr;
62 // get src_host if it's available (src is valid)
63 auto src_process = simgrid::s4u::Actor::by_pid(src);
65 src_host_ = src_process->get_host();
67 unmatched_types_ = false;
70 if (flags & MPI_REQ_PERSISTENT)
83 void Request::unref(MPI_Request* request)
85 xbt_assert(*request != MPI_REQUEST_NULL, "freeing an already free request");
87 (*request)->refcount_--;
88 if ((*request)->refcount_ < 0) {
89 (*request)->print_request("wrong refcount");
90 xbt_die("Whoops, wrong refcount");
92 if ((*request)->refcount_ == 0) {
93 if ((*request)->flags_ & MPI_REQ_GENERALIZED) {
94 ((*request)->generalized_funcs)->free_fn(((*request)->generalized_funcs)->extra_state);
96 Comm::unref((*request)->comm_);
97 Datatype::unref((*request)->type_);
99 if ((*request)->op_ != MPI_REPLACE && (*request)->op_ != MPI_OP_NULL)
100 Op::unref(&(*request)->op_);
102 (*request)->print_request("Destroying");
103 F2C::free_f((*request)->f2c_id());
105 *request = MPI_REQUEST_NULL;
107 (*request)->print_request("Decrementing");
111 bool Request::match_types(MPI_Datatype stype, MPI_Datatype rtype){
113 if ((stype == rtype) ||
114 //byte and packed always match with anything
115 (stype == MPI_PACKED || rtype == MPI_PACKED || stype == MPI_BYTE || rtype == MPI_BYTE) ||
116 //complex datatypes - we don't properly match these yet, as it would mean checking each subtype recursively.
117 (stype->flags() & DT_FLAG_DERIVED || rtype->flags() & DT_FLAG_DERIVED) ||
118 //duplicated datatypes, check if underlying is ok
119 (stype->duplicated_datatype()!=MPI_DATATYPE_NULL && match_types(stype->duplicated_datatype(), rtype)) ||
120 (rtype->duplicated_datatype()!=MPI_DATATYPE_NULL && match_types(stype, rtype->duplicated_datatype())))
123 XBT_WARN("Mismatched datatypes : sending %s and receiving %s", stype->name().c_str(), rtype->name().c_str());
128 bool Request::match_common(MPI_Request req, MPI_Request sender, MPI_Request receiver)
130 xbt_assert(sender, "Cannot match against null sender");
131 xbt_assert(receiver, "Cannot match against null receiver");
132 XBT_DEBUG("Trying to match %s of sender src %ld against %ld, tag %d against %d, id %d against %d",
133 (req == receiver ? "send" : "recv"), sender->src_, receiver->src_, sender->tag_, receiver->tag_,
134 sender->comm_->id(), receiver->comm_->id());
136 if ((receiver->comm_->id() == MPI_UNDEFINED || sender->comm_->id() == MPI_UNDEFINED ||
137 receiver->comm_->id() == sender->comm_->id()) &&
138 ((receiver->src_ == MPI_ANY_SOURCE && (receiver->comm_->group()->rank(sender->src_) != MPI_UNDEFINED)) ||
139 receiver->src_ == sender->src_) &&
140 ((receiver->tag_ == MPI_ANY_TAG && sender->tag_ >= 0) || receiver->tag_ == sender->tag_)) {
141 // we match, we can transfer some values
142 if (receiver->src_ == MPI_ANY_SOURCE) {
143 receiver->real_src_ = sender->src_;
144 receiver->src_host_ = sender->src_host_;
146 if (receiver->tag_ == MPI_ANY_TAG)
147 receiver->real_tag_ = sender->tag_;
148 if ((receiver->flags_ & MPI_REQ_PROBE) == 0 && receiver->real_size_ < sender->real_size_) {
149 XBT_DEBUG("Truncating message - should not happen: receiver size : %zu < sender size : %zu", receiver->real_size_,
151 receiver->truncated_ = true;
153 //0-sized datatypes/counts should not interfere and match
154 if ( sender->real_size_ != 0 && receiver->real_size_ != 0 &&
155 !match_types(sender->type_, receiver->type_))
156 receiver->unmatched_types_ = true;
157 if (sender->detached_)
158 receiver->detached_sender_ = sender; // tie the sender to the receiver, as it is detached and has to be freed in
160 req->flags_ |= MPI_REQ_MATCHED; // mark as impossible to cancel anymore
161 XBT_DEBUG("match succeeded");
167 void Request::init_buffer(int count){
168 // FIXME Handle the case of a partial shared malloc.
169 // This part handles the problem of non-contiguous memory (for the unserialization at the reception)
170 if (not smpi_process()->replaying() &&
171 ((((flags_ & MPI_REQ_RECV) != 0) && ((flags_ & MPI_REQ_ACCUMULATE) != 0)) || (type_->flags() & DT_FLAG_DERIVED))) {
172 // This part handles the problem of non-contiguous memory
177 buf_ = xbt_malloc(count*type_->size());
178 if ((type_->flags() & DT_FLAG_DERIVED) && ((flags_ & MPI_REQ_SEND) != 0)) {
179 type_->serialize(old_buf_, buf_, count);
185 bool Request::match_recv(void* a, void* b, simgrid::kernel::activity::CommImpl*)
187 auto ref = static_cast<MPI_Request>(a);
188 auto req = static_cast<MPI_Request>(b);
189 bool match = match_common(req, req, ref);
190 if (not match || ref->comm_ == MPI_COMM_UNINITIALIZED || ref->comm_->is_smp_comm())
193 if (ref->comm_->get_received_messages_count(ref->comm_->group()->rank(req->src_),
194 ref->comm_->group()->rank(req->dst_), req->tag_) == req->message_id_) {
195 if (((ref->flags_ & MPI_REQ_PROBE) == 0) && ((req->flags_ & MPI_REQ_PROBE) == 0)) {
196 XBT_DEBUG("increasing count in comm %p, which was %u from pid %ld, to pid %ld with tag %d", ref->comm_,
197 ref->comm_->get_received_messages_count(ref->comm_->group()->rank(req->src_),
198 ref->comm_->group()->rank(req->dst_), req->tag_),
199 req->src_, req->dst_, req->tag_);
200 ref->comm_->increment_received_messages_count(ref->comm_->group()->rank(req->src_),
201 ref->comm_->group()->rank(req->dst_), req->tag_);
202 if (ref->real_size_ > req->real_size_) {
203 ref->real_size_ = req->real_size_;
208 req->flags_ &= ~MPI_REQ_MATCHED;
209 ref->detached_sender_ = nullptr;
210 XBT_DEBUG("Refusing to match message, as its ID is not the one I expect. in comm %p, %u != %u, "
211 "from pid %ld to pid %ld, with tag %d",
213 ref->comm_->get_received_messages_count(ref->comm_->group()->rank(req->src_),
214 ref->comm_->group()->rank(req->dst_), req->tag_),
215 req->message_id_, req->src_, req->dst_, req->tag_);
220 bool Request::match_send(void* a, void* b, simgrid::kernel::activity::CommImpl*)
222 auto ref = static_cast<MPI_Request>(a);
223 auto req = static_cast<MPI_Request>(b);
224 return match_common(req, ref, req);
227 void Request::print_request(const char* message) const
229 XBT_VERB("%s request %p [buf = %p, size = %zu, src = %ld, dst = %ld, tag = %d, flags = %x]", message, this, buf_,
230 size_, src_, dst_, tag_, flags_);
233 /* factories, to hide the internal flags from the caller */
234 MPI_Request Request::bsend_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
236 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
237 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
238 MPI_REQ_PERSISTENT | MPI_REQ_SEND | MPI_REQ_PREPARED | MPI_REQ_BSEND);
241 MPI_Request Request::send_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
243 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
244 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
245 MPI_REQ_PERSISTENT | MPI_REQ_SEND | MPI_REQ_PREPARED);
248 MPI_Request Request::ssend_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
250 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
251 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
252 MPI_REQ_PERSISTENT | MPI_REQ_SSEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
255 MPI_Request Request::isend_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
257 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
258 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
259 MPI_REQ_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
262 MPI_Request Request::rma_send_init(const void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
267 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src),
268 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
269 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
271 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src),
272 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
273 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED |
280 MPI_Request Request::recv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
282 aid_t source = MPI_PROC_NULL;
283 if (src == MPI_ANY_SOURCE)
284 source = MPI_ANY_SOURCE;
285 else if (src != MPI_PROC_NULL)
286 source = comm->group()->actor(src);
287 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
289 simgrid::s4u::this_actor::get_pid(), tag, comm,
290 MPI_REQ_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
293 MPI_Request Request::rma_recv_init(void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
296 aid_t source = MPI_PROC_NULL;
297 if (src == MPI_ANY_SOURCE)
298 source = MPI_ANY_SOURCE;
299 else if (src != MPI_PROC_NULL)
300 source = comm->group()->actor(src);
303 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, source,
304 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
305 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
308 new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, source,
309 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
310 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED | MPI_REQ_ACCUMULATE, op);
315 MPI_Request Request::irecv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
317 aid_t source = MPI_PROC_NULL;
318 if (src == MPI_ANY_SOURCE)
319 source = MPI_ANY_SOURCE;
320 else if (src != MPI_PROC_NULL)
321 source = comm->group()->actor(src);
322 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
323 source, simgrid::s4u::this_actor::get_pid(), tag, comm,
324 MPI_REQ_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
327 MPI_Request Request::ibsend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
329 auto request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
330 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
331 MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_BSEND);
332 if(dst != MPI_PROC_NULL)
337 MPI_Request Request::isend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
339 auto request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
340 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
341 MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND);
342 if(dst != MPI_PROC_NULL)
347 MPI_Request Request::issend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
349 auto request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
350 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
351 MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SSEND | MPI_REQ_SEND);
352 if(dst != MPI_PROC_NULL)
357 MPI_Request Request::irecv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
359 aid_t source = MPI_PROC_NULL;
360 if (src == MPI_ANY_SOURCE)
361 source = MPI_ANY_SOURCE;
362 else if (src != MPI_PROC_NULL)
363 source = comm->group()->actor(src);
364 auto request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, source,
365 simgrid::s4u::this_actor::get_pid(), tag, comm, MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV);
366 if(src != MPI_PROC_NULL)
371 int Request::recv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status * status)
373 MPI_Request request = irecv(buf, count, datatype, src, tag, comm);
374 int retval = wait(&request,status);
379 void Request::bsend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
381 auto request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
382 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
383 MPI_REQ_NON_PERSISTENT | MPI_REQ_SEND | MPI_REQ_BSEND);
385 if(dst != MPI_PROC_NULL)
387 wait(&request, MPI_STATUS_IGNORE);
391 void Request::send(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
393 auto request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
394 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
395 MPI_REQ_NON_PERSISTENT | MPI_REQ_SEND);
396 if(dst != MPI_PROC_NULL)
398 wait(&request, MPI_STATUS_IGNORE);
402 void Request::ssend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
404 auto request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
405 dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL, tag, comm,
406 MPI_REQ_NON_PERSISTENT | MPI_REQ_SSEND | MPI_REQ_SEND);
408 if(dst != MPI_PROC_NULL)
410 wait(&request,MPI_STATUS_IGNORE);
414 void Request::sendrecv(const void *sendbuf, int sendcount, MPI_Datatype sendtype,int dst, int sendtag,
415 void *recvbuf, int recvcount, MPI_Datatype recvtype, int src, int recvtag,
416 MPI_Comm comm, MPI_Status * status)
418 aid_t source = MPI_PROC_NULL;
419 if (src == MPI_ANY_SOURCE)
420 source = MPI_ANY_SOURCE;
421 else if (src != MPI_PROC_NULL)
422 source = comm->group()->actor(src);
423 aid_t destination = dst != MPI_PROC_NULL ? comm->group()->actor(dst) : MPI_PROC_NULL;
425 std::array<MPI_Request, 2> requests;
426 std::array<MPI_Status, 2> stats;
427 aid_t myid = simgrid::s4u::this_actor::get_pid();
428 if ((destination == myid) && (source == myid)) {
429 Datatype::copy(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
430 if (status != MPI_STATUS_IGNORE) {
431 status->MPI_SOURCE = source;
432 status->MPI_TAG = recvtag;
433 status->MPI_ERROR = MPI_SUCCESS;
434 status->count = sendcount * sendtype->size();
438 requests[0] = isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
439 requests[1] = irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
440 startall(2, requests.data());
441 waitall(2, requests.data(), stats.data());
444 if(status != MPI_STATUS_IGNORE) {
445 // Copy receive status
450 void Request::start()
452 s4u::Mailbox* mailbox;
454 xbt_assert(action_ == nullptr, "Cannot (re-)start unfinished communication");
455 //reinitialize temporary buffer for persistent requests
456 if(real_size_ > 0 && flags_ & MPI_REQ_FINISHED){
458 init_buffer(real_size_/type_->size());
460 flags_ &= ~MPI_REQ_PREPARED;
461 flags_ &= ~MPI_REQ_FINISHED;
464 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
466 if ((flags_ & MPI_REQ_RECV) != 0) {
467 this->print_request("New recv");
469 simgrid::smpi::ActorExt* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(dst_));
471 simgrid::s4u::MutexPtr mut = process->mailboxes_mutex();
472 if (smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)
475 bool is_probe = ((flags_ & MPI_REQ_PROBE) != 0);
476 flags_ |= MPI_REQ_PROBE;
478 if (smpi_cfg_async_small_thresh() == 0 && (flags_ & MPI_REQ_RMA) == 0) {
479 mailbox = process->mailbox();
480 } else if (((flags_ & MPI_REQ_RMA) != 0) || static_cast<int>(size_) < smpi_cfg_async_small_thresh()) {
481 //We have to check both mailboxes (because SSEND messages are sent to the large mbox).
482 //begin with the more appropriate one : the small one.
483 mailbox = process->mailbox_small();
484 XBT_DEBUG("Is there a corresponding send already posted in the small mailbox %s (in case of SSEND)?",
485 mailbox->get_cname());
486 simgrid::kernel::activity::ActivityImplPtr action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
488 if (action == nullptr) {
489 mailbox = process->mailbox();
490 XBT_DEBUG("No, nothing in the small mailbox test the other one : %s", mailbox->get_cname());
491 action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
492 if (action == nullptr) {
493 XBT_DEBUG("Still nothing, switch back to the small mailbox : %s", mailbox->get_cname());
494 mailbox = process->mailbox_small();
497 XBT_DEBUG("yes there was something for us in the small mailbox");
500 mailbox = process->mailbox_small();
501 XBT_DEBUG("Is there a corresponding send already posted the small mailbox?");
502 simgrid::kernel::activity::ActivityImplPtr action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
504 if (action == nullptr) {
505 XBT_DEBUG("No, nothing in the permanent receive mailbox");
506 mailbox = process->mailbox();
508 XBT_DEBUG("yes there was something for us in the small mailbox");
512 flags_ &= ~MPI_REQ_PROBE;
513 action_ = simcall_comm_irecv(
514 process->get_actor()->get_impl(), mailbox->get_impl(), buf_, &real_size_, &match_recv,
515 process->replaying() ? &smpi_comm_null_copy_buffer_callback : smpi_comm_copy_data_callback, this, -1.0);
517 // kernel::actor::CommIrecvSimcall observer{process->get_actor()->get_impl(),
518 // mailbox->get_impl(),
519 // static_cast<unsigned char*>(buf_),
522 // process->replaying() ? &smpi_comm_null_copy_buffer_callback
523 // : smpi_comm_copy_data_callback,
527 // action_ = kernel::actor::simcall_blocking(
529 // return kernel::activity::CommImpl::irecv(
530 // observer.get_issuer(), observer.get_mailbox(), observer.get_dst_buff(),
531 // observer.get_dst_buff_size(), observer.match_fun_, observer.copy_data_fun_, observer.get_payload(),
532 // observer.get_rate());
536 XBT_DEBUG("recv simcall posted");
538 if (smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)
540 } else { /* the RECV flag was not set, so this is a send */
541 const simgrid::smpi::ActorExt* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(dst_));
542 xbt_assert(process, "Actor pid=%ld is gone??", dst_);
543 if (TRACE_smpi_view_internals())
544 TRACE_smpi_send(src_, src_, dst_, tag_, size_);
545 this->print_request("New send");
547 message_id_=comm_->get_sent_messages_count(comm_->group()->rank(src_), comm_->group()->rank(dst_), tag_);
548 comm_->increment_sent_messages_count(comm_->group()->rank(src_), comm_->group()->rank(dst_), tag_);
551 if ((flags_ & MPI_REQ_SSEND) == 0 &&
552 ((flags_ & MPI_REQ_RMA) != 0 || (flags_ & MPI_REQ_BSEND) != 0 ||
553 static_cast<int>(size_) < smpi_cfg_detached_send_thresh())) {
554 void *oldbuf = nullptr;
556 XBT_DEBUG("Send request %p is detached", this);
558 if (not(type_->flags() & DT_FLAG_DERIVED)) {
560 if (not process->replaying() && oldbuf != nullptr && size_ != 0) {
561 if (smpi_switch_data_segment(simgrid::s4u::Actor::by_pid(src_), buf_))
562 XBT_DEBUG("Privatization : We are sending from a zone inside global memory. Switch data segment ");
564 //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
565 //so actually ... don't use manually attached buffer space.
566 buf = xbt_malloc(size_);
567 memcpy(buf,oldbuf,size_);
568 XBT_DEBUG("buf %p copied into %p",oldbuf,buf);
573 //if we are giving back the control to the user without waiting for completion, we have to inject timings
574 double sleeptime = 0.0;
575 if (detached_ || ((flags_ & (MPI_REQ_ISEND | MPI_REQ_SSEND)) != 0)) { // issend should be treated as isend
576 // isend and send timings may be different
578 ((flags_ & MPI_REQ_ISEND) != 0)
579 ? simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->oisend(
580 size_, simgrid::s4u::Actor::by_pid(src_)->get_host(), simgrid::s4u::Actor::by_pid(dst_)->get_host())
581 : simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->osend(
582 size_, simgrid::s4u::Actor::by_pid(src_)->get_host(),
583 simgrid::s4u::Actor::by_pid(dst_)->get_host());
587 simgrid::s4u::this_actor::sleep_for(sleeptime);
588 XBT_DEBUG("sending size of %zu : sleep %f ", size_, sleeptime);
591 simgrid::s4u::MutexPtr mut = process->mailboxes_mutex();
593 if (smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)
596 if (not(smpi_cfg_async_small_thresh() != 0 || (flags_ & MPI_REQ_RMA) != 0)) {
597 mailbox = process->mailbox();
598 } else if (((flags_ & MPI_REQ_RMA) != 0) || static_cast<int>(size_) < smpi_cfg_async_small_thresh()) { // eager mode
599 bool is_probe = ((flags_ & MPI_REQ_PROBE) != 0);
600 flags_ |= MPI_REQ_PROBE;
602 mailbox = process->mailbox();
603 XBT_DEBUG("Is there a corresponding recv already posted in the large mailbox %s?", mailbox->get_cname());
604 simgrid::kernel::activity::ActivityImplPtr action = mailbox->iprobe(1, &match_send, static_cast<void*>(this));
605 if (action == nullptr) {
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 action = mailbox->iprobe(1, &match_send, static_cast<void*>(this));
615 if (action == nullptr) {
616 XBT_DEBUG("No, we are first, send to large mailbox");
617 mailbox = process->mailbox();
621 XBT_DEBUG("Yes there was something for us in the large mailbox");
624 flags_ &= ~MPI_REQ_PROBE;
626 mailbox = process->mailbox();
627 XBT_DEBUG("Send request %p is in the large mailbox %s (buf: %p)", this, mailbox->get_cname(), buf_);
630 size_t payload_size_ = size_ + 16;//MPI enveloppe size (tag+dest+communicator)
631 kernel::actor::CommIsendSimcall observer{
632 simgrid::kernel::actor::ActorImpl::by_pid(src_), mailbox->get_impl(), payload_size_, -1,
633 static_cast<unsigned char*>(buf), real_size_, &match_send,
634 &xbt_free_f, // how to free the userdata if a detached send fails
635 process->replaying() ? &smpi_comm_null_copy_buffer_callback : smpi_comm_copy_data_callback, this,
636 // detach if msg size < eager/rdv switch limit
638 action_ = kernel::actor::simcall_blocking(
640 return kernel::activity::CommImpl::isend(
641 observer.get_issuer(), observer.get_mailbox(), observer.get_payload_size(), observer.get_rate(),
642 observer.get_src_buff(), observer.get_src_buff_size(), observer.match_fun_, observer.clean_fun_,
643 observer.copy_data_fun_, observer.get_payload(), observer.is_detached());
646 XBT_DEBUG("send simcall posted");
648 /* FIXME: detached sends are not traceable (action_ == nullptr) */
649 if (action_ != nullptr) {
650 boost::static_pointer_cast<kernel::activity::CommImpl>(action_)->set_tracing_category(
651 smpi_process()->get_tracing_category());
654 if (smpi_cfg_async_small_thresh() != 0 || ((flags_ & MPI_REQ_RMA) != 0))
659 void Request::startall(int count, MPI_Request * requests)
661 if(requests== nullptr)
664 for(int i = 0; i < count; i++) {
665 if(requests[i]->src_ != MPI_PROC_NULL && requests[i]->dst_ != MPI_PROC_NULL)
666 requests[i]->start();
670 void Request::cancel()
672 this->flags_ |= MPI_REQ_CANCELLED;
673 if (this->action_ != nullptr)
674 (boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(this->action_))->cancel();
677 int Request::test(MPI_Request * request, MPI_Status * status, int* flag) {
678 // assume that *request is not MPI_REQUEST_NULL (filtered in PMPI_Test or testall before)
679 // to avoid deadlocks if used as a break condition, such as
680 // while (MPI_Test(request, flag, status) && flag) dostuff...
681 // because the time will not normally advance when only calls to MPI_Test are made -> deadlock
682 // multiplier to the sleeptime, to increase speed of execution, each failed test will increase it
683 xbt_assert(*request != MPI_REQUEST_NULL);
685 static int nsleeps = 1;
686 int ret = MPI_SUCCESS;
688 if(smpi_test_sleep > 0)
689 simgrid::s4u::this_actor::sleep_for(nsleeps * smpi_test_sleep);
691 Status::empty(status);
694 if ((*request)->flags_ & MPI_REQ_NBC){
695 *flag = finish_nbc_requests(request, 1);
698 if (((*request)->flags_ & (MPI_REQ_PREPARED | MPI_REQ_FINISHED)) == 0) {
699 if ((*request)->action_ != nullptr && ((*request)->flags_ & MPI_REQ_CANCELLED) == 0){
701 kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
702 kernel::actor::ActivityTestSimcall observer{issuer, (*request)->action_.get()};
703 *flag = kernel::actor::simcall_blocking([&observer] { observer.get_activity()->test(observer.get_issuer()); },
705 } catch (const Exception&) {
710 if (((*request)->flags_ & MPI_REQ_GENERALIZED) && not((*request)->flags_ & MPI_REQ_COMPLETE))
713 finish_wait(request, status); // may invalidate *request
714 if (*request != MPI_REQUEST_NULL && ((*request)->flags_ & MPI_REQ_GENERALIZED)){
715 MPI_Status tmp_status;
716 MPI_Status* mystatus;
717 if (status == MPI_STATUS_IGNORE) {
718 mystatus = &tmp_status;
719 Status::empty(mystatus);
723 ret = ((*request)->generalized_funcs)->query_fn(((*request)->generalized_funcs)->extra_state, mystatus);
725 nsleeps=1;//reset the number of sleeps we will do next time
726 if (*request != MPI_REQUEST_NULL && ((*request)->flags_ & MPI_REQ_PERSISTENT) == 0)
727 *request = MPI_REQUEST_NULL;
728 } else if (smpi_cfg_grow_injected_times()) {
735 int Request::testsome(int incount, MPI_Request requests[], int *count, int *indices, MPI_Status status[])
741 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
744 for (int i = 0; i < incount; i++) {
745 if (requests[i] != MPI_REQUEST_NULL && not (requests[i]->flags_ & MPI_REQ_FINISHED)) {
746 int ret = test(&requests[i], pstat, &flag);
751 if (status != MPI_STATUSES_IGNORE)
752 status[*count] = *pstat;
754 if ((requests[i] != MPI_REQUEST_NULL) && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
755 requests[i] = MPI_REQUEST_NULL;
761 if(count_dead==incount)*count=MPI_UNDEFINED;
763 return MPI_ERR_IN_STATUS;
768 int Request::testany(int count, MPI_Request requests[], int *index, int* flag, MPI_Status * status)
770 std::vector<simgrid::kernel::activity::ActivityImpl*> comms;
771 comms.reserve(count);
774 int ret = MPI_SUCCESS;
775 *index = MPI_UNDEFINED;
777 std::vector<int> map; /** Maps all matching comms back to their location in requests **/
778 for (int i = 0; i < count; i++) {
779 if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->action_ && not(requests[i]->flags_ & MPI_REQ_PREPARED)) {
780 comms.push_back(requests[i]->action_.get());
784 if (not map.empty()) {
785 //multiplier to the sleeptime, to increase speed of execution, each failed testany will increase it
786 static int nsleeps = 1;
787 if(smpi_test_sleep > 0)
788 simgrid::s4u::this_actor::sleep_for(nsleeps * smpi_test_sleep);
791 kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
792 kernel::actor::ActivityTestanySimcall observer{issuer, comms};
793 i = kernel::actor::simcall_blocking(
794 [&observer] { kernel::activity::ActivityImpl::test_any(observer.get_issuer(), observer.get_activities()); },
796 } catch (const Exception&) {
797 XBT_DEBUG("Exception in testany");
801 if (i != -1) { // -1 is not MPI_UNDEFINED but a SIMIX return code. (nothing matches)
803 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags_ & MPI_REQ_GENERALIZED) &&
804 not(requests[*index]->flags_ & MPI_REQ_COMPLETE)) {
807 finish_wait(&requests[*index],status);
808 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags_ & MPI_REQ_GENERALIZED)){
809 MPI_Status tmp_status;
810 MPI_Status* mystatus;
811 if (status == MPI_STATUS_IGNORE) {
812 mystatus = &tmp_status;
813 Status::empty(mystatus);
817 ret=(requests[*index]->generalized_funcs)->query_fn((requests[*index]->generalized_funcs)->extra_state, mystatus);
820 if (requests[*index] != MPI_REQUEST_NULL && requests[*index]->flags_ & MPI_REQ_NBC){
821 *flag = finish_nbc_requests(&requests[*index] , 1);
824 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags_ & MPI_REQ_NON_PERSISTENT))
825 requests[*index] = MPI_REQUEST_NULL;
826 XBT_DEBUG("Testany - returning with index %d", *index);
834 XBT_DEBUG("Testany on inactive handles, returning flag=1 but empty status");
835 //all requests are null or inactive, return true
837 *index = MPI_UNDEFINED;
838 Status::empty(status);
844 int Request::testall(int count, MPI_Request requests[], int* outflag, MPI_Status status[])
847 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
851 for(int i=0; i<count; i++){
852 if (requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & MPI_REQ_PREPARED)) {
853 int ret = test(&requests[i], pstat, &flag);
859 if (ret != MPI_SUCCESS)
862 Status::empty(pstat);
864 if(status != MPI_STATUSES_IGNORE) {
869 return MPI_ERR_IN_STATUS;
874 void Request::probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
876 //FIXME find another way to avoid busy waiting ?
877 // the issue here is that we have to wait on a nonexistent comm
879 iprobe(source, tag, comm, &flag, status);
880 XBT_DEBUG("Busy Waiting on probing : %d", flag);
884 void Request::iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
885 // to avoid deadlock, we have to sleep some time here, or the timer won't advance and we will only do iprobe simcalls
886 // especially when used as a break condition, such as while (MPI_Iprobe(...)) dostuff...
887 // nsleeps is a multiplier to the sleeptime, to increase speed of execution, each failed iprobe will increase it
888 // This can speed up the execution of certain applications by an order of magnitude, such as HPL
889 static int nsleeps = 1;
890 double speed = s4u::this_actor::get_host()->get_speed();
891 double maxrate = smpi_cfg_iprobe_cpu_usage();
893 new Request(nullptr, 0, MPI_CHAR, source == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : comm->group()->actor(source),
894 simgrid::s4u::this_actor::get_pid(), tag, comm, MPI_REQ_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PROBE);
895 if (smpi_iprobe_sleep > 0) {
896 /** Compute the number of flops we will sleep **/
897 s4u::this_actor::exec_init(/*nsleeps: See comment above */ nsleeps *
898 /*(seconds * flop/s -> total flops)*/ smpi_iprobe_sleep * speed * maxrate)
900 /* Not the entire CPU can be used when iprobing: This is important for
901 * the energy consumption caused by polling with iprobes.
902 * Note also that the number of flops that was
903 * computed above contains a maxrate factor and is hence reduced (maxrate < 1)
905 ->set_bound(maxrate * speed)
909 // behave like a receive, but don't do it
910 s4u::Mailbox* mailbox;
912 request->print_request("New iprobe");
913 // We have to test both mailboxes as we don't know if we will receive one or another
914 if (smpi_cfg_async_small_thresh() > 0) {
915 mailbox = smpi_process()->mailbox_small();
916 XBT_DEBUG("Trying to probe the perm recv mailbox");
917 request->action_ = mailbox->iprobe(0, &match_recv, static_cast<void*>(request));
920 if (request->action_ == nullptr){
921 mailbox = smpi_process()->mailbox();
922 XBT_DEBUG("trying to probe the other mailbox");
923 request->action_ = mailbox->iprobe(0, &match_recv, static_cast<void*>(request));
926 if (request->action_ != nullptr){
927 kernel::activity::CommImplPtr sync_comm = boost::static_pointer_cast<kernel::activity::CommImpl>(request->action_);
928 const Request* req = static_cast<MPI_Request>(sync_comm->src_data_);
930 if (status != MPI_STATUS_IGNORE && (req->flags_ & MPI_REQ_PREPARED) == 0) {
931 status->MPI_SOURCE = comm->group()->rank(req->src_);
932 status->MPI_TAG = req->tag_;
933 status->MPI_ERROR = MPI_SUCCESS;
934 status->count = req->real_size_;
936 nsleeps = 1;//reset the number of sleeps we will do next time
940 if (smpi_cfg_grow_injected_times())
944 xbt_assert(request == MPI_REQUEST_NULL);
947 int Request::finish_nbc_requests(MPI_Request* request, int test){
951 ret = waitall((*request)->nbc_requests_.size(), (*request)->nbc_requests_.data(), MPI_STATUSES_IGNORE);
953 ret = testall((*request)->nbc_requests_.size(), (*request)->nbc_requests_.data(), &flag, MPI_STATUSES_IGNORE);
956 xbt_die("Failure when waiting on non blocking collective sub-requests");
958 XBT_DEBUG("Finishing non blocking collective request with %zu sub-requests", (*request)->nbc_requests_.size());
959 for(auto& req: (*request)->nbc_requests_){
960 if((*request)->buf_!=nullptr && req!=MPI_REQUEST_NULL){//reduce case
961 void * buf=req->buf_;
962 if((*request)->type_->flags() & DT_FLAG_DERIVED)
964 if(req->flags_ & MPI_REQ_RECV ){
965 if((*request)->op_!=MPI_OP_NULL){
966 int count=(*request)->size_/ (*request)->type_->size();
967 (*request)->op_->apply(buf, (*request)->buf_, &count, (*request)->type_);
969 smpi_free_tmp_buffer(static_cast<unsigned char*>(buf));
972 if(req!=MPI_REQUEST_NULL)
973 Request::unref(&req);
975 (*request)->nbc_requests_.clear();
980 void Request::finish_wait(MPI_Request* request, MPI_Status * status)
982 MPI_Request req = *request;
983 Status::empty(status);
984 if((req->flags_ & MPI_REQ_CANCELLED) != 0 && (req->flags_ & MPI_REQ_MATCHED) == 0) {
985 if (status!=MPI_STATUS_IGNORE)
987 if(req->detached_sender_ != nullptr)
988 unref(&(req->detached_sender_));
993 if ((req->flags_ & (MPI_REQ_PREPARED | MPI_REQ_GENERALIZED | MPI_REQ_FINISHED)) == 0) {
994 if (status != MPI_STATUS_IGNORE) {
995 if (req->src_== MPI_PROC_NULL || req->dst_== MPI_PROC_NULL){
996 Status::empty(status);
997 status->MPI_SOURCE = MPI_PROC_NULL;
999 aid_t src = req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_;
1000 status->MPI_SOURCE = req->comm_->group()->rank(src);
1001 status->MPI_TAG = req->tag_ == MPI_ANY_TAG ? req->real_tag_ : req->tag_;
1002 status->MPI_ERROR = req->truncated_ ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
1004 // this handles the case were size in receive differs from size in send
1005 status->count = req->real_size_;
1007 //detached send will be finished at the other end
1008 if (not(req->detached_ && ((req->flags_ & MPI_REQ_SEND) != 0))) {
1009 req->print_request("Finishing");
1010 MPI_Datatype datatype = req->type_;
1012 // FIXME Handle the case of a partial shared malloc.
1013 if (not smpi_process()->replaying() &&
1014 (((req->flags_ & MPI_REQ_ACCUMULATE) != 0) || (datatype->flags() & DT_FLAG_DERIVED))) {
1015 if (smpi_switch_data_segment(simgrid::s4u::Actor::self(), req->old_buf_))
1016 XBT_VERB("Privatization : We are unserializing to a zone in global memory Switch data segment ");
1018 if(datatype->flags() & DT_FLAG_DERIVED){
1019 // This part handles the problem of non-contiguous memory the unserialization at the reception
1020 if ((req->flags_ & MPI_REQ_RECV) && datatype->size() != 0)
1021 datatype->unserialize(req->buf_, req->old_buf_, req->real_size_/datatype->size() , req->op_);
1022 xbt_free(req->buf_);
1024 } else if (req->flags_ & MPI_REQ_RECV) { // apply op on contiguous buffer for accumulate
1025 if (datatype->size() != 0) {
1026 int n = req->real_size_ / datatype->size();
1027 req->op_->apply(req->buf_, req->old_buf_, &n, datatype);
1029 xbt_free(req->buf_);
1036 if (TRACE_smpi_view_internals() && ((req->flags_ & MPI_REQ_RECV) != 0)) {
1037 aid_t rank = simgrid::s4u::this_actor::get_pid();
1038 aid_t src_traced = (req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_);
1039 TRACE_smpi_recv(src_traced, rank,req->tag_);
1041 if(req->detached_sender_ != nullptr){
1042 //integrate pseudo-timing for buffering of small messages, do not bother to execute the simcall if 0
1043 simgrid::s4u::Host* dst_host = simgrid::s4u::Actor::by_pid(req->dst_)->get_host();
1044 double sleeptime = simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->orecv(
1045 req->real_size(), req->src_host_, dst_host);
1046 if (sleeptime > 0.0) {
1047 simgrid::s4u::this_actor::sleep_for(sleeptime);
1048 XBT_DEBUG("receiving size of %zu : sleep %f ", req->real_size_, sleeptime);
1050 unref(&(req->detached_sender_));
1052 if (req->flags_ & MPI_REQ_PERSISTENT)
1053 req->action_ = nullptr;
1054 req->flags_ |= MPI_REQ_FINISHED;
1056 if (req->truncated_ || req->unmatched_types_) {
1057 char error_string[MPI_MAX_ERROR_STRING];
1060 if(req->truncated_ )
1061 errkind = MPI_ERR_TRUNCATE;
1063 errkind = MPI_ERR_TYPE;
1064 PMPI_Error_string(errkind, error_string, &error_size);
1065 MPI_Errhandler err = (req->comm_) ? (req->comm_)->errhandler() : MPI_ERRHANDLER_NULL;
1066 if (err == MPI_ERRHANDLER_NULL || err == MPI_ERRORS_RETURN)
1067 XBT_WARN("recv - returned %.*s instead of MPI_SUCCESS", error_size, error_string);
1068 else if (err == MPI_ERRORS_ARE_FATAL)
1069 xbt_die("recv - returned %.*s instead of MPI_SUCCESS", error_size, error_string);
1071 err->call((req->comm_), errkind);
1072 if (err != MPI_ERRHANDLER_NULL)
1073 simgrid::smpi::Errhandler::unref(err);
1074 MC_assert(not MC_is_active()); /* Only fail in MC mode */
1076 if(req->src_ != MPI_PROC_NULL && req->dst_ != MPI_PROC_NULL)
1080 int Request::wait(MPI_Request * request, MPI_Status * status)
1082 // assume that *request is not MPI_REQUEST_NULL (filtered in PMPI_Wait before)
1083 xbt_assert(*request != MPI_REQUEST_NULL);
1085 int ret=MPI_SUCCESS;
1087 if((*request)->src_ == MPI_PROC_NULL || (*request)->dst_ == MPI_PROC_NULL){
1088 if (status != MPI_STATUS_IGNORE) {
1089 Status::empty(status);
1090 status->MPI_SOURCE = MPI_PROC_NULL;
1092 (*request)=MPI_REQUEST_NULL;
1096 (*request)->print_request("Waiting");
1097 if ((*request)->flags_ & (MPI_REQ_PREPARED | MPI_REQ_FINISHED)) {
1098 Status::empty(status);
1102 if ((*request)->action_ != nullptr){
1104 // this is not a detached send
1105 kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
1106 kernel::actor::ActivityWaitSimcall observer{issuer, (*request)->action_.get(), -1};
1107 kernel::actor::simcall_blocking(
1108 [&observer] { observer.get_activity()->wait_for(observer.get_issuer(), observer.get_timeout()); },
1110 } catch (const CancelException&) {
1111 XBT_VERB("Request cancelled");
1115 if ((*request)->flags_ & MPI_REQ_GENERALIZED) {
1116 if (not((*request)->flags_ & MPI_REQ_COMPLETE)) {
1117 ((*request)->generalized_funcs)->mutex->lock();
1118 ((*request)->generalized_funcs)->cond->wait(((*request)->generalized_funcs)->mutex);
1119 ((*request)->generalized_funcs)->mutex->unlock();
1121 MPI_Status tmp_status;
1122 MPI_Status* mystatus;
1123 if (status == MPI_STATUS_IGNORE) {
1124 mystatus = &tmp_status;
1125 Status::empty(mystatus);
1129 ret = ((*request)->generalized_funcs)->query_fn(((*request)->generalized_funcs)->extra_state, mystatus);
1132 if ((*request)->truncated_)
1133 ret = MPI_ERR_TRUNCATE;
1135 if ((*request)->flags_ & MPI_REQ_NBC)
1136 finish_nbc_requests(request, 0);
1138 finish_wait(request, status); // may invalidate *request
1139 if (*request != MPI_REQUEST_NULL && (((*request)->flags_ & MPI_REQ_NON_PERSISTENT) != 0))
1140 *request = MPI_REQUEST_NULL;
1144 int Request::waitany(int count, MPI_Request requests[], MPI_Status * status)
1146 int index = MPI_UNDEFINED;
1149 // Wait for a request to complete
1150 std::vector<simgrid::kernel::activity::ActivityImpl*> comms;
1151 std::vector<int> map;
1152 XBT_DEBUG("Wait for one of %d", count);
1153 for(int i = 0; i < count; i++) {
1154 if (requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & MPI_REQ_PREPARED) &&
1155 not(requests[i]->flags_ & MPI_REQ_FINISHED)) {
1156 if (requests[i]->action_ != nullptr) {
1157 XBT_DEBUG("Waiting any %p ", requests[i]);
1158 comms.push_back(requests[i]->action_.get());
1161 // This is a finished detached request, let's return this one
1162 comms.clear(); // don't do the waitany call afterwards
1164 if (requests[index]->flags_ & MPI_REQ_NBC)
1165 finish_nbc_requests(&requests[index], 0);
1166 finish_wait(&requests[i], status); // cleanup if refcount = 0
1167 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
1168 requests[i] = MPI_REQUEST_NULL; // set to null
1173 if (not comms.empty()) {
1174 XBT_DEBUG("Enter waitany for %zu comms", comms.size());
1177 kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
1178 kernel::actor::ActivityWaitanySimcall observer{issuer, comms, -1};
1179 i = kernel::actor::simcall_blocking(
1181 kernel::activity::ActivityImpl::wait_any_for(observer.get_issuer(), observer.get_activities(),
1182 observer.get_timeout());
1185 } catch (const CancelException&) {
1186 XBT_INFO("request cancelled");
1190 // not MPI_UNDEFINED, as this is a simix return code
1193 //in case of an accumulate, we have to wait the end of all requests to apply the operation, ordered correctly.
1194 if ((requests[index] == MPI_REQUEST_NULL) ||
1195 (not((requests[index]->flags_ & MPI_REQ_ACCUMULATE) && (requests[index]->flags_ & MPI_REQ_RECV)))) {
1196 finish_wait(&requests[index],status);
1197 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_NON_PERSISTENT))
1198 requests[index] = MPI_REQUEST_NULL;
1205 if (index==MPI_UNDEFINED)
1206 Status::empty(status);
1211 static int sort_accumulates(const Request* a, const Request* b)
1213 return (a->tag() > b->tag());
1216 int Request::waitall(int count, MPI_Request requests[], MPI_Status status[])
1218 std::vector<MPI_Request> accumulates;
1221 MPI_Status *pstat = (status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat);
1222 int retvalue = MPI_SUCCESS;
1223 //tag invalid requests in the set
1224 if (status != MPI_STATUSES_IGNORE) {
1225 for (int c = 0; c < count; c++) {
1226 if (requests[c] == MPI_REQUEST_NULL || requests[c]->dst_ == MPI_PROC_NULL ||
1227 (requests[c]->flags_ & MPI_REQ_PREPARED)) {
1228 Status::empty(&status[c]);
1229 } else if (requests[c]->src_ == MPI_PROC_NULL) {
1230 Status::empty(&status[c]);
1231 status[c].MPI_SOURCE = MPI_PROC_NULL;
1235 for (int c = 0; c < count; c++) {
1236 if (MC_is_active() || MC_record_replay_is_active()) {
1237 wait(&requests[c],pstat);
1240 index = waitany(count, requests, pstat);
1242 if (index == MPI_UNDEFINED)
1245 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_RECV) &&
1246 (requests[index]->flags_ & MPI_REQ_ACCUMULATE))
1247 accumulates.push_back(requests[index]);
1248 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_NON_PERSISTENT))
1249 requests[index] = MPI_REQUEST_NULL;
1251 if (status != MPI_STATUSES_IGNORE) {
1252 status[index] = *pstat;
1253 if (status[index].MPI_ERROR == MPI_ERR_TRUNCATE)
1254 retvalue = MPI_ERR_IN_STATUS;
1258 std::sort(accumulates.begin(), accumulates.end(), sort_accumulates);
1259 for (auto& req : accumulates)
1260 finish_wait(&req, status);
1265 int Request::waitsome(int incount, MPI_Request requests[], int *indices, MPI_Status status[])
1271 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
1272 index = waitany(incount, requests, pstat);
1273 if(index==MPI_UNDEFINED) return MPI_UNDEFINED;
1274 if(status != MPI_STATUSES_IGNORE) {
1275 status[count] = *pstat;
1277 indices[count] = index;
1279 for (int i = 0; i < incount; i++) {
1280 if (i != index && requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & MPI_REQ_FINISHED)) {
1281 test(&requests[i], pstat,&flag);
1284 if(status != MPI_STATUSES_IGNORE) {
1285 status[count] = *pstat;
1287 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
1288 requests[i]=MPI_REQUEST_NULL;
1296 MPI_Request Request::f2c(int id)
1298 if(id==MPI_FORTRAN_REQUEST_NULL)
1299 return MPI_REQUEST_NULL;
1300 return static_cast<MPI_Request>(F2C::lookup()->at(id));
1303 void Request::free_f(int id)
1305 if (id != MPI_FORTRAN_REQUEST_NULL) {
1306 F2C::lookup()->erase(id);
1310 int Request::get_status(const Request* req, int* flag, MPI_Status* status)
1314 if(req != MPI_REQUEST_NULL && req->action_ != nullptr) {
1315 req->iprobe(req->comm_->group()->rank(req->src_), req->tag_, req->comm_, flag, status);
1319 if (req != MPI_REQUEST_NULL && (req->flags_ & MPI_REQ_GENERALIZED) && not(req->flags_ & MPI_REQ_COMPLETE)) {
1325 if(req != MPI_REQUEST_NULL &&
1326 status != MPI_STATUS_IGNORE) {
1327 aid_t src = req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_;
1328 status->MPI_SOURCE = req->comm_->group()->rank(src);
1329 status->MPI_TAG = req->tag_ == MPI_ANY_TAG ? req->real_tag_ : req->tag_;
1330 status->MPI_ERROR = req->truncated_ ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
1331 status->count = req->real_size_;
1336 int Request::grequest_start(MPI_Grequest_query_function* query_fn, MPI_Grequest_free_function* free_fn,
1337 MPI_Grequest_cancel_function* cancel_fn, void* extra_state, MPI_Request* request)
1339 *request = new Request();
1340 (*request)->flags_ |= MPI_REQ_GENERALIZED;
1341 (*request)->flags_ |= MPI_REQ_PERSISTENT;
1342 (*request)->refcount_ = 1;
1343 ((*request)->generalized_funcs) = std::make_unique<smpi_mpi_generalized_request_funcs_t>();
1344 ((*request)->generalized_funcs)->query_fn=query_fn;
1345 ((*request)->generalized_funcs)->free_fn=free_fn;
1346 ((*request)->generalized_funcs)->cancel_fn=cancel_fn;
1347 ((*request)->generalized_funcs)->extra_state=extra_state;
1348 ((*request)->generalized_funcs)->cond = simgrid::s4u::ConditionVariable::create();
1349 ((*request)->generalized_funcs)->mutex = simgrid::s4u::Mutex::create();
1353 int Request::grequest_complete(MPI_Request request)
1355 if ((not(request->flags_ & MPI_REQ_GENERALIZED)) || request->generalized_funcs->mutex == nullptr)
1356 return MPI_ERR_REQUEST;
1357 request->generalized_funcs->mutex->lock();
1358 request->flags_ |= MPI_REQ_COMPLETE; // in case wait would be called after complete
1359 request->generalized_funcs->cond->notify_one();
1360 request->generalized_funcs->mutex->unlock();
1364 void Request::start_nbc_requests(std::vector<MPI_Request> reqs){
1365 if (not reqs.empty()) {
1366 nbc_requests_ = reqs;
1367 Request::startall(reqs.size(), reqs.data());
1371 std::vector<MPI_Request> Request::get_nbc_requests() const
1373 return nbc_requests_;