1 /* Copyright (c) 2007-2017. 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"
9 #include "src/kernel/activity/CommImpl.hpp"
10 #include "src/mc/mc_replay.h"
11 #include "SmpiHost.hpp"
12 #include "private.hpp"
13 #include "smpi_comm.hpp"
14 #include "smpi_datatype.hpp"
15 #include "smpi_op.hpp"
16 #include "smpi_process.hpp"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_request, smpi, "Logging specific to SMPI (reques)");
22 static simgrid::config::Flag<double> smpi_iprobe_sleep(
23 "smpi/iprobe", "Minimum time to inject inside a call to MPI_Iprobe", 1e-4);
24 static simgrid::config::Flag<double> smpi_test_sleep(
25 "smpi/test", "Minimum time to inject inside a call to MPI_Test", 1e-4);
27 std::vector<s_smpi_factor_t> smpi_ois_values;
29 extern void (*smpi_comm_copy_data_callback) (smx_activity_t, void*, size_t);
34 Request::Request(void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm, unsigned flags) : buf_(buf), old_type_(datatype), src_(src), dst_(dst), tag_(tag), comm_(comm), flags_(flags)
36 void *old_buf = nullptr;
37 // FIXME Handle the case of a partial shared malloc.
38 if ((((flags & RECV) != 0) && ((flags & ACCUMULATE) != 0)) || (datatype->flags() & DT_FLAG_DERIVED)) {
39 // This part handles the problem of non-contiguous memory
44 buf_ = xbt_malloc(count*datatype->size());
45 if ((datatype->flags() & DT_FLAG_DERIVED) && ((flags & SEND) != 0)) {
46 datatype->serialize(old_buf, buf_, count);
50 // This part handles the problem of non-contiguous memory (for the unserialisation at the reception)
52 size_ = datatype->size() * count;
57 detached_sender_ = nullptr;
62 if (flags & PERSISTENT)
69 MPI_Comm Request::comm(){
89 int Request::detached(){
93 size_t Request::size(){
97 size_t Request::real_size(){
101 void Request::unref(MPI_Request* request)
103 if((*request) != MPI_REQUEST_NULL){
104 (*request)->refcount_--;
105 if((*request)->refcount_<0) xbt_die("wrong refcount");
106 if((*request)->refcount_==0){
107 Datatype::unref((*request)->old_type_);
108 Comm::unref((*request)->comm_);
109 (*request)->print_request("Destroying");
111 *request = MPI_REQUEST_NULL;
113 (*request)->print_request("Decrementing");
116 xbt_die("freeing an already free request");
120 int Request::match_recv(void* a, void* b, simgrid::kernel::activity::CommImpl* ignored)
122 MPI_Request ref = static_cast<MPI_Request>(a);
123 MPI_Request req = static_cast<MPI_Request>(b);
124 XBT_DEBUG("Trying to match a recv of src %d against %d, tag %d against %d",ref->src_,req->src_, ref->tag_, req->tag_);
126 xbt_assert(ref, "Cannot match recv against null reference");
127 xbt_assert(req, "Cannot match recv against null request");
128 if((ref->src_ == MPI_ANY_SOURCE || req->src_ == ref->src_)
129 && ((ref->tag_ == MPI_ANY_TAG && req->tag_ >=0) || req->tag_ == ref->tag_)){
130 //we match, we can transfer some values
131 if(ref->src_ == MPI_ANY_SOURCE)
132 ref->real_src_ = req->src_;
133 if(ref->tag_ == MPI_ANY_TAG)
134 ref->real_tag_ = req->tag_;
135 if(ref->real_size_ < req->real_size_)
137 if(req->detached_==1)
138 ref->detached_sender_=req; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
139 XBT_DEBUG("match succeeded");
144 int Request::match_send(void* a, void* b, simgrid::kernel::activity::CommImpl* ignored)
146 MPI_Request ref = static_cast<MPI_Request>(a);
147 MPI_Request req = static_cast<MPI_Request>(b);
148 XBT_DEBUG("Trying to match a send of src %d against %d, tag %d against %d",ref->src_,req->src_, ref->tag_, req->tag_);
149 xbt_assert(ref, "Cannot match send against null reference");
150 xbt_assert(req, "Cannot match send against null request");
152 if((req->src_ == MPI_ANY_SOURCE || req->src_ == ref->src_)
153 && ((req->tag_ == MPI_ANY_TAG && ref->tag_ >=0)|| req->tag_ == ref->tag_)){
154 if(req->src_ == MPI_ANY_SOURCE)
155 req->real_src_ = ref->src_;
156 if(req->tag_ == MPI_ANY_TAG)
157 req->real_tag_ = ref->tag_;
158 if(req->real_size_ < ref->real_size_)
160 if(ref->detached_==1)
161 req->detached_sender_=ref; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
162 XBT_DEBUG("match succeeded");
168 void Request::print_request(const char *message)
170 XBT_VERB("%s request %p [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
171 message, this, buf_, size_, src_, dst_, tag_, flags_);
175 /* factories, to hide the internal flags from the caller */
176 MPI_Request Request::send_init(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
179 return new Request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype, smpi_process()->index(),
180 comm->group()->index(dst), tag, comm, PERSISTENT | SEND | PREPARED);
183 MPI_Request Request::ssend_init(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
185 return new Request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype, smpi_process()->index(),
186 comm->group()->index(dst), tag, comm, PERSISTENT | SSEND | SEND | PREPARED);
189 MPI_Request Request::isend_init(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
191 return new Request(buf==MPI_BOTTOM ? nullptr : buf , count, datatype, smpi_process()->index(),
192 comm->group()->index(dst), tag,comm, PERSISTENT | ISEND | SEND | PREPARED);
196 MPI_Request Request::rma_send_init(void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
199 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
201 request = new Request(buf==MPI_BOTTOM ? nullptr : buf , count, datatype, src, dst, tag,
202 comm, RMA | NON_PERSISTENT | ISEND | SEND | PREPARED);
204 request = new Request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype, src, dst, tag,
205 comm, RMA | NON_PERSISTENT | ISEND | SEND | PREPARED | ACCUMULATE);
211 MPI_Request Request::recv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
213 return new Request(buf==MPI_BOTTOM ? nullptr : buf, count, datatype,
214 src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : comm->group()->index(src),
215 smpi_process()->index(), tag, comm, PERSISTENT | RECV | PREPARED);
218 MPI_Request Request::rma_recv_init(void *buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm,
221 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
223 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, src, dst, tag, comm,
224 RMA | NON_PERSISTENT | RECV | PREPARED);
226 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, src, dst, tag, comm,
227 RMA | NON_PERSISTENT | RECV | PREPARED | ACCUMULATE);
233 MPI_Request Request::irecv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm)
235 return new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype,
236 src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : comm->group()->index(src), smpi_process()->index(), tag,
237 comm, PERSISTENT | RECV | PREPARED);
240 MPI_Request Request::isend(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
242 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
243 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, smpi_process()->index(),
244 comm->group()->index(dst), tag, comm, NON_PERSISTENT | ISEND | SEND);
249 MPI_Request Request::issend(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
251 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
252 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, smpi_process()->index(),
253 comm->group()->index(dst), tag, comm, NON_PERSISTENT | ISEND | SSEND | SEND);
259 MPI_Request Request::irecv(void *buf, int count, MPI_Datatype datatype, int src, 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,
263 src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : comm->group()->index(src), smpi_process()->index(),
264 tag, comm, NON_PERSISTENT | RECV);
269 void Request::recv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status * status)
271 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
272 request = irecv(buf, count, datatype, src, tag, comm);
273 wait(&request,status);
277 void Request::send(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
279 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
280 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, smpi_process()->index(),
281 comm->group()->index(dst), tag, comm, NON_PERSISTENT | SEND);
284 wait(&request, MPI_STATUS_IGNORE);
288 void Request::ssend(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
290 MPI_Request request = nullptr; /* MC needs the comm to be set to nullptr during the call */
291 request = new Request(buf == MPI_BOTTOM ? nullptr : buf, count, datatype, smpi_process()->index(),
292 comm->group()->index(dst), tag, comm, NON_PERSISTENT | SSEND | SEND);
295 wait(&request,MPI_STATUS_IGNORE);
299 void Request::sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,int dst, int sendtag,
300 void *recvbuf, int recvcount, MPI_Datatype recvtype, int src, int recvtag,
301 MPI_Comm comm, MPI_Status * status)
303 MPI_Request requests[2];
305 int myid=smpi_process()->index();
306 if ((comm->group()->index(dst) == myid) && (comm->group()->index(src) == myid)){
307 Datatype::copy(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
310 requests[0] = isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
311 requests[1] = irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
312 startall(2, requests);
313 waitall(2, requests, stats);
316 if(status != MPI_STATUS_IGNORE) {
317 // Copy receive status
322 void Request::start()
324 smx_mailbox_t mailbox;
326 xbt_assert(action_ == nullptr, "Cannot (re-)start unfinished communication");
331 if ((flags_ & RECV) != 0) {
332 this->print_request("New recv");
334 simgrid::smpi::Process* process = smpi_process_remote(dst_);
336 int async_small_thresh = xbt_cfg_get_int("smpi/async-small-thresh");
338 xbt_mutex_t mut = process->mailboxes_mutex();
339 if (async_small_thresh != 0 || (flags_ & RMA) != 0)
340 xbt_mutex_acquire(mut);
342 if (async_small_thresh == 0 && (flags_ & RMA) == 0 ) {
343 mailbox = process->mailbox();
345 else if (((flags_ & RMA) != 0) || static_cast<int>(size_) < async_small_thresh) {
346 //We have to check both mailboxes (because SSEND messages are sent to the large mbox).
347 //begin with the more appropriate one : the small one.
348 mailbox = process->mailbox_small();
349 XBT_DEBUG("Is there a corresponding send already posted in the small mailbox %p (in case of SSEND)?", mailbox);
350 smx_activity_t action = simcall_comm_iprobe(mailbox, 0, &match_recv, static_cast<void*>(this));
352 if (action == nullptr) {
353 mailbox = process->mailbox();
354 XBT_DEBUG("No, nothing in the small mailbox test the other one : %p", mailbox);
355 action = simcall_comm_iprobe(mailbox, 0, &match_recv, static_cast<void*>(this));
356 if (action == nullptr) {
357 XBT_DEBUG("Still nothing, switch back to the small mailbox : %p", mailbox);
358 mailbox = process->mailbox_small();
361 XBT_DEBUG("yes there was something for us in the large mailbox");
364 mailbox = process->mailbox_small();
365 XBT_DEBUG("Is there a corresponding send already posted the small mailbox?");
366 smx_activity_t action = simcall_comm_iprobe(mailbox, 0, &match_recv, static_cast<void*>(this));
368 if (action == nullptr) {
369 XBT_DEBUG("No, nothing in the permanent receive mailbox");
370 mailbox = process->mailbox();
372 XBT_DEBUG("yes there was something for us in the small mailbox");
376 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
378 action_ = simcall_comm_irecv(
379 process->process(), mailbox, buf_, &real_size_, &match_recv,
380 process->replaying() ? &smpi_comm_null_copy_buffer_callback : smpi_comm_copy_data_callback, this, -1.0);
381 XBT_DEBUG("recv simcall posted");
383 if (async_small_thresh != 0 || (flags_ & RMA) != 0 )
384 xbt_mutex_release(mut);
385 } else { /* the RECV flag was not set, so this is a send */
386 simgrid::smpi::Process* process = smpi_process_remote(dst_);
388 if (TRACE_smpi_view_internals()) {
389 TRACE_smpi_send(rank, rank, dst_, tag_, size_);
391 this->print_request("New send");
394 if ((flags_ & SSEND) == 0 && ( (flags_ & RMA) != 0
395 || static_cast<int>(size_) < xbt_cfg_get_int("smpi/send-is-detached-thresh") ) ) {
396 void *oldbuf = nullptr;
398 XBT_DEBUG("Send request %p is detached", this);
400 if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
402 if (not process->replaying() && oldbuf != nullptr && size_ != 0) {
403 if ((smpi_privatize_global_variables != 0) && (static_cast<char*>(buf_) >= smpi_data_exe_start) &&
404 (static_cast<char*>(buf_) < smpi_data_exe_start + smpi_data_exe_size)) {
405 XBT_DEBUG("Privatization : We are sending from a zone inside global memory. Switch data segment ");
406 smpi_switch_data_segment(src_);
408 buf = xbt_malloc(size_);
409 memcpy(buf,oldbuf,size_);
410 XBT_DEBUG("buf %p copied into %p",oldbuf,buf);
415 //if we are giving back the control to the user without waiting for completion, we have to inject timings
416 double sleeptime = 0.0;
417 if (detached_ != 0 || ((flags_ & (ISEND | SSEND)) != 0)) { // issend should be treated as isend
418 // isend and send timings may be different
419 sleeptime = ((flags_ & ISEND) != 0)
420 ? simgrid::s4u::Actor::self()->getHost()->extension<simgrid::smpi::SmpiHost>()->oisend(size_)
421 : simgrid::s4u::Actor::self()->getHost()->extension<simgrid::smpi::SmpiHost>()->osend(size_);
425 simcall_process_sleep(sleeptime);
426 XBT_DEBUG("sending size of %zu : sleep %f ", size_, sleeptime);
429 int async_small_thresh = xbt_cfg_get_int("smpi/async-small-thresh");
431 xbt_mutex_t mut=process->mailboxes_mutex();
433 if (async_small_thresh != 0 || (flags_ & RMA) != 0)
434 xbt_mutex_acquire(mut);
436 if (not(async_small_thresh != 0 || (flags_ & RMA) != 0)) {
437 mailbox = process->mailbox();
438 } else if (((flags_ & RMA) != 0) || static_cast<int>(size_) < async_small_thresh) { // eager mode
439 mailbox = process->mailbox();
440 XBT_DEBUG("Is there a corresponding recv already posted in the large mailbox %p?", mailbox);
441 smx_activity_t action = simcall_comm_iprobe(mailbox, 1, &match_send, static_cast<void*>(this));
442 if (action == nullptr) {
443 if ((flags_ & SSEND) == 0){
444 mailbox = process->mailbox_small();
445 XBT_DEBUG("No, nothing in the large mailbox, message is to be sent on the small one %p", mailbox);
447 mailbox = process->mailbox_small();
448 XBT_DEBUG("SSEND : Is there a corresponding recv already posted in the small mailbox %p?", mailbox);
449 action = simcall_comm_iprobe(mailbox, 1, &match_send, static_cast<void*>(this));
450 if (action == nullptr) {
451 XBT_DEBUG("No, we are first, send to large mailbox");
452 mailbox = process->mailbox();
456 XBT_DEBUG("Yes there was something for us in the large mailbox");
459 mailbox = process->mailbox();
460 XBT_DEBUG("Send request %p is in the large mailbox %p (buf: %p)",mailbox, this,buf_);
463 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
465 action_ = simcall_comm_isend(
466 SIMIX_process_from_PID(src_ + 1), mailbox, size_, -1.0, buf, real_size_, &match_send,
467 &xbt_free_f, // how to free the userdata if a detached send fails
468 not process->replaying() ? smpi_comm_copy_data_callback : &smpi_comm_null_copy_buffer_callback, this,
469 // detach if msg size < eager/rdv switch limit
471 XBT_DEBUG("send simcall posted");
473 /* FIXME: detached sends are not traceable (action_ == nullptr) */
474 if (action_ != nullptr)
475 simcall_set_category(action_, TRACE_internal_smpi_get_category());
476 if (async_small_thresh != 0 || ((flags_ & RMA)!=0))
477 xbt_mutex_release(mut);
481 void Request::startall(int count, MPI_Request * requests)
483 if(requests== nullptr)
486 for(int i = 0; i < count; i++) {
487 requests[i]->start();
491 int Request::test(MPI_Request * request, MPI_Status * status) {
492 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or testall before)
493 // to avoid deadlocks if used as a break condition, such as
494 // while (MPI_Test(request, flag, status) && flag) dostuff...
495 // because the time will not normally advance when only calls to MPI_Test are made -> deadlock
496 // multiplier to the sleeptime, to increase speed of execution, each failed test will increase it
497 static int nsleeps = 1;
498 if(smpi_test_sleep > 0)
499 simcall_process_sleep(nsleeps*smpi_test_sleep);
501 Status::empty(status);
503 if (((*request)->flags_ & PREPARED) == 0) {
504 if ((*request)->action_ != nullptr)
505 flag = simcall_comm_test((*request)->action_);
507 finish_wait(request,status);
508 nsleeps=1;//reset the number of sleeps we will do next time
509 if (*request != MPI_REQUEST_NULL && ((*request)->flags_ & PERSISTENT) == 0)
510 *request = MPI_REQUEST_NULL;
511 } else if (xbt_cfg_get_boolean("smpi/grow-injected-times")){
518 int Request::testsome(int incount, MPI_Request requests[], int *indices, MPI_Status status[])
523 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
525 for (int i = 0; i < incount; i++) {
526 if (requests[i] != MPI_REQUEST_NULL) {
527 if (test(&requests[i], pstat)) {
530 if (status != MPI_STATUSES_IGNORE)
532 if ((requests[i] != MPI_REQUEST_NULL) && (requests[i]->flags_ & NON_PERSISTENT))
533 requests[i] = MPI_REQUEST_NULL;
539 if(count_dead==incount)
540 return MPI_UNDEFINED;
544 int Request::testany(int count, MPI_Request requests[], int *index, MPI_Status * status)
546 std::vector<simgrid::kernel::activity::ActivityImplPtr> comms;
547 comms.reserve(count);
552 *index = MPI_UNDEFINED;
554 std::vector<int> map; /** Maps all matching comms back to their location in requests **/
555 for(i = 0; i < count; i++) {
556 if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->action_ && not(requests[i]->flags_ & PREPARED)) {
557 comms.push_back(requests[i]->action_);
561 if (not map.empty()) {
562 //multiplier to the sleeptime, to increase speed of execution, each failed testany will increase it
563 static int nsleeps = 1;
564 if(smpi_test_sleep > 0)
565 simcall_process_sleep(nsleeps*smpi_test_sleep);
567 i = simcall_comm_testany(comms.data(), comms.size()); // The i-th element in comms matches!
568 if (i != -1) { // -1 is not MPI_UNDEFINED but a SIMIX return code. (nothing matches)
570 finish_wait(&requests[*index],status);
573 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags_ & NON_PERSISTENT)) {
574 requests[*index] = MPI_REQUEST_NULL;
580 //all requests are null or inactive, return true
582 Status::empty(status);
588 int Request::testall(int count, MPI_Request requests[], MPI_Status status[])
591 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
593 for(int i=0; i<count; i++){
594 if (requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & PREPARED)) {
595 if (test(&requests[i], pstat)!=1){
598 requests[i]=MPI_REQUEST_NULL;
601 Status::empty(pstat);
603 if(status != MPI_STATUSES_IGNORE) {
610 void Request::probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
612 //FIXME find another way to avoid busy waiting ?
613 // the issue here is that we have to wait on a nonexistent comm
615 iprobe(source, tag, comm, &flag, status);
616 XBT_DEBUG("Busy Waiting on probing : %d", flag);
620 void Request::iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
621 // to avoid deadlock, we have to sleep some time here, or the timer won't advance and we will only do iprobe simcalls
622 // especially when used as a break condition, such as while (MPI_Iprobe(...)) dostuff...
623 // nsleeps is a multiplier to the sleeptime, to increase speed of execution, each failed iprobe will increase it
624 // This can speed up the execution of certain applications by an order of magnitude, such as HPL
625 static int nsleeps = 1;
626 double speed = simgrid::s4u::Actor::self()->getHost()->getSpeed();
627 double maxrate = xbt_cfg_get_double("smpi/iprobe-cpu-usage");
628 MPI_Request request = new Request(nullptr, 0, MPI_CHAR, source == MPI_ANY_SOURCE ? MPI_ANY_SOURCE :
629 comm->group()->index(source), comm->rank(), tag, comm, PERSISTENT | RECV);
630 if (smpi_iprobe_sleep > 0) {
631 smx_activity_t iprobe_sleep = simcall_execution_start("iprobe", /* flops to executek*/nsleeps*smpi_iprobe_sleep*speed*maxrate, /* priority */1.0, /* performance bound */maxrate*speed);
632 simcall_execution_wait(iprobe_sleep);
634 // behave like a receive, but don't do it
635 smx_mailbox_t mailbox;
637 request->print_request("New iprobe");
638 // We have to test both mailboxes as we don't know if we will receive one one or another
639 if (xbt_cfg_get_int("smpi/async-small-thresh") > 0){
640 mailbox = smpi_process()->mailbox_small();
641 XBT_DEBUG("Trying to probe the perm recv mailbox");
642 request->action_ = simcall_comm_iprobe(mailbox, 0, &match_recv, static_cast<void*>(request));
645 if (request->action_ == nullptr){
646 mailbox = smpi_process()->mailbox();
647 XBT_DEBUG("trying to probe the other mailbox");
648 request->action_ = simcall_comm_iprobe(mailbox, 0, &match_recv, static_cast<void*>(request));
651 if (request->action_ != nullptr){
652 simgrid::kernel::activity::CommImplPtr sync_comm =
653 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(request->action_);
654 MPI_Request req = static_cast<MPI_Request>(sync_comm->src_data);
656 if(status != MPI_STATUS_IGNORE && (req->flags_ & PREPARED) == 0) {
657 status->MPI_SOURCE = comm->group()->rank(req->src_);
658 status->MPI_TAG = req->tag_;
659 status->MPI_ERROR = MPI_SUCCESS;
660 status->count = req->real_size_;
662 nsleeps = 1;//reset the number of sleeps we will do next time
666 if (xbt_cfg_get_boolean("smpi/grow-injected-times"))
672 void Request::finish_wait(MPI_Request* request, MPI_Status * status)
674 MPI_Request req = *request;
675 Status::empty(status);
677 if (not((req->detached_ != 0) && ((req->flags_ & SEND) != 0)) && ((req->flags_ & PREPARED) == 0)) {
678 if(status != MPI_STATUS_IGNORE) {
679 int src = req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_;
680 status->MPI_SOURCE = req->comm_->group()->rank(src);
681 status->MPI_TAG = req->tag_ == MPI_ANY_TAG ? req->real_tag_ : req->tag_;
682 status->MPI_ERROR = req->truncated_ != 0 ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
683 // this handles the case were size in receive differs from size in send
684 status->count = req->real_size_;
687 req->print_request("Finishing");
688 MPI_Datatype datatype = req->old_type_;
690 // FIXME Handle the case of a partial shared malloc.
691 if (((req->flags_ & ACCUMULATE) != 0) ||
692 (datatype->flags() & DT_FLAG_DERIVED)) { // && (not smpi_is_shared(req->old_buf_))){
694 if (not smpi_process()->replaying()) {
695 if (smpi_privatize_global_variables != 0 && (static_cast<char*>(req->old_buf_) >= smpi_data_exe_start) &&
696 ((char*)req->old_buf_ < smpi_data_exe_start + smpi_data_exe_size)) {
697 XBT_VERB("Privatization : We are unserializing to a zone in global memory Switch data segment ");
698 smpi_switch_data_segment(smpi_process()->index());
702 if(datatype->flags() & DT_FLAG_DERIVED){
703 // This part handles the problem of non-contignous memory the unserialization at the reception
704 if((req->flags_ & RECV) && datatype->size()!=0)
705 datatype->unserialize(req->buf_, req->old_buf_, req->real_size_/datatype->size() , req->op_);
707 }else if(req->flags_ & RECV){//apply op on contiguous buffer for accumulate
708 if(datatype->size()!=0){
709 int n =req->real_size_/datatype->size();
710 req->op_->apply(req->buf_, req->old_buf_, &n, datatype);
717 if (TRACE_smpi_view_internals() && ((req->flags_ & RECV) != 0)){
718 int rank = smpi_process()->index();
719 int src_traced = (req->src_ == MPI_ANY_SOURCE ? req->real_src_ : req->src_);
720 TRACE_smpi_recv(src_traced, rank,req->tag_);
722 if(req->detached_sender_ != nullptr){
723 //integrate pseudo-timing for buffering of small messages, do not bother to execute the simcall if 0
725 simgrid::s4u::Actor::self()->getHost()->extension<simgrid::smpi::SmpiHost>()->orecv(req->real_size());
727 simcall_process_sleep(sleeptime);
728 XBT_DEBUG("receiving size of %zu : sleep %f ", req->real_size_, sleeptime);
730 unref(&(req->detached_sender_));
732 if(req->flags_ & PERSISTENT)
733 req->action_ = nullptr;
734 req->flags_ |= FINISHED;
738 void Request::wait(MPI_Request * request, MPI_Status * status)
740 (*request)->print_request("Waiting");
741 if ((*request)->flags_ & PREPARED) {
742 Status::empty(status);
746 if ((*request)->action_ != nullptr)
747 // this is not a detached send
748 simcall_comm_wait((*request)->action_, -1.0);
750 finish_wait(request,status);
751 if (*request != MPI_REQUEST_NULL && (((*request)->flags_ & NON_PERSISTENT)!=0))
752 *request = MPI_REQUEST_NULL;
755 int Request::waitany(int count, MPI_Request requests[], MPI_Status * status)
757 s_xbt_dynar_t comms; // Keep it on stack to save some extra mallocs
758 int index = MPI_UNDEFINED;
762 // Wait for a request to complete
763 xbt_dynar_init(&comms, sizeof(smx_activity_t), [](void*ptr){
764 intrusive_ptr_release(*(simgrid::kernel::activity::ActivityImpl**)ptr);
766 int *map = xbt_new(int, count);
767 XBT_DEBUG("Wait for one of %d", count);
768 for(int i = 0; i < count; i++) {
769 if (requests[i] != MPI_REQUEST_NULL && not(requests[i]->flags_ & PREPARED) &&
770 not(requests[i]->flags_ & FINISHED)) {
771 if (requests[i]->action_ != nullptr) {
772 XBT_DEBUG("Waiting any %p ", requests[i]);
773 intrusive_ptr_add_ref(requests[i]->action_.get());
774 xbt_dynar_push_as(&comms, simgrid::kernel::activity::ActivityImpl*, requests[i]->action_.get());
778 // This is a finished detached request, let's return this one
779 size = 0; // so we free the dynar but don't do the waitany call
781 finish_wait(&requests[i], status); // cleanup if refcount = 0
782 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags_ & NON_PERSISTENT))
783 requests[i] = MPI_REQUEST_NULL; // set to null
789 XBT_DEBUG("Enter waitany for %lu comms", xbt_dynar_length(&comms));
790 int i = simcall_comm_waitany(&comms, -1);
792 // not MPI_UNDEFINED, as this is a simix return code
795 //in case of an accumulate, we have to wait the end of all requests to apply the operation, ordered correctly.
796 if ((requests[index] == MPI_REQUEST_NULL) ||
797 (not((requests[index]->flags_ & ACCUMULATE) && (requests[index]->flags_ & RECV)))) {
798 finish_wait(&requests[index],status);
799 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags_ & NON_PERSISTENT))
800 requests[index] = MPI_REQUEST_NULL;
805 xbt_dynar_free_data(&comms);
809 if (index==MPI_UNDEFINED)
810 Status::empty(status);
815 static int sort_accumulates(MPI_Request a, MPI_Request b)
817 return (a->tag() > b->tag());
820 int Request::waitall(int count, MPI_Request requests[], MPI_Status status[])
822 std::vector<MPI_Request> accumulates;
825 MPI_Status *pstat = (status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat);
826 int retvalue = MPI_SUCCESS;
827 //tag invalid requests in the set
828 if (status != MPI_STATUSES_IGNORE) {
829 for (int c = 0; c < count; c++) {
830 if (requests[c] == MPI_REQUEST_NULL || requests[c]->dst_ == MPI_PROC_NULL || (requests[c]->flags_ & PREPARED)) {
831 Status::empty(&status[c]);
832 } else if (requests[c]->src_ == MPI_PROC_NULL) {
833 Status::empty(&status[c]);
834 status[c].MPI_SOURCE = MPI_PROC_NULL;
838 for (int c = 0; c < count; c++) {
839 if (MC_is_active() || MC_record_replay_is_active()) {
840 wait(&requests[c],pstat);
843 index = waitany(count, (MPI_Request*)requests, pstat);
844 if (index == MPI_UNDEFINED)
847 if (requests[index] != MPI_REQUEST_NULL
848 && (requests[index]->flags_ & RECV)
849 && (requests[index]->flags_ & ACCUMULATE))
850 accumulates.push_back(requests[index]);
851 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & NON_PERSISTENT))
852 requests[index] = MPI_REQUEST_NULL;
854 if (status != MPI_STATUSES_IGNORE) {
855 status[index] = *pstat;
856 if (status[index].MPI_ERROR == MPI_ERR_TRUNCATE)
857 retvalue = MPI_ERR_IN_STATUS;
861 if (not accumulates.empty()) {
862 std::sort(accumulates.begin(), accumulates.end(), sort_accumulates);
863 for (auto& req : accumulates) {
864 finish_wait(&req, status);
871 int Request::waitsome(int incount, MPI_Request requests[], int *indices, MPI_Status status[])
875 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
877 for (int i = 0; i < incount; i++) {
878 int index = waitany(incount, requests, pstat);
879 if(index!=MPI_UNDEFINED){
880 indices[count] = index;
882 if(status != MPI_STATUSES_IGNORE) {
883 status[index] = *pstat;
885 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags_ & NON_PERSISTENT))
886 requests[index] = MPI_REQUEST_NULL;
888 return MPI_UNDEFINED;
894 MPI_Request Request::f2c(int id) {
896 if(id==MPI_FORTRAN_REQUEST_NULL)
897 return static_cast<MPI_Request>(MPI_REQUEST_NULL);
898 return static_cast<MPI_Request>(F2C::f2c_lookup()->at(get_key_id(key, id)));
903 if (F2C::f2c_lookup() == nullptr) {
904 F2C::set_f2c_lookup(new std::unordered_map<std::string, F2C*>);
907 (*(F2C::f2c_lookup()))[get_key_id(key, F2C::f2c_id())] = this;
908 F2C::f2c_id_increment();
909 return F2C::f2c_id()-1;
912 void Request::free_f(int id)
914 if (id != MPI_FORTRAN_REQUEST_NULL) {
916 F2C::f2c_lookup()->erase(get_key_id(key, id));