Debian Codegraph @

regressions/ck_barrier/validate/barrier_combining.c

raw symbols SCIP
1 /*
2 * Copyright 2011-2015 Samy Al Bahra.
3 * Copyright 2011 David Joseph.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <errno.h>
29 #include <inttypes.h>
30 #include <pthread.h>
31 #include <math.h>
32 #include <stdint.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <strings.h>
36 #include <unistd.h>
37 #include <sys/time.h>
38
39 #include <ck_pr.h>
40 #include <ck_barrier.h>
41
42 #include "../../common.h"
43
44 #ifndef ITERATE
45 #define ITERATE 5000000
46 #endif
47
48 #ifndef ENTRIES
49 #define ENTRIES 512
50 #endif
51
52 static struct affinity a;
53 static int nthr;
54 static int ngroups;
55 static int counters[ENTRIES];
56 static ck_barrier_combining_t barrier;
57 static int barrier_wait;
58
59 static void *
60 thread(void *group)
61 {
62 ck_barrier_combining_state_t state = CK_BARRIER_COMBINING_STATE_INITIALIZER;
63 int j, counter;
64 int i = 0;
65
66 aff_iterate(&a);
67
68 ck_pr_inc_int(&barrier_wait);
69 while (ck_pr_load_int(&barrier_wait) != (nthr * ngroups))
70 ck_pr_stall();
71
72 for (j = 0; j < ITERATE; j++) {
73 i = j++ & (ENTRIES - 1);
74 ck_pr_inc_int(&counters[i]);
75 ck_barrier_combining(&barrier, group, &state);
76 counter = ck_pr_load_int(&counters[i]);
77 if (counter != nthr * ngroups * (j / ENTRIES + 1)) {
78 ck_error("FAILED [%d:%d]: %d != %d\n", i, j - 1, counter, nthr * ngroups);
79 }
80 }
81
82 return (NULL);
83 }
84
85 int
86 main(int argc, char *argv[])
87 {
88 pthread_t *threads;
89 ck_barrier_combining_group_t *groupings;
90 ck_barrier_combining_group_t *init_root;
91 int i;
92
93 init_root = malloc(sizeof(ck_barrier_combining_group_t));
94 if (init_root == NULL) {
95 ck_error("ERROR: Could not allocate initial barrier structure\n");
96 }
97 ck_barrier_combining_init(&barrier, init_root);
98
99 if (argc < 4) {
100 ck_error("Usage: correct <total groups> <threads per group> <affinity delta>\n");
101 }
102
103 ngroups = atoi(argv[1]);
104 if (ngroups <= 0) {
105 ck_error("ERROR: Number of groups must be greater than 0\n");
106 }
107
108 nthr = atoi(argv[2]);
109 if (nthr <= 0) {
110 ck_error("ERROR: Number of threads must be greater than 0\n");
111 }
112
113 groupings = malloc(sizeof(ck_barrier_combining_group_t) * ngroups);
114 if (groupings == NULL) {
115 ck_error("Could not allocate thread barrier grouping structures\n");
116 }
117
118 threads = malloc(sizeof(pthread_t) * nthr * ngroups);
119 if (threads == NULL) {
120 ck_error("ERROR: Could not allocate thread structures\n");
121 }
122
123 a.delta = atoi(argv[3]);
124
125 for (i = 0; i < ngroups; i++)
126 ck_barrier_combining_group_init(&barrier, groupings + i, nthr);
127
128 fprintf(stderr, "Creating threads (barrier)...");
129 for (i = 0; i < (nthr * ngroups); i++) {
130 if (pthread_create(&threads[i], NULL, thread, groupings + (i % ngroups))) {
131 ck_error("ERROR: Could not create thread %d\n", i);
132 }
133 }
134 fprintf(stderr, "done\n");
135
136 fprintf(stderr, "Waiting for threads to finish correctness regression...");
137 for (i = 0; i < (nthr * ngroups); i++)
138 pthread_join(threads[i], NULL);
139 fprintf(stderr, "done (passed)\n");
140
141 return (0);
142 }
143