Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Very basic memoizer.
authorPierre-Nicolas Clauss <pini@ethernium.org>
Tue, 17 May 2011 19:10:51 +0000 (21:10 +0200)
committerPierre-Nicolas Clauss <pini@ethernium.org>
Tue, 17 May 2011 19:10:51 +0000 (21:10 +0200)
include/smpi/smpi.h
src/smpi/smpi_bench.c

index 68c9cf9..ce54b09 100644 (file)
@@ -444,6 +444,13 @@ XBT_PUBLIC(void *) smpi_shared_malloc(size_t size, const char *file,
 XBT_PUBLIC(void) smpi_shared_free(void *data);
 #define SMPI_SHARED_FREE(data) smpi_shared_free(data)
 
 XBT_PUBLIC(void) smpi_shared_free(void *data);
 #define SMPI_SHARED_FREE(data) smpi_shared_free(data)
 
+XBT_PUBLIC(int) smpi_shared_known_call(const char* func, const char* input);
+XBT_PUBLIC(void*) smpi_shared_get_call(const char* func, const char* input);
+XBT_PUBLIC(void*) smpi_shared_set_call(const char* func, const char* input, void* data);
+#define SMPI_SHARED_CALL(func, input, ...) \
+   (smpi_shared_known_call(#func, input) ? smpi_shared_get_call(#func, input) \
+                                         : smpi_shared_set_call(#func, input, func(__VA_ARGS__)))
+
 /* Fortran specific stuff */
 XBT_PUBLIC(int) MAIN__(void);
 
 /* Fortran specific stuff */
 XBT_PUBLIC(int) MAIN__(void);
 
index 8c81580..e531e85 100644 (file)
@@ -7,6 +7,7 @@
 #include "private.h"
 #include "xbt/dict.h"
 #include "xbt/sysdep.h"
 #include "private.h"
 #include "xbt/dict.h"
 #include "xbt/sysdep.h"
+#include "xbt/ex.h"
 #include "surf/surf.h"
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_bench, smpi,
 #include "surf/surf.h"
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_bench, smpi,
@@ -14,6 +15,7 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_bench, smpi,
 
 xbt_dict_t allocs = NULL;       /* Allocated on first use */
 xbt_dict_t samples = NULL;      /* Allocated on first use */
 
 xbt_dict_t allocs = NULL;       /* Allocated on first use */
 xbt_dict_t samples = NULL;      /* Allocated on first use */
+xbt_dict_t calls = NULL;        /* Allocated on first use */
 
 typedef struct {
   int count;
 
 typedef struct {
   int count;
@@ -39,6 +41,9 @@ void smpi_bench_destroy(void)
   if (samples) {
     xbt_dict_free(&samples);
   }
   if (samples) {
     xbt_dict_free(&samples);
   }
+  if(calls) {
+    xbt_dict_free(&calls);
+  }
 }
 
 static void smpi_execute_flops(double flops)
 }
 
 static void smpi_execute_flops(double flops)
@@ -223,3 +228,49 @@ void smpi_shared_free(void *ptr)
     xbt_dict_remove(allocs, loc);
   }
 }
     xbt_dict_remove(allocs, loc);
   }
 }
+
+int smpi_shared_known_call(const char* func, const char* input) {
+   char* loc = bprintf("%s:%s", func, input);
+   xbt_ex_t ex;
+   int known;
+
+   if(!calls) {
+      calls = xbt_dict_new();
+   }
+   TRY {
+      xbt_dict_get(calls, loc); /* Succeed or throw */
+      known = 1;
+   } CATCH(ex) {
+      if(ex.category == not_found_error) {
+         known = 0;
+         xbt_ex_free(ex);
+      } else {
+         RETHROW;
+      }
+   }
+   free(loc);
+   return known;
+}
+
+void* smpi_shared_get_call(const char* func, const char* input) {
+   char* loc = bprintf("%s:%s", func, input);
+   void* data;
+
+   if(!calls) {
+      calls = xbt_dict_new();
+   }
+   data = xbt_dict_get(calls, loc);
+   free(loc);
+   return data;
+}
+
+void* smpi_shared_set_call(const char* func, const char* input, void* data) {
+   char* loc = bprintf("%s:%s", func, input);
+
+   if(!calls) {
+      calls = xbt_dict_new();
+   }
+   xbt_dict_set(calls, loc, data, NULL);
+   free(loc);
+   return data;
+}