Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
SMPI : add leak detection.
[simgrid.git] / src / smpi / mpi / smpi_request.cpp
index 390fed1..8467be6 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2007-2020. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -65,6 +65,7 @@ Request::Request(const void* buf, int count, MPI_Datatype datatype, int src, int
   nbc_requests_=nullptr;
   nbc_requests_size_=0;
   init_buffer(count);
+  this->add_f();
 }
 
 void Request::ref(){
@@ -90,6 +91,7 @@ void Request::unref(MPI_Request* request)
         Op::unref(&(*request)->op_);
 
       (*request)->print_request("Destroying");
+      F2C::free_f((*request)->c2f());
       delete *request;
       *request = MPI_REQUEST_NULL;
     }else{
@@ -118,8 +120,10 @@ bool Request::match_common(MPI_Request req, MPI_Request sender, MPI_Request rece
       receiver->real_src_ = sender->src_;
     if (receiver->tag_ == MPI_ANY_TAG)
       receiver->real_tag_ = sender->tag_;
-    if (receiver->real_size_ < sender->real_size_)
+    if (receiver->real_size_ < sender->real_size_ && ((receiver->flags_ & MPI_REQ_PROBE) == 0 )){
+      XBT_DEBUG("Truncating message - should not happen: receiver size : %zu < sender size : %zu", receiver->real_size_, sender->real_size_);
       receiver->truncated_ = true;
+    }
     if (sender->detached_)
       receiver->detached_sender_ = sender; // tie the sender to the receiver, as it is detached and has to be freed in
                                            // the receiver
@@ -375,6 +379,8 @@ void Request::start()
   flags_ &= ~MPI_REQ_FINISHED;
   this->ref();
 
+  // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
+  real_size_=size_;
   if ((flags_ & MPI_REQ_RECV) != 0) {
     this->print_request("New recv");
 
@@ -418,8 +424,6 @@ void Request::start()
       }
     }
 
-    // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
-    real_size_=size_;
     action_   = simcall_comm_irecv(
         process->get_actor()->get_impl(), mailbox->get_impl(), buf_, &real_size_, &match_recv,
         process->replaying() ? &smpi_comm_null_copy_buffer_callback : smpi_comm_copy_data_callback, this, -1.0);
@@ -510,8 +514,6 @@ void Request::start()
       XBT_DEBUG("Send request %p is in the large mailbox %s (buf: %p)", this, mailbox->get_cname(), buf_);
     }
 
-    // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
-    real_size_=size_;
     size_t payload_size_ = size_ + 16;//MPI enveloppe size (tag+dest+communicator)
     action_   = simcall_comm_isend(
         simgrid::s4u::Actor::by_pid(src_)->get_impl(), mailbox->get_impl(), payload_size_, -1.0, buf, real_size_, &match_send,
@@ -765,7 +767,7 @@ void Request::iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status*
   double maxrate      = smpi_cfg_iprobe_cpu_usage();
   auto request        = new Request(nullptr, 0, MPI_CHAR,
                              source == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : comm->group()->actor(source)->get_pid(),
-                             simgrid::s4u::this_actor::get_pid(), tag, comm, MPI_REQ_PERSISTENT | MPI_REQ_RECV);
+                             simgrid::s4u::this_actor::get_pid(), tag, comm, MPI_REQ_PERSISTENT | MPI_REQ_RECV | MPI_REQ_PROBE);
   if (smpi_iprobe_sleep > 0) {
     /** Compute the number of flops we will sleep **/
     s4u::this_actor::exec_init(/*nsleeps: See comment above */ nsleeps *
@@ -892,7 +894,24 @@ void Request::finish_wait(MPI_Request* request, MPI_Status * status)
   if (req->flags_ & MPI_REQ_PERSISTENT)
     req->action_ = nullptr;
   req->flags_ |= MPI_REQ_FINISHED;
+
+  if (req->truncated_) {
+    char error_string[MPI_MAX_ERROR_STRING];
+    int error_size;
+    PMPI_Error_string(MPI_ERR_TRUNCATE, error_string, &error_size);
+    MPI_Errhandler err = (req->comm_) ? (req->comm_)->errhandler() : MPI_ERRHANDLER_NULL;
+    if (err == MPI_ERRHANDLER_NULL || err == MPI_ERRORS_RETURN)
+      XBT_WARN("recv - returned %.*s instead of MPI_SUCCESS", error_size, error_string);
+    else if (err == MPI_ERRORS_ARE_FATAL)
+      xbt_die("recv - returned %.*s instead of MPI_SUCCESS", error_size, error_string);
+    else
+      err->call((req->comm_), MPI_ERR_TRUNCATE);
+    if (err != MPI_ERRHANDLER_NULL)
+      simgrid::smpi::Errhandler::unref(err);
+    MC_assert(not MC_is_active()); /* Only fail in MC mode */
+  }
   unref(request);
+
 }
 
 int Request::wait(MPI_Request * request, MPI_Status * status)
@@ -968,12 +987,11 @@ int Request::wait(MPI_Request * request, MPI_Status * status)
 
 int Request::waitany(int count, MPI_Request requests[], MPI_Status * status)
 {
-  std::vector<simgrid::kernel::activity::CommImpl*> comms;
-  comms.reserve(count);
   int index = MPI_UNDEFINED;
 
   if(count > 0) {
     // Wait for a request to complete
+    std::vector<simgrid::kernel::activity::CommImpl*> comms;
     std::vector<int> map;
     XBT_DEBUG("Wait for one of %d", count);
     for(int i = 0; i < count; i++) {
@@ -985,7 +1003,7 @@ int Request::waitany(int count, MPI_Request requests[], MPI_Status * status)
           map.push_back(i);
         } else {
           // This is a finished detached request, let's return this one
-          comms.clear(); // so we free don't do the waitany call
+          comms.clear(); // don't do the waitany call afterwards
           index = i;
           finish_wait(&requests[i], status); // cleanup if refcount = 0
           if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags_ & MPI_REQ_NON_PERSISTENT))
@@ -996,13 +1014,12 @@ int Request::waitany(int count, MPI_Request requests[], MPI_Status * status)
     }
     if (not comms.empty()) {
       XBT_DEBUG("Enter waitany for %zu comms", comms.size());
-      int i=MPI_UNDEFINED;
+      int i;
       try{
-        // this is not a detached send
         i = simcall_comm_waitany(comms.data(), comms.size(), -1);
       } catch (const Exception&) {
-        XBT_INFO("request %d cancelled ", i);
-        return i;
+        XBT_INFO("request cancelled");
+        i = -1;
       }
 
       // not MPI_UNDEFINED, as this is a simix return code