File: opal_atomic_thread_bench.c

package info (click to toggle)
openmpi 5.0.8-4
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 201,684 kB
  • sloc: ansic: 613,078; makefile: 42,353; sh: 11,194; javascript: 9,244; f90: 7,052; java: 6,404; perl: 5,179; python: 1,859; lex: 740; fortran: 61; cpp: 20; tcl: 12
file content (296 lines) | stat: -rw-r--r-- 8,042 bytes parent folder | download | duplicates (4)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
 * Copyright (c) 2021 IBM Corporation.  All rights reserved.
 * Additional copyrights may follow
 *
 * $HEADER$
 */

#include "opal_config.h"

#include <stdio.h>
#include <time.h>

#include "support.h"
#include "opal/runtime/opal.h"
#include "opal/constants.h"
#include "opal/mca/threads/threads.h"
#include "opal/mca/threads/condition.h"
#include "opal/sys/atomic.h"


#define OPAL_TEST_THREAD_COUNT 8
#define ITERATIONS 1000000
#define ITEM_COUNT 100

#ifdef __APPLE__
/* pthread barrier implementation copied from stack overflow.
 * Source:
 * https://stackoverflow.com/questions/3640853/performance-test-sem-t-v-s-dispatch-semaphore-t-and-pthread-once-t-v-s-dispatch
 */
typedef int pthread_barrierattr_t;
struct pthread_barrier_t {
    pthread_mutex_t mutex;
    pthread_cond_t cond;
    int count;
    int tripCount;
};

typedef struct pthread_barrier_t pthread_barrier_t;

int pthread_barrier_init(pthread_barrier_t *barrier, const pthread_barrierattr_t *attr,
                         unsigned int count)
{
    if (count == 0) {
        errno = EINVAL;
        return -1;
    }
    if (pthread_mutex_init(&barrier->mutex, 0) < 0) {
        return -1;
    }
    if (pthread_cond_init(&barrier->cond, 0) < 0) {
        pthread_mutex_destroy(&barrier->mutex);
        return -1;
    }
    barrier->tripCount = count;
    barrier->count = 0;

    return 0;
}

int pthread_barrier_destroy(pthread_barrier_t *barrier)
{
    pthread_cond_destroy(&barrier->cond);
    pthread_mutex_destroy(&barrier->mutex);
    return 0;
}

int pthread_barrier_wait(pthread_barrier_t *barrier)
{
    pthread_mutex_lock(&barrier->mutex);
    ++(barrier->count);
    if (barrier->count >= barrier->tripCount) {
        barrier->count = 0;
        pthread_cond_broadcast(&barrier->cond);
        pthread_mutex_unlock(&barrier->mutex);
        return 1;
    }

    pthread_cond_wait(&barrier->cond, &(barrier->mutex));
    pthread_mutex_unlock(&barrier->mutex);
    return 0;
}
#endif

static opal_atomic_int64_t var_64 = 0;
static opal_atomic_int32_t var_32 = 0;
static pthread_barrier_t   barrier;

#if !defined(timersub)
#define timersub(a, b, r) \
    do {                  \
        (r)->tv_sec = (a)->tv_sec - (b)->tv_sec;        \
        if ((a)->tv_usec < (b)->tv_usec) {              \
            (r)->tv_sec--;                              \
            (a)->tv_usec += 1000000;                    \
        }                                               \
        (r)->tv_usec = (a)->tv_usec - (b)->tv_usec;     \
    } while (0)
#endif


#if !defined(OPAL_TEST_DONE)
#define OPAL_TEST_DONE(func, val) { \
    gettimeofday (&stop, NULL); \
    timersub(&stop, &start, &total); \
    timing = ((double) total.tv_sec + (double) total.tv_usec * 1e-6) / (double) ITERATIONS; \
    printf ("%s() thread finished. Time: %d s %d us %d nsec/per\n", func, (int) total.tv_sec, \
            (int)total.tv_usec, (int)(timing / 1e-9)); \
    memset(&stop, 0, sizeof(struct timeval)); \
    memset(&start, 0, sizeof(struct timeval)); \
    memset(&total, 0, sizeof(struct timeval)); \
    /* printf("%ld\n", val); */ \
    fflush(stdout); \
    pthread_barrier_wait (&barrier); \
}
#endif

#if !defined(OPAL_RESET_VAR)
#define OPAL_RESET_VAR(var) { \
    var = 0; \
    pthread_barrier_wait (&barrier); \
}
#endif

