1 /* Copyright (c) 2007-2018. 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"
8 #include "SmpiHost.hpp"
10 #include "private.hpp"
11 #include "smpi_comm.hpp"
12 #include "smpi_datatype.hpp"
13 #include "smpi_op.hpp"
14 #include "smpi_process.hpp"
15 #include "src/kernel/activity/CommImpl.hpp"
16 #include "src/mc/mc_replay.hpp"
17 #include "src/simix/ActorImpl.hpp"
18 #include "xbt/config.hpp"
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_request, smpi, "Logging specific to SMPI (reques)");
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) (smx_activity_t, 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 & RECV) != 0) && ((flags & 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 & 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 & 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(){
110 void Request::unref(MPI_Request* request)
112 if((*request) != MPI_REQUEST_NULL){
113 (*request)->refcount_--;
114 if((*request)->refcount_ < 0) {
115 (*request)->print_request("wrong refcount");
116 xbt_die("Whoops, wrong refcount");
118 if((*request)->refcount_==0){
119 Datatype::unref((*request)->old_type_);
120 Comm::unref((*request)->comm_);
121 (*request)->print_request("Destroying");
123 *request = MPI_REQUEST_NULL;
125 (*request)->print_request("Decrementing");
128 xbt_die("freeing an already free request");
132 int Request::match_recv(void* a, void* b, simgrid::kernel::activity::CommImpl* ignored)
134 MPI_Request ref = static_cast<MPI_Request>(a);
135 MPI_Request req = static_cast<MPI_Request>(b);
136 XBT_DEBUG("Trying to match a recv of src %d against %d, tag %d against %d",ref->src_,req->src_, ref->tag_, req->tag_);
138 xbt_assert(ref, "Cannot match recv against null reference");
139 xbt_assert(req, "Cannot match recv against null request");
140 if((ref->src_ == MPI_ANY_SOURCE || req->src_ == ref->src_)
141 && ((ref->tag_ == MPI_ANY_TAG && req->tag_ >=0) || req->tag_ == ref->tag_)){
142 //we match, we can transfer some values
143 if(ref->src_ == MPI_ANY_SOURCE)
144 ref->real_src_ = req->src_;
145 if(ref->tag_ == MPI_ANY_TAG)
146 ref->real_tag_ = req->tag_;
147 if(ref->real_size_ < req->real_size_)
149 if(req->detached_==1)
150 ref->detached_sender_=req; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
151 if(req->cancelled_==0)
152 req->cancelled_=-1;//mark as uncancellable
153 XBT_DEBUG("match succeeded");
158 int Request::match_send(void* a, void* b, simgrid::kernel::activity::CommImpl* ignored)
160 MPI_Request ref = static_cast<MPI_Request>(a);
161 MPI_Request req = static_cast<MPI_Request>(b);
162 XBT_DEBUG("Trying to match a send of src %d against %d, tag %d against %d",ref->src_,req->src_, ref->tag_, req->tag_);
163 xbt_assert(ref, "Cannot match send against null reference");
164 xbt_assert(req, "Cannot match send against null request");
166 if((req->src_ == MPI_ANY_SOURCE || req->src_ == ref->src_)
167 && ((req->tag_ == MPI_ANY_TAG && ref->tag_ >=0)|| req->tag_ == ref->tag_)){
168 if(req->src_ == MPI_ANY_SOURCE)
169 req->real_src_ = ref->src_;
170 if(req->tag_ == MPI_ANY_TAG)
171 req->real_tag_ = ref->tag_;
172 if(req->real_size_ < ref->real_size_)
174 if(ref->detached_==1)
175 req->detached_sender_=ref; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
176 if(req->cancelled_==0)
177 req->cancelled_=-1;//mark as uncancellable
178 XBT_DEBUG("match succeeded");
184 void Request::print_request(const char *message)
186 XBT_VERB("%s request %p [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
187 message, this, buf_, size_, src_, dst_, tag_, flags_);
191 /* factories, to hide the internal flags from the caller */
192 MPI_Request Request::send_init(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
195 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
196 comm->group()->actor(dst)->get_pid(), tag, comm, PERSISTENT | SEND | PREPARED);
199 MPI_Request Request::ssend_init(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
201 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
202 comm->group()->actor(dst)->get_pid(), tag, comm, PERSISTENT | SSEND | SEND | PREPARED);
205 MPI_Request Request::isend_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, PERSISTENT | ISEND | SEND | PREPARED);
212 MPI_Request Request::rma_send_init(void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
215 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
218 new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src)->get_pid(),
219 comm->group()->actor(dst)->get_pid(), tag, comm, RMA | NON_PERSISTENT | ISEND | SEND | PREPARED);
221 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src)->get_pid(),
222 comm->group()->actor(dst)->get_pid(), tag, comm,
223 RMA | NON_PERSISTENT | ISEND | SEND | PREPARED | ACCUMULATE);
229 MPI_Request Request::recv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
231 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
232 src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : comm->group()->actor(src)->get_pid(),
233 simgrid::s4u::this_actor::get_pid(), tag, comm, PERSISTENT | RECV | PREPARED);
236 MPI_Request Request::rma_recv_init(void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
239 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
241 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src)->get_pid(),
242 comm->group()->actor(dst)->get_pid(), tag, comm, RMA | NON_PERSISTENT | RECV | PREPARED);
244 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, comm->group()->actor(src)->get_pid(),
245 comm->group()->actor(dst)->get_pid(), tag, comm,
246 RMA | NON_PERSISTENT | RECV | PREPARED | ACCUMULATE);
252 MPI_Request Request::irecv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
254 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
255 src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : comm->group()->actor(src)->get_pid(),
256 simgrid::s4u::this_actor::get_pid(), tag, comm, PERSISTENT | RECV | PREPARED);
259 MPI_Request Request::isend(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
261 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
262 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
263 comm->group()->actor(dst)->get_pid(), tag, comm, NON_PERSISTENT | ISEND | SEND);
268 MPI_Request Request::issend(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
270 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
271 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
272 comm->group()->actor(dst)->get_pid(), tag, comm, NON_PERSISTENT | ISEND | SSEND | SEND);
278 MPI_Request Request::irecv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
280 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
281 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
282 src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : comm->group()->actor(src)->get_pid(),
283 simgrid::s4u::this_actor::get_pid(), tag, comm, NON_PERSISTENT | RECV);
288 void Request::recv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status * status)
290 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
291 request = irecv(buf, count, datatype, src, tag, comm);
292 wait(&request,status);
296 void Request::send(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, NON_PERSISTENT | SEND);
303 wait(&request, MPI_STATUS_IGNORE);
307 void Request::ssend(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
309 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
310 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, simgrid::s4u::this_actor::get_pid(),
311 comm->group()->actor(dst)->get_pid(), tag, comm, NON_PERSISTENT | SSEND | SEND);
314 wait(&request,MPI_STATUS_IGNORE);
318 void Request::sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,int dst, int sendtag,
319 void *recvbuf, int recvcount, MPI_Datatype recvtype, int src, int recvtag,
320 MPI_Comm comm, MPI_Status * status)
322 MPI_Request requests[2];
324 int myid = simgrid::s4u::this_actor::get_pid();
325 if ((comm->group()->actor(dst)->get_pid() == myid) && (comm->group()->actor(src)->get_pid() == myid)) {
326 Datatype::copy(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
327 if (status != MPI_STATUS_IGNORE) {
328 status->MPI_SOURCE = src;
329 status->MPI_TAG = recvtag;
330 status->MPI_ERROR = MPI_SUCCESS;
331 status->count = sendcount * sendtype->size();
335 requests[0] = isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
336 requests[1] = irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
337 startall(2, requests);
338 waitall(2, requests, stats);
341 if(status != MPI_STATUS_IGNORE) {
342 // Copy receive status
347 void Request::start()
349 smx_mailbox_t mailbox;
351 xbt_assert(action_ == nullptr, "Cannot (re-)start unfinished communication");
356 if ((flags_ & RECV) != 0) {
357 this->print_request("New recv");
359 simgrid::smpi::Process* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(dst_));
361 int async_small_thresh = xbt_cfg_get_int("smpi/async-small-thresh");
363 xbt_mutex_t mut = process->mailboxes_mutex();
364 if (async_small_thresh != 0 || (flags_ & RMA) != 0)
365 xbt_mutex_acquire(mut);
367 if (async_small_thresh == 0 && (flags_ & RMA) == 0 ) {
368 mailbox = process->mailbox();
370 else if (((flags_ & RMA) != 0) || static_cast<int>(size_) < async_small_thresh) {
371 //We have to check both mailboxes (because SSEND messages are sent to the large mbox).
372 //begin with the more appropriate one : the small one.
373 mailbox = process->mailbox_small();
374 XBT_DEBUG("Is there a corresponding send already posted in the small mailbox %p (in case of SSEND)?", mailbox);
375 smx_activity_t action = simcall_comm_iprobe(mailbox, 0, &match_recv, static_cast<void*>(this));
377 if (action == nullptr) {
378 mailbox = process->mailbox();
379 XBT_DEBUG("No, nothing in the small mailbox test the other one : %p", mailbox);
380 action = simcall_comm_iprobe(mailbox, 0, &match_recv, static_cast<void*>(this));
381 if (action == nullptr) {
382 XBT_DEBUG("Still nothing, switch back to the small mailbox : %p", mailbox);
383 mailbox = process->mailbox_small();
386 XBT_DEBUG("yes there was something for us in the large mailbox");
389 mailbox = process->mailbox_small();
390 XBT_DEBUG("Is there a corresponding send already posted the small mailbox?");
391 smx_activity_t action = simcall_comm_iprobe(mailbox, 0, &match_recv, static_cast<void*>(this));
393 if (action == nullptr) {
394 XBT_DEBUG("No, nothing in the permanent receive mailbox");
395 mailbox = process->mailbox();
397 XBT_DEBUG("yes there was something for us in the small mailbox");
401 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
403 action_ = simcall_comm_irecv(
404 process->process()->get_impl(), mailbox, buf_, &real_size_, &match_recv,
405 process->replaying() ? &smpi_comm_null_copy_buffer_callback : smpi_comm_copy_data_callback, this, -1.0);
406 XBT_DEBUG("recv simcall posted");
408 if (async_small_thresh != 0 || (flags_ & RMA) != 0 )
409 xbt_mutex_release(mut);
410 } else { /* the RECV flag was not set, so this is a send */
411 simgrid::smpi::Process* process = smpi_process_remote(simgrid::s4u::Actor::by_pid(dst_));
413 if (TRACE_smpi_view_internals()) {
414 TRACE_smpi_send(rank, rank, dst_, tag_, size_);
416 this->print_request("New send");
419 if ((flags_ & SSEND) == 0 && ( (flags_ & RMA) != 0
420 || static_cast<int>(size_) < xbt_cfg_get_int("smpi/send-is-detached-thresh") ) ) {
421 void *oldbuf = nullptr;
423 XBT_DEBUG("Send request %p is detached", this);
425 if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
427 if (not process->replaying() && oldbuf != nullptr && size_ != 0) {
428 if ((smpi_privatize_global_variables != SmpiPrivStrategies::None) &&
429 (static_cast<char*>(buf_) >= smpi_data_exe_start) &&
430 (static_cast<char*>(buf_) < smpi_data_exe_start + smpi_data_exe_size)) {
431 XBT_DEBUG("Privatization : We are sending from a zone inside global memory. Switch data segment ");
432 smpi_switch_data_segment(simgrid::s4u::Actor::by_pid(src_));
434 buf = xbt_malloc(size_);
435 memcpy(buf,oldbuf,size_);
436 XBT_DEBUG("buf %p copied into %p",oldbuf,buf);
441 //if we are giving back the control to the user without waiting for completion, we have to inject timings
442 double sleeptime = 0.0;
443 if (detached_ != 0 || ((flags_ & (ISEND | SSEND)) != 0)) { // issend should be treated as isend
444 // isend and send timings may be different
445 sleeptime = ((flags_ & ISEND) != 0)
446 ? simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::SmpiHost>()->oisend(size_)
447 : simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::SmpiHost>()->osend(size_);
451 simcall_process_sleep(sleeptime);
452 XBT_DEBUG("sending size of %zu : sleep %f ", size_, sleeptime);
455 int async_small_thresh = xbt_cfg_get_int("smpi/async-small-thresh");
457 xbt_mutex_t mut=process->mailboxes_mutex();
459 if (async_small_thresh != 0 || (flags_ & RMA) != 0)
460 xbt_mutex_acquire(mut);
462 if (not(async_small_thresh != 0 || (flags_ & RMA) != 0)) {
463 mailbox = process->mailbox();
464 } else if (((flags_ & RMA) != 0) || static_cast<int>(size_) < async_small_thresh) { // eager mode
465 mailbox = process->mailbox();
466 XBT_DEBUG("Is there a corresponding recv already posted in the large mailbox %p?", mailbox);
467 smx_activity_t action = simcall_comm_iprobe(mailbox, 1, &match_send, static_cast<void*>(this));
468 if (action == nullptr) {
469 if ((flags_ & SSEND) == 0){
470 mailbox = process->mailbox_small();
471 XBT_DEBUG("No, nothing in the large mailbox, message is to be sent on the small one %p", mailbox);
473 mailbox = process->mailbox_small();
474 XBT_DEBUG("SSEND : Is there a corresponding recv already posted in the small mailbox %p?", mailbox);
475 action = simcall_comm_iprobe(mailbox, 1, &match_send, static_cast<void*>(this));
476 if (action == nullptr) {
477 XBT_DEBUG("No, we are first, send to large mailbox");
478 mailbox = process->mailbox();
482 XBT_DEBUG("Yes there was something for us in the large mailbox");
485 mailbox = process->mailbox();
486 XBT_DEBUG("Send request %p is in the large mailbox %p (buf: %p)",mailbox, this,buf_);
489 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
491 action_ = simcall_comm_isend(
492 simgrid::s4u::Actor::by_pid(src_)->get_impl(), mailbox, size_, -1.0, buf, real_size_, &match_send,
493 &xbt_free_f, // how to free the userdata if a detached send fails
494 not process->replaying() ? smpi_comm_copy_data_callback : &smpi_comm_null_copy_buffer_callback, this,
495 // detach if msg size < eager/rdv switch limit
497 XBT_DEBUG("send simcall posted");
499 /* FIXME: detached sends are not traceable (action_ == nullptr) */
500 if (action_ != nullptr)
501 simcall_set_category(action_, TRACE_internal_smpi_get_category());
502 if (async_small_thresh != 0 || ((flags_ & RMA)!=0))
503 xbt_mutex_release(mut);
507 void Request::startall(int count, MPI_Request * requests)
509 if(requests== nullptr)
512 for(int i = 0; i < count; i++) {
513 requests[i]->start();
517 void Request::cancel()
521 if (this->action_ != nullptr)
522 (boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(this->action_))->cancel();
525 int Request::test(MPI_Request * request, MPI_Status * status) {
526 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or testall before)
527 // to avoid deadlocks if used as a break condition, such as
528 // while (MPI_Test(request, flag, status) && flag) dostuff...
529 // because the time will not normally advance when only calls to MPI_Test are made -> deadlock
530 // multiplier to the sleeptime, to increase speed of execution, each failed test will increase it
531 static int nsleeps = 1;
532 if(smpi_test_sleep > 0)
533 simcall_process_sleep(nsleeps*smpi_test_sleep);
535 Status::empty(status);
537 if (((*request)->flags_ & PREPARED) == 0) {
538 if ((*request)->action_ != nullptr){
540 flag = simcall_comm_test((*request)->action_);
546 finish_wait(request,status);
547 nsleeps=1;//reset the number of sleeps we will do next time
548 if (*request != MPI_REQUEST_NULL && ((*request)->flags_ & PERSISTENT) == 0)
549 *request = MPI_REQUEST_NULL;
550 } else if (simgrid::config::get_config<bool>("smpi/grow-injected-times")) {
557 int Request::testsome(int incount, MPI_Request requests[], int *indices, MPI_Status status[])
562 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
564 for (int i = 0; i < incount; i++) {
565 if (requests[i] != MPI_REQUEST_NULL) {
566 if (test(&requests[i], pstat)) {
569 if (status != MPI_STATUSES_IGNORE)
571 if ((requests[i] != MPI_REQUEST_NULL) && (requests[i]->flags_ & NON_PERSISTENT))
572 requests[i] = MPI_REQUEST_NULL;
578 if(count_dead==incount)
579 return MPI_UNDEFINED;
583 int Request::testany(int count, MPI_Request requests[], int *index, MPI_Status * status)
585 std::vector<simgrid::kernel::activity::ActivityImplPtr> comms;
586 comms.reserve(count);
591 *index = MPI_UNDEFINED;
593 std::vector<int> map; /** Maps all matching comms back to their location in requests **/
594 for(i = 0; i < count; i++) {
595 if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->action_ && not(requests[i]->flags_ & PREPARED)) {
596 comms.push_back(requests[i]->action_);
600 if (not map.empty()) {
601 //multiplier to the sleeptime, to increase speed of execution, each failed testany will increase it
602 static int nsleeps = 1;
603 if(smpi_test_sleep > 0)
604 simcall_process_sleep(nsleeps*smpi_test_sleep);
606 i = simcall_comm_testany(comms.data(), comms.size()); // The i-th element in comms matches!
611 if (i != -1) { // -1 is not MPI_UNDEFINED but a SIMIX return code. (nothing matches)
613 finish_wait(&requests[*index],status);
616 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags_ & NON_PERSISTENT)) {
617 requests[*index] = MPI_REQUEST_NULL;
623 //all requests are null or inactive, return true
625 Status::empty(status);
631 int Request::testall(int count, MPI_Request requests[], MPI_Status status[])
634 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
636 for(int i=0; i<count; i++){
637 if (requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & PREPARED)) {
638 if (test(&requests[i], pstat)!=1){
641 requests[i]=MPI_REQUEST_NULL;
644 Status::empty(pstat);
646 if(status != MPI_STATUSES_IGNORE) {
653 void Request::probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
655 //FIXME find another way to avoid busy waiting ?
656 // the issue here is that we have to wait on a nonexistent comm
658 iprobe(source, tag, comm, &flag, status);
659 XBT_DEBUG("Busy Waiting on probing : %d", flag);
663 void Request::iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
664 // to avoid deadlock, we have to sleep some time here, or the timer won't advance and we will only do iprobe simcalls
665 // especially when used as a break condition, such as while (MPI_Iprobe(...)) dostuff...
666 // nsleeps is a multiplier to the sleeptime, to increase speed of execution, each failed iprobe will increase it
667 // This can speed up the execution of certain applications by an order of magnitude, such as HPL
668 static int nsleeps = 1;
669 double speed = simgrid::s4u::Actor::self()->get_host()->getSpeed();
670 double maxrate = xbt_cfg_get_double("smpi/iprobe-cpu-usage");
671 MPI_Request request = new Request(nullptr, 0, MPI_CHAR,
672 source == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : comm->group()->actor(source)->get_pid(),
673 simgrid::s4u::this_actor::get_pid(), tag, comm, PERSISTENT | RECV);
674 if (smpi_iprobe_sleep > 0) {
675 smx_activity_t iprobe_sleep = simcall_execution_start(
676 "iprobe", /* flops to executek*/ nsleeps * smpi_iprobe_sleep * speed * maxrate, /* priority */ 1.0,
677 /* performance bound */ maxrate * speed, smpi_process()->process()->get_impl()->host);
678 simcall_execution_wait(iprobe_sleep);
680 // behave like a receive, but don't do it
681 smx_mailbox_t mailbox;
683 request->print_request("New iprobe");
684 // We have to test both mailboxes as we don't know if we will receive one one or another
685 if (xbt_cfg_get_int("smpi/async-small-thresh") > 0){
686 mailbox = smpi_process()->mailbox_small();
687 XBT_DEBUG("Trying to probe the perm recv mailbox");
688 request->action_ = simcall_comm_iprobe(mailbox, 0, &match_recv, static_cast<void*>(request));
691 if (request->action_ == nullptr){
692 mailbox = smpi_process()->mailbox();
693 XBT_DEBUG("trying to probe the other mailbox");
694 request->action_ = simcall_comm_iprobe(mailbox, 0, &match_recv, static_cast<void*>(request));
697 if (request->action_ != nullptr){
698 simgrid::kernel::activity::CommImplPtr sync_comm =
699 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(request->action_);
700 MPI_Request req = static_cast<MPI_Request>(sync_comm->src_data);
702 if(status != MPI_STATUS_IGNORE && (req->flags_ & PREPARED) == 0) {
703 status->MPI_SOURCE = comm->group()->rank(req->src_);
704 status->MPI_TAG = req->tag_;
705 status->MPI_ERROR = MPI_SUCCESS;
706 status->count = req->real_size_;
708 nsleeps = 1;//reset the number of sleeps we will do next time
712 if (simgrid::config::get_config<bool>("smpi/grow-injected-times"))
718 void Request::finish_wait(MPI_Request* request, MPI_Status * status)
720 MPI_Request req = *request;
721 Status::empty(status);
723 if (req->cancelled_==1){
724 if (status!=MPI_STATUS_IGNORE)
729 if (not((req->detached_ != 0) && ((req->flags_ & SEND) != 0)) && ((req->flags_ & PREPARED) == 0)) {
730 if(status != MPI_STATUS_IGNORE) {
731 int src = req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_;
732 status->MPI_SOURCE = req->comm_->group()->rank(src);
733 status->MPI_TAG = req->tag_ == MPI_ANY_TAG ? req->real_tag_ : req->tag_;
734 status->MPI_ERROR = req->truncated_ != 0 ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
735 // this handles the case were size in receive differs from size in send
736 status->count = req->real_size_;
739 req->print_request("Finishing");
740 MPI_Datatype datatype = req->old_type_;
742 // FIXME Handle the case of a partial shared malloc.
743 if (((req->flags_ & ACCUMULATE) != 0) ||
744 (datatype->flags() & DT_FLAG_DERIVED)) { // && (not smpi_is_shared(req->old_buf_))){
746 if (not smpi_process()->replaying() && smpi_privatize_global_variables != SmpiPrivStrategies::None &&
747 static_cast<char*>(req->old_buf_) >= smpi_data_exe_start &&
748 static_cast<char*>(req->old_buf_) < smpi_data_exe_start + smpi_data_exe_size) {
749 XBT_VERB("Privatization : We are unserializing to a zone in global memory Switch data segment ");
750 smpi_switch_data_segment(simgrid::s4u::Actor::self());
753 if(datatype->flags() & DT_FLAG_DERIVED){
754 // This part handles the problem of non-contignous memory the unserialization at the reception
755 if((req->flags_ & RECV) && datatype->size()!=0)
756 datatype->unserialize(req->buf_, req->old_buf_, req->real_size_/datatype->size() , req->op_);
758 }else if(req->flags_ & RECV){//apply op on contiguous buffer for accumulate
759 if(datatype->size()!=0){
760 int n =req->real_size_/datatype->size();
761 req->op_->apply(req->buf_, req->old_buf_, &n, datatype);
768 if (TRACE_smpi_view_internals() && ((req->flags_ & RECV) != 0)){
769 int rank = simgrid::s4u::this_actor::get_pid();
770 int src_traced = (req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_);
771 TRACE_smpi_recv(src_traced, rank,req->tag_);
773 if(req->detached_sender_ != nullptr){
774 //integrate pseudo-timing for buffering of small messages, do not bother to execute the simcall if 0
776 simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::SmpiHost>()->orecv(req->real_size());
778 simcall_process_sleep(sleeptime);
779 XBT_DEBUG("receiving size of %zu : sleep %f ", req->real_size_, sleeptime);
781 unref(&(req->detached_sender_));
783 if(req->flags_ & PERSISTENT)
784 req->action_ = nullptr;
785 req->flags_ |= FINISHED;
789 void Request::wait(MPI_Request * request, MPI_Status * status)
791 (*request)->print_request("Waiting");
792 if ((*request)->flags_ & PREPARED) {
793 Status::empty(status);
797 if ((*request)->action_ != nullptr){
799 // this is not a detached send
800 simcall_comm_wait((*request)->action_, -1.0);
802 XBT_VERB("Request cancelled");
807 finish_wait(request,status);
808 if (*request != MPI_REQUEST_NULL && (((*request)->flags_ & NON_PERSISTENT)!=0))
809 *request = MPI_REQUEST_NULL;
812 int Request::waitany(int count, MPI_Request requests[], MPI_Status * status)
814 s_xbt_dynar_t comms; // Keep it on stack to save some extra mallocs
815 int index = MPI_UNDEFINED;
819 // Wait for a request to complete
820 xbt_dynar_init(&comms, sizeof(smx_activity_t), [](void*ptr){
821 intrusive_ptr_release(*(simgrid::kernel::activity::ActivityImpl**)ptr);
823 int *map = xbt_new(int, count);
824 XBT_DEBUG("Wait for one of %d", count);
825 for(int i = 0; i < count; i++) {
826 if (requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & PREPARED) &&
827 not(requests[i]->flags_ & FINISHED)) {
828 if (requests[i]->action_ != nullptr) {
829 XBT_DEBUG("Waiting any %p ", requests[i]);
830 intrusive_ptr_add_ref(requests[i]->action_.get());
831 xbt_dynar_push_as(&comms, simgrid::kernel::activity::ActivityImpl*, requests[i]->action_.get());
835 // This is a finished detached request, let's return this one
836 size = 0; // so we free the dynar but don't do the waitany call
838 finish_wait(&requests[i], status); // cleanup if refcount = 0
839 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags_ & NON_PERSISTENT))
840 requests[i] = MPI_REQUEST_NULL; // set to null
846 XBT_DEBUG("Enter waitany for %lu comms", xbt_dynar_length(&comms));
849 // this is not a detached send
850 i = simcall_comm_waitany(&comms, -1);
852 XBT_INFO("request %d cancelled ",i);
856 // not MPI_UNDEFINED, as this is a simix return code
859 //in case of an accumulate, we have to wait the end of all requests to apply the operation, ordered correctly.
860 if ((requests[index] == MPI_REQUEST_NULL) ||
861 (not((requests[index]->flags_ & ACCUMULATE) && (requests[index]->flags_ & RECV)))) {
862 finish_wait(&requests[index],status);
863 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags_ & NON_PERSISTENT))
864 requests[index] = MPI_REQUEST_NULL;
869 xbt_dynar_free_data(&comms);
873 if (index==MPI_UNDEFINED)
874 Status::empty(status);
879 static int sort_accumulates(MPI_Request a, MPI_Request b)
881 return (a->tag() > b->tag());
884 int Request::waitall(int count, MPI_Request requests[], MPI_Status status[])
886 std::vector<MPI_Request> accumulates;
889 MPI_Status *pstat = (status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat);
890 int retvalue = MPI_SUCCESS;
891 //tag invalid requests in the set
892 if (status != MPI_STATUSES_IGNORE) {
893 for (int c = 0; c < count; c++) {
894 if (requests[c] == MPI_REQUEST_NULL || requests[c]->dst_ == MPI_PROC_NULL || (requests[c]->flags_ & PREPARED)) {
895 Status::empty(&status[c]);
896 } else if (requests[c]->src_ == MPI_PROC_NULL) {
897 Status::empty(&status[c]);
898 status[c].MPI_SOURCE = MPI_PROC_NULL;
902 for (int c = 0; c < count; c++) {
903 if (MC_is_active() || MC_record_replay_is_active()) {
904 wait(&requests[c],pstat);
907 index = waitany(count, (MPI_Request*)requests, pstat);
909 if (index == MPI_UNDEFINED)
912 if (requests[index] != MPI_REQUEST_NULL
913 && (requests[index]->flags_ & RECV)
914 && (requests[index]->flags_ & ACCUMULATE))
915 accumulates.push_back(requests[index]);
916 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & NON_PERSISTENT))
917 requests[index] = MPI_REQUEST_NULL;
919 if (status != MPI_STATUSES_IGNORE) {
920 status[index] = *pstat;
921 if (status[index].MPI_ERROR == MPI_ERR_TRUNCATE)
922 retvalue = MPI_ERR_IN_STATUS;
926 if (not accumulates.empty()) {
927 std::sort(accumulates.begin(), accumulates.end(), sort_accumulates);
928 for (auto& req : accumulates) {
929 finish_wait(&req, status);
936 int Request::waitsome(int incount, MPI_Request requests[], int *indices, MPI_Status status[])
940 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
942 for (int i = 0; i < incount; i++) {
943 int index = waitany(incount, requests, pstat);
944 if(index!=MPI_UNDEFINED){
945 indices[count] = index;
947 if(status != MPI_STATUSES_IGNORE) {
948 status[index] = *pstat;
950 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & NON_PERSISTENT))
951 requests[index] = MPI_REQUEST_NULL;
953 return MPI_UNDEFINED;
959 MPI_Request Request::f2c(int id) {
961 if(id==MPI_FORTRAN_REQUEST_NULL)
962 return static_cast<MPI_Request>(MPI_REQUEST_NULL);
963 return static_cast<MPI_Request>(F2C::f2c_lookup()->at(get_key_id(key, id)));
968 if (F2C::f2c_lookup() == nullptr) {
969 F2C::set_f2c_lookup(new std::unordered_map<std::string, F2C*>);
972 (*(F2C::f2c_lookup()))[get_key_id(key, F2C::f2c_id())] = this;
973 F2C::f2c_id_increment();
974 return F2C::f2c_id()-1;
977 void Request::free_f(int id)
979 if (id != MPI_FORTRAN_REQUEST_NULL) {
981 F2C::f2c_lookup()->erase(get_key_id(key, id));