1 /* parmap_test -- test parmap */
3 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "simgrid/simix.h"
12 #include "xbt/xbt_os_time.h"
13 #include "internal_config.h"
15 XBT_LOG_NEW_DEFAULT_CATEGORY(parmap_test, "Test for parmap");
17 static void fun_double(void *arg)
23 static void test_parmap_basic(e_xbt_parmap_mode_t mode)
26 for (num_workers = 1 ; num_workers <= 16 ; num_workers *= 2) {
27 const unsigned len = 1033;
28 const unsigned num = 5;
34 parmap = xbt_parmap_new(num_workers, mode);
36 a = xbt_malloc(len * sizeof *a);
37 data = xbt_dynar_new(sizeof a, NULL);
38 for (i = 0; i < len; i++) {
40 xbt_dynar_push_as(data, void *, &a[i]);
43 for (i = 0; i < num; i++)
44 xbt_parmap_apply(parmap, fun_double, data);
46 for (i = 0; i < len; i++) {
47 unsigned expected = (1U << num) * (i + 1) - 1;
48 xbt_test_assert(a[i] == expected,
49 "a[%u]: expected %u, got %u", i, expected, a[i]);
52 xbt_dynar_free(&data);
54 xbt_parmap_destroy(parmap);
58 static void fun_get_id(void *arg)
60 *(uintptr_t *)arg = (uintptr_t)xbt_os_thread_self();
64 static int fun_compare(const void *pa, const void *pb)
66 uintptr_t a = *(uintptr_t *)pa;
67 uintptr_t b = *(uintptr_t *)pb;
68 return a < b ? -1 : a > b ? 1 : 0;
71 static void test_parmap_extended(e_xbt_parmap_mode_t mode)
75 for (num_workers = 1 ; num_workers <= 16 ; num_workers *= 2) {
76 const unsigned len = 2 * num_workers;
83 parmap = xbt_parmap_new(num_workers, mode);
85 a = xbt_malloc(len * sizeof *a);
86 data = xbt_dynar_new(sizeof a, NULL);
87 for (i = 0; i < len; i++)
88 xbt_dynar_push_as(data, void *, &a[i]);
90 xbt_parmap_apply(parmap, fun_get_id, data);
92 qsort(a, len, sizeof a[0], fun_compare);
94 for (i = 1; i < len; i++)
97 xbt_test_assert(count == num_workers,
98 "only %u/%u threads did some work", count, num_workers);
100 xbt_dynar_free(&data);
102 xbt_parmap_destroy(parmap);
106 int main(int argc, char** argv)
108 SIMIX_global_init(&argc, argv);
110 XBT_INFO("Basic testing posix");
111 test_parmap_basic(XBT_PARMAP_POSIX);
112 XBT_INFO("Basic testing futex");
114 test_parmap_basic(XBT_PARMAP_FUTEX);
116 XBT_INFO("Basic testing busy wait");
117 test_parmap_basic(XBT_PARMAP_BUSY_WAIT);
119 XBT_INFO("Extended testing posix");
120 test_parmap_extended(XBT_PARMAP_POSIX);
121 XBT_INFO("Extended testing futex");
123 test_parmap_extended(XBT_PARMAP_FUTEX);
125 XBT_INFO("Extended testing busy wait");
126 test_parmap_extended(XBT_PARMAP_BUSY_WAIT);