Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Insane amount of debugs
[simgrid.git] / src / simix / smx_action.c
index 8be954c..b136398 100644 (file)
 
 #include "private.h"
 #include "xbt/log.h"
+#include "xbt/ex.h"
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_action, simix,
                                "Logging specific to SIMIX (action)");
 
 /************************************* Actions *********************************/
-
-smx_action_t SIMIX_communicate(smx_host_t sender,smx_host_t receiver, double size)
+/** \brief Creates a new SIMIX action to communicate two hosts.
+ *
+ *     This function creates a SURF action and allocates the data necessary to create the SIMIX action. It can raise a network_error exception if the host is unavailable. 
+ *     \param sender SIMIX host sender
+ *     \param receiver SIMIX host receiver
+ *     \param name Action name
+ *     \param size Communication size (in bytes)
+ *     \param rate Communication rate between hosts.
+ *     \return A new SIMIX action
+ * */
+smx_action_t SIMIX_action_communicate(smx_host_t sender,smx_host_t receiver,char * name, double size, double rate)
 {
-       return xbt_new0(s_smx_action_t,1);
+       /* check if the host is active */
+       if ( surf_workstation_resource->extension_public->get_state(sender->simdata->host)!=SURF_CPU_ON) {
+               THROW1(network_error,0,"Host %s failed, you cannot call this function",sender->name);
+       }
+       if ( surf_workstation_resource->extension_public->get_state(receiver->simdata->host)!=SURF_CPU_ON) {
+               THROW1(network_error,0,"Host %s failed, you cannot call this function",receiver->name);
+       }
+
+       /* alloc structures */
+       smx_action_t act = xbt_new0(s_smx_action_t,1);
+       act->simdata = xbt_new0(s_smx_simdata_action_t,1);
+       smx_simdata_action_t simdata = act->simdata;
+       act->cond_list = xbt_fifo_new();
+       
+       /* initialize them */
+       act->name = xbt_strdup(name);
+       simdata->source = sender;
+
+
+       simdata->surf_action = surf_workstation_resource->extension_public->
+               communicate(sender->simdata->host,
+                               receiver->simdata->host, size, rate);
+       surf_workstation_resource->common_public->action_set_data(simdata->surf_action,act);
+
+  DEBUG1("Create communicate action %p",act);
+       return act;
 }
 
-smx_action_t SIMIX_execute(smx_host_t host,double amount)
+/** \brief Creates a new SIMIX action to execute an action.
+ *
+ *     This function creates a SURF action and allocates the data necessary to create the SIMIX action. It can raise a host_error exception if the host crashed. 
+ *     \param host SIMIX host where the action will be executed
+ *     \param name Action name
+ *     \param amount Task amount (in bytes)
+ *     \return A new SIMIX action
+ * */
+smx_action_t SIMIX_action_execute(smx_host_t host, char * name, double amount)
 {
-       return xbt_new0(s_smx_action_t,1);
+       /* check if the host is active */
+       if ( surf_workstation_resource->extension_public->get_state(host->simdata->host)!=SURF_CPU_ON) {
+               THROW1(host_error,0,"Host %s failed, you cannot call this function",host->name);
+       }
+
+       /* alloc structures */
+       smx_action_t act = xbt_new0(s_smx_action_t,1);
+       act->simdata = xbt_new0(s_smx_simdata_action_t,1);
+       smx_simdata_action_t simdata = act->simdata;
+       act->cond_list = xbt_fifo_new();
+       
+       /* initialize them */
+       simdata->source = host;
+       act-> name = xbt_strdup(name);
+
+       /* set communication */
+       simdata->surf_action = surf_workstation_resource->extension_public->
+               execute(host->simdata->host, amount);
+
+       surf_workstation_resource->common_public->action_set_data(simdata->surf_action,act);
+
+  DEBUG1("Create execute action %p",act);
+       return act;
 }
 
-SIMIX_error_t SIMIX_action_cancel(smx_action_t action)
+/** \brief Creates a new sleep SIMIX action.
+ *
+ *     This function creates a SURF action and allocates the data necessary to create the SIMIX action. It can raise a host_error exception if the host crashed. The default SIMIX name of the action is "sleep".  
+ *     \param host SIMIX host where the sleep will run.
+ *     \param duration Time duration of the sleep.
+ *     \return A new SIMIX action
+ * */
+smx_action_t SIMIX_action_sleep(smx_host_t host,  double duration)
+{      
+       char name[] = "sleep";
+
+       /* check if the host is active */
+       if ( surf_workstation_resource->extension_public->get_state(host->simdata->host)!=SURF_CPU_ON) {
+               THROW1(host_error,0,"Host %s failed, you cannot call this function",host->name);
+       }
+
+       /* alloc structures */
+       smx_action_t act = xbt_new0(s_smx_action_t,1);
+       act->simdata = xbt_new0(s_smx_simdata_action_t,1);
+       smx_simdata_action_t simdata = act->simdata;
+       act->cond_list = xbt_fifo_new();
+       
+       /* initialize them */
+       simdata->source = host;
+       act->name = xbt_strdup(name);
+
+       simdata->surf_action = surf_workstation_resource->extension_public->
+               sleep(host->simdata->host, duration);
+
+       surf_workstation_resource->common_public->action_set_data(simdata->surf_action,act);
+
+  DEBUG1("Create sleep action %p",act);
+       return act;
+}
+
+/**
+ *     \brief Cancels an action.
+ *
+ *     This functions stops the execution of an action. It calls a surf functions.
+ *     \param action The SIMIX action
+ */
+void SIMIX_action_cancel(smx_action_t action)
 {
+  xbt_assert0((action != NULL), "Invalid parameter");
 
-       return SIMIX_OK;
+  DEBUG1("Cancel action %p",action);
+  if(action->simdata->surf_action) {
+    surf_workstation_resource->common_public->action_cancel(action->simdata->surf_action);
+  }
+  return;
 }
 
