Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics for sonar
[simgrid.git] / src / smpi / smpi_pmpi.cpp
index 6504976..0cb559c 100644 (file)
@@ -22,6 +22,7 @@ void TRACE_smpi_set_category(const char *category)
 }
 
 /* PMPI User level calls */
+extern "C" { // Obviously, the C MPI interface should use the C linkage
 
 int PMPI_Init(int *argc, char ***argv)
 {
@@ -2208,17 +2209,25 @@ int PMPI_Reduce_scatter(void *sendbuf, void *recvbuf, int *recvcounts, MPI_Datat
     dt_size_send = smpi_datatype_size(datatype);
   extra->send_size = 0;
   extra->recvcounts= xbt_new(int, size);
-  for(i=0; i< size; i++)//copy data to avoid bad free
+  int totalcount = 0;
+  for(i=0; i< size; i++){//copy data to avoid bad free
     extra->recvcounts[i] = recvcounts[i]*dt_size_send;
-  TRACE_smpi_collective_in(rank, -1, __FUNCTION__,extra);
-
+    totalcount+= recvcounts[i];
+  }
   void* sendtmpbuf=sendbuf;
-    if(sendbuf==MPI_IN_PLACE)
-      sendtmpbuf=recvbuf;
+  if(sendbuf==MPI_IN_PLACE){
+    sendtmpbuf= static_cast<void*>(xbt_malloc(totalcount*smpi_datatype_size(datatype)));
+    memcpy(sendtmpbuf,recvbuf, totalcount*smpi_datatype_size(datatype));
+  }
 
-    mpi_coll_reduce_scatter_fun(sendtmpbuf, recvbuf, recvcounts, datatype,  op, comm);
-    retval = MPI_SUCCESS;
+  TRACE_smpi_collective_in(rank, -1, __FUNCTION__,extra);
+
+  mpi_coll_reduce_scatter_fun(sendtmpbuf, recvbuf, recvcounts, datatype,  op, comm);
+  retval = MPI_SUCCESS;
   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
+
+  if(sendbuf==MPI_IN_PLACE)
+    xbt_free(sendtmpbuf);
   }
 
   smpi_bench_begin();
@@ -2240,7 +2249,7 @@ int PMPI_Reduce_scatter_block(void *sendbuf, void *recvbuf, int recvcount,
   } else if (recvcount < 0) {
     retval = MPI_ERR_ARG;
   } else {
-    int count=smpi_comm_size(comm);
+  int count=smpi_comm_size(comm);
 
   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
@@ -2255,17 +2264,25 @@ int PMPI_Reduce_scatter_block(void *sendbuf, void *recvbuf, int recvcount,
   extra->recvcounts= xbt_new(int, count);
   for(i=0; i< count; i++)//copy data to avoid bad free
     extra->recvcounts[i] = recvcount*dt_size_send;
+  void* sendtmpbuf=sendbuf;
+  if(sendbuf==MPI_IN_PLACE){
+    sendtmpbuf= static_cast<void*>(xbt_malloc(recvcount*count*smpi_datatype_size(datatype)));
+    memcpy(sendtmpbuf,recvbuf, recvcount*count*smpi_datatype_size(datatype));
+  }
 
   TRACE_smpi_collective_in(rank, -1, __FUNCTION__,extra);
 
   int* recvcounts=static_cast<int*>(xbt_malloc(count*sizeof(int)));
-    for (i=0; i<count;i++)
-      recvcounts[i]=recvcount;
-    mpi_coll_reduce_scatter_fun(sendbuf, recvbuf, recvcounts, datatype,  op, comm);
-    xbt_free(recvcounts);
-    retval = MPI_SUCCESS;
+  for (i=0; i<count;i++)
+    recvcounts[i]=recvcount;
+  mpi_coll_reduce_scatter_fun(sendtmpbuf, recvbuf, recvcounts, datatype,  op, comm);
+  xbt_free(recvcounts);
+  retval = MPI_SUCCESS;
 
     TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
+
+  if(sendbuf==MPI_IN_PLACE)
+    xbt_free(sendtmpbuf);
   }
 
   smpi_bench_begin();
@@ -3329,3 +3346,4 @@ int PMPI_Pack_size(int incount, MPI_Datatype datatype, MPI_Comm comm, int* size)
   return MPI_SUCCESS;
 }
 
+} // extern "C"