Debian Codegraph @

regressions/ck_barrier/validate/barrier_tournament.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 static ck_barrier_tournament_t barrier;
57
58 static void *
59 thread(CK_CC_UNUSED void *unused)
60 {
61 ck_barrier_tournament_state_t state;
62 int j, counter;
63 int i = 0;
64
65 aff_iterate(&a);
66 ck_barrier_tournament_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; j < ITERATE; j++) {
73 i = j++ & (ENTRIES - 1);
74 ck_pr_inc_int(&counters[i]);
75 ck_barrier_tournament(&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 ck_pr_inc_int(&barrier_wait);
83 while (ck_pr_load_int(&barrier_wait) != nthr * 2)
84 ck_pr_stall();
85
86 return (NULL);
87 }
88
89 int
90 main(int argc, char *argv[])
91 {
92 pthread_t *threads;
93 ck_barrier_tournament_round_t **rounds;
94 int i;
95 unsigned int size;
96
97 if (argc < 3) {
98 ck_error("Usage: correct <number of threads> <affinity delta>\n");
99 }
100
101 nthr = atoi(argv[1]);
102 if (nthr <= 0) {
103 ck_error("ERROR: Number of threads must be greater than 0\n");
104 }
105 a.delta = atoi(argv[2]);
106
107 threads = malloc(sizeof(pthread_t) * nthr);
108 if (threads == NULL) {
109 ck_error("ERROR: Could not allocate thread structures\n");
110 }
111
112 rounds = malloc(sizeof(ck_barrier_tournament_round_t *) * nthr);
113 if (rounds == NULL) {
114 ck_error("ERROR: Could not allocate barrier structures\n");
115 }
116
117 size = ck_barrier_tournament_size(nthr);
118 for (i = 0; i < nthr; ++i) {
119 rounds[i] = malloc(sizeof(ck_barrier_tournament_round_t) * size);
120 if (rounds[i] == NULL) {
121 ck_error("ERROR: Could not allocate barrier structures\n");
122 }
123 }
124
125 ck_barrier_tournament_init(&barrier, rounds, nthr);
126
127 fprintf(stderr, "Creating threads (barrier)...");
128 for (i = 0; i < nthr; i++) {
129 if (pthread_create(&threads[i], NULL, thread, NULL)) {
130 ck_error("ERROR: Could not create thread %d\n", i);
131 }
132 }
133 fprintf(stderr, "done\n");
134
135 fprintf(stderr, "Waiting for threads to finish correctness regression...");
136 for (i = 0; i < nthr; i++)
137 pthread_join(threads[i], NULL);
138 fprintf(stderr, "done (passed)\n");
139
140 return (0);
141 }
142