Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Restore parmap unitTesting as part of teshsuite.
authorGuillaume Serrière <guillaume.serriere@esial.net>
Fri, 17 May 2013 14:29:17 +0000 (16:29 +0200)
committerGuillaume Serrière <guillaume.serriere@esial.net>
Fri, 17 May 2013 14:30:17 +0000 (16:30 +0200)
Signed-off-by: Guillaume Serrière <guillaume.serriere@esial.net>
buildtools/Cmake/AddTests.cmake
teshsuite/xbt/CMakeLists.txt
teshsuite/xbt/parmap_test.c [new file with mode: 0644]
teshsuite/xbt/parmap_test.tesh [new file with mode: 0644]

index af482aa..ce6acf7 100644 (file)
@@ -58,6 +58,7 @@ if(NOT enable_memcheck)
       ADD_TEST(xbt-mmalloc-64                    ${CMAKE_BINARY_DIR}/bin/tesh ${TESH_OPTION} --cd ${CMAKE_BINARY_DIR}/teshsuite ${CMAKE_HOME_DIRECTORY}/teshsuite/xbt/mmalloc_64.tesh)
     ENDIF()
   ENDIF()
+  ADD_TEST(xbt-parmap                           ${CMAKE_BINARY_DIR}/bin/tesh ${TESH_OPTION} --cd ${CMAKE_BINARY_DIR}/teshsuite ${CMAKE_HOME_DIRECTORY}/teshsuite/xbt/parmap_test.tesh)
 
   # BEGIN CONTEXTS FACTORY
   ADD_TEST(tesh-msg-get-sender-thread           ${CMAKE_BINARY_DIR}/bin/tesh ${TESH_OPTION} --cfg contexts/factory:thread --setenv srcdir=${CMAKE_HOME_DIRECTORY}/teshsuite --cd ${CMAKE_BINARY_DIR}/teshsuite ${CMAKE_HOME_DIRECTORY}/teshsuite/msg/get_sender.tesh)
index 8a8b7b7..0a4b766 100644 (file)
@@ -7,6 +7,7 @@ if(NOT WIN32)
   if(HAVE_MMAP)
     add_executable(mmalloc_test mmalloc_test.c)
   endif()
+  add_executable(parmap_test parmap_test.c)
 endif()
 
 ### Add definitions for compile
@@ -16,6 +17,7 @@ if(NOT WIN32)
   if(HAVE_MMAP)
     target_link_libraries(mmalloc_test simgrid m pthread )
   endif()
+  target_link_libraries(parmap_test simgrid m pthread )
 endif()
 
 set(tesh_files
@@ -25,6 +27,7 @@ set(tesh_files
   ${CMAKE_CURRENT_SOURCE_DIR}/xbt.tesh
   ${CMAKE_CURRENT_SOURCE_DIR}/mmalloc_64.tesh
   ${CMAKE_CURRENT_SOURCE_DIR}/mmalloc_32.tesh
+  ${CMAKE_CURRENT_SOURCE_DIR}/parmap_test.tesh
   PARENT_SCOPE
   )
 set(xml_files
@@ -36,6 +39,7 @@ set(teshsuite_src
   ${CMAKE_CURRENT_SOURCE_DIR}/log_large_test.c
   ${CMAKE_CURRENT_SOURCE_DIR}/parallel_log_crashtest.c
   ${CMAKE_CURRENT_SOURCE_DIR}/mmalloc_test.c
+  ${CMAKE_CURRENT_SOURCE_DIR}/parmap_test.c
   PARENT_SCOPE
   )
 set(bin_files
diff --git a/teshsuite/xbt/parmap_test.c b/teshsuite/xbt/parmap_test.c
new file mode 100644 (file)
index 0000000..6ec875e
--- /dev/null
@@ -0,0 +1,129 @@
+/* parmap_test -- test parmap                                               */
+
+/* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
+ * All rights reserved.                                                     */
+
+/* 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/simix.h"
+#include "xbt.h"
+#include "xbt/ex.h"
+#include "xbt/xbt_os_time.h"
+#include "internal_config.h"
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(parmap_test, "Test for parmap");
+
+static void fun_double(void *arg)
+{
+  unsigned *u = arg;
+  *u = 2 * *u + 1;
+}
+
+static void test_parmap_basic(e_xbt_parmap_mode_t mode)
+{
+  unsigned num_workers;
+  for (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);
+
+    for (i = 0; i < len; i++) {
+      unsigned expected = (1U << num) * (i + 1) - 1;
+      xbt_test_assert(a[i] == expected,
+                      "a[%u]: expected %u, got %u", i, expected, a[i]);
+    }
+
+    xbt_dynar_free(&data);
+    xbt_free(a);
+    xbt_parmap_destroy(parmap);
+  }
+}
+
+static void fun_get_id(void *arg)
+{
+  *(uintptr_t *)arg = (uintptr_t)xbt_os_thread_self();
+  xbt_os_sleep(0.5);
+}
+
+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 void test_parmap_extended(e_xbt_parmap_mode_t mode)
+{
+  unsigned num_workers;
+
+  for (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++;
+    xbt_test_assert(count == num_workers,
+                    "only %u/%u threads did some work", count, num_workers);
+
+    xbt_dynar_free(&data);
+    xbt_free(a);
+    xbt_parmap_destroy(parmap);
+  }
+}
+
+int main(int argc, char** argv)
+{
+  SIMIX_global_init(&argc, argv);
+
+  XBT_INFO("Basic testing posix");
+  test_parmap_basic(XBT_PARMAP_POSIX);
+  XBT_INFO("Basic testing futex");
+#ifdef HAVE_FUTEX_H
+  test_parmap_basic(XBT_PARMAP_FUTEX);
+#endif
+  XBT_INFO("Basic testing busy wait");
+  test_parmap_basic(XBT_PARMAP_BUSY_WAIT);
+
+  XBT_INFO("Extended testing posix");
+  test_parmap_extended(XBT_PARMAP_POSIX);
+  XBT_INFO("Extended testing futex");
+#ifdef HAVE_FUTEX_H
+  test_parmap_extended(XBT_PARMAP_FUTEX);
+#endif
+  XBT_INFO("Extended testing busy wait");
+  test_parmap_extended(XBT_PARMAP_BUSY_WAIT);
+
+  return 0;
+}
diff --git a/teshsuite/xbt/parmap_test.tesh b/teshsuite/xbt/parmap_test.tesh
new file mode 100644 (file)
index 0000000..2b85ff6
--- /dev/null
@@ -0,0 +1,8 @@
+! timeout 120
+$ ./xbt/parmap_test --log=root.fmt:%m%n
+> Basic testing posix
+> Basic testing futex
+> Basic testing busy wait
+> Extended testing posix
+> Extended testing futex
+> Extended testing busy wait