Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement SD_task_schedule, SD_task_unschedule and other SimDag stuff.
[simgrid.git] / src / gras / DataDesc / ddt_create.c
index 2ab1378..f8f7fc0 100644 (file)
@@ -13,7 +13,7 @@
 #include "xbt/ex.h"
 #include "gras/DataDesc/datadesc_private.h"
 
-XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ddt_create,datadesc,"Creating new datadescriptions");
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_ddt_create,gras_ddt,"Creating new datadescriptions");
 
 /*** prototypes ***/
 static gras_dd_cat_field_t
@@ -53,22 +53,33 @@ static gras_datadesc_type_t gras_ddt_new(const char *name) {
  */
 gras_datadesc_type_t gras_datadesc_by_name(const char *name) {
   xbt_ex_t e;
-  gras_datadesc_type_t res;
+  gras_datadesc_type_t res = NULL;
   TRY {
     res = (gras_datadesc_type_t)xbt_set_get_by_name(gras_datadesc_set_local,name);
   } CATCH(e) {
-    if (e.category != mismatch_error)
+    if (e.category != not_found_error)
       RETHROW;
+    xbt_ex_free(e);
     res = NULL;
   }
   return res;
 }
 
 /**
- * Retrieve a type from its code
+ * Retrieve a type from its code (or NULL if not found)
  */
 gras_datadesc_type_t gras_datadesc_by_id(long int code) {
-  return (gras_datadesc_type_t)xbt_set_get_by_id(gras_datadesc_set_local,code);
+  xbt_ex_t e;
+  gras_datadesc_type_t res=NULL;
+  TRY {
+    res = (gras_datadesc_type_t)xbt_set_get_by_id(gras_datadesc_set_local,code);
+  } CATCH(e) {
+    if (e.category != not_found_error)
+      RETHROW;
+    xbt_ex_free(e);
+    res = NULL;
+  }
+  return res;
 }
 
 /**
@@ -561,6 +572,12 @@ gras_datadesc_type_t
   return res;
 }
 
+/*
+ *##
+ *## Constructor of container datatypes
+ *##
+ */
+
 #include "xbt/dynar_private.h"
 static void gras_datadesc_dynar_cb(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data) {
   gras_datadesc_type_t subtype;
@@ -581,6 +598,11 @@ static void gras_datadesc_dynar_cb(gras_datadesc_type_t typedesc, gras_cbps_t va
   dynar->size = dynar->used;
 }
 
+/** \brief Declare a new type being a dynar in which each elements are of the given type
+ * 
+ *  \param elm_t: the datadesc of the elements
+ *  \param free_func: the function to use to free the elements when the dynar gets freed
+ */
 gras_datadesc_type_t
 gras_datadesc_dynar(gras_datadesc_type_t elm_t,
                    void_f_pvoid_t *free_func) {
@@ -588,39 +610,98 @@ gras_datadesc_dynar(gras_datadesc_type_t elm_t,
   char *buffname;
   gras_datadesc_type_t res;
   
-  buffname=xbt_new0(char, strlen(elm_t->name)+10);
-  sprintf(buffname,"dynar(%s)_s",elm_t->name);
+  asprintf(&buffname,"dynar(%s)_s",elm_t->name);
    
   res = gras_datadesc_struct(buffname);
   
-  gras_datadesc_struct_append(res, "size",    gras_datadesc_by_name("unsigned long int"));
+  gras_datadesc_struct_append(res, "size",    
+                             gras_datadesc_by_name("unsigned long int"));
    
-  gras_datadesc_struct_append(res, "used",    gras_datadesc_by_name("unsigned long int"));
-  gras_datadesc_cb_field_push(res, "used");
+  gras_datadesc_struct_append(res, "used",    
+                             gras_datadesc_by_name("unsigned long int"));
    
-  gras_datadesc_struct_append(res, "elmsize", gras_datadesc_by_name("unsigned long int"));
+  gras_datadesc_struct_append(res, "elmsize", 
+                             gras_datadesc_by_name("unsigned long int"));
    
-  gras_datadesc_struct_append(res, "data",    gras_datadesc_ref_pop_arr (elm_t));
+  gras_datadesc_struct_append(res, "data",    
+                             gras_datadesc_ref_pop_arr (elm_t));
 
-  gras_datadesc_struct_append(res, "free_f",  gras_datadesc_by_name("function pointer"));
+  gras_datadesc_struct_append(res, "free_f",  
+                             gras_datadesc_by_name("function pointer"));
   memcpy(res->extra,&free_func,sizeof(free_func));
       
   gras_datadesc_struct_close(res);
    
+  gras_datadesc_cb_field_push(res, "used");
   gras_datadesc_cb_recv(res,  &gras_datadesc_dynar_cb);
 
   /* build a ref to it */
-  sprintf(buffname,"dynar(%s)",elm_t->name);
+  free(buffname);
+  asprintf(&buffname,"dynar(%s)",elm_t->name);
   res=gras_datadesc_ref(buffname,res);
   free(buffname);
   return res;
 }
 
-xbt_error_t
+#include "xbt/matrix.h"
+static void gras_datadesc_matrix_cb(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data) {
+  gras_datadesc_type_t subtype;
+  xbt_matrix_t matrix=(xbt_matrix_t)data;
+   
+  memcpy(&matrix->free_f, &typedesc->extra, sizeof(matrix->free_f));
+
+  /* search for the elemsize in what we have. If elements are "int", typedesc got is "int[]*" */
+  subtype = gras_dd_find_field(typedesc,"data")->type;
+  
+  /* this is now a ref to array of what we're looking for */
+  subtype = subtype->category.ref_data.type;
+  subtype = subtype->category.array_data.type;
+   
+  DEBUG1("subtype is %s",subtype->name);
+
+  matrix->elmsize = subtype->size[GRAS_THISARCH];
+}
+gras_datadesc_type_t
+gras_datadesc_matrix(gras_datadesc_type_t elm_t,
+                    void_f_pvoid_t * const free_f) {
+  char *buffname;
+  gras_datadesc_type_t res;
+
+  asprintf(&buffname,"s_xbt_matrix_t(%s)",elm_t->name);   
+  res = gras_datadesc_struct(buffname);
+  
+  gras_datadesc_struct_append(res, "lines",    
+                             gras_datadesc_by_name("unsigned int"));
+  gras_datadesc_struct_append(res, "rows",    
+                             gras_datadesc_by_name("unsigned int"));
+   
+  gras_datadesc_struct_append(res, "elmsize", 
+                             gras_datadesc_by_name("unsigned long int"));
+   
+  gras_datadesc_struct_append(res, "data",    
+                             gras_datadesc_ref_pop_arr (elm_t));
+  gras_datadesc_struct_append(res, "free_f",  
+                             gras_datadesc_by_name("function pointer"));      
+  gras_datadesc_struct_close(res);
+    
+  gras_datadesc_cb_field_push(res, "lines");
+  gras_datadesc_cb_field_push_multiplier(res, "rows");
+
+  gras_datadesc_cb_recv(res,  &gras_datadesc_dynar_cb);
+  memcpy(res->extra,&free_f,sizeof(free_f));
+
+  /* build a ref to it */
+  free(buffname);
+  asprintf(&buffname,"xbt_matrix_t(%s)",elm_t->name);
+  res=gras_datadesc_ref(buffname,res);
+  free(buffname);
+  return res;
+}
+
+gras_datadesc_type_t
 gras_datadesc_import_nws(const char           *name,
                         const DataDescriptor *desc,
-                        unsigned long         howmany,
-              /* OUT */ gras_datadesc_type_t *dst) {
+                        unsigned long         howmany) {
   THROW_UNIMPLEMENTED;
 }
 
@@ -681,6 +762,7 @@ void gras_datadesc_cb_field_send (gras_datadesc_type_t          type,
    field->send = send;
 }
 
+
 /**
  * The value, which must be an int, unsigned int, long int or unsigned long int
  * is pushed to the stacks of sizes and can then be retrieved with 
@@ -708,6 +790,38 @@ void gras_datadesc_cb_field_push (gras_datadesc_type_t  type,
       xbt_abort();
    }
 }
+
+/**
+ * Any previously pushed value is poped and the field value is multiplied to
+ * it. The result is then pushed back into the stack of sizes. It can then be
+ * retrieved with \ref gras_datadesc_ref_pop_arr or directly with \ref
+ * gras_cbps_i_pop.
+ *
+ * The field must be an int, unsigned int, long int or unsigned long int.
+ */
+void gras_datadesc_cb_field_push_multiplier (gras_datadesc_type_t  type,
+                                            const char         *field_name) {
+   
+   gras_dd_cat_field_t  field=gras_dd_find_field(type,field_name);
+   gras_datadesc_type_t sub_type=field->type;
+   
+   DEBUG3("add a MPUSHy cb to '%s' field (type '%s') of '%s'",
+         field_name,sub_type->name,type->name);
+   if (!strcmp("int",sub_type->name)) {
+      field->send = gras_datadesc_cb_push_int_mult;
+   } else if (!strcmp("unsigned int",sub_type->name)) {
+      field->send = gras_datadesc_cb_push_uint_mult;
+   } else if (!strcmp("long int",sub_type->name)) {
+      field->send = gras_datadesc_cb_push_lint_mult;
+   } else if (!strcmp("unsigned long int",sub_type->name)) {
+      field->send = gras_datadesc_cb_push_ulint_mult;
+   } else {
+      ERROR1("Field %s is not an int, unsigned int, long int neither unsigned long int",
+            sub_type->name);
+      xbt_abort();
+   }
+}
+
 /**
  * The given datadesc must be a struct or union (abort if not).
  * (useful to put the function pointers to the right value, for example)