Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Port parmap_test to C++.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Thu, 3 Aug 2017 11:56:50 +0000 (13:56 +0200)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Thu, 3 Aug 2017 11:56:50 +0000 (13:56 +0200)
teshsuite/xbt/CMakeLists.txt
teshsuite/xbt/parmap_test/parmap_test.cpp [moved from teshsuite/xbt/parmap_test/parmap_test.c with 51% similarity]

index da0f1a1..42e8902 100644 (file)
@@ -1,4 +1,4 @@
-foreach(x heap_bench log_large log_usage mallocator parallel_log_crashtest parmap_bench parmap_test)
+foreach(x heap_bench log_large log_usage mallocator parallel_log_crashtest parmap_bench)
   add_executable       (${x}  ${x}/${x}.c)
   target_link_libraries(${x}  simgrid)
   set_target_properties(${x}  PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${x})
@@ -7,6 +7,15 @@ foreach(x heap_bench log_large log_usage mallocator parallel_log_crashtest parma
   set(teshsuite_src ${teshsuite_src} ${CMAKE_CURRENT_SOURCE_DIR}/${x}/${x}.c)
 endforeach()
 
+foreach(x parmap_test)
+  add_executable       (${x}  ${x}/${x}.cpp)
+  target_link_libraries(${x}  simgrid)
+  set_target_properties(${x}  PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${x})
+
+  set(tesh_files    ${tesh_files}    ${CMAKE_CURRENT_SOURCE_DIR}/${x}/${x}.tesh)
+  set(teshsuite_src ${teshsuite_src} ${CMAKE_CURRENT_SOURCE_DIR}/${x}/${x}.cpp)
+endforeach()
+
 if(HAVE_MMALLOC)
   add_executable       (mmalloc_test ${CMAKE_CURRENT_SOURCE_DIR}/mmalloc/mmalloc_test.cpp)
   target_link_libraries(mmalloc_test simgrid)
similarity index 51%
rename from teshsuite/xbt/parmap_test/parmap_test.c
rename to teshsuite/xbt/parmap_test/parmap_test.cpp
index b520b5d..cfb1684 100644 (file)
@@ -5,45 +5,40 @@
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
-#include "simgrid/msg.h"
-#include "src/internal_config.h"
-#include "xbt.h"
-#include "xbt/ex.h"
-#include "xbt/xbt_os_time.h"
+#include "src/internal_config.h" // HAVE_FUTEX_H
+#include <simgrid/msg.h>
+#include <xbt.h>
+#include <xbt/parmap.hpp>
+
+#include <algorithm>
+#include <cstdlib>
+#include <numeric> // std::iota
+#include <vector>
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(parmap_test, "Test for parmap");
 
-static void fun_double(void *arg)
+static void fun_double(unsigned* arg)
 {
-  unsigned *u = arg;
-  *u = 2 * *u + 1;
+  *arg = 2 * *arg + 1;
 }
 
 static int test_parmap_basic(e_xbt_parmap_mode_t mode)
 {
   int ret = 0;
-  unsigned num_workers;
-  for (num_workers = 1 ; num_workers <= 16 ; num_workers *= 2) {
+  for (unsigned num_workers = 1; num_workers <= 16; num_workers *= 2) {
     const unsigned len = 1033;
     const unsigned num = 5;
-    unsigned *a;
-    xbt_dynar_t data;
-    xbt_parmap_t parmap;
-    unsigned i;
-
-    parmap = xbt_parmap_new(num_workers, mode);
-
-    a = xbt_malloc(len * sizeof *a);
-    data = xbt_dynar_new(sizeof a, NULL);
-    for (i = 0; i < len; i++) {
-      a[i] = i;
-      xbt_dynar_push_as(data, void *, &a[i]);
-    }
 
-    for (i = 0; i < num; i++)
-      xbt_parmap_apply(parmap, fun_double, data);
+    simgrid::xbt::Parmap<unsigned*> parmap(num_workers, mode);
+    std::vector<unsigned> a(len);
+    std::vector<unsigned*> data(len);
+    std::iota(begin(a), end(a), 0);
+    std::iota(begin(data), end(data), &a[0]);
+
+    for (unsigned i = 0; i < num; i++)
+      parmap.apply(fun_double, data);
 
-    for (i = 0; i < len; i++) {
+    for (unsigned i = 0; i < len; i++) {
       unsigned expected = (1U << num) * (i + 1) - 1;
       if (a[i] != expected) {
         XBT_CRITICAL("with %u threads, a[%u]: expected %u, got %u", num_workers, i, expected, a[i]);
@@ -51,62 +46,36 @@ static int test_parmap_basic(e_xbt_parmap_mode_t mode)
         break;
       }
     }
-
-    xbt_dynar_free(&data);
-    xbt_free(a);
-    xbt_parmap_destroy(parmap);
   }
   return ret;
 }
 
-static void fun_get_id(void *arg)
+static void fun_get_id(uintptr_t* arg)
 {
-  *(uintptr_t *)arg = (uintptr_t)xbt_os_thread_self();
+  *arg = (uintptr_t)xbt_os_thread_self();
   xbt_os_sleep(0.05);
 }
 
-static int fun_compare(const void *pa, const void *pb)
-{
-  uintptr_t a = *(uintptr_t *)pa;
-  uintptr_t b = *(uintptr_t *)pb;
-  return a < b ? -1 : a > b ? 1 : 0;
-}
-
 static int test_parmap_extended(e_xbt_parmap_mode_t mode)
 {
   int ret = 0;
-  unsigned num_workers;
 
-  for (num_workers = 1 ; num_workers <= 16 ; num_workers *= 2) {
+  for (unsigned num_workers = 1; num_workers <= 16; num_workers *= 2) {
     const unsigned len = 2 * num_workers;
-    uintptr_t *a;
-    xbt_parmap_t parmap;
-    xbt_dynar_t data;
-    unsigned i;
-    unsigned count;
-
-    parmap = xbt_parmap_new(num_workers, mode);
-
-    a = xbt_malloc(len * sizeof *a);
-    data = xbt_dynar_new(sizeof a, NULL);
-    for (i = 0; i < len; i++)
-      xbt_dynar_push_as(data, void *, &a[i]);
-
-    xbt_parmap_apply(parmap, fun_get_id, data);
-
-    qsort(a, len, sizeof a[0], fun_compare);
-    count = 1;
-    for (i = 1; i < len; i++)
-      if (a[i] != a[i - 1])
-        count++;
+
+    simgrid::xbt::Parmap<uintptr_t*> parmap(num_workers, mode);
+    std::vector<uintptr_t> a(len);
+    std::vector<uintptr_t*> data(len);
+    std::iota(begin(data), end(data), &a[0]);
+
+    parmap.apply(fun_get_id, data);
+
+    std::sort(begin(a), end(a));
+    unsigned count = std::distance(begin(a), std::unique(begin(a), end(a)));
     if (count != num_workers) {
       XBT_CRITICAL("only %u/%u threads did some work", count, num_workers);
       ret = 1;
     }
-
-    xbt_dynar_free(&data);
-    xbt_free(a);
-    xbt_parmap_destroy(parmap);
   }
   return ret;
 }