Debian Codegraph @

regressions/ck_barrier/benchmark/throughput.c

raw symbols SCIP
1 /*
2 * Copyright 2011 David Joseph.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <pthread.h>
28 #include <unistd.h>
29 #include <ck_stdint.h>
30 #include <inttypes.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #include <ck_pr.h>
35 #include <ck_barrier.h>
36
37 #include "../../common.h"
38
39 #if defined(CK_F_PR_INC_64) && defined(CK_F_PR_LOAD_64)
40 static int done = 0;
41 static struct affinity a;
42 static int nthr;
43 static int tid;
44 static ck_barrier_centralized_t barrier = CK_BARRIER_CENTRALIZED_INITIALIZER;
45 struct counter {
46 uint64_t value;
47 } CK_CC_CACHELINE;
48 struct counter *counters;
49
50 static void *
51 thread(void *null CK_CC_UNUSED)
52 {
53 ck_barrier_centralized_state_t state = CK_BARRIER_CENTRALIZED_STATE_INITIALIZER;
54 int id;
55
56 id = ck_pr_faa_int(&tid, 1);
57 aff_iterate(&a);
58
59 while (ck_pr_load_int(&done) == 0) {
60 ck_barrier_centralized(&barrier, &state, nthr);
61 ck_pr_inc_64(&counters[id].value);
62 ck_barrier_centralized(&barrier, &state, nthr);
63 ck_pr_inc_64(&counters[id].value);
64 ck_barrier_centralized(&barrier, &state, nthr);
65 ck_pr_inc_64(&counters[id].value);
66 ck_barrier_centralized(&barrier, &state, nthr);
67 ck_pr_inc_64(&counters[id].value);
68 ck_barrier_centralized(&barrier, &state, nthr);
69 ck_pr_inc_64(&counters[id].value);
70 ck_barrier_centralized(&barrier, &state, nthr);
71 ck_pr_inc_64(&counters[id].value);
72 ck_barrier_centralized(&barrier, &state, nthr);
73 ck_pr_inc_64(&counters[id].value);
74 ck_barrier_centralized(&barrier, &state, nthr);
75 ck_pr_inc_64(&counters[id].value);
76 }
77
78 return (NULL);
79 }
80
81 int
82 main(int argc, char *argv[])
83 {
84 pthread_t *threads;
85 uint64_t count;
86 int i;
87
88 if (argc != 3) {
89 ck_error("Correct usage: <number of threads> <affinity delta>\n");
90 }
91
92 nthr = atoi(argv[1]);
93 if (nthr <= 0) {
94 ck_error("ERROR: Number of threads must be greater than 0\n");
95 }
96
97 threads = malloc(sizeof(pthread_t) * nthr);
98 if (threads == NULL) {
99 ck_error("ERROR: Could not allocate thread structures\n");
100 }
101
102 counters = calloc(sizeof(struct counter), nthr);
103 if (counters == NULL) {
104 ck_error("ERROR: Could not allocate counters\n");
105 }
106
107 a.delta = atoi(argv[2]);
108
109 fprintf(stderr, "Creating threads (barrier)...");
110 for (i = 0; i < nthr; ++i) {
111 if (pthread_create(&threads[i], NULL, thread, NULL)) {
112 ck_error("ERROR: Could not create thread %d\n", i);
113 }
114 }
115 fprintf(stderr, "done\n");
116
117 common_sleep(10);
118
119 count = 0;
120 ck_pr_store_int(&done, 1);
121 for (i = 0; i < nthr; ++i)
122 count += ck_pr_load_64(&counters[i].value);
123 printf("%d %16" PRIu64 "\n", nthr, count);
124
125 return (0);
126 }
127 #else
128 int
129 main(void)
130 {
131
132 fputs("Unsupported.", stderr);
133 return 0;
134 }
135 #endif
136