+/**
+ *     \brief Changes the action's priority
+ *
+ *     This functions changes the priority only. It calls a surf functions.
+ *     \param action The SIMIX action
+ *     \param priority The new priority
+ */
 void SIMIX_action_set_priority(smx_action_t action, double priority)
 {
+       xbt_assert0( (action != NULL) && (action->simdata != NULL), "Invalid parameter" );
+
+       surf_workstation_resource->common_public->
+               set_priority(action->simdata->surf_action, priority);
        return;
 }
 
-SIMIX_error_t SIMIX_action_destroy(smx_action_t action)
+/**
+ *     \brief Destroys an action
+ *
+ *     Destroys an action, freing its memory. This function cannot be called if there are a conditional waiting for it. 
+ *     \param action The SIMIX action
+ */
+void SIMIX_action_destroy(smx_action_t action)
 {
-       return SIMIX_OK;
+
+       xbt_assert0((action != NULL), "Invalid parameter");
+
+       xbt_assert1((xbt_fifo_size(action->cond_list)==0), 
+                       "Conditional list not empty %d. There is a problem. Cannot destroy it now!", xbt_fifo_size(action->cond_list));
+
+  DEBUG1("Destroy action %p",action);
+       if(action->name) xbt_free(action->name);
+
+       xbt_fifo_free(action->cond_list);
+
+       if(action->simdata->surf_action) 
+               action->simdata->surf_action->resource_type->common_public->action_free(action->simdata->surf_action);
+
+       xbt_free(action->simdata);
+       xbt_free(action);
+       return;
 }
 
-void SIMIX_create_link(smx_action_t action, smx_cond_t cond)
+/**
+ *     \brief Set an action to a condition
+ *
+ *     Creates the "link" between an action and a condition. You have to call this function when you create an action and want to wait its ending. 
+ *     \param action SIMIX action
+ *     \param cond SIMIX cond
+ */
+void SIMIX_register_action_to_condition(smx_action_t action, smx_cond_t cond)
 {
+       xbt_assert0( (action != NULL) && (cond != NULL), "Invalid parameters");
+
+  DEBUG2("Register action %p to condtion %p",action,cond);
+       xbt_fifo_push(cond->actions,action);
+}
 
+/**
+ *     \brief Return how much remais to be done in the action.
+ *
+ *     \param action The SIMIX action
+ *     \return Remains cost
+ */
+double SIMIX_action_get_remains(smx_action_t action) 
+{
+  xbt_assert0((action != NULL), "Invalid parameter");
+       return action->simdata->surf_action->remains;
 }
 
-SIMIX_error_t __SIMIX_wait_for_action(smx_process_t process, smx_action_t action)
+smx_action_t SIMIX_action_parallel_execute(char *name, int workstation_nb, void **workstation_list, double *computation_amount, double *communication_amount, double amount, double rate)
 {
-       e_surf_action_state_t state = SURF_ACTION_NOT_IN_THE_SYSTEM;
-       simdata_action_t simdata = action->simdata;
 
-       xbt_assert0(((process != NULL) && (action != NULL) && (action->simdata != NULL)), "Invalid parameters");
-       
-       /* change context while the action is running  */
-       do {
-               xbt_context_yield();
-               state=surf_workstation_resource->common_public->action_get_state(simdata->surf_action);
-       } while (state==SURF_ACTION_RUNNING);
+       /* alloc structures */
+       smx_action_t act = xbt_new0(s_smx_action_t,1);
+       act->simdata = xbt_new0(s_smx_simdata_action_t,1);
+       smx_simdata_action_t simdata = act->simdata;
+       act->cond_list = xbt_fifo_new();
        
-       /* action finished, we can continue */
-
-       if(state == SURF_ACTION_DONE) {
-               if(surf_workstation_resource->common_public->action_free(simdata->surf_action)) 
-                       simdata->surf_action = NULL;
-               SIMIX_RETURN(SIMIX_OK);
-       } else if(surf_workstation_resource->extension_public->
-                       get_state(SIMIX_process_get_host(process)->simdata->host) 
-                       == SURF_CPU_OFF) {
-               if(surf_workstation_resource->common_public->action_free(simdata->surf_action)) 
-                       simdata->surf_action = NULL;
-               SIMIX_RETURN(SIMIX_HOST_FAILURE);
-       } else {
-               if(surf_workstation_resource->common_public->action_free(simdata->surf_action)) 
-                       simdata->surf_action = NULL;
-               SIMIX_RETURN(SIMIX_ACTION_CANCELLED);
-       }
+       /* initialize them */
+       act-> name = xbt_strdup(name);
+
+       /* set communication */
+  simdata->surf_action = surf_workstation_resource->extension_public->execute_parallel_task(workstation_nb, workstation_list, computation_amount, communication_amount, amount, rate);
+
+       surf_workstation_resource->common_public->action_set_data(simdata->surf_action,act);
+
+       return act;
+}
+
+e_surf_action_state_t SIMIX_action_get_state(smx_action_t action)
+{
+  xbt_assert0((action != NULL), "Invalid parameter");
+       return surf_workstation_resource->common_public->action_get_state(action->simdata->surf_action);
 
 }