Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Yet another batch of fixes
[simgrid.git] / src / smpi / smpi_base.cpp
index dac899c..e892f89 100644 (file)
@@ -252,10 +252,12 @@ static MPI_Request build_request(void *buf, int count, MPI_Datatype datatype, in
   request->old_type = datatype;
 
   request->size = smpi_datatype_size(datatype) * count;
+  smpi_datatype_use(datatype);
   request->src = src;
   request->dst = dst;
   request->tag = tag;
   request->comm = comm;
+  smpi_comm_use(request->comm);
   request->action = NULL;
   request->flags = flags;
   request->detached = 0;
@@ -272,7 +274,6 @@ static MPI_Request build_request(void *buf, int count, MPI_Datatype datatype, in
   request->op = MPI_REPLACE;
   request->send = 0;
   request->recv = 0;
- // if (flags & SEND) smpi_datatype_unuse(datatype);
 
   return request;
 }
@@ -382,8 +383,6 @@ void smpi_mpi_start(MPI_Request request)
 
     // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
     request->real_size=request->size;
-    smpi_datatype_use(request->old_type);
-    smpi_comm_use(request->comm);
     request->action = simcall_comm_irecv(SIMIX_process_self(), mailbox, request->buf, &request->real_size, &match_recv,
                                          !smpi_process_get_replaying()? &smpi_comm_copy_buffer_callback
                                          : &smpi_comm_null_copy_buffer_callback, request, -1.0);
@@ -472,8 +471,6 @@ void smpi_mpi_start(MPI_Request request)
 
     // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
     request->real_size=request->size;
-    smpi_datatype_use(request->old_type);
-    smpi_comm_use(request->comm);
     request->action = simcall_comm_isend(SIMIX_process_from_PID(request->src+1), mailbox, request->size, -1.0,
                                          buf, request->real_size, &match_send,
                          &xbt_free_f, // how to free the userdata if a detached send fails
@@ -508,6 +505,8 @@ void smpi_mpi_request_free(MPI_Request * request)
     if((*request)->refcount<0) xbt_die("wrong refcount");
 
     if((*request)->refcount==0){
+        smpi_datatype_unuse((*request)->old_type);
+        smpi_comm_unuse((*request)->comm);
         print_request("Destroying", (*request));
         xbt_free(*request);
         *request = MPI_REQUEST_NULL;
@@ -530,6 +529,7 @@ MPI_Request smpi_rma_send_init(void *buf, int count, MPI_Datatype datatype, int
     request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype,  src, dst, tag,
                             comm, RMA | NON_PERSISTENT | ISEND | SEND | PREPARED | ACCUMULATE);
     request->op = op;
+//    smpi_datatype_use(datatype);
   }
   return request;
 }
@@ -545,6 +545,7 @@ MPI_Request smpi_rma_recv_init(void *buf, int count, MPI_Datatype datatype, int
     request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype,  src, dst, tag,
                             comm, RMA | NON_PERSISTENT | RECV | PREPARED | ACCUMULATE);
     request->op = op;
+//    smpi_datatype_use(datatype);
   }
   return request;
 }
@@ -686,14 +687,13 @@ static void finish_wait(MPI_Request * request, MPI_Status * status)
         if(req->flags & RECV)
           subtype->unserialize(req->buf, req->old_buf, req->real_size/smpi_datatype_size(datatype) ,
                                datatype->substruct, req->op);
-        if(req->detached == 0) free(req->buf);
+        xbt_free(req->buf);
       }else if(req->flags & RECV){//apply op on contiguous buffer for accumulate
           int n =req->real_size/smpi_datatype_size(datatype);
           smpi_op_apply(req->op, req->buf, req->old_buf, &n, &datatype);
+          xbt_free(req->buf);
       }
     }
-    smpi_comm_unuse(req->comm);
-    smpi_datatype_unuse(req->old_type);
   }
 
   if (TRACE_smpi_view_internals()) {
@@ -705,8 +705,6 @@ static void finish_wait(MPI_Request * request, MPI_Status * status)
   }
 
   if(req->detached_sender!=NULL){
-    smpi_datatype_unuse(req->detached_sender->old_type);
-    smpi_comm_unuse(req->detached_sender->comm);
     smpi_mpi_request_free(&(req->detached_sender));
   }
   if(req->flags & PERSISTENT)
@@ -1308,10 +1306,7 @@ void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datat
   void **tmpbufs;
 
   char* sendtmpbuf = (char*) sendbuf;
-  if( sendbuf == MPI_IN_PLACE ) {
-    sendtmpbuf = (char *)smpi_get_tmp_sendbuffer(count*smpi_datatype_get_extent(datatype));
-    smpi_datatype_copy(recvbuf, count, datatype,sendtmpbuf, count, datatype);
-  }
+
 
   rank = smpi_comm_rank(comm);
   size = smpi_comm_size(comm);
@@ -1320,6 +1315,11 @@ void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datat
     smpi_coll_tuned_reduce_ompi_basic_linear(sendtmpbuf, recvbuf, count, datatype, op, root, comm);
     return;
   }
+
+  if( sendbuf == MPI_IN_PLACE ) {
+    sendtmpbuf = (char *)smpi_get_tmp_sendbuffer(count*smpi_datatype_get_extent(datatype));
+    smpi_datatype_copy(recvbuf, count, datatype,sendtmpbuf, count, datatype);
+  }
   
   if(rank != root) {
     // Send buffer to root
@@ -1367,9 +1367,9 @@ void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datat
     xbt_free(tmpbufs);
     xbt_free(requests);
 
-    if( sendbuf == MPI_IN_PLACE ) {
-      smpi_free_tmp_buffer(sendtmpbuf);
-    }
+  }
+  if( sendbuf == MPI_IN_PLACE ) {
+    smpi_free_tmp_buffer(sendtmpbuf);
   }
 }