Debian Codegraph @

regressions/ck_barrier/validate/barrier_dissemination.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 counters[ENTRIES];
55 static int barrier_wait;
56
57 static void *
58 thread(void *b)
59 {
60 ck_barrier_dissemination_t *barrier = b;
61 ck_barrier_dissemination_state_t state;
62 int j, k, counter;
63 int i = 0;
64
65 aff_iterate(&a);
66 ck_barrier_dissemination_subscribe(barrier, &state);
67
68 ck_pr_inc_int(&barrier_wait);
69 while (ck_pr_load_int(&barrier_wait) != nthr)
70 ck_pr_stall();
71
72 for (j = 0, k = 0; j < ITERATE; j++, k++) {
73 i = j++ & (ENTRIES - 1);
74 ck_pr_inc_int(&counters[i]);
75 ck_barrier_dissemination(barrier, &state);
76 counter = ck_pr_load_int(&counters[i]);
77 if (counter != nthr * (j / ENTRIES + 1)) {
78 ck_error("FAILED [%d:%d]: %d != %d\n", i, j - 1, counter, nthr);
79 }
80 }
81
82 return (NULL);
83 }
84
85 int
86 main(int argc, char *argv[])
87 {
88 ck_barrier_dissemination_t *barrier;
89 ck_barrier_dissemination_flag_t **barrier_internal;
90 pthread_t *threads;
91 int i, size;
92
93 if (argc < 3) {
94 ck_error("Usage: correct <number of threads> <affinity delta>\n");
95 }
96
97 nthr = atoi(argv[1]);
98 if (nthr <= 0) {
99 ck_error("ERROR: Number of threads must be greater than 0\n");
100 }
101
102 threads = malloc(sizeof(pthread_t) * nthr);
103 if (threads == NULL) {
104 ck_error("ERROR: Could not allocate thread structures\n");
105 }
106
107 a.delta = atoi(argv[2]);
108
109 barrier = malloc(sizeof(ck_barrier_dissemination_t) * nthr);
110 if (barrier == NULL) {
111 ck_error("ERROR: Could not allocate barrier structures\n");
112 }
113
114 barrier_internal = malloc(sizeof(ck_barrier_dissemination_flag_t *) * nthr);
115 if (barrier_internal == NULL) {
116 ck_error("ERROR: Could not allocate barrier structures\n");
117 }
118
119 size = ck_barrier_dissemination_size(nthr);
120 for (i = 0; i < nthr; ++i) {
121 barrier_internal[i] = malloc(sizeof(ck_barrier_dissemination_flag_t) * size);
122 if (barrier_internal[i] == NULL) {
123 ck_error("ERROR: Could not allocate barrier structures\n");
124 }
125 }
126 ck_barrier_dissemination_init(barrier, barrier_internal, nthr);
127
128 fprintf(stderr, "Creating threads (barrier)...");
129 for (i = 0; i < nthr; i++) {
130 if (pthread_create(&threads[i], NULL, thread, barrier)) {
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; i++)
138 pthread_join(threads[i], NULL);
139 fprintf(stderr, "done (passed)\n");
140
141
142 return (0);
143 }
144