1 /* Copyright (c) 2007-2019. 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. */
5 #include "simgrid/s4u/Mutex.hpp"
6 #include "simgrid/s4u/ConditionVariable.hpp"
7 #include "smpi_request.hpp"
10 #include "private.hpp"
11 #include "simgrid/Exception.hpp"
12 #include "simgrid/s4u/Exec.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/activity/CommImpl.hpp"
18 #include "src/mc/mc_replay.hpp"
19 #include "src/smpi/include/smpi_actor.hpp"
20 #include "xbt/config.hpp"
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_request, smpi, "Logging specific to SMPI (request)");
27 static simgrid::config::Flag<double> smpi_iprobe_sleep(
28 "smpi/iprobe", "Minimum time to inject inside a call to MPI_Iprobe", 1e-4);
29 static simgrid::config::Flag<double> smpi_test_sleep(
30 "smpi/test", "Minimum time to inject inside a call to MPI_Test", 1e-4);
32 std::vector<s_smpi_factor_t> smpi_ois_values;
34 extern void (*smpi_comm_copy_data_callback)(simgrid::kernel::activity::CommImpl*, void*, size_t);
39 Request::Request(const void* buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm, unsigned flags, MPI_Op op)
40 : buf_(const_cast<void*>(buf)), old_type_(datatype), src_(src), dst_(dst), tag_(tag), comm_(comm), flags_(flags), op_(op)
42 void *old_buf = nullptr;
43 // FIXME Handle the case of a partial shared malloc.
44 if ((((flags & MPI_REQ_RECV) != 0) && ((flags & MPI_REQ_ACCUMULATE) != 0)) || (datatype->flags() & DT_FLAG_DERIVED)) {
45 // This part handles the problem of non-contiguous memory
46 old_buf = const_cast<void*>(buf);
50 buf_ = xbt_malloc(count*datatype->size());
51 if ((datatype->flags() & DT_FLAG_DERIVED) && ((flags & MPI_REQ_SEND) != 0)) {
52 datatype->serialize(old_buf, buf_, count);
56 // This part handles the problem of non-contiguous memory (for the unserialisation at the reception)
58 size_ = datatype->size() * count;
61 if(op != MPI_REPLACE && op != MPI_OP_NULL)
65 detached_sender_ = nullptr;
70 if (flags & MPI_REQ_PERSISTENT)
75 generalized_funcs=nullptr;
76 nbc_requests_=nullptr;
84 void Request::unref(MPI_Request* request)
86 if((*request) != MPI_REQUEST_NULL){
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);
95 delete (*request)->generalized_funcs;
97 Comm::unref((*request)->comm_);
98 Datatype::unref((*request)->old_type_);
100 if ((*request)->op_!=MPI_REPLACE && (*request)->op_!=MPI_OP_NULL)
101 Op::unref(&(*request)->op_);
103 (*request)->print_request("Destroying");
105 *request = MPI_REQUEST_NULL;
107 (*request)->print_request("Decrementing");
110 xbt_die("freeing an already free request");
114 int Request::match_recv(void* a, void* b, simgrid::kernel::activity::CommImpl*)
116 MPI_Request ref = static_cast<MPI_Request>(a);
117 MPI_Request req = static_cast<MPI_Request>(b);
118 XBT_DEBUG("Trying to match a recv of src %d against %d, tag %d against %d",ref->src_,req->src_, ref->tag_, req->tag_);
120 xbt_assert(ref, "Cannot match recv against null reference");
121 xbt_assert(req, "Cannot match recv against null request");
122 if(((ref->src_ == MPI_ANY_SOURCE && (ref->comm_->group()->rank(req->src_) != MPI_UNDEFINED)) || req->src_ == ref->src_)
123 && ((ref->tag_ == MPI_ANY_TAG && req->tag_ >=0) || req->tag_ == ref->tag_)){
124 //we match, we can transfer some values
125 if(ref->src_ == MPI_ANY_SOURCE)
126 ref->real_src_ = req->src_;
127 if(ref->tag_ == MPI_ANY_TAG)
128 ref->real_tag_ = req->tag_;
129 if(ref->real_size_ < req->real_size_)
130 ref->truncated_ = true;
132 ref->detached_sender_=req; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
133 if(req->cancelled_==0)
134 req->cancelled_=-1;//mark as uncancellable
135 XBT_DEBUG("match succeeded");
140 int Request::match_send(void* a, void* b, simgrid::kernel::activity::CommImpl*)
142 MPI_Request ref = static_cast<MPI_Request>(a);
143 MPI_Request req = static_cast<MPI_Request>(b);
144 XBT_DEBUG("Trying to match a send of src %d against %d, tag %d against %d",ref->src_,req->src_, ref->tag_, req->tag_);
145 xbt_assert(ref, "Cannot match send against null reference");
146 xbt_assert(req, "Cannot match send against null request");
148 if(((req->src_ == MPI_ANY_SOURCE && (req->comm_->group()->rank(ref->src_) != MPI_UNDEFINED)) || req->src_ == ref->src_)
149 && ((req->tag_ == MPI_ANY_TAG && ref->tag_ >=0)|| req->tag_ == ref->tag_)){
150 if(req->src_ == MPI_ANY_SOURCE)
151 req->real_src_ = ref->src_;
152 if(req->tag_ == MPI_ANY_TAG)
153 req->real_tag_ = ref->tag_;
154 if(req->real_size_ < ref->real_size_)
155 req->truncated_ = true;
157 req->detached_sender_=ref; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
158 if(req->cancelled_==0)
159 req->cancelled_=-1;//mark as uncancellable
160 XBT_DEBUG("match succeeded");
166 void Request::print_request(const char *message)
168 XBT_VERB("%s request %p [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
169 message, this, buf_, size_, src_, dst_, tag_, flags_);
173 /* factories, to hide the internal flags from the caller */
174 MPI_Request Request::send_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
177 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
178 comm->group()->actor(dst)->get_pid(), tag, comm,
179 MPI_REQ_PERSISTENT | MPI_REQ_SEND | MPI_REQ_PREPARED);
182 MPI_Request Request::ssend_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
184 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
185 comm->group()->actor(dst)->get_pid(), tag, comm,
186 MPI_REQ_PERSISTENT | MPI_REQ_SSEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
189 MPI_Request Request::isend_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
191 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
192 comm->group()->actor(dst)->get_pid(), tag, comm,
193 MPI_REQ_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
197 MPI_Request Request::rma_send_init(const void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
200 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
202 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src)->get_pid(),
203 comm->group()->actor(dst)->get_pid(), tag, comm,
204 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
206 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src)->get_pid(),
207 comm->group()->actor(dst)->get_pid(), tag, comm,
208 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED |
209 MPI_REQ_ACCUMULATE, op);
214 MPI_Request Request::recv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
216 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
217 src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : comm->group()->actor(src)->get_pid(),
218 simgrid::s4u::this_actor::get_pid(), tag, comm,
219 MPI_REQ_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
222 MPI_Request Request::rma_recv_init(void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
225 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
227 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src)->get_pid(),
228 comm->group()->actor(dst)->get_pid(), tag, comm,
229 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
231 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src)->get_pid(),
232 comm->group()->actor(dst)->get_pid(), tag, comm,
233 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED | MPI_REQ_ACCUMULATE, op);
238 MPI_Request Request::irecv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
240 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
241 src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : comm->group()->actor(src)->get_pid(),
242 simgrid::s4u::this_actor::get_pid(), tag, comm,
243 MPI_REQ_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
246 MPI_Request Request::isend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
248 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
249 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
250 comm->group()->actor(dst)->get_pid(), tag, comm,
251 MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND);
256 MPI_Request Request::issend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
258 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
259 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
260 comm->group()->actor(dst)->get_pid(), tag, comm,
261 MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SSEND | MPI_REQ_SEND);
267 MPI_Request Request::irecv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
269 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
270 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
271 src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : comm->group()->actor(src)->get_pid(),
272 simgrid::s4u::this_actor::get_pid(), tag, comm, MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV);
277 void Request::recv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status * status)
279 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
280 request = irecv(buf, count, datatype, src, tag, comm);
281 wait(&request,status);
285 void Request::send(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
287 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
288 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
289 comm->group()->actor(dst)->get_pid(), tag, comm, MPI_REQ_NON_PERSISTENT | MPI_REQ_SEND);
292 wait(&request, MPI_STATUS_IGNORE);
296 void Request::ssend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
298 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
299 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
300 comm->group()->actor(dst)->get_pid(), tag, comm,
301 MPI_REQ_NON_PERSISTENT | MPI_REQ_SSEND | MPI_REQ_SEND);
304 wait(&request,MPI_STATUS_IGNORE);
308 void Request::sendrecv(const void *sendbuf, int sendcount, MPI_Datatype sendtype,int dst, int sendtag,
309 void *recvbuf, int recvcount, MPI_Datatype recvtype, int src, int recvtag,
310 MPI_Comm comm, MPI_Status * status)
312 MPI_Request requests[2];
314 int myid = simgrid::s4u::this_actor::get_pid();
315 if ((comm->group()->actor(dst)->get_pid() == myid) && (comm->group()->actor(src)->get_pid() == myid)) {
316 Datatype::copy(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
317 if (status != MPI_STATUS_IGNORE) {
318 status->MPI_SOURCE = src;
319 status->MPI_TAG = recvtag;
320 status->MPI_ERROR = MPI_SUCCESS;
321 status->count = sendcount * sendtype->size();
325 requests[0] = isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
326 requests[1] = irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
327 startall(2, requests);
328 waitall(2, requests, stats);
331 if(status != MPI_STATUS_IGNORE) {
332 // Copy receive status
337 void Request::start()
339 s4u::Mailbox* mailbox;
341 xbt_assert(action_ == nullptr, "Cannot (re-)start unfinished communication");
342 flags_ &= ~MPI_REQ_PREPARED;
343 flags_ &= ~MPI_REQ_FINISHED;
346 if ((flags_ & MPI_REQ_RECV) != 0) {
347 this->print_request("New recv");
349 simgrid::smpi::ActorExt* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(dst_));
351 int async_small_thresh = simgrid::config::get_value<int>("smpi/async-small-thresh");
353 simgrid::s4u::MutexPtr mut = process->mailboxes_mutex();
354 if (async_small_thresh != 0 || (flags_ & MPI_REQ_RMA) != 0)
357 if (async_small_thresh == 0 && (flags_ & MPI_REQ_RMA) == 0) {
358 mailbox = process->mailbox();
359 } else if (((flags_ & MPI_REQ_RMA) != 0) || static_cast<int>(size_) < async_small_thresh) {
360 //We have to check both mailboxes (because SSEND messages are sent to the large mbox).
361 //begin with the more appropriate one : the small one.
362 mailbox = process->mailbox_small();
363 XBT_DEBUG("Is there a corresponding send already posted in the small mailbox %s (in case of SSEND)?",
364 mailbox->get_cname());
365 smx_activity_t action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
367 if (action == nullptr) {
368 mailbox = process->mailbox();
369 XBT_DEBUG("No, nothing in the small mailbox test the other one : %s", mailbox->get_cname());
370 action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
371 if (action == nullptr) {
372 XBT_DEBUG("Still nothing, switch back to the small mailbox : %s", mailbox->get_cname());
373 mailbox = process->mailbox_small();
376 XBT_DEBUG("yes there was something for us in the large mailbox");
379 mailbox = process->mailbox_small();
380 XBT_DEBUG("Is there a corresponding send already posted the small mailbox?");
381 smx_activity_t action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
383 if (action == nullptr) {
384 XBT_DEBUG("No, nothing in the permanent receive mailbox");
385 mailbox = process->mailbox();
387 XBT_DEBUG("yes there was something for us in the small mailbox");
391 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
393 action_ = simcall_comm_irecv(
394 process->get_actor()->get_impl(), mailbox->get_impl(), buf_, &real_size_, &match_recv,
395 process->replaying() ? &smpi_comm_null_copy_buffer_callback : smpi_comm_copy_data_callback, this, -1.0);
396 XBT_DEBUG("recv simcall posted");
398 if (async_small_thresh != 0 || (flags_ & MPI_REQ_RMA) != 0)
400 } else { /* the RECV flag was not set, so this is a send */
401 simgrid::smpi::ActorExt* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(dst_));
403 if (TRACE_smpi_view_internals()) {
404 TRACE_smpi_send(rank, rank, dst_, tag_, size_);
406 this->print_request("New send");
409 if ((flags_ & MPI_REQ_SSEND) == 0 &&
410 ((flags_ & MPI_REQ_RMA) != 0 ||
411 static_cast<int>(size_) < simgrid::config::get_value<int>("smpi/send-is-detached-thresh"))) {
412 void *oldbuf = nullptr;
414 XBT_DEBUG("Send request %p is detached", this);
416 if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
418 if (not process->replaying() && oldbuf != nullptr && size_ != 0) {
419 if ((smpi_privatize_global_variables != SmpiPrivStrategies::NONE) &&
420 (static_cast<char*>(buf_) >= smpi_data_exe_start) &&
421 (static_cast<char*>(buf_) < smpi_data_exe_start + smpi_data_exe_size)) {
422 XBT_DEBUG("Privatization : We are sending from a zone inside global memory. Switch data segment ");
423 smpi_switch_data_segment(simgrid::s4u::Actor::by_pid(src_));
425 buf = xbt_malloc(size_);
426 memcpy(buf,oldbuf,size_);
427 XBT_DEBUG("buf %p copied into %p",oldbuf,buf);
432 //if we are giving back the control to the user without waiting for completion, we have to inject timings
433 double sleeptime = 0.0;
434 if (detached_ || ((flags_ & (MPI_REQ_ISEND | MPI_REQ_SSEND)) != 0)) { // issend should be treated as isend
435 // isend and send timings may be different
436 sleeptime = ((flags_ & MPI_REQ_ISEND) != 0)
437 ? simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->oisend(size_)
438 : simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->osend(size_);
442 simcall_process_sleep(sleeptime);
443 XBT_DEBUG("sending size of %zu : sleep %f ", size_, sleeptime);
446 int async_small_thresh = simgrid::config::get_value<int>("smpi/async-small-thresh");
448 simgrid::s4u::MutexPtr mut = process->mailboxes_mutex();
450 if (async_small_thresh != 0 || (flags_ & MPI_REQ_RMA) != 0)
453 if (not(async_small_thresh != 0 || (flags_ & MPI_REQ_RMA) != 0)) {
454 mailbox = process->mailbox();
455 } else if (((flags_ & MPI_REQ_RMA) != 0) || static_cast<int>(size_) < async_small_thresh) { // eager mode
456 mailbox = process->mailbox();
457 XBT_DEBUG("Is there a corresponding recv already posted in the large mailbox %s?", mailbox->get_cname());
458 smx_activity_t action = mailbox->iprobe(1, &match_send, static_cast<void*>(this));
459 if (action == nullptr) {
460 if ((flags_ & MPI_REQ_SSEND) == 0) {
461 mailbox = process->mailbox_small();
462 XBT_DEBUG("No, nothing in the large mailbox, message is to be sent on the small one %s",
463 mailbox->get_cname());
465 mailbox = process->mailbox_small();
466 XBT_DEBUG("SSEND : Is there a corresponding recv already posted in the small mailbox %s?",
467 mailbox->get_cname());
468 action = mailbox->iprobe(1, &match_send, static_cast<void*>(this));
469 if (action == nullptr) {
470 XBT_DEBUG("No, we are first, send to large mailbox");
471 mailbox = process->mailbox();
475 XBT_DEBUG("Yes there was something for us in the large mailbox");
478 mailbox = process->mailbox();
479 XBT_DEBUG("Send request %p is in the large mailbox %s (buf: %p)", this, mailbox->get_cname(), buf_);
482 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
484 action_ = simcall_comm_isend(
485 simgrid::s4u::Actor::by_pid(src_)->get_impl(), mailbox->get_impl(), size_, -1.0, buf, real_size_, &match_send,
486 &xbt_free_f, // how to free the userdata if a detached send fails
487 not process->replaying() ? smpi_comm_copy_data_callback : &smpi_comm_null_copy_buffer_callback, this,
488 // detach if msg size < eager/rdv switch limit
490 XBT_DEBUG("send simcall posted");
492 /* FIXME: detached sends are not traceable (action_ == nullptr) */
493 if (action_ != nullptr) {
494 boost::static_pointer_cast<kernel::activity::CommImpl>(action_)->set_tracing_category(
495 smpi_process()->get_tracing_category());
498 if (async_small_thresh != 0 || ((flags_ & MPI_REQ_RMA) != 0))
503 void Request::startall(int count, MPI_Request * requests)
505 if(requests== nullptr)
508 for(int i = 0; i < count; i++) {
509 requests[i]->start();
513 void Request::cancel()
517 if (this->action_ != nullptr)
518 (boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(this->action_))->cancel();
521 int Request::test(MPI_Request * request, MPI_Status * status, int* flag) {
522 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or testall before)
523 // to avoid deadlocks if used as a break condition, such as
524 // while (MPI_Test(request, flag, status) && flag) dostuff...
525 // because the time will not normally advance when only calls to MPI_Test are made -> deadlock
526 // multiplier to the sleeptime, to increase speed of execution, each failed test will increase it
527 static int nsleeps = 1;
528 int ret = MPI_SUCCESS;
530 // Are we testing a request meant for non blocking collectives ?
531 // If so, test all the subrequests.
532 if ((*request)->nbc_requests_size_>0){
533 ret = testall((*request)->nbc_requests_size_, (*request)->nbc_requests_, flag, MPI_STATUSES_IGNORE);
535 delete[] (*request)->nbc_requests_;
536 (*request)->nbc_requests_size_=0;
542 if(smpi_test_sleep > 0)
543 simcall_process_sleep(nsleeps*smpi_test_sleep);
545 Status::empty(status);
547 if (((*request)->flags_ & MPI_REQ_PREPARED) == 0) {
548 if ((*request)->action_ != nullptr){
550 *flag = simcall_comm_test((*request)->action_);
551 } catch (const xbt_ex&) {
556 if (*request != MPI_REQUEST_NULL &&
557 ((*request)->flags_ & MPI_REQ_GENERALIZED)
558 && !((*request)->flags_ & MPI_REQ_COMPLETE))
561 finish_wait(request,status);
562 if (*request != MPI_REQUEST_NULL && ((*request)->flags_ & MPI_REQ_GENERALIZED)){
563 MPI_Status* mystatus;
564 if(status==MPI_STATUS_IGNORE){
565 mystatus=new MPI_Status();
566 Status::empty(mystatus);
570 ret = ((*request)->generalized_funcs)->query_fn(((*request)->generalized_funcs)->extra_state, mystatus);
571 if(status==MPI_STATUS_IGNORE)
574 nsleeps=1;//reset the number of sleeps we will do next time
575 if (*request != MPI_REQUEST_NULL && ((*request)->flags_ & MPI_REQ_PERSISTENT) == 0)
576 *request = MPI_REQUEST_NULL;
577 } else if (simgrid::config::get_value<bool>("smpi/grow-injected-times")) {
584 int Request::testsome(int incount, MPI_Request requests[], int *count, int *indices, MPI_Status status[])
586 int ret = MPI_SUCCESS;
591 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
594 for (int i = 0; i < incount; i++) {
595 if (requests[i] != MPI_REQUEST_NULL) {
596 ret = test(&requests[i], pstat, &flag);
602 if (status != MPI_STATUSES_IGNORE)
604 if ((requests[i] != MPI_REQUEST_NULL) && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
605 requests[i] = MPI_REQUEST_NULL;
611 if(count_dead==incount)*count=MPI_UNDEFINED;
613 return MPI_ERR_IN_STATUS;
618 int Request::testany(int count, MPI_Request requests[], int *index, int* flag, MPI_Status * status)
620 std::vector<simgrid::kernel::activity::CommImpl*> comms;
621 comms.reserve(count);
625 int ret = MPI_SUCCESS;
626 *index = MPI_UNDEFINED;
628 std::vector<int> map; /** Maps all matching comms back to their location in requests **/
629 for(i = 0; i < count; i++) {
630 if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->action_ && not(requests[i]->flags_ & MPI_REQ_PREPARED)) {
631 comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(requests[i]->action_.get()));
635 if (not map.empty()) {
636 //multiplier to the sleeptime, to increase speed of execution, each failed testany will increase it
637 static int nsleeps = 1;
638 if(smpi_test_sleep > 0)
639 simcall_process_sleep(nsleeps*smpi_test_sleep);
641 i = simcall_comm_testany(comms.data(), comms.size()); // The i-th element in comms matches!
642 } catch (const xbt_ex&) {
643 XBT_DEBUG("Exception in testany");
647 if (i != -1) { // -1 is not MPI_UNDEFINED but a SIMIX return code. (nothing matches)
649 if (requests[*index] != MPI_REQUEST_NULL &&
650 (requests[*index]->flags_ & MPI_REQ_GENERALIZED)
651 && !(requests[*index]->flags_ & MPI_REQ_COMPLETE)) {
654 finish_wait(&requests[*index],status);
655 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags_ & MPI_REQ_GENERALIZED)){
656 MPI_Status* mystatus;
657 if(status==MPI_STATUS_IGNORE){
658 mystatus=new MPI_Status();
659 Status::empty(mystatus);
663 ret=(requests[*index]->generalized_funcs)->query_fn((requests[*index]->generalized_funcs)->extra_state, mystatus);
664 if(status==MPI_STATUS_IGNORE)
668 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags_ & MPI_REQ_NON_PERSISTENT))
669 requests[*index] = MPI_REQUEST_NULL;
670 XBT_DEBUG("Testany - returning with index %d", *index);
678 XBT_DEBUG("Testany on inactive handles, returning flag=1 but empty status");
679 //all requests are null or inactive, return true
681 *index = MPI_UNDEFINED;
682 Status::empty(status);
688 int Request::testall(int count, MPI_Request requests[], int* outflag, MPI_Status status[])
691 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
696 for(int i=0; i<count; i++){
697 if (requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & MPI_REQ_PREPARED)) {
698 ret = test(&requests[i], pstat, &flag);
701 requests[i]=MPI_REQUEST_NULL;
705 if (ret != MPI_SUCCESS)
708 Status::empty(pstat);
710 if(status != MPI_STATUSES_IGNORE) {
715 return MPI_ERR_IN_STATUS;
720 void Request::probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
722 //FIXME find another way to avoid busy waiting ?
723 // the issue here is that we have to wait on a nonexistent comm
725 iprobe(source, tag, comm, &flag, status);
726 XBT_DEBUG("Busy Waiting on probing : %d", flag);
730 void Request::iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
731 // to avoid deadlock, we have to sleep some time here, or the timer won't advance and we will only do iprobe simcalls
732 // especially when used as a break condition, such as while (MPI_Iprobe(...)) dostuff...
733 // nsleeps is a multiplier to the sleeptime, to increase speed of execution, each failed iprobe will increase it
734 // This can speed up the execution of certain applications by an order of magnitude, such as HPL
735 static int nsleeps = 1;
736 double speed = s4u::this_actor::get_host()->get_speed();
737 double maxrate = simgrid::config::get_value<double>("smpi/iprobe-cpu-usage");
738 MPI_Request request = new Request(nullptr, 0, MPI_CHAR,
739 source == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : comm->group()->actor(source)->get_pid(),
740 simgrid::s4u::this_actor::get_pid(), tag, comm, MPI_REQ_PERSISTENT | MPI_REQ_RECV);
741 if (smpi_iprobe_sleep > 0) {
742 /** Compute the number of flops we will sleep **/
743 s4u::this_actor::exec_init(/*nsleeps: See comment above */ nsleeps *
744 /*(seconds * flop/s -> total flops)*/ smpi_iprobe_sleep * speed * maxrate)
746 /* Not the entire CPU can be used when iprobing: This is important for
747 * the energy consumption caused by polling with iprobes.
748 * Note also that the number of flops that was
749 * computed above contains a maxrate factor and is hence reduced (maxrate < 1)
751 ->set_bound(maxrate*speed)
755 // behave like a receive, but don't do it
756 s4u::Mailbox* mailbox;
758 request->print_request("New iprobe");
759 // We have to test both mailboxes as we don't know if we will receive one one or another
760 if (simgrid::config::get_value<int>("smpi/async-small-thresh") > 0) {
761 mailbox = smpi_process()->mailbox_small();
762 XBT_DEBUG("Trying to probe the perm recv mailbox");
763 request->action_ = mailbox->iprobe(0, &match_recv, static_cast<void*>(request));
766 if (request->action_ == nullptr){
767 mailbox = smpi_process()->mailbox();
768 XBT_DEBUG("trying to probe the other mailbox");
769 request->action_ = mailbox->iprobe(0, &match_recv, static_cast<void*>(request));
772 if (request->action_ != nullptr){
773 kernel::activity::CommImplPtr sync_comm = boost::static_pointer_cast<kernel::activity::CommImpl>(request->action_);
774 MPI_Request req = static_cast<MPI_Request>(sync_comm->src_data_);
776 if (status != MPI_STATUS_IGNORE && (req->flags_ & MPI_REQ_PREPARED) == 0) {
777 status->MPI_SOURCE = comm->group()->rank(req->src_);
778 status->MPI_TAG = req->tag_;
779 status->MPI_ERROR = MPI_SUCCESS;
780 status->count = req->real_size_;
782 nsleeps = 1;//reset the number of sleeps we will do next time
786 if (simgrid::config::get_value<bool>("smpi/grow-injected-times"))
790 xbt_assert(request == MPI_REQUEST_NULL);
793 void Request::finish_wait(MPI_Request* request, MPI_Status * status)
795 MPI_Request req = *request;
796 Status::empty(status);
798 if (req->cancelled_==1){
799 if (status!=MPI_STATUS_IGNORE)
801 if(req->detached_sender_ != nullptr)
802 unref(&(req->detached_sender_));
807 if (not(req->detached_ && ((req->flags_ & MPI_REQ_SEND) != 0)) && ((req->flags_ & MPI_REQ_PREPARED) == 0) &&
808 ((req->flags_ & MPI_REQ_GENERALIZED) == 0)) {
809 if(status != MPI_STATUS_IGNORE) {
810 int src = req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_;
811 status->MPI_SOURCE = req->comm_->group()->rank(src);
812 status->MPI_TAG = req->tag_ == MPI_ANY_TAG ? req->real_tag_ : req->tag_;
813 status->MPI_ERROR = req->truncated_ ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
814 // this handles the case were size in receive differs from size in send
815 status->count = req->real_size_;
818 req->print_request("Finishing");
819 MPI_Datatype datatype = req->old_type_;
821 // FIXME Handle the case of a partial shared malloc.
822 if (((req->flags_ & MPI_REQ_ACCUMULATE) != 0) ||
823 (datatype->flags() & DT_FLAG_DERIVED)) { // && (not smpi_is_shared(req->old_buf_))){
825 if (not smpi_process()->replaying() && smpi_privatize_global_variables != SmpiPrivStrategies::NONE &&
826 static_cast<char*>(req->old_buf_) >= smpi_data_exe_start &&
827 static_cast<char*>(req->old_buf_) < smpi_data_exe_start + smpi_data_exe_size) {
828 XBT_VERB("Privatization : We are unserializing to a zone in global memory Switch data segment ");
829 smpi_switch_data_segment(simgrid::s4u::Actor::self());
832 if(datatype->flags() & DT_FLAG_DERIVED){
833 // This part handles the problem of non-contignous memory the unserialization at the reception
834 if ((req->flags_ & MPI_REQ_RECV) && datatype->size() != 0)
835 datatype->unserialize(req->buf_, req->old_buf_, req->real_size_/datatype->size() , req->op_);
837 } else if (req->flags_ & MPI_REQ_RECV) { // apply op on contiguous buffer for accumulate
838 if (datatype->size() != 0) {
839 int n = req->real_size_ / datatype->size();
840 req->op_->apply(req->buf_, req->old_buf_, &n, datatype);
847 if (TRACE_smpi_view_internals() && ((req->flags_ & MPI_REQ_RECV) != 0)) {
848 int rank = simgrid::s4u::this_actor::get_pid();
849 int src_traced = (req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_);
850 TRACE_smpi_recv(src_traced, rank,req->tag_);
852 if(req->detached_sender_ != nullptr){
853 //integrate pseudo-timing for buffering of small messages, do not bother to execute the simcall if 0
855 simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->orecv(req->real_size());
857 simcall_process_sleep(sleeptime);
858 XBT_DEBUG("receiving size of %zu : sleep %f ", req->real_size_, sleeptime);
860 unref(&(req->detached_sender_));
862 if (req->flags_ & MPI_REQ_PERSISTENT)
863 req->action_ = nullptr;
864 req->flags_ |= MPI_REQ_FINISHED;
868 int Request::wait(MPI_Request * request, MPI_Status * status)
871 // Are we waiting on a request meant for non blocking collectives ?
872 // If so, wait for all the subrequests.
873 if ((*request)->nbc_requests_size_>0){
874 ret = waitall((*request)->nbc_requests_size_, (*request)->nbc_requests_, MPI_STATUSES_IGNORE);
875 for (int i = 0; i < (*request)->nbc_requests_size_; i++) {
876 if((*request)->buf_!=nullptr && (*request)->nbc_requests_[i]!=MPI_REQUEST_NULL){//reduce case
877 void * buf=(*request)->nbc_requests_[i]->buf_;
878 if((*request)->old_type_->flags() & DT_FLAG_DERIVED)
879 buf=(*request)->nbc_requests_[i]->old_buf_;
880 if((*request)->nbc_requests_[i]->flags_ & MPI_REQ_RECV ){
881 if((*request)->op_!=MPI_OP_NULL){
882 int count=(*request)->size_/ (*request)->old_type_->size();
883 (*request)->op_->apply(buf, (*request)->buf_, &count, (*request)->old_type_);
885 smpi_free_tmp_buffer(static_cast<unsigned char*>(buf));
888 if((*request)->nbc_requests_[i]!=MPI_REQUEST_NULL)
889 Request::unref(&((*request)->nbc_requests_[i]));
891 delete[] (*request)->nbc_requests_;
892 (*request)->nbc_requests_size_=0;
894 (*request)=MPI_REQUEST_NULL;
898 (*request)->print_request("Waiting");
899 if ((*request)->flags_ & MPI_REQ_PREPARED) {
900 Status::empty(status);
904 if ((*request)->action_ != nullptr){
906 // this is not a detached send
907 simcall_comm_wait((*request)->action_, -1.0);
908 } catch (const xbt_ex&) {
909 XBT_VERB("Request cancelled");
913 if (*request != MPI_REQUEST_NULL && ((*request)->flags_ & MPI_REQ_GENERALIZED)){
914 MPI_Status* mystatus;
915 if(!((*request)->flags_ & MPI_REQ_COMPLETE)){
916 ((*request)->generalized_funcs)->mutex->lock();
917 ((*request)->generalized_funcs)->cond->wait(((*request)->generalized_funcs)->mutex);
918 ((*request)->generalized_funcs)->mutex->unlock();
920 if(status==MPI_STATUS_IGNORE){
921 mystatus=new MPI_Status();
922 Status::empty(mystatus);
926 ret = ((*request)->generalized_funcs)->query_fn(((*request)->generalized_funcs)->extra_state, mystatus);
927 if(status==MPI_STATUS_IGNORE)
931 finish_wait(request,status);
932 if (*request != MPI_REQUEST_NULL && (((*request)->flags_ & MPI_REQ_NON_PERSISTENT) != 0))
933 *request = MPI_REQUEST_NULL;
937 int Request::waitany(int count, MPI_Request requests[], MPI_Status * status)
939 std::vector<simgrid::kernel::activity::CommImpl*> comms;
940 comms.reserve(count);
941 int index = MPI_UNDEFINED;
944 // Wait for a request to complete
945 std::vector<int> map;
946 XBT_DEBUG("Wait for one of %d", count);
947 for(int i = 0; i < count; i++) {
948 if (requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & MPI_REQ_PREPARED) &&
949 not(requests[i]->flags_ & MPI_REQ_FINISHED)) {
950 if (requests[i]->action_ != nullptr) {
951 XBT_DEBUG("Waiting any %p ", requests[i]);
952 comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(requests[i]->action_.get()));
955 // This is a finished detached request, let's return this one
956 comms.clear(); // so we free don't do the waitany call
958 finish_wait(&requests[i], status); // cleanup if refcount = 0
959 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
960 requests[i] = MPI_REQUEST_NULL; // set to null
965 if (not comms.empty()) {
966 XBT_DEBUG("Enter waitany for %zu comms", comms.size());
969 // this is not a detached send
970 i = simcall_comm_waitany(comms.data(), comms.size(), -1);
971 } catch (const xbt_ex&) {
972 XBT_INFO("request %d cancelled ", i);
976 // not MPI_UNDEFINED, as this is a simix return code
979 //in case of an accumulate, we have to wait the end of all requests to apply the operation, ordered correctly.
980 if ((requests[index] == MPI_REQUEST_NULL) ||
981 (not((requests[index]->flags_ & MPI_REQ_ACCUMULATE) && (requests[index]->flags_ & MPI_REQ_RECV)))) {
982 finish_wait(&requests[index],status);
983 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_NON_PERSISTENT))
984 requests[index] = MPI_REQUEST_NULL;
990 if (index==MPI_UNDEFINED)
991 Status::empty(status);
996 static int sort_accumulates(MPI_Request a, MPI_Request b)
998 return (a->tag() > b->tag());
1001 int Request::waitall(int count, MPI_Request requests[], MPI_Status status[])
1003 std::vector<MPI_Request> accumulates;
1006 MPI_Status *pstat = (status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat);
1007 int retvalue = MPI_SUCCESS;
1008 //tag invalid requests in the set
1009 if (status != MPI_STATUSES_IGNORE) {
1010 for (int c = 0; c < count; c++) {
1011 if (requests[c] == MPI_REQUEST_NULL || requests[c]->dst_ == MPI_PROC_NULL ||
1012 (requests[c]->flags_ & MPI_REQ_PREPARED)) {
1013 Status::empty(&status[c]);
1014 } else if (requests[c]->src_ == MPI_PROC_NULL) {
1015 Status::empty(&status[c]);
1016 status[c].MPI_SOURCE = MPI_PROC_NULL;
1020 for (int c = 0; c < count; c++) {
1021 if (MC_is_active() || MC_record_replay_is_active()) {
1022 wait(&requests[c],pstat);
1025 index = waitany(count, (MPI_Request*)requests, pstat);
1027 if (index == MPI_UNDEFINED)
1030 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_RECV) &&
1031 (requests[index]->flags_ & MPI_REQ_ACCUMULATE))
1032 accumulates.push_back(requests[index]);
1033 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_NON_PERSISTENT))
1034 requests[index] = MPI_REQUEST_NULL;
1036 if (status != MPI_STATUSES_IGNORE) {
1037 status[index] = *pstat;
1038 if (status[index].MPI_ERROR == MPI_ERR_TRUNCATE)
1039 retvalue = MPI_ERR_IN_STATUS;
1043 if (not accumulates.empty()) {
1044 std::sort(accumulates.begin(), accumulates.end(), sort_accumulates);
1045 for (auto& req : accumulates) {
1046 finish_wait(&req, status);
1053 int Request::waitsome(int incount, MPI_Request requests[], int *indices, MPI_Status status[])
1059 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
1061 index = waitany(incount, (MPI_Request*)requests, pstat);
1062 if(index==MPI_UNDEFINED) return MPI_UNDEFINED;
1063 if(status != MPI_STATUSES_IGNORE) {
1064 status[count] = *pstat;
1066 indices[count] = index;
1068 for (int i = 0; i < incount; i++) {
1069 if (requests[i] != MPI_REQUEST_NULL) {
1070 test(&requests[i], pstat,&flag);
1073 if(status != MPI_STATUSES_IGNORE) {
1074 status[count] = *pstat;
1076 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
1077 requests[i]=MPI_REQUEST_NULL;
1085 MPI_Request Request::f2c(int id) {
1087 if(id==MPI_FORTRAN_REQUEST_NULL)
1088 return static_cast<MPI_Request>(MPI_REQUEST_NULL);
1089 return static_cast<MPI_Request>(F2C::f2c_lookup()->at(get_key_id(key, id)));
1092 int Request::add_f()
1094 if (F2C::f2c_lookup() == nullptr) {
1095 F2C::set_f2c_lookup(new std::unordered_map<std::string, F2C*>);
1098 (*(F2C::f2c_lookup()))[get_key_id(key, F2C::f2c_id())] = this;
1099 F2C::f2c_id_increment();
1100 return F2C::f2c_id()-1;
1103 void Request::free_f(int id)
1105 if (id != MPI_FORTRAN_REQUEST_NULL) {
1107 F2C::f2c_lookup()->erase(get_key_id(key, id));
1112 int Request::get_status(MPI_Request req, int* flag, MPI_Status * status){
1115 if(req != MPI_REQUEST_NULL && req->action_ != nullptr) {
1116 req->iprobe(req->src_, req->tag_, req->comm_, flag, status);
1120 if (req != MPI_REQUEST_NULL &&
1121 (req->flags_ & MPI_REQ_GENERALIZED)
1122 && !(req->flags_ & MPI_REQ_COMPLETE)) {
1128 if(req != MPI_REQUEST_NULL &&
1129 status != MPI_STATUS_IGNORE) {
1130 int src = req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_;
1131 status->MPI_SOURCE = req->comm_->group()->rank(src);
1132 status->MPI_TAG = req->tag_ == MPI_ANY_TAG ? req->real_tag_ : req->tag_;
1133 status->MPI_ERROR = req->truncated_ ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
1134 status->count = req->real_size_;
1139 int Request::grequest_start( MPI_Grequest_query_function *query_fn, MPI_Grequest_free_function *free_fn, MPI_Grequest_cancel_function *cancel_fn, void *extra_state, MPI_Request *request){
1141 *request = new Request();
1142 (*request)->flags_ |= MPI_REQ_GENERALIZED;
1143 (*request)->flags_ |= MPI_REQ_PERSISTENT;
1144 (*request)->refcount_ = 1;
1145 ((*request)->generalized_funcs) = new s_smpi_mpi_generalized_request_funcs_t;
1146 ((*request)->generalized_funcs)->query_fn=query_fn;
1147 ((*request)->generalized_funcs)->free_fn=free_fn;
1148 ((*request)->generalized_funcs)->cancel_fn=cancel_fn;
1149 ((*request)->generalized_funcs)->extra_state=extra_state;
1150 ((*request)->generalized_funcs)->cond = simgrid::s4u::ConditionVariable::create();
1151 ((*request)->generalized_funcs)->mutex = simgrid::s4u::Mutex::create();
1155 int Request::grequest_complete( MPI_Request request){
1156 if ((!(request->flags_ & MPI_REQ_GENERALIZED)) || request->generalized_funcs->mutex==NULL)
1157 return MPI_ERR_REQUEST;
1158 request->generalized_funcs->mutex->lock();
1159 request->flags_ |= MPI_REQ_COMPLETE; // in case wait would be called after complete
1160 request->generalized_funcs->cond->notify_one();
1161 request->generalized_funcs->mutex->unlock();
1165 void Request::set_nbc_requests(MPI_Request* reqs, int size){
1166 nbc_requests_size_ = size;
1168 nbc_requests_ = reqs;
1171 nbc_requests_ = nullptr;
1175 int Request::get_nbc_requests_size(){
1176 return nbc_requests_size_;
1179 MPI_Request* Request::get_nbc_requests(){
1180 return nbc_requests_;