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/smpi/include/smpi_actor.hpp"
19 #include "xbt/config.hpp"
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_request, smpi, "Logging specific to SMPI (request)");
25 static simgrid::config::Flag<double> smpi_iprobe_sleep(
26 "smpi/iprobe", "Minimum time to inject inside a call to MPI_Iprobe", 1e-4);
27 static simgrid::config::Flag<double> smpi_test_sleep(
28 "smpi/test", "Minimum time to inject inside a call to MPI_Test", 1e-4);
30 std::vector<s_smpi_factor_t> smpi_ois_values;
32 extern void (*smpi_comm_copy_data_callback)(simgrid::kernel::activity::CommImpl*, void*, size_t);
37 Request::Request(void* buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm, unsigned flags)
38 : buf_(buf), old_type_(datatype), src_(src), dst_(dst), tag_(tag), comm_(comm), flags_(flags)
40 void *old_buf = nullptr;
41 // FIXME Handle the case of a partial shared malloc.
42 if ((((flags & MPI_REQ_RECV) != 0) && ((flags & MPI_REQ_ACCUMULATE) != 0)) || (datatype->flags() & DT_FLAG_DERIVED)) {
43 // This part handles the problem of non-contiguous memory
48 buf_ = xbt_malloc(count*datatype->size());
49 if ((datatype->flags() & DT_FLAG_DERIVED) && ((flags & MPI_REQ_SEND) != 0)) {
50 datatype->serialize(old_buf, buf_, count);
54 // This part handles the problem of non-contiguous memory (for the unserialisation at the reception)
56 size_ = datatype->size() * count;
61 detached_sender_ = nullptr;
66 if (flags & MPI_REQ_PERSISTENT)
74 MPI_Comm Request::comm(){
94 int Request::detached(){
98 MPI_Datatype Request::type(){
102 size_t Request::size(){
106 size_t Request::real_size(){
114 void Request::unref(MPI_Request* request)
116 if((*request) != MPI_REQUEST_NULL){
117 (*request)->refcount_--;
118 if((*request)->refcount_ < 0) {
119 (*request)->print_request("wrong refcount");
120 xbt_die("Whoops, wrong refcount");
122 if((*request)->refcount_==0){
123 Datatype::unref((*request)->old_type_);
124 Comm::unref((*request)->comm_);
125 (*request)->print_request("Destroying");
127 *request = MPI_REQUEST_NULL;
129 (*request)->print_request("Decrementing");
132 xbt_die("freeing an already free request");
136 int Request::match_recv(void* a, void* b, simgrid::kernel::activity::CommImpl*)
138 MPI_Request ref = static_cast<MPI_Request>(a);
139 MPI_Request req = static_cast<MPI_Request>(b);
140 XBT_DEBUG("Trying to match a recv of src %d against %d, tag %d against %d",ref->src_,req->src_, ref->tag_, req->tag_);
142 xbt_assert(ref, "Cannot match recv against null reference");
143 xbt_assert(req, "Cannot match recv against null request");
144 if((ref->src_ == MPI_ANY_SOURCE || req->src_ == ref->src_)
145 && ((ref->tag_ == MPI_ANY_TAG && req->tag_ >=0) || req->tag_ == ref->tag_)){
146 //we match, we can transfer some values
147 if(ref->src_ == MPI_ANY_SOURCE)
148 ref->real_src_ = req->src_;
149 if(ref->tag_ == MPI_ANY_TAG)
150 ref->real_tag_ = req->tag_;
151 if(ref->real_size_ < req->real_size_)
153 if(req->detached_==1)
154 ref->detached_sender_=req; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
155 if(req->cancelled_==0)
156 req->cancelled_=-1;//mark as uncancellable
157 XBT_DEBUG("match succeeded");
162 int Request::match_send(void* a, void* b, simgrid::kernel::activity::CommImpl*)
164 MPI_Request ref = static_cast<MPI_Request>(a);
165 MPI_Request req = static_cast<MPI_Request>(b);
166 XBT_DEBUG("Trying to match a send of src %d against %d, tag %d against %d",ref->src_,req->src_, ref->tag_, req->tag_);
167 xbt_assert(ref, "Cannot match send against null reference");
168 xbt_assert(req, "Cannot match send against null request");
170 if((req->src_ == MPI_ANY_SOURCE || req->src_ == ref->src_)
171 && ((req->tag_ == MPI_ANY_TAG && ref->tag_ >=0)|| req->tag_ == ref->tag_)){
172 if(req->src_ == MPI_ANY_SOURCE)
173 req->real_src_ = ref->src_;
174 if(req->tag_ == MPI_ANY_TAG)
175 req->real_tag_ = ref->tag_;
176 if(req->real_size_ < ref->real_size_)
178 if(ref->detached_==1)
179 req->detached_sender_=ref; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
180 if(req->cancelled_==0)
181 req->cancelled_=-1;//mark as uncancellable
182 XBT_DEBUG("match succeeded");
188 void Request::print_request(const char *message)
190 XBT_VERB("%s request %p [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
191 message, this, buf_, size_, src_, dst_, tag_, flags_);
195 /* factories, to hide the internal flags from the caller */
196 MPI_Request Request::send_init(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
199 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
200 comm->group()->actor(dst)->get_pid(), tag, comm,
201 MPI_REQ_PERSISTENT | MPI_REQ_SEND | MPI_REQ_PREPARED);
204 MPI_Request Request::ssend_init(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
206 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
207 comm->group()->actor(dst)->get_pid(), tag, comm,
208 MPI_REQ_PERSISTENT | MPI_REQ_SSEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
211 MPI_Request Request::isend_init(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
213 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
214 comm->group()->actor(dst)->get_pid(), tag, comm,
215 MPI_REQ_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
219 MPI_Request Request::rma_send_init(void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
222 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
224 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src)->get_pid(),
225 comm->group()->actor(dst)->get_pid(), tag, comm,
226 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED);
228 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src)->get_pid(),
229 comm->group()->actor(dst)->get_pid(), tag, comm,
230 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND | MPI_REQ_PREPARED |
237 MPI_Request Request::recv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
239 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
240 src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : comm->group()->actor(src)->get_pid(),
241 simgrid::s4u::this_actor::get_pid(), tag, comm,
242 MPI_REQ_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
245 MPI_Request Request::rma_recv_init(void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
248 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
250 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src)->get_pid(),
251 comm->group()->actor(dst)->get_pid(), tag, comm,
252 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
254 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src)->get_pid(),
255 comm->group()->actor(dst)->get_pid(), tag, comm,
256 MPI_REQ_RMA | MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED | MPI_REQ_ACCUMULATE);
262 MPI_Request Request::irecv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
264 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
265 src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : comm->group()->actor(src)->get_pid(),
266 simgrid::s4u::this_actor::get_pid(), tag, comm,
267 MPI_REQ_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PREPARED);
270 MPI_Request Request::isend(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
272 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
273 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
274 comm->group()->actor(dst)->get_pid(), tag, comm,
275 MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SEND);
280 MPI_Request Request::issend(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
282 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
283 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
284 comm->group()->actor(dst)->get_pid(), tag, comm,
285 MPI_REQ_NON_PERSISTENT | MPI_REQ_ISEND | MPI_REQ_SSEND | MPI_REQ_SEND);
291 MPI_Request Request::irecv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
293 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
294 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
295 src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : comm->group()->actor(src)->get_pid(),
296 simgrid::s4u::this_actor::get_pid(), tag, comm, MPI_REQ_NON_PERSISTENT | MPI_REQ_RECV);
301 void Request::recv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status * status)
303 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
304 request = irecv(buf, count, datatype, src, tag, comm);
305 wait(&request,status);
309 void Request::send(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
311 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
312 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
313 comm->group()->actor(dst)->get_pid(), tag, comm, MPI_REQ_NON_PERSISTENT | MPI_REQ_SEND);
316 wait(&request, MPI_STATUS_IGNORE);
320 void Request::ssend(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
322 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
323 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
324 comm->group()->actor(dst)->get_pid(), tag, comm,
325 MPI_REQ_NON_PERSISTENT | MPI_REQ_SSEND | MPI_REQ_SEND);
328 wait(&request,MPI_STATUS_IGNORE);
332 void Request::sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,int dst, int sendtag,
333 void *recvbuf, int recvcount, MPI_Datatype recvtype, int src, int recvtag,
334 MPI_Comm comm, MPI_Status * status)
336 MPI_Request requests[2];
338 int myid = simgrid::s4u::this_actor::get_pid();
339 if ((comm->group()->actor(dst)->get_pid() == myid) && (comm->group()->actor(src)->get_pid() == myid)) {
340 Datatype::copy(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
341 if (status != MPI_STATUS_IGNORE) {
342 status->MPI_SOURCE = src;
343 status->MPI_TAG = recvtag;
344 status->MPI_ERROR = MPI_SUCCESS;
345 status->count = sendcount * sendtype->size();
349 requests[0] = isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
350 requests[1] = irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
351 startall(2, requests);
352 waitall(2, requests, stats);
355 if(status != MPI_STATUS_IGNORE) {
356 // Copy receive status
361 void Request::start()
363 s4u::Mailbox* mailbox;
365 xbt_assert(action_ == nullptr, "Cannot (re-)start unfinished communication");
366 flags_ &= ~MPI_REQ_PREPARED;
367 flags_ &= ~MPI_REQ_FINISHED;
370 if ((flags_ & MPI_REQ_RECV) != 0) {
371 this->print_request("New recv");
373 simgrid::smpi::ActorExt* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(dst_));
375 int async_small_thresh = simgrid::config::get_value<int>("smpi/async-small-thresh");
377 simgrid::s4u::MutexPtr mut = process->mailboxes_mutex();
378 if (async_small_thresh != 0 || (flags_ & MPI_REQ_RMA) != 0)
381 if (async_small_thresh == 0 && (flags_ & MPI_REQ_RMA) == 0) {
382 mailbox = process->mailbox();
383 } else if (((flags_ & MPI_REQ_RMA) != 0) || static_cast<int>(size_) < async_small_thresh) {
384 //We have to check both mailboxes (because SSEND messages are sent to the large mbox).
385 //begin with the more appropriate one : the small one.
386 mailbox = process->mailbox_small();
387 XBT_DEBUG("Is there a corresponding send already posted in the small mailbox %s (in case of SSEND)?",
388 mailbox->get_cname());
389 smx_activity_t action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
391 if (action == nullptr) {
392 mailbox = process->mailbox();
393 XBT_DEBUG("No, nothing in the small mailbox test the other one : %s", mailbox->get_cname());
394 action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
395 if (action == nullptr) {
396 XBT_DEBUG("Still nothing, switch back to the small mailbox : %s", mailbox->get_cname());
397 mailbox = process->mailbox_small();
400 XBT_DEBUG("yes there was something for us in the large mailbox");
403 mailbox = process->mailbox_small();
404 XBT_DEBUG("Is there a corresponding send already posted the small mailbox?");
405 smx_activity_t action = mailbox->iprobe(0, &match_recv, static_cast<void*>(this));
407 if (action == nullptr) {
408 XBT_DEBUG("No, nothing in the permanent receive mailbox");
409 mailbox = process->mailbox();
411 XBT_DEBUG("yes there was something for us in the small mailbox");
415 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
417 action_ = simcall_comm_irecv(
418 process->get_actor()->get_impl(), mailbox->get_impl(), buf_, &real_size_, &match_recv,
419 process->replaying() ? &smpi_comm_null_copy_buffer_callback : smpi_comm_copy_data_callback, this, -1.0);
420 XBT_DEBUG("recv simcall posted");
422 if (async_small_thresh != 0 || (flags_ & MPI_REQ_RMA) != 0)
424 } else { /* the RECV flag was not set, so this is a send */
425 simgrid::smpi::ActorExt* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(dst_));
427 if (TRACE_smpi_view_internals()) {
428 TRACE_smpi_send(rank, rank, dst_, tag_, size_);
430 this->print_request("New send");
433 if ((flags_ & MPI_REQ_SSEND) == 0 &&
434 ((flags_ & MPI_REQ_RMA) != 0 ||
435 static_cast<int>(size_) < simgrid::config::get_value<int>("smpi/send-is-detached-thresh"))) {
436 void *oldbuf = nullptr;
438 XBT_DEBUG("Send request %p is detached", this);
440 if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
442 if (not process->replaying() && oldbuf != nullptr && size_ != 0) {
443 if ((smpi_privatize_global_variables != SmpiPrivStrategies::NONE) &&
444 (static_cast<char*>(buf_) >= smpi_data_exe_start) &&
445 (static_cast<char*>(buf_) < smpi_data_exe_start + smpi_data_exe_size)) {
446 XBT_DEBUG("Privatization : We are sending from a zone inside global memory. Switch data segment ");
447 smpi_switch_data_segment(simgrid::s4u::Actor::by_pid(src_));
449 buf = xbt_malloc(size_);
450 memcpy(buf,oldbuf,size_);
451 XBT_DEBUG("buf %p copied into %p",oldbuf,buf);
456 //if we are giving back the control to the user without waiting for completion, we have to inject timings
457 double sleeptime = 0.0;
458 if (detached_ != 0 || ((flags_ & (MPI_REQ_ISEND | MPI_REQ_SSEND)) != 0)) { // issend should be treated as isend
459 // isend and send timings may be different
460 sleeptime = ((flags_ & MPI_REQ_ISEND) != 0)
461 ? simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->oisend(size_)
462 : simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->osend(size_);
466 simcall_process_sleep(sleeptime);
467 XBT_DEBUG("sending size of %zu : sleep %f ", size_, sleeptime);
470 int async_small_thresh = simgrid::config::get_value<int>("smpi/async-small-thresh");
472 simgrid::s4u::MutexPtr mut = process->mailboxes_mutex();
474 if (async_small_thresh != 0 || (flags_ & MPI_REQ_RMA) != 0)
477 if (not(async_small_thresh != 0 || (flags_ & MPI_REQ_RMA) != 0)) {
478 mailbox = process->mailbox();
479 } else if (((flags_ & MPI_REQ_RMA) != 0) || static_cast<int>(size_) < async_small_thresh) { // eager mode
480 mailbox = process->mailbox();
481 XBT_DEBUG("Is there a corresponding recv already posted in the large mailbox %s?", mailbox->get_cname());
482 smx_activity_t action = mailbox->iprobe(1, &match_send, static_cast<void*>(this));
483 if (action == nullptr) {
484 if ((flags_ & MPI_REQ_SSEND) == 0) {
485 mailbox = process->mailbox_small();
486 XBT_DEBUG("No, nothing in the large mailbox, message is to be sent on the small one %s",
487 mailbox->get_cname());
489 mailbox = process->mailbox_small();
490 XBT_DEBUG("SSEND : Is there a corresponding recv already posted in the small mailbox %s?",
491 mailbox->get_cname());
492 action = mailbox->iprobe(1, &match_send, static_cast<void*>(this));
493 if (action == nullptr) {
494 XBT_DEBUG("No, we are first, send to large mailbox");
495 mailbox = process->mailbox();
499 XBT_DEBUG("Yes there was something for us in the large mailbox");
502 mailbox = process->mailbox();
503 XBT_DEBUG("Send request %p is in the large mailbox %s (buf: %p)", this, mailbox->get_cname(), buf_);
506 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
508 action_ = simcall_comm_isend(
509 simgrid::s4u::Actor::by_pid(src_)->get_impl(), mailbox->get_impl(), size_, -1.0, buf, real_size_, &match_send,
510 &xbt_free_f, // how to free the userdata if a detached send fails
511 not process->replaying() ? smpi_comm_copy_data_callback : &smpi_comm_null_copy_buffer_callback, this,
512 // detach if msg size < eager/rdv switch limit
514 XBT_DEBUG("send simcall posted");
516 /* FIXME: detached sends are not traceable (action_ == nullptr) */
517 if (action_ != nullptr) {
518 std::string category = smpi_process()->get_tracing_category();
519 simgrid::simix::simcall([this, category] { this->action_->set_category(category); });
522 if (async_small_thresh != 0 || ((flags_ & MPI_REQ_RMA) != 0))
527 void Request::startall(int count, MPI_Request * requests)
529 if(requests== nullptr)
532 for(int i = 0; i < count; i++) {
533 requests[i]->start();
537 void Request::cancel()
541 if (this->action_ != nullptr)
542 (boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(this->action_))->cancel();
545 int Request::test(MPI_Request * request, MPI_Status * status) {
546 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or testall before)
547 // to avoid deadlocks if used as a break condition, such as
548 // while (MPI_Test(request, flag, status) && flag) dostuff...
549 // because the time will not normally advance when only calls to MPI_Test are made -> deadlock
550 // multiplier to the sleeptime, to increase speed of execution, each failed test will increase it
551 static int nsleeps = 1;
552 if(smpi_test_sleep > 0)
553 simcall_process_sleep(nsleeps*smpi_test_sleep);
555 Status::empty(status);
557 if (((*request)->flags_ & MPI_REQ_PREPARED) == 0) {
558 if ((*request)->action_ != nullptr){
560 flag = simcall_comm_test((*request)->action_);
566 finish_wait(request,status);
567 nsleeps=1;//reset the number of sleeps we will do next time
568 if (*request != MPI_REQUEST_NULL && ((*request)->flags_ & MPI_REQ_PERSISTENT) == 0)
569 *request = MPI_REQUEST_NULL;
570 } else if (simgrid::config::get_value<bool>("smpi/grow-injected-times")) {
577 int Request::testsome(int incount, MPI_Request requests[], int *indices, MPI_Status status[])
582 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
584 for (int i = 0; i < incount; i++) {
585 if (requests[i] != MPI_REQUEST_NULL) {
586 if (test(&requests[i], pstat)) {
589 if (status != MPI_STATUSES_IGNORE)
591 if ((requests[i] != MPI_REQUEST_NULL) && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
592 requests[i] = MPI_REQUEST_NULL;
598 if(count_dead==incount)
599 return MPI_UNDEFINED;
603 int Request::testany(int count, MPI_Request requests[], int *index, MPI_Status * status)
605 std::vector<simgrid::kernel::activity::CommImpl*> comms;
606 comms.reserve(count);
611 *index = MPI_UNDEFINED;
613 std::vector<int> map; /** Maps all matching comms back to their location in requests **/
614 for(i = 0; i < count; i++) {
615 if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->action_ && not(requests[i]->flags_ & MPI_REQ_PREPARED)) {
616 comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(requests[i]->action_.get()));
620 if (not map.empty()) {
621 //multiplier to the sleeptime, to increase speed of execution, each failed testany will increase it
622 static int nsleeps = 1;
623 if(smpi_test_sleep > 0)
624 simcall_process_sleep(nsleeps*smpi_test_sleep);
626 i = simcall_comm_testany(comms.data(), comms.size()); // The i-th element in comms matches!
631 if (i != -1) { // -1 is not MPI_UNDEFINED but a SIMIX return code. (nothing matches)
633 finish_wait(&requests[*index],status);
636 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags_ & MPI_REQ_NON_PERSISTENT)) {
637 requests[*index] = MPI_REQUEST_NULL;
643 //all requests are null or inactive, return true
645 Status::empty(status);
651 int Request::testall(int count, MPI_Request requests[], MPI_Status status[])
654 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
656 for(int i=0; i<count; i++){
657 if (requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & MPI_REQ_PREPARED)) {
658 if (test(&requests[i], pstat)!=1){
661 requests[i]=MPI_REQUEST_NULL;
664 Status::empty(pstat);
666 if(status != MPI_STATUSES_IGNORE) {
673 void Request::probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
675 //FIXME find another way to avoid busy waiting ?
676 // the issue here is that we have to wait on a nonexistent comm
678 iprobe(source, tag, comm, &flag, status);
679 XBT_DEBUG("Busy Waiting on probing : %d", flag);
683 void Request::iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
684 // to avoid deadlock, we have to sleep some time here, or the timer won't advance and we will only do iprobe simcalls
685 // especially when used as a break condition, such as while (MPI_Iprobe(...)) dostuff...
686 // nsleeps is a multiplier to the sleeptime, to increase speed of execution, each failed iprobe will increase it
687 // This can speed up the execution of certain applications by an order of magnitude, such as HPL
688 static int nsleeps = 1;
689 double speed = s4u::this_actor::get_host()->get_speed();
690 double maxrate = simgrid::config::get_value<double>("smpi/iprobe-cpu-usage");
691 MPI_Request request = new Request(nullptr, 0, MPI_CHAR,
692 source == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : comm->group()->actor(source)->get_pid(),
693 simgrid::s4u::this_actor::get_pid(), tag, comm, MPI_REQ_PERSISTENT | MPI_REQ_RECV);
694 if (smpi_iprobe_sleep > 0) {
695 /** Compute the number of flops we will sleep **/
696 s4u::this_actor::exec_init(/*nsleeps: See comment above */ nsleeps *
697 /*(seconds * flop/s -> total flops)*/ smpi_iprobe_sleep * speed * maxrate)
699 /* Not the entire CPU can be used when iprobing: This is important for
700 * the energy consumption caused by polling with iprobes.
701 * Note also that the number of flops that was
702 * computed above contains a maxrate factor and is hence reduced (maxrate < 1)
704 ->set_bound(maxrate*speed)
708 // behave like a receive, but don't do it
709 s4u::Mailbox* mailbox;
711 request->print_request("New iprobe");
712 // We have to test both mailboxes as we don't know if we will receive one one or another
713 if (simgrid::config::get_value<int>("smpi/async-small-thresh") > 0) {
714 mailbox = smpi_process()->mailbox_small();
715 XBT_DEBUG("Trying to probe the perm recv mailbox");
716 request->action_ = mailbox->iprobe(0, &match_recv, static_cast<void*>(request));
719 if (request->action_ == nullptr){
720 mailbox = smpi_process()->mailbox();
721 XBT_DEBUG("trying to probe the other mailbox");
722 request->action_ = mailbox->iprobe(0, &match_recv, static_cast<void*>(request));
725 if (request->action_ != nullptr){
726 kernel::activity::CommImplPtr sync_comm = boost::static_pointer_cast<kernel::activity::CommImpl>(request->action_);
727 MPI_Request req = static_cast<MPI_Request>(sync_comm->src_data_);
729 if (status != MPI_STATUS_IGNORE && (req->flags_ & MPI_REQ_PREPARED) == 0) {
730 status->MPI_SOURCE = comm->group()->rank(req->src_);
731 status->MPI_TAG = req->tag_;
732 status->MPI_ERROR = MPI_SUCCESS;
733 status->count = req->real_size_;
735 nsleeps = 1;//reset the number of sleeps we will do next time
739 if (simgrid::config::get_value<bool>("smpi/grow-injected-times"))
745 void Request::finish_wait(MPI_Request* request, MPI_Status * status)
747 MPI_Request req = *request;
748 Status::empty(status);
750 if (req->cancelled_==1){
751 if (status!=MPI_STATUS_IGNORE)
753 if(req->detached_sender_ != nullptr)
754 unref(&(req->detached_sender_));
759 if (not((req->detached_ != 0) && ((req->flags_ & MPI_REQ_SEND) != 0)) && ((req->flags_ & MPI_REQ_PREPARED) == 0)) {
760 if(status != MPI_STATUS_IGNORE) {
761 int src = req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_;
762 status->MPI_SOURCE = req->comm_->group()->rank(src);
763 status->MPI_TAG = req->tag_ == MPI_ANY_TAG ? req->real_tag_ : req->tag_;
764 status->MPI_ERROR = req->truncated_ != 0 ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
765 // this handles the case were size in receive differs from size in send
766 status->count = req->real_size_;
769 req->print_request("Finishing");
770 MPI_Datatype datatype = req->old_type_;
772 // FIXME Handle the case of a partial shared malloc.
773 if (((req->flags_ & MPI_REQ_ACCUMULATE) != 0) ||
774 (datatype->flags() & DT_FLAG_DERIVED)) { // && (not smpi_is_shared(req->old_buf_))){
776 if (not smpi_process()->replaying() && smpi_privatize_global_variables != SmpiPrivStrategies::NONE &&
777 static_cast<char*>(req->old_buf_) >= smpi_data_exe_start &&
778 static_cast<char*>(req->old_buf_) < smpi_data_exe_start + smpi_data_exe_size) {
779 XBT_VERB("Privatization : We are unserializing to a zone in global memory Switch data segment ");
780 smpi_switch_data_segment(simgrid::s4u::Actor::self());
783 if(datatype->flags() & DT_FLAG_DERIVED){
784 // This part handles the problem of non-contignous memory the unserialization at the reception
785 if ((req->flags_ & MPI_REQ_RECV) && datatype->size() != 0)
786 datatype->unserialize(req->buf_, req->old_buf_, req->real_size_/datatype->size() , req->op_);
788 } else if (req->flags_ & MPI_REQ_RECV) { // apply op on contiguous buffer for accumulate
789 if (datatype->size() != 0) {
790 int n = req->real_size_ / datatype->size();
791 req->op_->apply(req->buf_, req->old_buf_, &n, datatype);
798 if (TRACE_smpi_view_internals() && ((req->flags_ & MPI_REQ_RECV) != 0)) {
799 int rank = simgrid::s4u::this_actor::get_pid();
800 int src_traced = (req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_);
801 TRACE_smpi_recv(src_traced, rank,req->tag_);
803 if(req->detached_sender_ != nullptr){
804 //integrate pseudo-timing for buffering of small messages, do not bother to execute the simcall if 0
806 simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::Host>()->orecv(req->real_size());
808 simcall_process_sleep(sleeptime);
809 XBT_DEBUG("receiving size of %zu : sleep %f ", req->real_size_, sleeptime);
811 unref(&(req->detached_sender_));
813 if (req->flags_ & MPI_REQ_PERSISTENT)
814 req->action_ = nullptr;
815 req->flags_ |= MPI_REQ_FINISHED;
819 void Request::wait(MPI_Request * request, MPI_Status * status)
821 (*request)->print_request("Waiting");
822 if ((*request)->flags_ & MPI_REQ_PREPARED) {
823 Status::empty(status);
827 if ((*request)->action_ != nullptr){
829 // this is not a detached send
830 simcall_comm_wait((*request)->action_, -1.0);
832 XBT_VERB("Request cancelled");
837 finish_wait(request,status);
838 if (*request != MPI_REQUEST_NULL && (((*request)->flags_ & MPI_REQ_NON_PERSISTENT) != 0))
839 *request = MPI_REQUEST_NULL;
842 int Request::waitany(int count, MPI_Request requests[], MPI_Status * status)
844 std::vector<simgrid::kernel::activity::CommImpl*> comms;
845 comms.reserve(count);
846 int index = MPI_UNDEFINED;
849 // Wait for a request to complete
850 std::vector<int> map;
851 XBT_DEBUG("Wait for one of %d", count);
852 for(int i = 0; i < count; i++) {
853 if (requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & MPI_REQ_PREPARED) &&
854 not(requests[i]->flags_ & MPI_REQ_FINISHED)) {
855 if (requests[i]->action_ != nullptr) {
856 XBT_DEBUG("Waiting any %p ", requests[i]);
857 comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(requests[i]->action_.get()));
860 // This is a finished detached request, let's return this one
861 comms.clear(); // so we free don't do the waitany call
863 finish_wait(&requests[i], status); // cleanup if refcount = 0
864 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
865 requests[i] = MPI_REQUEST_NULL; // set to null
870 if (not comms.empty()) {
871 XBT_DEBUG("Enter waitany for %zu comms", comms.size());
874 // this is not a detached send
875 i = simcall_comm_waitany(comms.data(), comms.size(), -1);
877 XBT_INFO("request %d cancelled ",i);
881 // not MPI_UNDEFINED, as this is a simix return code
884 //in case of an accumulate, we have to wait the end of all requests to apply the operation, ordered correctly.
885 if ((requests[index] == MPI_REQUEST_NULL) ||
886 (not((requests[index]->flags_ & MPI_REQ_ACCUMULATE) && (requests[index]->flags_ & MPI_REQ_RECV)))) {
887 finish_wait(&requests[index],status);
888 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_NON_PERSISTENT))
889 requests[index] = MPI_REQUEST_NULL;
895 if (index==MPI_UNDEFINED)
896 Status::empty(status);
901 static int sort_accumulates(MPI_Request a, MPI_Request b)
903 return (a->tag() > b->tag());
906 int Request::waitall(int count, MPI_Request requests[], MPI_Status status[])
908 std::vector<MPI_Request> accumulates;
911 MPI_Status *pstat = (status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat);
912 int retvalue = MPI_SUCCESS;
913 //tag invalid requests in the set
914 if (status != MPI_STATUSES_IGNORE) {
915 for (int c = 0; c < count; c++) {
916 if (requests[c] == MPI_REQUEST_NULL || requests[c]->dst_ == MPI_PROC_NULL ||
917 (requests[c]->flags_ & MPI_REQ_PREPARED)) {
918 Status::empty(&status[c]);
919 } else if (requests[c]->src_ == MPI_PROC_NULL) {
920 Status::empty(&status[c]);
921 status[c].MPI_SOURCE = MPI_PROC_NULL;
925 for (int c = 0; c < count; c++) {
926 if (MC_is_active() || MC_record_replay_is_active()) {
927 wait(&requests[c],pstat);
930 index = waitany(count, (MPI_Request*)requests, pstat);
932 if (index == MPI_UNDEFINED)
935 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_RECV) &&
936 (requests[index]->flags_ & MPI_REQ_ACCUMULATE))
937 accumulates.push_back(requests[index]);
938 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_NON_PERSISTENT))
939 requests[index] = MPI_REQUEST_NULL;
941 if (status != MPI_STATUSES_IGNORE) {
942 status[index] = *pstat;
943 if (status[index].MPI_ERROR == MPI_ERR_TRUNCATE)
944 retvalue = MPI_ERR_IN_STATUS;
948 if (not accumulates.empty()) {
949 std::sort(accumulates.begin(), accumulates.end(), sort_accumulates);
950 for (auto& req : accumulates) {
951 finish_wait(&req, status);
958 int Request::waitsome(int incount, MPI_Request requests[], int *indices, MPI_Status status[])
962 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
964 for (int i = 0; i < incount; i++) {
965 int index = waitany(incount, requests, pstat);
966 if(index!=MPI_UNDEFINED){
967 indices[count] = index;
969 if(status != MPI_STATUSES_IGNORE) {
970 status[index] = *pstat;
972 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & MPI_REQ_NON_PERSISTENT))
973 requests[index] = MPI_REQUEST_NULL;
975 return MPI_UNDEFINED;
981 MPI_Request Request::f2c(int id) {
983 if(id==MPI_FORTRAN_REQUEST_NULL)
984 return static_cast<MPI_Request>(MPI_REQUEST_NULL);
985 return static_cast<MPI_Request>(F2C::f2c_lookup()->at(get_key_id(key, id)));
990 if (F2C::f2c_lookup() == nullptr) {
991 F2C::set_f2c_lookup(new std::unordered_map<std::string, F2C*>);
994 (*(F2C::f2c_lookup()))[get_key_id(key, F2C::f2c_id())] = this;
995 F2C::f2c_id_increment();
996 return F2C::f2c_id()-1;
999 void Request::free_f(int id)
1001 if (id != MPI_FORTRAN_REQUEST_NULL) {
1003 F2C::f2c_lookup()->erase(get_key_id(key, id));