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 int test_parmap_basic(e_xbt_parmap_mode_t mode)
27 for (num_workers = 1 ; num_workers <= 16 ; num_workers *= 2) {
28 const unsigned len = 1033;
29 const unsigned num = 5;
35 parmap = xbt_parmap_new(num_workers, mode);
37 a = xbt_malloc(len * sizeof *a);
38 data = xbt_dynar_new(sizeof a, NULL);
39 for (i = 0; i < len; i++) {
41 xbt_dynar_push_as(data, void *, &a[i]);
44 for (i = 0; i < num; i++)
45 xbt_parmap_apply(parmap, fun_double, data);
47 for (i = 0; i < len; i++) {
48 unsigned expected = (1U << num) * (i + 1) - 1;
49 if (a[i] != expected) {
50 XBT_CRITICAL("with %u threads, a[%u]: expected %u, got %u",
51 num_workers, i, expected, a[i]);
57 xbt_dynar_free(&data);
59 xbt_parmap_destroy(parmap);
64 static void fun_get_id(void *arg)
66 *(uintptr_t *)arg = (uintptr_t)xbt_os_thread_self();
70 static int fun_compare(const void *pa, const void *pb)
72 uintptr_t a = *(uintptr_t *)pa;
73 uintptr_t b = *(uintptr_t *)pb;
74 return a < b ? -1 : a > b ? 1 : 0;
77 static int test_parmap_extended(e_xbt_parmap_mode_t mode)
82 for (num_workers = 1 ; num_workers <= 16 ; num_workers *= 2) {
83 const unsigned len = 2 * num_workers;
90 parmap = xbt_parmap_new(num_workers, mode);
92 a = xbt_malloc(len * sizeof *a);
93 data = xbt_dynar_new(sizeof a, NULL);
94 for (i = 0; i < len; i++)
95 xbt_dynar_push_as(data, void *, &a[i]);
97 xbt_parmap_apply(parmap, fun_get_id, data);
99 qsort(a, len, sizeof a[0], fun_compare);
101 for (i = 1; i < len; i++)
102 if (a[i] != a[i - 1])
104 if (count != num_workers) {
105 XBT_CRITICAL("only %u/%u threads did some work", count, num_workers);
109 xbt_dynar_free(&data);
111 xbt_parmap_destroy(parmap);
116 int main(int argc, char** argv)
119 SIMIX_global_init(&argc, argv);
121 XBT_INFO("Basic testing posix");
122 status += test_parmap_basic(XBT_PARMAP_POSIX);
123 XBT_INFO("Basic testing futex");
125 status += test_parmap_basic(XBT_PARMAP_FUTEX);
127 XBT_INFO("Basic testing busy wait");
128 status += test_parmap_basic(XBT_PARMAP_BUSY_WAIT);
130 XBT_INFO("Extended testing posix");
131 status += test_parmap_extended(XBT_PARMAP_POSIX);
132 XBT_INFO("Extended testing futex");
134 status += test_parmap_extended(XBT_PARMAP_FUTEX);
136 XBT_INFO("Extended testing busy wait");
137 status += test_parmap_extended(XBT_PARMAP_BUSY_WAIT);
139 return status == 0 ? EXIT_SUCCESS : EXIT_FAILURE;