Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
only test 5th field if 4th exists
[simgrid.git] / src / smpi / smpi_replay.c
index 205208a..ef4cc43 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2009-2014. The SimGrid Team.
+/* Copyright (c) 2009-2015. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
@@ -9,11 +9,13 @@
 #include <xbt.h>
 #include <xbt/replay.h>
 
+#define KEY_SIZE (sizeof(int) * 2 + 1)
+
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_replay,smpi,"Trace Replay with SMPI");
 
 int communicator_size = 0;
 static int active_processes = 0;
-xbt_dynar_t *reqq = NULL;
+xbt_dict_t reqq = NULL;
 
 MPI_Datatype MPI_DEFAULT_TYPE;
 MPI_Datatype MPI_CURRENT_TYPE;
@@ -31,6 +33,30 @@ static void log_timed_action (const char *const *action, double clock){
   }
 }
 
+
+static xbt_dynar_t get_reqq_self(){
+   char * key;
+   
+   int size = asprintf(&key, "%d", smpi_process_index());
+   if(size==-1)
+     xbt_die("could not allocate memory for asprintf");
+   xbt_dynar_t dynar_mpi_request = (xbt_dynar_t) xbt_dict_get(reqq, key);
+   free(key);
+   return dynar_mpi_request;
+}
+
+static void set_reqq_self(xbt_dynar_t mpi_request){
+   char * key;
+   
+   int size = asprintf(&key, "%d", smpi_process_index());
+   if(size==-1)
+     xbt_die("could not allocate memory for asprintf");
+   xbt_dict_set(reqq, key, mpi_request, free);
+   free(key);
+}
+
+
 //allocate a single buffer for all sends, growing it if needed
 void* smpi_get_tmp_sendbuffer(int size){
   if (!smpi_process_get_replaying())
@@ -49,7 +75,7 @@ void* smpi_get_tmp_recvbuffer(int size){
     recvbuffer=xbt_realloc(recvbuffer,size);
     recvbuffer_size=size;
   }
-  return sendbuffer;
+  return recvbuffer;
 }
 
 void smpi_free_tmp_buffer(void* buf){
@@ -145,7 +171,6 @@ const char* encode_datatype(MPI_Datatype datatype, int* known)
 
 static void action_init(const char *const *action)
 {
-  int i;
   XBT_DEBUG("Initialize the counters");
   CHECK_ACTION_PARAMS(action, 0, 1);
   if(action[2]) MPI_DEFAULT_TYPE= MPI_DOUBLE; // default MPE dataype 
@@ -157,12 +182,19 @@ static void action_init(const char *const *action)
   active_processes = smpi_process_count();
 
   if (!reqq) {
+    reqq = xbt_dict_new();
+  }
+
+  set_reqq_self(xbt_dynar_new(sizeof(MPI_Request),&xbt_free_ref));
+
+  /*
     reqq=xbt_new0(xbt_dynar_t,active_processes);
 
     for(i=0;i<active_processes;i++){
       reqq[i]=xbt_dynar_new(sizeof(MPI_Request),&xbt_free_ref);
     }
   }
+  */
 }
 
 static void action_finalize(const char *const *action)
@@ -231,7 +263,9 @@ static void action_send(const char *const *action)
   extra->dst = dst_traced;
   extra->datatype1 = encode_datatype(MPI_CURRENT_TYPE, NULL);
   TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, extra);
-  TRACE_smpi_send(rank, rank, dst_traced, size*smpi_datatype_size(MPI_CURRENT_TYPE));
+  if (!TRACE_smpi_view_internals()) {
+    TRACE_smpi_send(rank, rank, dst_traced, size*smpi_datatype_size(MPI_CURRENT_TYPE));
+  }
 
   smpi_mpi_send(NULL, size, MPI_CURRENT_TYPE, to , 0, MPI_COMM_WORLD);
 
@@ -260,14 +294,16 @@ static void action_Isend(const char *const *action)
   extra->dst = dst_traced;
   extra->datatype1 = encode_datatype(MPI_CURRENT_TYPE, NULL);
   TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, extra);
-  TRACE_smpi_send(rank, rank, dst_traced, size*smpi_datatype_size(MPI_CURRENT_TYPE));
+  if (!TRACE_smpi_view_internals()) {
+    TRACE_smpi_send(rank, rank, dst_traced, size*smpi_datatype_size(MPI_CURRENT_TYPE));
+  }
 
   request = smpi_mpi_isend(NULL, size, MPI_CURRENT_TYPE, to, 0,MPI_COMM_WORLD);
 
   TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
   request->send = 1;
 
