| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 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 |
|