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. */
6 #include "smpi_request.hpp"
10 #include "simgrid/Exception.hpp"
11 #include "simgrid/s4u/Exec.hpp"
12 #include "smpi_comm.hpp"
13 #include "smpi_datatype.hpp"
14 #include "smpi_host.hpp"
15 #include "smpi_op.hpp"
16 #include "src/kernel/activity/CommImpl.hpp"
17 #include "src/mc/mc_replay.hpp"
18 #include "src/simix/ActorImpl.hpp"
19 #include "src/smpi/include/smpi_actor.hpp"
20 #include "xbt/config.hpp"
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_request, smpi, "Logging specific to SMPI (request)");
26 static simgrid::config::Flag<double> smpi_iprobe_sleep(
27 "smpi/iprobe", "Minimum time to inject inside a call to MPI_Iprobe", 1e-4);
28 static simgrid::config::Flag<double> smpi_test_sleep(
29 "smpi/test", "Minimum time to inject inside a call to MPI_Test", 1e-4);
31 std::vector<s_smpi_factor_t> smpi_ois_values;
33 extern void (*smpi_comm_copy_data_callback) (smx_activity_t, void*, size_t);
38 Request::Request(void* buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm, unsigned flags)
39 : buf_(buf), old_type_(datatype), src_(src), dst_(dst), tag_(tag), comm_(comm), flags_(flags)
41 void *old_buf = nullptr;
42 // FIXME Handle the case of a partial shared malloc.
43 if ((((flags & MPI_REQ_RECV) != 0) && ((flags & MPI_REQ_ACCUMULATE) != 0)) || (datatype->flags() & DT_FLAG_DERIVED)) {
44 // This part handles the problem of non-contiguous memory
49 buf_ = xbt_malloc(count*datatype->size());
50 if ((datatype->flags() & DT_FLAG_DERIVED) && ((flags & MPI_REQ_SEND) != 0)) {
51 datatype->serialize(old_buf, buf_, count);
55 // This part handles the problem of non-contiguous memory (for the unserialisation at the reception)
57 size_ = datatype->size() * count;
62 detached_sender_ = nullptr;
67 if (flags & MPI_REQ_PERSISTENT)
75 MPI_Comm Request::comm(){
95 int Request::detached(){
99 MPI_Datatype Request::type(){
103 size_t Request::size(){
107 size_t Request::real_size(){
115 void Request::unref(MPI_Request* request)
117 if((*request) != MPI_REQUEST_NULL){
118 (*request)->refcount_--;
119 if((*request)->refcount_ < 0) {
120 (*request)->print_request("wrong refcount");
121 xbt_die("Whoops, wrong refcount");
123 if((*request)->refcount_==0){
124 Datatype::unref((*request)->old_type_);
125 Comm::unref((*request)->comm_);
126 (*request)->print_request("Destroying");
128 *request = MPI_REQUEST_NULL;
130 (*request)->print_request("Decrementing");
133 xbt_die("freeing an already free request");
137 int Request::match_recv(void* a, void* b, simgrid::kernel::activity::CommImpl*)
139 MPI_Request ref = static_cast<MPI_Request>(a);
140 MPI_Request req = static_cast<MPI_Request>(b);
141 XBT_DEBUG("Trying to match a recv of src %d against %d, tag %d against %d",ref->src_,req->src_, ref->tag_, req->tag_);
143 xbt_assert(ref, "Cannot match recv against null reference");
144 xbt_assert(req, "Cannot match recv against null request");
145 if((ref->src_ == MPI_ANY_SOURCE || req->src_ == ref->src_)
146 && ((ref->tag_ == MPI_ANY_TAG && req->tag_ >=0) || req->tag_ == ref->tag_)){
147 //we match, we can transfer some values
148 if(ref->src_ == MPI_ANY_SOURCE)
149 ref->real_src_ = req->src_;
150 if(ref->tag_ == MPI_ANY_TAG)
151 ref->real_tag_ = req->tag_;
152 if(ref->real_size_ < req->real_size_)
154 if(req->detached_==1)
155 ref->detached_sender_=req; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
156 if(req->cancelled_==0)
157 req->cancelled_=-1;//mark as uncancellable
158 XBT_DEBUG("match succeeded");
163 int Request::match_send(void* a, void* b, simgrid::kernel::activity::CommImpl*)
165 MPI_Request ref = static_cast<MPI_Request>(a);
166 MPI_Request req = static_cast<MPI_Request>(b);
167 XBT_DEBUG("Trying to match a send of src %d against %d, tag %d against %d",ref->src_,req->src_, ref->tag_, req->tag_);
168 xbt_assert(ref, "Cannot match send against null reference");
169 xbt_assert(req, "Cannot match send against null request");
171 if((req->src_ == MPI_ANY_SOURCE || req->src_ == ref->src_)
172 && ((req->tag_ == MPI_ANY_TAG && ref->tag_ >=0)|| req->tag_ == ref->tag_)){
173 if(req->src_ == MPI_ANY_SOURCE)
174 req->real_src_ = ref->src_;
175 if(req->tag_ == MPI_ANY_TAG)
176 req->real_tag_ = ref->tag_;
177 if(req->real_size_ < ref->real_size_)
179 if(ref->detached_==1)
180 req->detached_sender_=ref; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
181 if(req->cancelled_==0)
182 req->cancelled_=-1;//mark as uncancellable
183 XBT_DEBUG("match succeeded");
189 void Request::print_request(const char *message)
191 XBT_VERB("%s request %p [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
192 message, this, buf_, size_, src_, dst_, tag_, flags_);
196 /* factories, to hide the internal flags from the caller */
197 MPI_Request Request::send_init(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
200 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
201 comm->group()->actor(dst)->get_pid(), tag, comm,
202 MPI_REQ_PERSISTENT | MPI_REQ_SEND | MPI_REQ_PREPARED);
205 MPI_Request Request::ssend_init(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
207 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
208 comm->group()->actor(dst)->get_pid(), tag, comm,
209 MPI_REQ_PERSISTENT | MPI_REQ_SSEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
212 MPI_Request Request::isend_init(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
214 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
215 comm->group()->actor(dst)->get_pid(), tag, comm,
216 MPI_REQ_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
220 MPI_Request Request::rma_send_init(void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
223 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
225 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src)->get_pid(),
226 comm->group()->actor(dst)->get_pid(), tag, comm,
227 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
229 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src)->get_pid(),
230 comm->group()->actor(dst)->get_pid(), tag, comm,
231 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED |
238 MPI_Request Request::recv_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::rma_recv_init(void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
249 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
251 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src)->get_pid(),
252 comm->group()->actor(dst)->get_pid(), tag, comm,
253 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
255 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src)->get_pid(),
256 comm->group()->actor(dst)->get_pid(), tag, comm,
257 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED | MPI_REQ_ACCUMULATE);
263 MPI_Request Request::irecv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
265 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
266 src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : comm->group()->actor(src)->get_pid(),
267 simgrid::s4u::this_actor::get_pid(), tag, comm,
268 MPI_REQ_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
271 MPI_Request Request::isend(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
273 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
274 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
275 comm->group()->actor(dst)->get_pid(), tag, comm,
276 MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND);
281 MPI_Request Request::issend(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
283 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
284 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
285 comm->group()->actor(dst)->get_pid(), tag, comm,
286 MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SSEND | MPI_REQ_SEND);
292 MPI_Request Request::irecv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
294 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
295 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
296 src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : comm->group()->actor(src)->get_pid(),
297 simgrid::s4u::this_actor::get_pid(), tag, comm, MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV);
302 void Request::recv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status * status)
304 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
305 request = irecv(buf, count, datatype, src, tag, comm);
306 wait(&request,status);
310 void Request::send(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
312 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
313 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
314 comm->group()->actor(dst)->get_pid(), tag, comm, MPI_REQ_NON_PERSISTENT | MPI_REQ_SEND);
317 wait(&request, MPI_STATUS_IGNORE);
321 void Request::ssend(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
323 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
324 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
325 comm->group()->actor(dst)->get_pid(), tag, comm,
326 MPI_REQ_NON_PERSISTENT | MPI_REQ_SSEND | MPI_REQ_SEND);
329 wait(&request,MPI_STATUS_IGNORE);
333 void Request::sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,int dst, int sendtag,
334 void *recvbuf, int recvcount, MPI_Datatype recvtype, int src, int recvtag,
335 MPI_Comm comm, MPI_Status * status)
337 MPI_Request requests[2];
339 int myid = simgrid::s4u::this_actor::get_pid();
340 if ((comm->group()->actor(dst)->get_pid() == myid) && (comm->group()->actor(src)->get_pid() == myid)) {
341 Datatype::copy(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
342 if (status != MPI_STATUS_IGNORE) {
343 status->MPI_SOURCE = src;
344 status->MPI_TAG = recvtag;
345 status->MPI_ERROR = MPI_SUCCESS;
346 status->count = sendcount * sendtype->size();
350 requests[0] = isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
351 requests[1] = irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
352 startall(2, requests);
353 waitall(2, requests, stats);
356 if(status != MPI_STATUS_IGNORE) {
357 // Copy receive status
362 void Request::start()
364 s4u::MailboxPtr mailbox;
366 xbt_assert(action_ == nullptr, "Cannot (re-)start unfinished communication");
367 flags_ &= ~MPI_REQ_PREPARED;
368 flags_ &= ~MPI_REQ_FINISHED;
371 if ((flags_ & MPI_REQ_RECV) != 0) {
372 this->print_request("New recv");
374 simgrid::smpi::ActorExt* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(dst_));
376 int async_small_thresh = simgrid::config::get_value<int>("smpi/async-small-thresh");
378 xbt_mutex_t mut = process->mailboxes_mutex();
379 if (async_small_thresh != 0 || (flags_ & MPI_REQ_RMA) != 0)
380 xbt_mutex_acquire(mut);
382 if (async_small_thresh == 0 && (flags_ & MPI_REQ_RMA) == 0) {
383 mailbox = process->mailbox();
384 } else if (((flags_ & MPI_REQ_RMA) != 0) || static_cast<int>(size_) < async_small_thresh) {
385 //We have to check both mailboxes (because SSEND messages are sent to the large mbox).
386 //begin with the more appropriate one : the small one.
387 mailbox = process->mailbox_small();
388 XBT_DEBUG("Is there a corresponding send already posted in the small mailbox %s (in case of SSEND)?",
389 mailbox->get_cname());
390 smx_activity_t action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
392 if (action == nullptr) {
393 mailbox = process->mailbox();
394 XBT_DEBUG("No, nothing in the small mailbox test the other one : %s", mailbox->get_cname());
395 action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
396 if (action == nullptr) {
397 XBT_DEBUG("Still nothing, switch back to the small mailbox : %s", mailbox->get_cname());
398 mailbox = process->mailbox_small();
401 XBT_DEBUG("yes there was something for us in the large mailbox");
404 mailbox = process->mailbox_small();
405 XBT_DEBUG("Is there a corresponding send already posted the small mailbox?");
406 smx_activity_t action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
408 if (action == nullptr) {
409 XBT_DEBUG("No, nothing in the permanent receive mailbox");
410 mailbox = process->mailbox();
412 XBT_DEBUG("yes there was something for us in the small mailbox");
416 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
418 action_ = simcall_comm_irecv(
419 process->get_actor()->get_impl(), mailbox->get_impl(), buf_, &real_size_, &match_recv,
420 process->replaying() ? &smpi_comm_null_copy_buffer_callback : smpi_comm_copy_data_callback, this, -1.0);
421 XBT_DEBUG("recv simcall posted");
423 if (async_small_thresh != 0 || (flags_ & MPI_REQ_RMA) != 0)
424 xbt_mutex_release(mut);
425 } else { /* the RECV flag was not set, so this is a send */
426 simgrid::smpi::ActorExt* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(dst_));
428 if (TRACE_smpi_view_internals()) {
429 TRACE_smpi_send(rank, rank, dst_, tag_, size_);
431 this->print_request("New send");
434 if ((flags_ & MPI_REQ_SSEND) == 0 &&
435 ((flags_ & MPI_REQ_RMA) != 0 ||
436 static_cast<int>(size_) < simgrid::config::get_value<int>("smpi/send-is-detached-thresh"))) {
437 void *oldbuf = nullptr;
439 XBT_DEBUG("Send request %p is detached", this);
441 if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
443 if (not process->replaying() && oldbuf != nullptr && size_ != 0) {
444 if ((smpi_privatize_global_variables != SmpiPrivStrategies::NONE) &&
445 (static_cast<char*>(buf_) >= smpi_data_exe_start) &&
446 (static_cast<char*>(buf_) < smpi_data_exe_start + smpi_data_exe_size)) {
447 XBT_DEBUG("Privatization : We are sending from a zone inside global memory. Switch data segment ");
448 smpi_switch_data_segment(simgrid::s4u::Actor::by_pid(src_));
450 buf = xbt_malloc(size_);
451 memcpy(buf,oldbuf,size_);
452 XBT_DEBUG("buf %p copied into %p",oldbuf,buf);
457 //if we are giving back the control to the user without waiting for completion, we have to inject timings
458 double sleeptime = 0.0;
459 if (detached_ != 0 || ((flags_ & (MPI_REQ_ISEND | MPI_REQ_SSEND)) != 0)) { // issend should be treated as isend
460 // isend and send timings may be different
461 sleeptime = ((flags_ & MPI_REQ_ISEND) != 0)
462 ? simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->oisend(size_)
463 : simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->osend(size_);
467 simcall_process_sleep(sleeptime);
468 XBT_DEBUG("sending size of %zu : sleep %f ", size_, sleeptime);
471 int async_small_thresh = simgrid::config::get_value<int>("smpi/async-small-thresh");
473 xbt_mutex_t mut=process->mailboxes_mutex();
475 if (async_small_thresh != 0 || (flags_ & MPI_REQ_RMA) != 0)
476 xbt_mutex_acquire(mut);
478 if (not(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_) < async_small_thresh) { // eager mode
481 mailbox = process->mailbox();
482 XBT_DEBUG("Is there a corresponding recv already posted in the large mailbox %s?", mailbox->get_cname());
483 smx_activity_t action = mailbox->iprobe(1, &match_send, static_cast<void*>(this));
484 if (action == nullptr) {
485 if ((flags_ & MPI_REQ_SSEND) == 0) {
486 mailbox = process->mailbox_small();
487 XBT_DEBUG("No, nothing in the large mailbox, message is to be sent on the small one %s",
488 mailbox->get_cname());
490 mailbox = process->mailbox_small();
491 XBT_DEBUG("SSEND : Is there a corresponding recv already posted in the small mailbox %s?",
492 mailbox->get_cname());
493 action = mailbox->iprobe(1, &match_send, static_cast<void*>(this));
494 if (action != nullptr) {
495 XBT_DEBUG("No, we are first, send to large mailbox");
496 mailbox = process->mailbox();
500 XBT_DEBUG("Yes there was something for us in the large mailbox");
503 mailbox = process->mailbox();
504 XBT_DEBUG("Send request %p is in the large mailbox %s (buf: %p)", this, mailbox->get_cname(), buf_);
507 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
509 action_ = simcall_comm_isend(
510 simgrid::s4u::Actor::by_pid(src_)->get_impl(), mailbox->get_impl(), size_, -1.0, buf, real_size_, &match_send,
511 &xbt_free_f, // how to free the userdata if a detached send fails
512 not process->replaying() ? smpi_comm_copy_data_callback : &smpi_comm_null_copy_buffer_callback, this,
513 // detach if msg size < eager/rdv switch limit
515 XBT_DEBUG("send simcall posted");
517 /* FIXME: detached sends are not traceable (action_ == nullptr) */
518 if (action_ != nullptr) {
519 std::string category = TRACE_internal_smpi_get_category();
520 simgrid::simix::simcall([this, category] { this->action_->set_category(category); });
523 if (async_small_thresh != 0 || ((flags_ & MPI_REQ_RMA) != 0))
524 xbt_mutex_release(mut);
528 void Request::startall(int count, MPI_Request * requests)
530 if(requests== nullptr)
533 for(int i = 0; i < count; i++) {
534 requests[i]->start();
538 void Request::cancel()
542 if (this->action_ != nullptr)
543 (boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(this->action_))->cancel();
546 int Request::test(MPI_Request * request, MPI_Status * status) {
547 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or testall before)
548 // to avoid deadlocks if used as a break condition, such as
549 // while (MPI_Test(request, flag, status) && flag) dostuff...
550 // because the time will not normally advance when only calls to MPI_Test are made -> deadlock
551 // multiplier to the sleeptime, to increase speed of execution, each failed test will increase it
552 static int nsleeps = 1;
553 if(smpi_test_sleep > 0)
554 simcall_process_sleep(nsleeps*smpi_test_sleep);
556 Status::empty(status);
558 if (((*request)->flags_ & MPI_REQ_PREPARED) == 0) {
559 if ((*request)->action_ != nullptr){
561 flag = simcall_comm_test((*request)->action_);
567 finish_wait(request,status);
568 nsleeps=1;//reset the number of sleeps we will do next time
569 if (*request != MPI_REQUEST_NULL && ((*request)->flags_ & MPI_REQ_PERSISTENT) == 0)
570 *request = MPI_REQUEST_NULL;
571 } else if (simgrid::config::get_value<bool>("smpi/grow-injected-times")) {
578 int Request::testsome(int incount, MPI_Request requests[], int *indices, MPI_Status status[])
583 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
585 for (int i = 0; i < incount; i++) {
586 if (requests[i] != MPI_REQUEST_NULL) {
587 if (test(&requests[i], pstat)) {
590 if (status != MPI_STATUSES_IGNORE)
592 if ((requests[i] != MPI_REQUEST_NULL) && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
593 requests[i] = MPI_REQUEST_NULL;
599 if(count_dead==incount)
600 return MPI_UNDEFINED;
604 int Request::testany(int count, MPI_Request requests[], int *index, MPI_Status * status)
606 std::vector<simgrid::kernel::activity::ActivityImplPtr> comms;
607 comms.reserve(count);
612 *index = MPI_UNDEFINED;
614 std::vector<int> map; /** Maps all matching comms back to their location in requests **/
615 for(i = 0; i < count; i++) {
616 if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->action_ && not(requests[i]->flags_ & MPI_REQ_PREPARED)) {
617 comms.push_back(requests[i]->action_);
621 if (not map.empty()) {
622 //multiplier to the sleeptime, to increase speed of execution, each failed testany will increase it
623 static int nsleeps = 1;
624 if(smpi_test_sleep > 0)
625 simcall_process_sleep(nsleeps*smpi_test_sleep);
627 i = simcall_comm_testany(comms.data(), comms.size()); // The i-th element in comms matches!
632 if (i != -1) { // -1 is not MPI_UNDEFINED but a SIMIX return code. (nothing matches)
634 finish_wait(&requests[*index],status);
637 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags_ & MPI_REQ_NON_PERSISTENT)) {
638 requests[*index] = MPI_REQUEST_NULL;
644 //all requests are null or inactive, return true
646 Status::empty(status);
652 int Request::testall(int count, MPI_Request requests[], MPI_Status status[])
655 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
657 for(int i=0; i<count; i++){
658 if (requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & MPI_REQ_PREPARED)) {
659 if (test(&requests[i], pstat)!=1){
662 requests[i]=MPI_REQUEST_NULL;
665 Status::empty(pstat);
667 if(status != MPI_STATUSES_IGNORE) {
674 void Request::probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
676 //FIXME find another way to avoid busy waiting ?
677 // the issue here is that we have to wait on a nonexistent comm
679 iprobe(source, tag, comm, &flag, status);
680 XBT_DEBUG("Busy Waiting on probing : %d", flag);
684 void Request::iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
685 // to avoid deadlock, we have to sleep some time here, or the timer won't advance and we will only do iprobe simcalls
686 // especially when used as a break condition, such as while (MPI_Iprobe(...)) dostuff...
687 // nsleeps is a multiplier to the sleeptime, to increase speed of execution, each failed iprobe will increase it
688 // This can speed up the execution of certain applications by an order of magnitude, such as HPL
689 static int nsleeps = 1;
690 double speed = s4u::this_actor::get_host()->get_speed();
691 double maxrate = simgrid::config::get_value<double>("smpi/iprobe-cpu-usage");
692 MPI_Request request = new Request(nullptr, 0, MPI_CHAR,
693 source == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : comm->group()->actor(source)->get_pid(),
694 simgrid::s4u::this_actor::get_pid(), tag, comm, MPI_REQ_PERSISTENT | MPI_REQ_RECV);
695 if (smpi_iprobe_sleep > 0) {
696 /** Compute the number of flops we will sleep **/
697 s4u::this_actor::exec_init(/*nsleeps: See comment above */ nsleeps *
698 /*(seconds * flop/s -> total flops)*/ smpi_iprobe_sleep * speed * maxrate)
700 /* Not the entire CPU can be used when iprobing: This is important for
701 * the energy consumption caused by polling with iprobes.
702 * Note also that the number of flops that was
703 * computed above contains a maxrate factor and is hence reduced (maxrate < 1)
705 ->set_bound(maxrate*speed)
709 // behave like a receive, but don't do it
710 s4u::MailboxPtr mailbox;
712 request->print_request("New iprobe");
713 // We have to test both mailboxes as we don't know if we will receive one one or another
714 if (simgrid::config::get_value<int>("smpi/async-small-thresh") > 0) {
715 mailbox = smpi_process()->mailbox_small();
716 XBT_DEBUG("Trying to probe the perm recv mailbox");
717 request->action_ = mailbox->iprobe(0, &match_recv, static_cast<void*>(request));
720 if (request->action_ == nullptr){
721 mailbox = smpi_process()->mailbox();
722 XBT_DEBUG("trying to probe the other mailbox");
723 request->action_ = mailbox->iprobe(0, &match_recv, static_cast<void*>(request));
726 if (request->action_ != nullptr){
727 kernel::activity::CommImplPtr sync_comm = boost::static_pointer_cast<kernel::activity::CommImpl>(request->action_);
728 MPI_Request req = static_cast<MPI_Request>(sync_comm->src_data_);
730 if (status != MPI_STATUS_IGNORE && (req->flags_ & MPI_REQ_PREPARED) == 0) {
731 status->MPI_SOURCE = comm->group()->rank(req->src_);
732 status->MPI_TAG = req->tag_;
733 status->MPI_ERROR = MPI_SUCCESS;
734 status->count = req->real_size_;
736 nsleeps = 1;//reset the number of sleeps we will do next time
740 if (simgrid::config::get_value<bool>("smpi/grow-injected-times"))
746 void Request::finish_wait(MPI_Request* request, MPI_Status * status)
748 MPI_Request req = *request;
749 Status::empty(status);
751 if (req->cancelled_==1){
752 if (status!=MPI_STATUS_IGNORE)
754 if(req->detached_sender_ != nullptr)
755 unref(&(req->detached_sender_));
760 if (not((req->detached_ != 0) && ((req->flags_ & MPI_REQ_SEND) != 0)) && ((req->flags_ & MPI_REQ_PREPARED) == 0)) {
761 if(status != MPI_STATUS_IGNORE) {
762 int src = req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_;
763 status->MPI_SOURCE = req->comm_->group()->rank(src);
764 status->MPI_TAG = req->tag_ == MPI_ANY_TAG ? req->real_tag_ : req->tag_;
765 status->MPI_ERROR = req->truncated_ != 0 ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
766 // this handles the case were size in receive differs from size in send
767 status->count = req->real_size_;
770 req->print_request("Finishing");
771 MPI_Datatype datatype = req->old_type_;
773 // FIXME Handle the case of a partial shared malloc.
774 if (((req->flags_ & MPI_REQ_ACCUMULATE) != 0) ||
775 (datatype->flags() & DT_FLAG_DERIVED)) { // && (not smpi_is_shared(req->old_buf_))){
777 if (not smpi_process()->replaying() && smpi_privatize_global_variables != SmpiPrivStrategies::NONE &&
778 static_cast<char*>(req->old_buf_) >= smpi_data_exe_start &&
779 static_cast<char*>(req->old_buf_) < smpi_data_exe_start + smpi_data_exe_size) {
780 XBT_VERB("Privatization : We are unserializing to a zone in global memory Switch data segment ");
781 smpi_switch_data_segment(simgrid::s4u::Actor::self());
784 if(datatype->flags() & DT_FLAG_DERIVED){
785 // This part handles the problem of non-contignous memory the unserialization at the reception
786 if ((req->flags_ & MPI_REQ_RECV) && datatype->size() != 0)
787 datatype->unserialize(req->buf_, req->old_buf_, req->real_size_/datatype->size() , req->op_);
789 } else if (req->flags_ & MPI_REQ_RECV) { // apply op on contiguous buffer for accumulate
790 if (datatype->size() != 0) {
791 int n = req->real_size_ / datatype->size();
792 req->op_->apply(req->buf_, req->old_buf_, &n, datatype);
799 if (TRACE_smpi_view_internals() && ((req->flags_ & MPI_REQ_RECV) != 0)) {
800 int rank = simgrid::s4u::this_actor::get_pid();
801 int src_traced = (req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_);
802 TRACE_smpi_recv(src_traced, rank,req->tag_);
804 if(req->detached_sender_ != nullptr){
805 //integrate pseudo-timing for buffering of small messages, do not bother to execute the simcall if 0
807 simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->orecv(req->real_size());
809 simcall_process_sleep(sleeptime);
810 XBT_DEBUG("receiving size of %zu : sleep %f ", req->real_size_, sleeptime);
812 unref(&(req->detached_sender_));
814 if (req->flags_ & MPI_REQ_PERSISTENT)
815 req->action_ = nullptr;
816 req->flags_ |= MPI_REQ_FINISHED;
820 void Request::wait(MPI_Request * request, MPI_Status * status)
822 (*request)->print_request("Waiting");
823 if ((*request)->flags_ & MPI_REQ_PREPARED) {
824 Status::empty(status);
828 if ((*request)->action_ != nullptr){
830 // this is not a detached send
831 simcall_comm_wait((*request)->action_, -1.0);
833 XBT_VERB("Request cancelled");
838 finish_wait(request,status);
839 if (*request != MPI_REQUEST_NULL && (((*request)->flags_ & MPI_REQ_NON_PERSISTENT) != 0))
840 *request = MPI_REQUEST_NULL;
843 int Request::waitany(int count, MPI_Request requests[], MPI_Status * status)
845 s_xbt_dynar_t comms; // Keep it on stack to save some extra mallocs
846 int index = MPI_UNDEFINED;
850 // Wait for a request to complete
851 xbt_dynar_init(&comms, sizeof(smx_activity_t), [](void*ptr){
852 intrusive_ptr_release(*(simgrid::kernel::activity::ActivityImpl**)ptr);
854 int *map = xbt_new(int, count);
855 XBT_DEBUG("Wait for one of %d", count);
856 for(int i = 0; i < count; i++) {
857 if (requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & MPI_REQ_PREPARED) &&
858 not(requests[i]->flags_ & MPI_REQ_FINISHED)) {
859 if (requests[i]->action_ != nullptr) {
860 XBT_DEBUG("Waiting any %p ", requests[i]);
861 intrusive_ptr_add_ref(requests[i]->action_.get());
862 xbt_dynar_push_as(&comms, simgrid::kernel::activity::ActivityImpl*, requests[i]->action_.get());
866 // This is a finished detached request, let's return this one
867 size = 0; // so we free the dynar but don't do the waitany call
869 finish_wait(&requests[i], status); // cleanup if refcount = 0
870 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
871 requests[i] = MPI_REQUEST_NULL; // set to null
877 XBT_DEBUG("Enter waitany for %lu comms", xbt_dynar_length(&comms));
880 // this is not a detached send
881 i = simcall_comm_waitany(&comms, -1);
883 XBT_INFO("request %d cancelled ",i);
887 // not MPI_UNDEFINED, as this is a simix return code
890 //in case of an accumulate, we have to wait the end of all requests to apply the operation, ordered correctly.
891 if ((requests[index] == MPI_REQUEST_NULL) ||
892 (not((requests[index]->flags_ & MPI_REQ_ACCUMULATE) && (requests[index]->flags_ & MPI_REQ_RECV)))) {
893 finish_wait(&requests[index],status);
894 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_NON_PERSISTENT))
895 requests[index] = MPI_REQUEST_NULL;
900 xbt_dynar_free_data(&comms);
904 if (index==MPI_UNDEFINED)
905 Status::empty(status);
910 static int sort_accumulates(MPI_Request a, MPI_Request b)
912 return (a->tag() > b->tag());
915 int Request::waitall(int count, MPI_Request requests[], MPI_Status status[])
917 std::vector<MPI_Request> accumulates;
920 MPI_Status *pstat = (status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat);
921 int retvalue = MPI_SUCCESS;
922 //tag invalid requests in the set
923 if (status != MPI_STATUSES_IGNORE) {
924 for (int c = 0; c < count; c++) {
925 if (requests[c] == MPI_REQUEST_NULL || requests[c]->dst_ == MPI_PROC_NULL ||
926 (requests[c]->flags_ & MPI_REQ_PREPARED)) {
927 Status::empty(&status[c]);
928 } else if (requests[c]->src_ == MPI_PROC_NULL) {
929 Status::empty(&status[c]);
930 status[c].MPI_SOURCE = MPI_PROC_NULL;
934 for (int c = 0; c < count; c++) {
935 if (MC_is_active() || MC_record_replay_is_active()) {
936 wait(&requests[c],pstat);
939 index = waitany(count, (MPI_Request*)requests, pstat);
941 if (index == MPI_UNDEFINED)
944 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_RECV) &&
945 (requests[index]->flags_ & MPI_REQ_ACCUMULATE))
946 accumulates.push_back(requests[index]);
947 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_NON_PERSISTENT))
948 requests[index] = MPI_REQUEST_NULL;
950 if (status != MPI_STATUSES_IGNORE) {
951 status[index] = *pstat;
952 if (status[index].MPI_ERROR == MPI_ERR_TRUNCATE)
953 retvalue = MPI_ERR_IN_STATUS;
957 if (not accumulates.empty()) {
958 std::sort(accumulates.begin(), accumulates.end(), sort_accumulates);
959 for (auto& req : accumulates) {
960 finish_wait(&req, status);
967 int Request::waitsome(int incount, MPI_Request requests[], int *indices, MPI_Status status[])
971 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
973 for (int i = 0; i < incount; i++) {
974 int index = waitany(incount, requests, pstat);
975 if(index!=MPI_UNDEFINED){
976 indices[count] = index;
978 if(status != MPI_STATUSES_IGNORE) {
979 status[index] = *pstat;
981 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_NON_PERSISTENT))
982 requests[index] = MPI_REQUEST_NULL;
984 return MPI_UNDEFINED;
990 MPI_Request Request::f2c(int id) {
992 if(id==MPI_FORTRAN_REQUEST_NULL)
993 return static_cast<MPI_Request>(MPI_REQUEST_NULL);
994 return static_cast<MPI_Request>(F2C::f2c_lookup()->at(get_key_id(key, id)));
999 if (F2C::f2c_lookup() == nullptr) {
1000 F2C::set_f2c_lookup(new std::unordered_map<std::string, F2C*>);
1003 (*(F2C::f2c_lookup()))[get_key_id(key, F2C::f2c_id())] = this;
1004 F2C::f2c_id_increment();
1005 return F2C::f2c_id()-1;
1008 void Request::free_f(int id)
1010 if (id != MPI_FORTRAN_REQUEST_NULL) {
1012 F2C::f2c_lookup()->erase(get_key_id(key, id));