1 /* parmap_test -- test parmap */
3 /* Copyright (c) 2007-2017. The SimGrid Team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "simgrid/msg.h"
9 #include "src/internal_config.h"
12 #include "xbt/xbt_os_time.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(parmap_test, "Test for parmap");
16 static void fun_double(void *arg)
22 static int 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 if (a[i] != expected) {
49 XBT_CRITICAL("with %u threads, a[%u]: expected %u, got %u", num_workers, i, expected, a[i]);
55 xbt_dynar_free(&data);
57 xbt_parmap_destroy(parmap);
62 static void fun_get_id(void *arg)
64 *(uintptr_t *)arg = (uintptr_t)xbt_os_thread_self();
68 static int fun_compare(const void *pa, const void *pb)
70 uintptr_t a = *(uintptr_t *)pa;
71 uintptr_t b = *(uintptr_t *)pb;
72 return a < b ? -1 : a > b ? 1 : 0;
75 static int test_parmap_extended(e_xbt_parmap_mode_t mode)
80 for (num_workers = 1 ; num_workers <= 16 ; num_workers *= 2) {
81 const unsigned len = 2 * num_workers;
88 parmap = xbt_parmap_new(num_workers, mode);
90 a = xbt_malloc(len * sizeof *a);
91 data = xbt_dynar_new(sizeof a, NULL);
92 for (i = 0; i < len; i++)
93 xbt_dynar_push_as(data, void *, &a[i]);
95 xbt_parmap_apply(parmap, fun_get_id, data);
97 qsort(a, len, sizeof a[0], fun_compare);
99 for (i = 1; i < len; i++)
100 if (a[i] != a[i - 1])
102 if (count != num_workers) {
103 XBT_CRITICAL("only %u/%u threads did some work", count, num_workers);
107 xbt_dynar_free(&data);
109 xbt_parmap_destroy(parmap);
114 int main(int argc, char** argv)
117 MSG_init(&argc, argv);
119 XBT_INFO("Basic testing posix");
120 status += test_parmap_basic(XBT_PARMAP_POSIX);
121 XBT_INFO("Basic testing futex");
123 status += test_parmap_basic(XBT_PARMAP_FUTEX);
125 XBT_INFO("Basic testing busy wait");
126 status += test_parmap_basic(XBT_PARMAP_BUSY_WAIT);
128 XBT_INFO("Extended testing posix");
129 status += test_parmap_extended(XBT_PARMAP_POSIX);
130 XBT_INFO("Extended testing futex");
132 status += test_parmap_extended(XBT_PARMAP_FUTEX);
134 XBT_INFO("Extended testing busy wait");
135 status += test_parmap_extended(XBT_PARMAP_BUSY_WAIT);
137 return status == 0 ? EXIT_SUCCESS : EXIT_FAILURE;