Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Return action state on SIMIX_req_host_execution_wait().
[simgrid.git] / src / msg / gos.c
index 859bb08..46124d8 100644 (file)
@@ -38,6 +38,7 @@ MSG_error_t MSG_task_execute(m_task_t task)
 {
   simdata_task_t simdata = NULL;
   m_process_t self = MSG_process_self();
+  e_smx_state_t comp_state;
   CHECK_HOST();
 
   simdata = task->simdata;
@@ -49,9 +50,9 @@ MSG_error_t MSG_task_execute(m_task_t task)
   TRACE_msg_task_execute_start(task);
 #endif
 
-  xbt_assert1((!simdata->compute) && (task->simdata->refcount == 1),
+  xbt_assert1((!simdata->compute) && (task->simdata->isused == 0),
               "This task is executed somewhere else. Go fix your code! %d",
-              task->simdata->refcount);
+              task->simdata->isused);
 
   DEBUG1("Computing on %s", MSG_process_self()->simdata->m_host->name);
 
@@ -61,23 +62,23 @@ MSG_error_t MSG_task_execute(m_task_t task)
 #endif
     return MSG_OK;
   }
-  simdata->refcount++;
+  simdata->isused=1;
   simdata->compute =
       SIMIX_req_host_execute(task->name, SIMIX_host_self(),
-                           simdata->computation_amount);
-  SIMIX_req_host_execution_set_priority(simdata->compute, simdata->priority);
+                           simdata->computation_amount,
+                           simdata->priority);
 #ifdef HAVE_TRACING
   SIMIX_req_set_category(simdata->compute, task->category);
 #endif
 
   self->simdata->waiting_action = simdata->compute;
-  SIMIX_req_host_execution_wait(simdata->compute);
+  comp_state = SIMIX_req_host_execution_wait(simdata->compute);
   self->simdata->waiting_action = NULL;
 
-  simdata->refcount--;
+  simdata->isused=0;
 
