Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cleanup toward persistent communications.
[simgrid.git] / src / smpi / smpi_base.c
index 40b1b58..e1a0c56 100644 (file)
@@ -13,6 +13,9 @@ XBT_LOG_EXTERNAL_CATEGORY(smpi_receiver);
 XBT_LOG_EXTERNAL_CATEGORY(smpi_sender);
 XBT_LOG_EXTERNAL_CATEGORY(smpi_util);
 
+#define EAGER_LIMIT 65536
+#define RDV_TAG     (-10)
+
 void smpi_process_init(int* argc, char*** argv) {
   int index;
   smpi_process_data_t data;
@@ -23,6 +26,7 @@ void smpi_process_init(int* argc, char*** argv) {
   data = smpi_process_remote_data(index);
   SIMIX_process_set_data(proc, data);
   if (*argc > 2) {
+    free((*argv)[1]);
     memmove(&(*argv)[1], &(*argv)[2], sizeof(char *) * (*argc - 2));
     (*argv)[(*argc) - 1] = NULL;
   }
@@ -39,30 +43,44 @@ void smpi_process_destroy(void) {
 /* MPI Low level calls */
 MPI_Request smpi_mpi_isend(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
   MPI_Request request;
+  size_t size = smpi_datatype_size(datatype) * count;
 
+  if (size > EAGER_LIMIT) {
+       /* Warning: this (zero-length synchronous) call will come back here with size == 0 */
+   DEBUG1("RDV send to %d", dst);
+  }
   request = xbt_new(s_smpi_mpi_request_t, 1);
   request->comm = comm;
   request->src = smpi_comm_rank(comm);
   request->dst = dst;
   request->tag = tag;
-  request->size = smpi_datatype_size(datatype) * count;
+  request->size = size;
   request->complete = 0;
+  request->match = MPI_REQUEST_NULL;
   smpi_process_post_send(comm, request);
-  request->pair = SIMIX_network_isend(request->rdv, request->size, -1.0, buf, request->size, request);
+  DEBUG3("NEW send request %p with rdv %p and match %p", request, request->rdv, request->match);
+  request->pair = SIMIX_network_isend(request->rdv, request->size, -1.0, buf, request->size, NULL);
   return request;
 }
 
 MPI_Request smpi_mpi_irecv(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm) {
   MPI_Request request;
+  size_t size = smpi_datatype_size(datatype) * count;
 
+  if (size > EAGER_LIMIT) {
+       /* Warning: this (zero-length synchronous) call will come back here with size == 0 */
+   DEBUG1("RDV recv from %d", src);
+  }
   request = xbt_new(s_smpi_mpi_request_t, 1);
   request->comm = comm;
   request->src = src;
   request->dst = smpi_comm_rank(comm);
   request->tag = tag;
-  request->size = smpi_datatype_size(datatype) * count;
+  request->size = size;
   request->complete = 0;
+  request->match = MPI_REQUEST_NULL;
   smpi_process_post_recv(request);
+  DEBUG3("NEW recv request %p with rdv %p and match %p", request, request->rdv, request->match);
   request->pair = SIMIX_network_irecv(request->rdv, buf, &request->size);
   return request;
 }
@@ -74,10 +92,10 @@ void smpi_mpi_recv(void* buf, int count, MPI_Datatype datatype, int src, int tag
   smpi_mpi_wait(&request, status);
 }
 
-void smpi_mpi_send(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm) {
+void smpi_mpi_send(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
   MPI_Request request;
 
-  request = smpi_mpi_isend(buf, count, datatype, src, tag, comm);
+  request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
   smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
 }
 
@@ -95,72 +113,58 @@ void smpi_mpi_sendrecv(void* sendbuf, int sendcount, MPI_Datatype sendtype, int
 }
 
 static void finish_wait(MPI_Request* request, MPI_Status* status) {
-  MPI_Request data = (MPI_Request)SIMIX_communication_get_data((*request)->pair);
-
-  xbt_assert0(data != MPI_REQUEST_NULL, "Erroneous situation");
   if(status != MPI_STATUS_IGNORE) {
     status->MPI_SOURCE = (*request)->src;
     status->MPI_TAG = (*request)->tag;
     status->MPI_ERROR = MPI_SUCCESS;
   }
-  DEBUG3("finishing wait for %p [data = %p, complete = %d]", *request, data, data->complete);
-  // data == *request if sender is first to finish its wait
-  // data != *request if receiver is first to finish its wait
-  if(data->complete == 0) {
-    // first arrives here
-    data->complete = 1;
-    if(data != *request) {
-      // receveiver cleans its part
-      xbt_free(*request);
-    }
+  DEBUG5("finishing wait for %p [src = %d, dst = %d, tag = %d, complete = %d]",
+         *request, (*request)->src, (*request)->dst, (*request)->tag, (*request)->complete);
+  DEBUG2("match %p, complete %d", (*request)->match, (*request)->match ? (*request)->match->complete : -1);
+  if((*request)->complete == 1) {
+    SIMIX_rdv_destroy((*request)->rdv);
   } else {
-    // second arrives here
-    if(data != *request) {
-      // receiver cleans everything
-      xbt_free(data);
-    }
-    xbt_free(*request);
+    (*request)->match->complete = 1;
+    (*request)->match->match = MPI_REQUEST_NULL;
   }
+  xbt_free(*request);
   *request = MPI_REQUEST_NULL;
 }
 
 int smpi_mpi_test(MPI_Request* request, MPI_Status* status) {
-  MPI_Request data = (MPI_Request)SIMIX_communication_get_data((*request)->pair);
-  int flag = data && data->complete == 1;
+  int flag = (*request)->complete;
 
   if(flag) {
+    SIMIX_communication_destroy((*request)->pair);
     finish_wait(request, status);
   }
   return flag;
 }
 
 int smpi_mpi_testany(int count, MPI_Request requests[], int* index, MPI_Status* status) {
-  MPI_Request data;
   int i, flag;
 
   *index = MPI_UNDEFINED;
   flag = 0;
   for(i = 0; i < count; i++) {
-    if(requests[i] != MPI_REQUEST_NULL) {
-      data = (MPI_Request)SIMIX_communication_get_data(requests[i]->pair);
-      if(data != MPI_REQUEST_NULL && data->complete == 1) {
-        finish_wait(&requests[i], status);
-        *index = i;
-        flag = 1;
-        break;
-      }
+    if(requests[i] != MPI_REQUEST_NULL && requests[i]->complete) {
+      SIMIX_communication_destroy(requests[i]->pair);
+      finish_wait(&requests[i], status);
+      *index = i;
+      flag = 1;
+      break;
     }
   }
   return flag;
 }
 
 void smpi_mpi_wait(MPI_Request* request, MPI_Status* status) {
-  MPI_Request data = (MPI_Request)SIMIX_communication_get_data((*request)->pair);
-
-  DEBUG6("wait for request %p (%p: %p) [src = %d, dst = %d, tag = %d]",
-         *request, (*request)->pair, data, (*request)->src, (*request)->dst, (*request)->tag);
+  DEBUG6("wait for request %p (%p) [src = %d, dst = %d, tag = %d, complete = %d]",
+         *request, (*request)->pair, (*request)->src, (*request)->dst, (*request)->tag, (*request)->complete);
   // data is null if receiver waits before sender enters the rdv
-  if(data == MPI_REQUEST_NULL || data->complete == 0) {
+  if((*request)->complete) {
+    SIMIX_communication_destroy((*request)->pair);
+  } else {
     SIMIX_network_wait((*request)->pair, -1.0);
   }
   finish_wait(request, status);
@@ -168,7 +172,6 @@ void smpi_mpi_wait(MPI_Request* request, MPI_Status* status) {
 
 int smpi_mpi_waitany(int count, MPI_Request requests[], MPI_Status* status) {
   xbt_dynar_t comms;
-  MPI_Request data;
   int i, size, index;
   int* map;
 
@@ -176,12 +179,10 @@ int smpi_mpi_waitany(int count, MPI_Request requests[], MPI_Status* status) {
   if(count > 0) {
     // First check for already completed requests
     for(i = 0; i < count; i++) {
-      if(requests[i] != MPI_REQUEST_NULL) {
-        data = (MPI_Request)SIMIX_communication_get_data(requests[i]->pair);
-        if(data != MPI_REQUEST_NULL && data->complete == 1) {
-          index = i;
-          break;
-        }
+      if(requests[i] != MPI_REQUEST_NULL && requests[i]->complete) {
+        index = i;
+        SIMIX_communication_destroy(requests[index]->pair); // always succeeds (but cleans the simix layer)
+        break;
       }
     }
     if(index == MPI_UNDEFINED) {
@@ -204,7 +205,7 @@ int smpi_mpi_waitany(int count, MPI_Request requests[], MPI_Status* status) {
         index = map[index];
       }
       xbt_free(map);
-      xbt_dynar_free_container(&comms);
+      xbt_dynar_free(&comms);
     }
     if(index != MPI_UNDEFINED) {
       finish_wait(&requests[index], status);
@@ -225,6 +226,7 @@ void smpi_mpi_waitall(int count, MPI_Request requests[],  MPI_Status status[]) {
     if(status != MPI_STATUS_IGNORE) {
       memcpy(&status[index], &stat, sizeof(stat));
     }
+    // FIXME: check this -v
     // Move the last request to the found position
     requests[index] = requests[count - 1];
     requests[count - 1] = MPI_REQUEST_NULL;
@@ -233,18 +235,15 @@ void smpi_mpi_waitall(int count, MPI_Request requests[],  MPI_Status status[]) {
 }
 
 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int* indices, MPI_Status status[]) {
-  MPI_Request data;
   int i, count;
 
   count = 0;
-  for(i = 0; i < count; i++) {
-    if(requests[i] != MPI_REQUEST_NULL) {
-      data = (MPI_Request)SIMIX_communication_get_data(requests[i]->pair);
-      if(data != MPI_REQUEST_NULL && data->complete == 1) {
-        finish_wait(&requests[i], status != MPI_STATUS_IGNORE ? &status[i] : MPI_STATUS_IGNORE);
-        indices[count] = i;
-        count++;
-      }
+  for(i = 0; i < incount; i++) {
+    if(requests[i] != MPI_REQUEST_NULL && requests[i]->complete) {
+      SIMIX_communication_destroy(requests[i]->pair);
+      finish_wait(&requests[i], status != MPI_STATUS_IGNORE ? &status[i] : MPI_STATUS_IGNORE);
+      indices[count] = i;
+      count++;
     }
   }
   return count;
@@ -438,7 +437,7 @@ void smpi_mpi_reduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datat
   int rank, size, src, index, datasize;
   MPI_Request* requests;
   void** tmpbufs;
+
   rank = smpi_comm_rank(comm);
   size = smpi_comm_size(comm);
   if(rank != root) {
@@ -447,7 +446,7 @@ void smpi_mpi_reduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datat
   } else {
     datasize = smpi_datatype_size(datatype);
     // Local copy from root
-    memcpy(recvbuf, sendbuf, count * datasize * sizeof(char)); 
+    memcpy(recvbuf, sendbuf, count * datasize * sizeof(char));
     // Receive buffers from senders
     //TODO: make a MPI_barrier here ?
     requests = xbt_new(MPI_Request, size - 1);
@@ -487,12 +486,12 @@ FIXME: buggy implementation
   int rank, size, other, index, datasize;
   MPI_Request* requests;
   void** tmpbufs;
+
   rank = smpi_comm_rank(comm);
   size = smpi_comm_size(comm);
   datasize = smpi_datatype_size(datatype);
   // Local copy from self
-  memcpy(recvbuf, sendbuf, count * datasize * sizeof(char)); 
+  memcpy(recvbuf, sendbuf, count * datasize * sizeof(char));
   // Send/Recv buffers to/from others;
   //TODO: make a MPI_barrier here ?
   requests = xbt_new(MPI_Request, 2 * (size - 1));
@@ -524,3 +523,47 @@ FIXME: buggy implementation
   xbt_free(requests);
 */
 }
+
+void smpi_mpi_scan(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm) {
+  int system_tag = 666;
+  int rank, size, other, index, datasize;
+  int total;
+  MPI_Request* requests;
+  void** tmpbufs;
+
+  rank = smpi_comm_rank(comm);
+  size = smpi_comm_size(comm);
+  datasize = smpi_datatype_size(datatype);
+  // Local copy from self
+  memcpy(recvbuf, sendbuf, count * datasize * sizeof(char));
+  // Send/Recv buffers to/from others;
+  total = rank + (size - (rank + 1));
+  requests = xbt_new(MPI_Request, total);
+  tmpbufs = xbt_new(void*, rank);
+  index = 0;
+  for(other = 0; other < rank; other++) {
+    tmpbufs[index] = xbt_malloc(count * datasize);
+    requests[index] = smpi_mpi_irecv(tmpbufs[index], count, datatype, other, system_tag, comm);
+    index++;
+  }
+  for(other = rank + 1; other < size; other++) {
+    requests[index] = smpi_mpi_isend(sendbuf, count, datatype, other, system_tag, comm);
+    index++;
+  }
+  // Wait for completion of all comms.
+  for(other = 0; other < total; other++) {
+    index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
+    if(index == MPI_UNDEFINED) {
+      break;
+    }
+    if(index < rank) {
+      // #Request is below rank: it's a irecv
+      smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
+    }
+  }
+  for(index = 0; index < size - 1; index++) {
+    xbt_free(tmpbufs[index]);
+  }
+  xbt_free(tmpbufs);
+  xbt_free(requests);
+}