static void *thread_test (void *arg) {
    struct timeval start, stop, total;
    double timing;

    gettimeofday (&start, NULL);
    for (int64_t i = 0 ; i < ITERATIONS ; ++i) {
       opal_atomic_compare_exchange_strong_64(&var_64, &i, i+1);
    }
    OPAL_TEST_DONE("opal_atomic_compare_exchange_strong_64", var_64);

    OPAL_RESET_VAR(var_64);

    gettimeofday (&start, NULL);
    for (int64_t i = 0 ; i < ITERATIONS ; ++i) {
       opal_atomic_compare_exchange_strong_rel_64(&var_64, &i, i+1);
    }
    OPAL_TEST_DONE("opal_atomic_compare_exchange_strong_rel_64", var_64);

    OPAL_RESET_VAR(var_64);

    gettimeofday (&start, NULL);
    for (int64_t i = 0 ; i < ITERATIONS ; ++i) {
       opal_atomic_compare_exchange_strong_acq_64(&var_64, &i, i+1);
    }
    OPAL_TEST_DONE("opal_atomic_compare_exchange_strong_acq_64", var_64);

    OPAL_RESET_VAR(var_64);

    gettimeofday (&start, NULL);
    for (int64_t i = 0 ; i < ITERATIONS ; ++i) {
       opal_atomic_fetch_add_64(&var_64, 1);
    }
    OPAL_TEST_DONE("opal_atomic_fetch_add_64", var_64);

    OPAL_RESET_VAR(var_64);

    gettimeofday (&start, NULL);
    for (int64_t i = 0 ; i < ITERATIONS ; ++i) {
       opal_atomic_fetch_sub_64(&var_64, 1);
    }
    OPAL_TEST_DONE("opal_atomic_fetch_sub_64", var_64);

    OPAL_RESET_VAR(var_64);

    gettimeofday (&start, NULL);
    for (int64_t i = 0 ; i < ITERATIONS ; ++i) {
       opal_atomic_fetch_xor_64(&var_64, i);
    }
    OPAL_TEST_DONE("opal_atomic_fetch_xor_64", var_64);

    OPAL_RESET_VAR(var_64);

    gettimeofday (&start, NULL);
    for (int64_t i = 0 ; i < ITERATIONS ; ++i) {
       opal_atomic_swap_64(&var_64, i);
    }
    OPAL_TEST_DONE("opal_atomic_swap_64", var_64);

    OPAL_RESET_VAR(var_64);

#if OPAL_HAVE_ATOMIC_LLSC_64
    gettimeofday (&start, NULL);
    for (int64_t i = 0 ; i < ITERATIONS ; ++i) {
       int ret;
       opal_atomic_sc_64(&var_64, i, ret);
    }
    OPAL_TEST_DONE("opal_atomic_sc_64", var_64);

    OPAL_RESET_VAR(var_64);

    gettimeofday (&start, NULL);
    for (int64_t i = 0 ; i < ITERATIONS ; ++i) {
       int ret;
       opal_atomic_sc_64(&var_64, i, ret);
    }
    OPAL_TEST_DONE("opal_atomic_ll_64", var_64);

    OPAL_RESET_VAR(var_64);
#endif

    gettimeofday (&start, NULL);
    for (int32_t i = 0 ; i < ITERATIONS ; ++i) {
       opal_atomic_compare_exchange_strong_32(&var_32, &i, i+1);
    }
    OPAL_TEST_DONE("opal_atomic_compare_exchange_strong_32", var_32);

    OPAL_RESET_VAR(var_32);
 
    gettimeofday (&start, NULL);
    for (int32_t i = 0 ; i < ITERATIONS ; ++i) {
       opal_atomic_compare_exchange_strong_rel_32(&var_32, &i, i+1);
    }
    OPAL_TEST_DONE("opal_atomic_compare_exchange_strong_rel_32", var_32);

    OPAL_RESET_VAR(var_32);

    gettimeofday (&start, NULL);
    for (int32_t i = 0 ; i < ITERATIONS ; ++i) {
       opal_atomic_compare_exchange_strong_acq_32(&var_32, &i, i+1);
    }
    OPAL_TEST_DONE("opal_atomic_compare_exchange_strong_acq_32", var_32);

    OPAL_RESET_VAR(var_32);

    gettimeofday (&start, NULL);
    for (int32_t i = 0 ; i < ITERATIONS ; ++i) {
       opal_atomic_fetch_add_32(&var_32, 1);
    }
    OPAL_TEST_DONE("opal_atomic_fetch_add_32", var_32);

    OPAL_RESET_VAR(var_32);

    gettimeofday (&start, NULL);
    for (int32_t i = 0 ; i < ITERATIONS ; ++i) {
       opal_atomic_fetch_sub_32(&var_32, 1);
    }
    OPAL_TEST_DONE("opal_atomic_fetch_sub_32", var_32);

    OPAL_RESET_VAR(var_32);

    gettimeofday (&start, NULL);
    for (int32_t i = 0 ; i < ITERATIONS ; ++i) {
       opal_atomic_fetch_xor_32(&var_32, i);
    }
    OPAL_TEST_DONE("opal_atomic_fetch_xor_32", var_32);

    OPAL_RESET_VAR(var_32);

    gettimeofday (&start, NULL);
    for (int32_t i = 0 ; i < ITERATIONS ; ++i) {
       opal_atomic_swap_32(&var_32, i);
    }
    OPAL_TEST_DONE("opal_atomic_swap_32", var_32);

    OPAL_RESET_VAR(var_32);

#if OPAL_HAVE_ATOMIC_LLSC_32
    gettimeofday (&start, NULL);
    for (int32_t i = 0 ; i < ITERATIONS ; ++i) {
       int ret;
       opal_atomic_sc_32(&var_32, i, ret);
    }
    OPAL_TEST_DONE("opal_atomic_sc_32", var_32);

    OPAL_RESET_VAR(var_32);

    gettimeofday (&start, NULL);
    for (int32_t i = 0 ; i < ITERATIONS ; ++i) {
       int ret;
       opal_atomic_sc_32(&var_32, i, ret);
    }
    OPAL_TEST_DONE("opal_atomic_ll_32", var_32);

    OPAL_RESET_VAR(var_32);
#endif
   
    return NULL;
}

int main(void) {
    
    pthread_barrier_init (&barrier, NULL, OPAL_TEST_THREAD_COUNT);

    pthread_t ts[OPAL_TEST_THREAD_COUNT];
    for(int i = 0; i < OPAL_TEST_THREAD_COUNT; i++) {
        pthread_create(&ts[i], NULL,  &thread_test, NULL);
    }

    for(int i = 0; i < OPAL_TEST_THREAD_COUNT; i++) {
        pthread_join(ts[i], NULL);
    }
    return 0;
}