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 = simgrid::config::get_value<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 &&
420 ((flags_ & RMA) != 0 ||
421 static_cast<int>(size_) < simgrid::config::get_value<int>("smpi/send-is-detached-thresh"))) {
422 void *oldbuf = nullptr;
424 XBT_DEBUG("Send request %p is detached", this);
426 if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
428 if (not process->replaying() && oldbuf != nullptr && size_ != 0) {
429 if ((smpi_privatize_global_variables != SmpiPrivStrategies::None) &&
430 (static_cast<char*>(buf_) >= smpi_data_exe_start) &&
431 (static_cast<char*>(buf_) < smpi_data_exe_start + smpi_data_exe_size)) {
432 XBT_DEBUG("Privatization : We are sending from a zone inside global memory. Switch data segment ");
433 smpi_switch_data_segment(simgrid::s4u::Actor::by_pid(src_));
435 buf = xbt_malloc(size_);
436 memcpy(buf,oldbuf,size_);
437 XBT_DEBUG("buf %p copied into %p",oldbuf,buf);
442 //if we are giving back the control to the user without waiting for completion, we have to inject timings
443 double sleeptime = 0.0;
444 if (detached_ != 0 || ((flags_ & (ISEND | SSEND)) != 0)) { // issend should be treated as isend
445 // isend and send timings may be different
446 sleeptime = ((flags_ & ISEND) != 0)
447 ? simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::SmpiHost>()->oisend(size_)
448 : simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::SmpiHost>()->osend(size_);
452 simcall_process_sleep(sleeptime);
453 XBT_DEBUG("sending size of %zu : sleep %f ", size_, sleeptime);
456 int async_small_thresh = simgrid::config::get_value<int>("smpi/async-small-thresh");
458 xbt_mutex_t mut=process->mailboxes_mutex();
460 if (async_small_thresh != 0 || (flags_ & RMA) != 0)
461 xbt_mutex_acquire(mut);
463 if (not(async_small_thresh != 0 || (flags_ & RMA) != 0)) {
464 mailbox = process->mailbox();
465 } else if (((flags_ & RMA) != 0) || static_cast<int>(size_) < async_small_thresh) { // eager mode
466 mailbox = process->mailbox();
467 XBT_DEBUG("Is there a corresponding recv already posted in the large mailbox %p?", mailbox);
468 smx_activity_t action = simcall_comm_iprobe(mailbox, 1, &match_send, static_cast<void*>(this));
469 if (action == nullptr) {
470 if ((flags_ & SSEND) == 0){
471 mailbox = process->mailbox_small();
472 XBT_DEBUG("No, nothing in the large mailbox, message is to be sent on the small one %p", mailbox);
474 mailbox = process->mailbox_small();
475 XBT_DEBUG("SSEND : Is there a corresponding recv already posted in the small mailbox %p?", mailbox);
476 action = simcall_comm_iprobe(mailbox, 1, &match_send, static_cast<void*>(this));
477 if (action == nullptr) {
478 XBT_DEBUG("No, we are first, send to large mailbox");
479 mailbox = process->mailbox();
483 XBT_DEBUG("Yes there was something for us in the large mailbox");
486 mailbox = process->mailbox();
487 XBT_DEBUG("Send request %p is in the large mailbox %p (buf: %p)",mailbox, this,buf_);
490 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
492 action_ = simcall_comm_isend(
493 simgrid::s4u::Actor::by_pid(src_)->get_impl(), mailbox, size_, -1.0, buf, real_size_, &match_send,
494 &xbt_free_f, // how to free the userdata if a detached send fails
495 not process->replaying() ? smpi_comm_copy_data_callback : &smpi_comm_null_copy_buffer_callback, this,
496 // detach if msg size < eager/rdv switch limit
498 XBT_DEBUG("send simcall posted");
500 /* FIXME: detached sends are not traceable (action_ == nullptr) */
501 if (action_ != nullptr)
502 simcall_set_category(action_, TRACE_internal_smpi_get_category());
503 if (async_small_thresh != 0 || ((flags_ & RMA)!=0))
504 xbt_mutex_release(mut);
508 void Request::startall(int count, MPI_Request * requests)
510 if(requests== nullptr)
513 for(int i = 0; i < count; i++) {
514 requests[i]->start();
518 void Request::cancel()
522 if (this->action_ != nullptr)
523 (boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(this->action_))->cancel();
526 int Request::test(MPI_Request * request, MPI_Status * status) {
527 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or testall before)
528 // to avoid deadlocks if used as a break condition, such as
529 // while (MPI_Test(request, flag, status) && flag) dostuff...
530 // because the time will not normally advance when only calls to MPI_Test are made -> deadlock
531 // multiplier to the sleeptime, to increase speed of execution, each failed test will increase it
532 static int nsleeps = 1;
533 if(smpi_test_sleep > 0)
534 simcall_process_sleep(nsleeps*smpi_test_sleep);
536 Status::empty(status);
538 if (((*request)->flags_ & PREPARED) == 0) {
539 if ((*request)->action_ != nullptr){
541 flag = simcall_comm_test((*request)->action_);
547 finish_wait(request,status);
548 nsleeps=1;//reset the number of sleeps we will do next time
549 if (*request != MPI_REQUEST_NULL && ((*request)->flags_ & PERSISTENT) == 0)
550 *request = MPI_REQUEST_NULL;
551 } else if (simgrid::config::get_value<bool>("smpi/grow-injected-times")) {
558 int Request::testsome(int incount, MPI_Request requests[], int *indices, MPI_Status status[])
563 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
565 for (int i = 0; i < incount; i++) {
566 if (requests[i] != MPI_REQUEST_NULL) {
567 if (test(&requests[i], pstat)) {
570 if (status != MPI_STATUSES_IGNORE)
572 if ((requests[i] != MPI_REQUEST_NULL) && (requests[i]->flags_ & NON_PERSISTENT))
573 requests[i] = MPI_REQUEST_NULL;
579 if(count_dead==incount)
580 return MPI_UNDEFINED;
584 int Request::testany(int count, MPI_Request requests[], int *index, MPI_Status * status)
586 std::vector<simgrid::kernel::activity::ActivityImplPtr> comms;
587 comms.reserve(count);
592 *index = MPI_UNDEFINED;
594 std::vector<int> map; /** Maps all matching comms back to their location in requests **/
595 for(i = 0; i < count; i++) {
596 if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->action_ && not(requests[i]->flags_ & PREPARED)) {
597 comms.push_back(requests[i]->action_);
601 if (not map.empty()) {
602 //multiplier to the sleeptime, to increase speed of execution, each failed testany will increase it
603 static int nsleeps = 1;
604 if(smpi_test_sleep > 0)
605 simcall_process_sleep(nsleeps*smpi_test_sleep);
607 i = simcall_comm_testany(comms.data(), comms.size()); // The i-th element in comms matches!
612 if (i != -1) { // -1 is not MPI_UNDEFINED but a SIMIX return code. (nothing matches)
614 finish_wait(&requests[*index],status);
617 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags_ & NON_PERSISTENT)) {
618 requests[*index] = MPI_REQUEST_NULL;
624 //all requests are null or inactive, return true
626 Status::empty(status);
632 int Request::testall(int count, MPI_Request requests[], MPI_Status status[])
635 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
637 for(int i=0; i<count; i++){
638 if (requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & PREPARED)) {
639 if (test(&requests[i], pstat)!=1){
642 requests[i]=MPI_REQUEST_NULL;
645 Status::empty(pstat);
647 if(status != MPI_STATUSES_IGNORE) {
654 void Request::probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
656 //FIXME find another way to avoid busy waiting ?
657 // the issue here is that we have to wait on a nonexistent comm
659 iprobe(source, tag, comm, &flag, status);
660 XBT_DEBUG("Busy Waiting on probing : %d", flag);
664 void Request::iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
665 // to avoid deadlock, we have to sleep some time here, or the timer won't advance and we will only do iprobe simcalls
666 // especially when used as a break condition, such as while (MPI_Iprobe(...)) dostuff...
667 // nsleeps is a multiplier to the sleeptime, to increase speed of execution, each failed iprobe will increase it
668 // This can speed up the execution of certain applications by an order of magnitude, such as HPL
669 static int nsleeps = 1;
670 double speed = simgrid::s4u::Actor::self()->get_host()->getSpeed();
671 double maxrate = simgrid::config::get_value<double>("smpi/iprobe-cpu-usage");
672 MPI_Request request = new Request(nullptr, 0, MPI_CHAR,
673 source == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : comm->group()->actor(source)->get_pid(),
674 simgrid::s4u::this_actor::get_pid(), tag, comm, PERSISTENT | RECV);
675 if (smpi_iprobe_sleep > 0) {
676 smx_activity_t iprobe_sleep = simcall_execution_start(
677 "iprobe", /* flops to executek*/ nsleeps * smpi_iprobe_sleep * speed * maxrate, /* priority */ 1.0,
678 /* performance bound */ maxrate * speed, smpi_process()->process()->get_impl()->host);
679 simcall_execution_wait(iprobe_sleep);
681 // behave like a receive, but don't do it
682 smx_mailbox_t mailbox;
684 request->print_request("New iprobe");
685 // We have to test both mailboxes as we don't know if we will receive one one or another
686 if (simgrid::config::get_value<int>("smpi/async-small-thresh") > 0) {
687 mailbox = smpi_process()->mailbox_small();
688 XBT_DEBUG("Trying to probe the perm recv mailbox");
689 request->action_ = simcall_comm_iprobe(mailbox, 0, &match_recv, static_cast<void*>(request));
692 if (request->action_ == nullptr){
693 mailbox = smpi_process()->mailbox();
694 XBT_DEBUG("trying to probe the other mailbox");
695 request->action_ = simcall_comm_iprobe(mailbox, 0, &match_recv, static_cast<void*>(request));
698 if (request->action_ != nullptr){
699 simgrid::kernel::activity::CommImplPtr sync_comm =
700 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(request->action_);
701 MPI_Request req = static_cast<MPI_Request>(sync_comm->src_data);
703 if(status != MPI_STATUS_IGNORE && (req->flags_ & PREPARED) == 0) {
704 status->MPI_SOURCE = comm->group()->rank(req->src_);
705 status->MPI_TAG = req->tag_;
706 status->MPI_ERROR = MPI_SUCCESS;
707 status->count = req->real_size_;
709 nsleeps = 1;//reset the number of sleeps we will do next time
713 if (simgrid::config::get_value<bool>("smpi/grow-injected-times"))
719 void Request::finish_wait(MPI_Request* request, MPI_Status * status)
721 MPI_Request req = *request;
722 Status::empty(status);
724 if (req->cancelled_==1){
725 if (status!=MPI_STATUS_IGNORE)
730 if (not((req->detached_ != 0) && ((req->flags_ & SEND) != 0)) && ((req->flags_ & PREPARED) == 0)) {
731 if(status != MPI_STATUS_IGNORE) {
732 int src = req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_;
733 status->MPI_SOURCE = req->comm_->group()->rank(src);
734 status->MPI_TAG = req->tag_ == MPI_ANY_TAG ? req->real_tag_ : req->tag_;
735 status->MPI_ERROR = req->truncated_ != 0 ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
736 // this handles the case were size in receive differs from size in send
737 status->count = req->real_size_;
740 req->print_request("Finishing");
741 MPI_Datatype datatype = req->old_type_;
743 // FIXME Handle the case of a partial shared malloc.
744 if (((req->flags_ & ACCUMULATE) != 0) ||
745 (datatype->flags() & DT_FLAG_DERIVED)) { // && (not smpi_is_shared(req->old_buf_))){
747 if (not smpi_process()->replaying() && smpi_privatize_global_variables != SmpiPrivStrategies::None &&
748 static_cast<char*>(req->old_buf_) >= smpi_data_exe_start &&
749 static_cast<char*>(req->old_buf_) < smpi_data_exe_start + smpi_data_exe_size) {
750 XBT_VERB("Privatization : We are unserializing to a zone in global memory Switch data segment ");
751 smpi_switch_data_segment(simgrid::s4u::Actor::self());
754 if(datatype->flags() & DT_FLAG_DERIVED){
755 // This part handles the problem of non-contignous memory the unserialization at the reception
756 if((req->flags_ & RECV) && datatype->size()!=0)
757 datatype->unserialize(req->buf_, req->old_buf_, req->real_size_/datatype->size() , req->op_);
759 }else if(req->flags_ & RECV){//apply op on contiguous buffer for accumulate
760 if(datatype->size()!=0){
761 int n =req->real_size_/datatype->size();
762 req->op_->apply(req->buf_, req->old_buf_, &n, datatype);
769 if (TRACE_smpi_view_internals() && ((req->flags_ & RECV) != 0)){
770 int rank = simgrid::s4u::this_actor::get_pid();
771 int src_traced = (req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_);
772 TRACE_smpi_recv(src_traced, rank,req->tag_);
774 if(req->detached_sender_ != nullptr){
775 //integrate pseudo-timing for buffering of small messages, do not bother to execute the simcall if 0
777 simgrid::s4u::Actor::self()->get_host()->extension<simgrid::smpi::SmpiHost>()->orecv(req->real_size());
779 simcall_process_sleep(sleeptime);
780 XBT_DEBUG("receiving size of %zu : sleep %f ", req->real_size_, sleeptime);
782 unref(&(req->detached_sender_));
784 if(req->flags_ & PERSISTENT)
785 req->action_ = nullptr;
786 req->flags_ |= FINISHED;
790 void Request::wait(MPI_Request * request, MPI_Status * status)
792 (*request)->print_request("Waiting");
793 if ((*request)->flags_ & PREPARED) {
794 Status::empty(status);
798 if ((*request)->action_ != nullptr){
800 // this is not a detached send
801 simcall_comm_wait((*request)->action_, -1.0);
803 XBT_VERB("Request cancelled");
808 finish_wait(request,status);
809 if (*request != MPI_REQUEST_NULL && (((*request)->flags_ & NON_PERSISTENT)!=0))
810 *request = MPI_REQUEST_NULL;
813 int Request::waitany(int count, MPI_Request requests[], MPI_Status * status)
815 s_xbt_dynar_t comms; // Keep it on stack to save some extra mallocs
816 int index = MPI_UNDEFINED;
820 // Wait for a request to complete
821 xbt_dynar_init(&comms, sizeof(smx_activity_t), [](void*ptr){
822 intrusive_ptr_release(*(simgrid::kernel::activity::ActivityImpl**)ptr);
824 int *map = xbt_new(int, count);
825 XBT_DEBUG("Wait for one of %d", count);
826 for(int i = 0; i < count; i++) {
827 if (requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & PREPARED) &&
828 not(requests[i]->flags_ & FINISHED)) {
829 if (requests[i]->action_ != nullptr) {
830 XBT_DEBUG("Waiting any %p ", requests[i]);
831 intrusive_ptr_add_ref(requests[i]->action_.get());
832 xbt_dynar_push_as(&comms, simgrid::kernel::activity::ActivityImpl*, requests[i]->action_.get());
836 // This is a finished detached request, let's return this one
837 size = 0; // so we free the dynar but don't do the waitany call
839 finish_wait(&requests[i], status); // cleanup if refcount = 0
840 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags_ & NON_PERSISTENT))
841 requests[i] = MPI_REQUEST_NULL; // set to null
847 XBT_DEBUG("Enter waitany for %lu comms", xbt_dynar_length(&comms));
850 // this is not a detached send
851 i = simcall_comm_waitany(&comms, -1);
853 XBT_INFO("request %d cancelled ",i);
857 // not MPI_UNDEFINED, as this is a simix return code
860 //in case of an accumulate, we have to wait the end of all requests to apply the operation, ordered correctly.
861 if ((requests[index] == MPI_REQUEST_NULL) ||
862 (not((requests[index]->flags_ & ACCUMULATE) && (requests[index]->flags_ & RECV)))) {
863 finish_wait(&requests[index],status);
864 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags_ & NON_PERSISTENT))
865 requests[index] = MPI_REQUEST_NULL;
870 xbt_dynar_free_data(&comms);
874 if (index==MPI_UNDEFINED)
875 Status::empty(status);
880 static int sort_accumulates(MPI_Request a, MPI_Request b)
882 return (a->tag() > b->tag());
885 int Request::waitall(int count, MPI_Request requests[], MPI_Status status[])
887 std::vector<MPI_Request> accumulates;
890 MPI_Status *pstat = (status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat);
891 int retvalue = MPI_SUCCESS;
892 //tag invalid requests in the set
893 if (status != MPI_STATUSES_IGNORE) {
894 for (int c = 0; c < count; c++) {
895 if (requests[c] == MPI_REQUEST_NULL || requests[c]->dst_ == MPI_PROC_NULL || (requests[c]->flags_ & PREPARED)) {
896 Status::empty(&status[c]);
897 } else if (requests[c]->src_ == MPI_PROC_NULL) {
898 Status::empty(&status[c]);
899 status[c].MPI_SOURCE = MPI_PROC_NULL;
903 for (int c = 0; c < count; c++) {
904 if (MC_is_active() || MC_record_replay_is_active()) {
905 wait(&requests[c],pstat);
908 index = waitany(count, (MPI_Request*)requests, pstat);
910 if (index == MPI_UNDEFINED)
913 if (requests[index] != MPI_REQUEST_NULL
914 && (requests[index]->flags_ & RECV)
915 && (requests[index]->flags_ & ACCUMULATE))
916 accumulates.push_back(requests[index]);
917 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & NON_PERSISTENT))
918 requests[index] = MPI_REQUEST_NULL;
920 if (status != MPI_STATUSES_IGNORE) {
921 status[index] = *pstat;
922 if (status[index].MPI_ERROR == MPI_ERR_TRUNCATE)
923 retvalue = MPI_ERR_IN_STATUS;
927 if (not accumulates.empty()) {
928 std::sort(accumulates.begin(), accumulates.end(), sort_accumulates);
929 for (auto& req : accumulates) {
930 finish_wait(&req, status);
937 int Request::waitsome(int incount, MPI_Request requests[], int *indices, MPI_Status status[])
941 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
943 for (int i = 0; i < incount; i++) {
944 int index = waitany(incount, requests, pstat);
945 if(index!=MPI_UNDEFINED){
946 indices[count] = index;
948 if(status != MPI_STATUSES_IGNORE) {
949 status[index] = *pstat;
951 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & NON_PERSISTENT))
952 requests[index] = MPI_REQUEST_NULL;
954 return MPI_UNDEFINED;
960 MPI_Request Request::f2c(int id) {
962 if(id==MPI_FORTRAN_REQUEST_NULL)
963 return static_cast<MPI_Request>(MPI_REQUEST_NULL);
964 return static_cast<MPI_Request>(F2C::f2c_lookup()->at(get_key_id(key, id)));
969 if (F2C::f2c_lookup() == nullptr) {
970 F2C::set_f2c_lookup(new std::unordered_map<std::string, F2C*>);
973 (*(F2C::f2c_lookup()))[get_key_id(key, F2C::f2c_id())] = this;
974 F2C::f2c_id_increment();
975 return F2C::f2c_id()-1;
978 void Request::free_f(int id)
980 if (id != MPI_FORTRAN_REQUEST_NULL) {
982 F2C::f2c_lookup()->erase(get_key_id(key, id));