-  DEBUG2("Execution task '%s' finished in state %d", task->name, SIMIX_req_host_execution_get_state(task->simdata->compute));
-  if (SIMIX_req_host_execution_get_state(task->simdata->compute) == SIMIX_DONE) {
+  DEBUG2("Execution task '%s' finished in state %d", task->name, comp_state);
+  if (comp_state == SIMIX_DONE) {
     /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
     SIMIX_req_host_execution_destroy(task->simdata->compute);
     simdata->computation_amount = 0.0;
@@ -148,7 +149,7 @@ MSG_parallel_task_create(const char *name, int host_nb,
   simdata->compute = NULL;
   simdata->comm = NULL;
   simdata->rate = -1.0;
-  simdata->refcount = 1;
+  simdata->isused = 0;
   simdata->sender = NULL;
   simdata->receiver = NULL;
   simdata->source = NULL;
@@ -173,7 +174,7 @@ MSG_error_t MSG_parallel_task_execute(m_task_t task)
   simdata = task->simdata;
 
   xbt_assert0((!simdata->compute)
-              && (task->simdata->refcount == 1),
+              && (task->simdata->isused == 0),
               "This task is executed somewhere else. Go fix your code!");
 
   xbt_assert0(simdata->host_nb,
@@ -181,7 +182,7 @@ MSG_error_t MSG_parallel_task_execute(m_task_t task)
 
   DEBUG1("Parallel computing on %s", MSG_process_self()->simdata->m_host->name);
 
-  simdata->refcount++;
+  simdata->isused=1;
 
   simdata->compute =
       SIMIX_req_host_parallel_execute(task->name, simdata->host_nb,
@@ -196,7 +197,7 @@ MSG_error_t MSG_parallel_task_execute(m_task_t task)
 
   DEBUG2("Finished waiting for execution of action %p, state = %d", simdata->compute, SIMIX_req_host_execution_get_state(task->simdata->compute));
 
-  simdata->refcount--;
+  simdata->isused=0;
 
   if (SIMIX_req_host_execution_get_state(task->simdata->compute) == SIMIX_DONE) {
     /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
@@ -404,18 +405,17 @@ msg_comm_t MSG_task_isend(m_task_t task, const char *alias)
   t_simdata->sender = process;
   t_simdata->source = MSG_host_self();
 
-  xbt_assert0(t_simdata->refcount == 1,
+  xbt_assert0(t_simdata->isused == 0,
               "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
 
-  t_simdata->refcount++;
+  t_simdata->isused=1;
   msg_global->sent_msg++;
-  process->simdata->waiting_task = task;
 
   /* Send it by calling SIMIX network layer */
-
-  return SIMIX_req_comm_isend(mailbox, t_simdata->message_size,
-                             t_simdata->rate, task, sizeof(void *),
-                             &t_simdata->comm);
+  t_simdata->comm =
+    SIMIX_req_comm_isend(mailbox, t_simdata->message_size,
+                         t_simdata->rate, task, sizeof(void *), NULL, NULL);
+  return t_simdata->comm;
 }
 
 /** \ingroup msg_gos_functions
@@ -444,36 +444,85 @@ msg_comm_t MSG_task_irecv(m_task_t * task, const char *alias)
         ("MSG_task_get() was asked to write in a non empty task struct.");
 
   /* Try to receive it by calling SIMIX network layer */
-  return SIMIX_req_comm_irecv(rdv, task, NULL);
+  return SIMIX_req_comm_irecv(rdv, task, NULL, NULL, NULL);
 }
 
 /** \ingroup msg_gos_functions
- * \brief Test the status of a communication.
- *
- * It takes one parameter.
- * \param comm the communication to test.
- * \return the status of the communication:
- *             TRUE : the communication is completed
- *             FALSE: the communication is incompleted
+ * \brief Returns whether a communication is finished.
+ * \param comm the communication to test
+ * \return TRUE if the communication is finished
+ * (but it may have failed, use MSG_comm_get_status() to know its status)
+ * or FALSE if the communication is not finished yet
  * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
  */
 int MSG_comm_test(msg_comm_t comm)
 {
-  return SIMIX_req_comm_test(comm);
+  xbt_ex_t e;
+  int finished = 0;
+  TRY {
+    finished = SIMIX_req_comm_test(comm);
+  }
+  CATCH(e) {
+    switch (e.category) {
+
+      case host_error:
+      case network_error:
+      case timeout_error:
+        finished = 1;
+        break;
+
+      default:
+        RETHROW;
+    }
+    xbt_ex_free(e);
+  }
+  return finished;
+}
+
+/** \ingroup msg_gos_functions
+ * \brief This function checks if a communication is finished
+ * \param comms a vector of communications
+ * \return the position of the finished communication if any
+ * (but it may have failed, use MSG_comm_get_status() to know its status),
+ * or -1 if none is finished
+ */
+int MSG_comm_testany(xbt_dynar_t comms)
+{
+  xbt_ex_t e;
+  int finished_index = -1;
+  TRY {
+    finished_index = SIMIX_req_comm_testany(comms);
+  }
+  CATCH(e) {
+    switch (e.category) {
+
+      case host_error:
+      case network_error:
+      case timeout_error:
+        finished_index = e.value;
+        break;
+
+      default:
+        RETHROW;
+    }
+    xbt_ex_free(e);
+  }
+  return finished_index;
 }
 
 /** \ingroup msg_gos_functions
- * \brief After received TRUE to MSG_comm_test(), the communication must be destroyed.
+ * \brief After received TRUE to MSG_comm_test(), the communication should be destroyed.
  *
  * It takes one parameter.
  * \param comm the communication to destroy.
  */
 void MSG_comm_destroy(msg_comm_t comm)
 {
-  if (SIMIX_req_comm_get_src_proc(comm) != SIMIX_process_self()) {
+  if (SIMIX_req_comm_get_src_proc(comm) != SIMIX_process_self()
+      && MSG_comm_get_status(comm) == MSG_OK) {
     m_task_t task;
     task = (m_task_t) SIMIX_req_comm_get_src_buff(comm);
-    task->simdata->refcount--;
+    task->simdata->isused=0;
   }
   SIMIX_req_comm_destroy(comm);
 }
@@ -496,9 +545,8 @@ MSG_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
     if (SIMIX_req_comm_get_src_proc(comm) != SIMIX_process_self()) {
       m_task_t task;
       task = (m_task_t) SIMIX_req_comm_get_src_buff(comm);
-      task->simdata->refcount--;
+      task->simdata->isused=0;
     }
-    SIMIX_req_comm_destroy(comm);
 
     /* FIXME: these functions are not tracable */
   }
@@ -514,7 +562,7 @@ MSG_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
       res = MSG_TIMEOUT;
       break;
     default:
-      xbt_die(bprintf("Unhandled SIMIX network exception: %s", e.msg));
+      RETHROW;
     }
     xbt_ex_free(e);
   }
@@ -546,19 +594,64 @@ void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
 */
 int MSG_comm_waitany(xbt_dynar_t comms)
 {
-  return SIMIX_req_comm_waitany(comms);
+  xbt_ex_t e;
+  int finished_index = -1;
+  TRY {
+    finished_index = SIMIX_req_comm_waitany(comms);
+  }
+  CATCH(e) {
+    switch (e.category) {
+
+      case host_error:
+      case network_error:
+      case timeout_error:
+        finished_index = e.value;
+      default:
+        RETHROW;
+    }
+    xbt_ex_free(e);
+  }
+  return finished_index;
 }
 
-/** \ingroup msg_gos_functions
-* \brief This function wait for the first completed communication
-*
-* It takes on parameter.
-* \param comms a vector of communication
-* \return the position of the completed communication, if any, or -1 if none was completed
-*/
-int MSG_comm_testany(xbt_dynar_t comms)
-{
-  return SIMIX_req_comm_testany(comms);
+/**
+ * \ingroup msg_gos_functions
+ * \brief Returns the error (if any) that occured during a finished communication.
+ * \param comm a finished communication
+ * \return the status of the communication, or MSG_OK if the communication
+ * was successfully completed
+ */
+MSG_error_t MSG_comm_get_status(msg_comm_t comm) {
+
+  MSG_error_t result;
+  e_smx_state_t smx_state = SIMIX_req_comm_get_state(comm);
+
+  switch (smx_state) {
+
+    case SIMIX_CANCELED:
+      result = MSG_TASK_CANCELLED;
+      break;
+
+    case SIMIX_FAILED:
+    case SIMIX_SRC_HOST_FAILURE:
+    case SIMIX_DST_HOST_FAILURE:
+      result = MSG_HOST_FAILURE;
+      break;
+
+    case SIMIX_LINK_FAILURE:
+      result = MSG_TRANSFER_FAILURE;
+      break;
+
+    case SIMIX_SRC_TIMEOUT:
+    case SIMIX_DST_TIMEOUT:
+      result = MSG_TIMEOUT;
+      break;
+
+    default:
+      result = MSG_OK;
+      break;
+  }
+  return result;
 }
 
 m_task_t MSG_comm_get_task(msg_comm_t comm)