-  xbt_dynar_push(reqq[smpi_process_index()],&request);
+  xbt_dynar_push(get_reqq_self(),&request);
 
   log_timed_action (action, clock);
 }
@@ -302,7 +338,9 @@ static void action_recv(const char *const *action) {
   smpi_mpi_recv(NULL, size, MPI_CURRENT_TYPE, from, 0, MPI_COMM_WORLD, &status);
 
   TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
-  TRACE_smpi_recv(rank, src_traced, rank);
+  if (!TRACE_smpi_view_internals()) {
+    TRACE_smpi_recv(rank, src_traced, rank);
+  }
 
   log_timed_action (action, clock);
 }
@@ -338,7 +376,7 @@ static void action_Irecv(const char *const *action)
 
   TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
   request->recv = 1;
-  xbt_dynar_push(reqq[smpi_process_index()],&request);
+  xbt_dynar_push(get_reqq_self(),&request);
 
   log_timed_action (action, clock);
 }
@@ -350,7 +388,7 @@ static void action_test(const char *const *action){
   MPI_Status status;
   int flag = TRUE;
 
-  request = xbt_dynar_pop_as(reqq[smpi_process_index()],MPI_Request);
+  request = xbt_dynar_pop_as(get_reqq_self(),MPI_Request);
   //if request is null here, this may mean that a previous test has succeeded 
   //Different times in traced application and replayed version may lead to this 
   //In this case, ignore the extra calls.
@@ -366,7 +404,7 @@ static void action_test(const char *const *action){
          /* push back request in dynar to be caught by a subsequent wait. if the test
           * did succeed, the request is now NULL.
           */
-         xbt_dynar_push_as(reqq[smpi_process_index()],MPI_Request, request);
+         xbt_dynar_push_as(get_reqq_self(),MPI_Request, request);
 
          TRACE_smpi_testing_out(rank);
   }
@@ -379,10 +417,10 @@ static void action_wait(const char *const *action){
   MPI_Request request;
   MPI_Status status;
 
-  xbt_assert(xbt_dynar_length(reqq[smpi_process_index()]),
+  xbt_assert(xbt_dynar_length(get_reqq_self()),
       "action wait not preceded by any irecv or isend: %s",
       xbt_str_join_array(action," "));
-  request = xbt_dynar_pop_as(reqq[smpi_process_index()],MPI_Request);
+  request = xbt_dynar_pop_as(get_reqq_self(),MPI_Request);
 
   if (!request){
     /* Assuming that the trace is well formed, this mean the comm might have
@@ -417,7 +455,7 @@ static void action_waitall(const char *const *action){
   int count_requests=0;
   unsigned int i=0;
 
-  count_requests=xbt_dynar_length(reqq[smpi_process_index()]);
+  count_requests=xbt_dynar_length(get_reqq_self());
 
   if (count_requests>0) {
     MPI_Request requests[count_requests];
@@ -425,7 +463,7 @@ static void action_waitall(const char *const *action){
 
     /*  The reqq is an array of dynars. Its index corresponds to the rank.
      Thus each rank saves its own requests to the array request. */
-    xbt_dynar_foreach(reqq[smpi_process_index()],i,requests[i]); 
+    xbt_dynar_foreach(get_reqq_self(),i,requests[i]); 
 
    //save information from requests
 
@@ -477,9 +515,8 @@ static void action_waitall(const char *const *action){
    xbt_dynar_free(&dsts);
    xbt_dynar_free(&recvs);
 
-   int freedrank=smpi_process_index();
-   xbt_dynar_free_container(&(reqq[freedrank]));
-   reqq[freedrank]=xbt_dynar_new(sizeof(MPI_Request),&xbt_free_ref);
+   //TODO xbt_dynar_free_container(get_reqq_self());
+   set_reqq_self(xbt_dynar_new(sizeof(MPI_Request),&xbt_free_ref));
   }
   log_timed_action (action, clock);
 }
@@ -652,7 +689,7 @@ static void action_gather(const char *const *action) {
   int send_size = parse_double(action[2]);
   int recv_size = parse_double(action[3]);
   MPI_Datatype MPI_CURRENT_TYPE2;
-  if(action[5]) {
+  if(action[4] && action[5]) {
     MPI_CURRENT_TYPE=decode_datatype(action[5]);
     MPI_CURRENT_TYPE2=decode_datatype(action[6]);
   } else {
@@ -1006,7 +1043,8 @@ static void action_allToAllv(const char *const *action) {
   xbt_free(recvdisps);
 }
 
-void smpi_replay_init(int *argc, char***argv){
+void smpi_replay_run(int *argc, char***argv){
+  /* First initializes everything */
   smpi_process_init(argc, argv);
   smpi_process_mark_as_initialized();
   smpi_process_set_replaying(1);
@@ -1016,11 +1054,12 @@ void smpi_replay_init(int *argc, char***argv){
   TRACE_smpi_computing_init(rank);
   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
   extra->type = TRACING_INIT;
-  TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
-  TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
+  char *operation =bprintf("%s_init",__FUNCTION__);
+  TRACE_smpi_collective_in(rank, -1, operation, extra);
+  TRACE_smpi_collective_out(rank, -1, operation);
+  free(operation);
 
-  if (!action_funs){
-    _xbt_replay_action_init();
+  if (!_xbt_replay_action_init()) {
     xbt_replay_action_register("init",       action_init);
     xbt_replay_action_register("finalize",   action_finalize);
     xbt_replay_action_register("comm_size",  action_comm_size);
@@ -1055,22 +1094,27 @@ void smpi_replay_init(int *argc, char***argv){
       THROWF(unknown_error, 0, "%s is not a double", (*argv)[2]);
     XBT_VERB("Delayed start for instance - Sleeping for %f flops ",value );
     smpi_execute_flops(value);
+  } else {
+    //UGLY: force a context switch to be sure that all MSG_processes begin initialization
+    XBT_DEBUG("Force context switch by smpi_execute_flops  - Sleeping for 0.0 flops ");
+    smpi_execute_flops(0.0);
   }
+  
+  /* Actually run the replay */
   xbt_replay_action_runner(*argc, *argv);
-}
 
-int smpi_replay_finalize(){
+  /* and now, finalize everything */
   double sim_time= 1.;
   /* One active process will stop. Decrease the counter*/
   XBT_DEBUG("There are %lu elements in reqq[*]",
-            xbt_dynar_length(reqq[smpi_process_index()]));
-  if (!xbt_dynar_is_empty(reqq[smpi_process_index()])){
-    int count_requests=xbt_dynar_length(reqq[smpi_process_index()]);
+            xbt_dynar_length(get_reqq_self()));
+  if (!xbt_dynar_is_empty(get_reqq_self())){
+    int count_requests=xbt_dynar_length(get_reqq_self());
     MPI_Request requests[count_requests];
     MPI_Status status[count_requests];
     unsigned int i;
 
-    xbt_dynar_foreach(reqq[smpi_process_index()],i,requests[i]);
+    xbt_dynar_foreach(get_reqq_self(),i,requests[i]);
     smpi_mpi_waitall(count_requests, requests, status);
     active_processes--;
   } else {
@@ -1084,27 +1128,27 @@ int smpi_replay_finalize(){
   }
   
 
-  xbt_dynar_free_container(&(reqq[smpi_process_index()]));
+  //TODO xbt_dynar_free_container(get_reqq_self()));
 
   if(!active_processes){
     XBT_INFO("Simulation time %f", sim_time);
     _xbt_replay_action_exit();
     xbt_free(sendbuffer);
     xbt_free(recvbuffer);
-    xbt_free(reqq);
+    //xbt_free(reqq);
+    xbt_dict_free(&reqq); //not need, data have been freed ???
     reqq = NULL;
   }
   
-
-  int rank = smpi_process_index();
-  instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
-  extra->type = TRACING_FINALIZE;
-  TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
+  instr_extra_data extra_fin = xbt_new0(s_instr_extra_data_t,1);
+  extra_fin->type = TRACING_FINALIZE;
+  operation =bprintf("%s_finalize",__FUNCTION__);
+  TRACE_smpi_collective_in(rank, -1, operation, extra_fin);
 
   smpi_process_finalize();
 
-  TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
+  TRACE_smpi_collective_out(rank, -1, operation);
   TRACE_smpi_finalize(smpi_process_index());
   smpi_process_destroy();
-  return MPI_SUCCESS;
+  free(operation);
 }