Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[SMPI] Move the pid calls to helper function that has more checks
[simgrid.git] / src / smpi / bindings / smpi_pmpi_request.cpp
index d6268ca..49efe86 100644 (file)
 
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(smpi_pmpi);
 
+static int getPid(MPI_Comm, int);
+static int getPid(MPI_Comm comm, int id)
+{
+  simgrid::s4u::ActorPtr actor = comm->group()->actor(id);
+  return (actor == nullptr) ? MPI_UNDEFINED : actor->getPid() - 1;
+}
+
 /* PMPI User level calls */
 extern "C" { // Obviously, the C MPI interface should use the C linkage
 
@@ -158,9 +165,10 @@ int PMPI_Irecv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MP
   } else {
 
     int rank       = smpi_process()->index();
+    int src_traced = getPid(comm, src);
 
     TRACE_smpi_comm_in(rank, __FUNCTION__,
-                       new simgrid::instr::Pt2PtTIData("Irecv", comm->group()->index(src),
+                       new simgrid::instr::Pt2PtTIData("Irecv", src_traced,
                                                        datatype->is_replayable() ? count : count * datatype->size(),
                                                        encode_datatype(datatype)));
 
@@ -199,7 +207,7 @@ int PMPI_Isend(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MP
     retval = MPI_ERR_TAG;
   } else {
     int rank      = smpi_process()->index();
-    int trace_dst = comm->group()->index(dst);
+    int trace_dst = getPid(comm, dst);
     TRACE_smpi_comm_in(rank, __FUNCTION__,
                        new simgrid::instr::Pt2PtTIData("Isend", trace_dst,
                                                        datatype->is_replayable() ? count : count * datatype->size(),
@@ -241,7 +249,7 @@ int PMPI_Issend(void* buf, int count, MPI_Datatype datatype, int dst, int tag, M
     retval = MPI_ERR_TAG;
   } else {
     int rank      = smpi_process()->index();
-    int trace_dst = comm->group()->index(dst);
+    int trace_dst = getPid(comm, dst);
     TRACE_smpi_comm_in(rank, __FUNCTION__,
                        new simgrid::instr::Pt2PtTIData("ISsend", trace_dst,
                                                        datatype->is_replayable() ? count : count * datatype->size(),
@@ -281,7 +289,7 @@ int PMPI_Recv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI
     retval = MPI_ERR_TAG;
   } else {
     int rank               = smpi_process()->index();
-    int src_traced         = comm->group()->index(src);
+    int src_traced         = getPid(comm, src);
     TRACE_smpi_comm_in(rank, __FUNCTION__,
                        new simgrid::instr::Pt2PtTIData("recv", src_traced,
                                                        datatype->is_replayable() ? count : count * datatype->size(),
@@ -292,7 +300,7 @@ int PMPI_Recv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI
 
     // the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
     if (status != MPI_STATUS_IGNORE) {
-      src_traced = comm->group()->index(status->MPI_SOURCE);
+      src_traced = getPid(comm, status->MPI_SOURCE);
       if (not TRACE_smpi_view_internals()) {
         TRACE_smpi_recv(src_traced, rank, tag);
       }
@@ -324,7 +332,7 @@ int PMPI_Send(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI
     retval = MPI_ERR_TAG;
   } else {
     int rank               = smpi_process()->index();
-    int dst_traced         = comm->group()->index(dst);
+    int dst_traced         = getPid(comm, dst);
     TRACE_smpi_comm_in(rank, __FUNCTION__,
                        new simgrid::instr::Pt2PtTIData("send", dst_traced,
                                                        datatype->is_replayable() ? count : count * datatype->size(),
@@ -362,7 +370,7 @@ int PMPI_Ssend(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MP
     retval = MPI_ERR_TAG;
   } else {
     int rank               = smpi_process()->index();
-    int dst_traced         = comm->group()->index(dst);
+    int dst_traced         = getPid(comm, dst);
     TRACE_smpi_comm_in(rank, __FUNCTION__,
                        new simgrid::instr::Pt2PtTIData("Ssend", dst_traced,
                                                        datatype->is_replayable() ? count : count * datatype->size(),
@@ -404,8 +412,8 @@ int PMPI_Sendrecv(void* sendbuf, int sendcount, MPI_Datatype sendtype, int dst,
     retval = MPI_ERR_TAG;
   } else {
     int rank               = smpi_process()->index();
-    int dst_traced         = comm->group()->index(dst);
-    int src_traced         = comm->group()->index(src);
+    int dst_traced         = getPid(comm, dst);
+    int src_traced         = getPid(comm, src);
 
     // FIXME: Hack the way to trace this one
     std::vector<int>* dst_hack = new std::vector<int>;
@@ -548,6 +556,23 @@ int PMPI_Iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* statu
   return retval;
 }
 
+// TODO: cheinrich: Move declaration to other file? Rename this function - it's used for PMPI_Wait*?
+static void trace_smpi_recv_helper(MPI_Request* request, MPI_Status* status);
+static void trace_smpi_recv_helper(MPI_Request* request, MPI_Status* status)
+{
+  MPI_Request req = *request;
+  if (req != MPI_REQUEST_NULL) { // Received requests become null
+    int src_traced = req->src();
+    // the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
+    int dst_traced = req->dst();
+    if (req->flags() & RECV) { // Is this request a wait for RECV?
+      if (src_traced == MPI_ANY_SOURCE)
+        src_traced = (status != MPI_STATUSES_IGNORE) ? req->comm()->group()->rank(status->MPI_SOURCE) : req->src();
+      TRACE_smpi_recv(src_traced, dst_traced, req->tag());
+    }
+  }
+}
+
 int PMPI_Wait(MPI_Request * request, MPI_Status * status)
 {
   int retval = 0;
@@ -563,11 +588,6 @@ int PMPI_Wait(MPI_Request * request, MPI_Status * status)
   } else {
     int rank = (*request)->comm() != MPI_COMM_NULL ? smpi_process()->index() : -1;
 
-    int src_traced = (*request)->src();
-    int dst_traced = (*request)->dst();
-    int tag_traced= (*request)->tag();
-    MPI_Comm comm = (*request)->comm();
-    int is_wait_for_receive = ((*request)->flags() & RECV);
     TRACE_smpi_comm_in(rank, __FUNCTION__, new simgrid::instr::NoOpTIData("wait"));
 
     simgrid::smpi::Request::wait(request, status);
@@ -575,11 +595,7 @@ int PMPI_Wait(MPI_Request * request, MPI_Status * status)
 
     //the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
     TRACE_smpi_comm_out(rank);
-    if (is_wait_for_receive) {
-      if(src_traced==MPI_ANY_SOURCE)
-        src_traced = (status != MPI_STATUS_IGNORE) ? comm->group()->rank(status->MPI_SOURCE) : src_traced;
-      TRACE_smpi_recv(src_traced, dst_traced, tag_traced);
-    }
+    trace_smpi_recv_helper(request, status);
   }
 
   smpi_bench_begin();
@@ -595,43 +611,16 @@ int PMPI_Waitany(int count, MPI_Request requests[], int *index, MPI_Status * sta
     return MPI_SUCCESS;
 
   smpi_bench_end();
-  //save requests information for tracing
-  struct savedvalstype {
-    int src;
-    int dst;
-    int recv;
-    int tag;
-    MPI_Comm comm;
-  };
-  savedvalstype* savedvals = xbt_new0(savedvalstype, count);
 
-  for (int i = 0; i < count; i++) {
-    MPI_Request req = requests[i];      //already received requests are no longer valid
-    if (req) {
-      savedvals[i]=(savedvalstype){req->src(), req->dst(), (req->flags() & RECV), req->tag(), req->comm()};
-    }
-  }
-  int rank_traced = smpi_process()->index();
+  int rank_traced = smpi_process()->index(); // FIXME: In PMPI_Wait, we check if the comm is null?
   TRACE_smpi_comm_in(rank_traced, __FUNCTION__, new simgrid::instr::CpuTIData("waitAny", static_cast<double>(count)));
 
   *index = simgrid::smpi::Request::waitany(count, requests, status);
 
   if(*index!=MPI_UNDEFINED){
-    MPI_Request req = requests[*index];
-    if (req != nullptr) { // Requests that were already received will be a nullptr
-      int src_traced = req->src();
-      // the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
-      int dst_traced          = req->dst();
-      int is_wait_for_receive = req->flags() & RECV;
-      if (is_wait_for_receive) {
-        if (req->src() == MPI_ANY_SOURCE)
-          src_traced = (status != MPI_STATUSES_IGNORE) ? req->comm()->group()->rank(status->MPI_SOURCE) : req->src();
-        TRACE_smpi_recv(src_traced, dst_traced, req->tag());
-      }
-    }
+    trace_smpi_recv_helper(&requests[*index], status);
     TRACE_smpi_comm_out(rank_traced);
   }
-  xbt_free(savedvals);
 
   smpi_bench_begin();
   return MPI_SUCCESS;
@@ -640,46 +629,16 @@ int PMPI_Waitany(int count, MPI_Request requests[], int *index, MPI_Status * sta
 int PMPI_Waitall(int count, MPI_Request requests[], MPI_Status status[])
 {
   smpi_bench_end();
-  //save information from requests
-  struct savedvalstype {
-    int src;
-    int dst;
-    int recv;
-    int tag;
-    int valid;
-    MPI_Comm comm;
-  };
-  savedvalstype* savedvals=xbt_new0(savedvalstype, count);
 
-  for (int i = 0; i < count; i++) {
-    MPI_Request req = requests[i];
-    if(req!=MPI_REQUEST_NULL){
-      savedvals[i]=(savedvalstype){req->src(), req->dst(), (req->flags() & RECV), req->tag(), 1, req->comm()};
-    }else{
-      savedvals[i].valid=0;
-    }
-  }
-  int rank_traced = smpi_process()->index();
+  int rank_traced = smpi_process()->index(); // FIXME: In PMPI_Wait, we check if the comm is null?
   TRACE_smpi_comm_in(rank_traced, __FUNCTION__, new simgrid::instr::CpuTIData("waitAll", static_cast<double>(count)));
 
   int retval = simgrid::smpi::Request::waitall(count, requests, status);
 
   for (int i = 0; i < count; i++) {
-    if(savedvals[i].valid){
-      // the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
-      int src_traced = savedvals[i].src;
-      int dst_traced = savedvals[i].dst;
-      int is_wait_for_receive = savedvals[i].recv;
-      if (is_wait_for_receive) {
-        if(src_traced==MPI_ANY_SOURCE)
-          src_traced = (status != MPI_STATUSES_IGNORE) ? savedvals[i].comm->group()->rank(status[i].MPI_SOURCE)
-                                                       : savedvals[i].src;
-        TRACE_smpi_recv(src_traced, dst_traced,savedvals[i].tag);
-      }
-    }
+    trace_smpi_recv_helper(&requests[i], &status[i]);
   }
   TRACE_smpi_comm_out(rank_traced);
-  xbt_free(savedvals);
 
   smpi_bench_begin();
   return retval;