File: thread_usage.h

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 (266 lines) | stat: -rw-r--r-- 13,256 bytes parent folder | download | duplicates (5)
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
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
/*
 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
 *                         University Research and Technology
 *                         Corporation.  All rights reserved.
 * Copyright (c) 2004-2007 The University of Tennessee and The University
 *                         of Tennessee Research Foundation.  All rights
 *                         reserved.
 * Copyright (c) 2004-2006 High Performance Computing Center Stuttgart,
 *                         University of Stuttgart.  All rights reserved.
 * Copyright (c) 2004-2005 The Regents of the University of California.
 *                         All rights reserved.
 * Copyright (c) 2007-2014 Cisco Systems, Inc.  All rights reserved.
 * Copyright (c) 2014-2016 Research Organization for Information Science
 *                         and Technology (RIST). All rights reserved.
 * Copyright (c) 2015-2018 Los Alamos National Security, LLC. All rights
 *                         reserved.
 * Copyright (c) 2019      Sandia National Laboratories. All rights reserved.
 *
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */

#ifndef OPAL_MCA_THREADS_THREAD_USAGE_H
#define OPAL_MCA_THREADS_THREAD_USAGE_H

#include "opal_config.h"

#include "opal/prefetch.h"
#include "opal/sys/atomic.h"

OPAL_DECLSPEC extern bool opal_uses_threads;

/**
 * Check and see if the process is using multiple threads.
 *
 * @retval true If the process may have more than one thread.
 * @retval false If the process only has a single thread.
 *
 * The value that this function returns is influenced by:
 *
 * - how MPI_INIT or MPI_INIT_THREAD was invoked,
 * - what the final MPI thread level was determined to be,
 * - whether the OMPI or MPI libraries are multi-threaded
 *
 * MPI_INIT and MPI_INIT_THREAD (specifically, back-end OMPI startup
 * functions) invoke opal_set_using_threads() to influence the value of
 * this function, depending on their situation. Some examples:
 *
 * - if MPI_INIT is invoked, and the ompi components in use are
 * single-threaded, this value will be false.
 *
 * - if MPI_INIT_THREAD is invoked with MPI_THREAD_MULTIPLE, we have
 * thread support, and the final thread level is determined to be
 * MPI_THREAD_MULTIPLE, this value will be true.
 *
 * - if the process is a single-threaded OMPI executable (e.g., mpicc),
 * this value will be false.
 *
 * Hence, this function will return false if there is guaranteed to
 * only be one thread in the process.  If there is even the
 * possibility that we may have multiple threads, true will be
 * returned.
 */
#define opal_using_threads() opal_uses_threads

/**
 * Set whether the process is using multiple threads or not.
 *
 * @param have Boolean indicating whether the process is using
 * multiple threads or not.
 *
 * @retval opal_using_threads The new return value from
 * opal_using_threads().
 *
 * This function is used to influence the return value of
 * opal_using_threads().  If configure detected that we have thread
 * support, the return value of future invocations of
 * opal_using_threads() will be the parameter's value.  If configure
 * detected that we have no thread support, then the return from
 * opal_using_threads() will always be false.
 */
static inline bool opal_set_using_threads(bool have)
{
    opal_uses_threads = have;
    return opal_using_threads();
}

/**
 * Use an atomic operation for increment/decrement if opal_using_threads()
 * indicates that threads are in use by the application or library.
 */

#define OPAL_THREAD_DEFINE_ATOMIC_OP(type, name, operator, suffix)                               \
    static inline type opal_thread_##name##_fetch_##suffix(opal_atomic_##type *addr, type delta) \
    {                                                                                            \
        if (OPAL_UNLIKELY(opal_using_threads())) {                                               \
            return opal_atomic_##name##_fetch_##suffix(addr, delta);                             \
        }                                                                                        \
                                                                                                 \
        *addr = *addr operator delta;                                                            \
        return *addr;                                                                            \
    }                                                                                            \
                                                                                                 \
    static inline type opal_thread_fetch_##name##_##suffix(opal_atomic_##type *addr, type delta) \
    {                                                                                            \
        if (OPAL_UNLIKELY(opal_using_threads())) {                                               \
            return opal_atomic_fetch_##name##_##suffix(addr, delta);                             \
        }                                                                                        \
                                                                                                 \
        type old = *addr;                                                                        \
        *addr = old operator delta;                                                              \
        return old;                                                                              \
    }

#define OPAL_THREAD_DEFINE_ATOMIC_COMPARE_EXCHANGE(type, addr_type, suffix)                        \
    static inline bool opal_thread_compare_exchange_strong_##suffix(opal_atomic_##addr_type *addr, \
                                                                    type *compare, type value)     \
    {                                                                                              \
        if (OPAL_UNLIKELY(opal_using_threads())) {                                                 \
            return opal_atomic_compare_exchange_strong_##suffix(addr, (addr_type *) compare,       \
                                                                (addr_type) value);                \
        }                                                                                          \
                                                                                                   \
        if ((type) *addr == *compare) {                                                            \
            ((type *) addr)[0] = value;                                                            \
            return true;                                                                           \
        }                                                                                          \
                                                                                                   \
        *compare = ((type *) addr)[0];                                                             \
                                                                                                   \
        return false;                                                                              \
    }

#define OPAL_THREAD_DEFINE_ATOMIC_SWAP(type, addr_type, suffix)                               \
    static inline type opal_thread_swap_##suffix(opal_atomic_##addr_type *ptr, type newvalue) \
    {                                                                                         \
        if (opal_using_threads()) {                                                           \
            return (type) opal_atomic_swap_##suffix(ptr, (addr_type) newvalue);               \
        }                                                                                     \
                                                                                              \
        type old = ((type *) ptr)[0];                                                         \
        ((type *) ptr)[0] = newvalue;                                                         \
                                                                                              \
        return old;                                                                           \
    }

OPAL_THREAD_DEFINE_ATOMIC_OP(int32_t, add, +, 32)
OPAL_THREAD_DEFINE_ATOMIC_OP(size_t, add, +, size_t)
OPAL_THREAD_DEFINE_ATOMIC_OP(int32_t, and, &, 32)
OPAL_THREAD_DEFINE_ATOMIC_OP(int32_t, or, |, 32)
OPAL_THREAD_DEFINE_ATOMIC_OP(int32_t, xor, ^, 32)
OPAL_THREAD_DEFINE_ATOMIC_OP(int32_t, sub, -, 32)
OPAL_THREAD_DEFINE_ATOMIC_OP(size_t, sub, -, size_t)

OPAL_THREAD_DEFINE_ATOMIC_COMPARE_EXCHANGE(int32_t, int32_t, 32)
OPAL_THREAD_DEFINE_ATOMIC_COMPARE_EXCHANGE(intptr_t, intptr_t, ptr)
OPAL_THREAD_DEFINE_ATOMIC_SWAP(int32_t, int32_t, 32)
OPAL_THREAD_DEFINE_ATOMIC_SWAP(intptr_t, intptr_t, ptr)

#define OPAL_THREAD_ADD_FETCH32 opal_thread_add_fetch_32
#define OPAL_ATOMIC_ADD_FETCH32 opal_thread_add_fetch_32

#define OPAL_THREAD_AND_FETCH32 opal_thread_and_fetch_32
#define OPAL_ATOMIC_AND_FETCH32 opal_thread_and_fetch_32

#define OPAL_THREAD_OR_FETCH32 opal_thread_or_fetch_32
#define OPAL_ATOMIC_OR_FETCH32 opal_thread_or_fetch_32

#define OPAL_THREAD_XOR_FETCH32 opal_thread_xor_fetch_32
#define OPAL_ATOMIC_XOR_FETCH32 opal_thread_xor_fetch_32

#define OPAL_THREAD_ADD_FETCH_SIZE_T opal_thread_add_fetch_size_t
#define OPAL_ATOMIC_ADD_FETCH_SIZE_T opal_thread_add_fetch_size_t

#define OPAL_THREAD_SUB_FETCH_SIZE_T opal_thread_sub_fetch_size_t
#define OPAL_ATOMIC_SUB_FETCH_SIZE_T opal_thread_sub_fetch_size_t

#define OPAL_THREAD_FETCH_ADD32 opal_thread_fetch_add_32
#define OPAL_ATOMIC_FETCH_ADD32 opal_thread_fetch_add_32

#define OPAL_THREAD_FETCH_AND32 opal_thread_fetch_and_32
#define OPAL_ATOMIC_FETCH_AND32 opal_thread_fetch_and_32

#define OPAL_THREAD_FETCH_OR32 opal_thread_fetch_or_32
#define OPAL_ATOMIC_FETCH_OR32 opal_thread_fetch_or_32

#define OPAL_THREAD_FETCH_XOR32 opal_thread_fetch_xor_32
#define OPAL_ATOMIC_FETCH_XOR32 opal_thread_fetch_xor_32

#define OPAL_THREAD_FETCH_ADD_SIZE_T opal_thread_fetch_add_size_t
#define OPAL_ATOMIC_FETCH_ADD_SIZE_T opal_thread_fetch_add_size_t

#define OPAL_THREAD_FETCH_SUB_SIZE_T opal_thread_fetch_sub_size_t
#define OPAL_ATOMIC_FETCH_SUB_SIZE_T opal_thread_fetch_sub_size_t

#define OPAL_THREAD_COMPARE_EXCHANGE_STRONG_32 opal_thread_compare_exchange_strong_32
#define OPAL_ATOMIC_COMPARE_EXCHANGE_STRONG_32 opal_thread_compare_exchange_strong_32

#define OPAL_THREAD_COMPARE_EXCHANGE_STRONG_PTR(x, y, z)                                \
    opal_thread_compare_exchange_strong_ptr((opal_atomic_intptr_t *) x, (intptr_t *) y, \
                                            (intptr_t) z)
#define OPAL_ATOMIC_COMPARE_EXCHANGE_STRONG_PTR OPAL_THREAD_COMPARE_EXCHANGE_STRONG_PTR

#define OPAL_THREAD_SWAP_32 opal_thread_swap_32
#define OPAL_ATOMIC_SWAP_32 opal_thread_swap_32

#define OPAL_THREAD_SWAP_PTR(x, y) opal_thread_swap_ptr((opal_atomic_intptr_t *) x, (intptr_t) y)
#define OPAL_ATOMIC_SWAP_PTR       OPAL_THREAD_SWAP_PTR

OPAL_THREAD_DEFINE_ATOMIC_OP(int64_t, add, +, 64)
OPAL_THREAD_DEFINE_ATOMIC_OP(int64_t, and, &, 64)
OPAL_THREAD_DEFINE_ATOMIC_OP(int64_t, or, |, 64)
OPAL_THREAD_DEFINE_ATOMIC_OP(int64_t, xor, ^, 64)
OPAL_THREAD_DEFINE_ATOMIC_OP(int64_t, sub, -, 64)
OPAL_THREAD_DEFINE_ATOMIC_COMPARE_EXCHANGE(int64_t, int64_t, 64)
OPAL_THREAD_DEFINE_ATOMIC_SWAP(int64_t, int64_t, 64)

#    define OPAL_THREAD_ADD_FETCH64 opal_thread_add_fetch_64
#    define OPAL_ATOMIC_ADD_FETCH64 opal_thread_add_fetch_64

#    define OPAL_THREAD_AND_FETCH64 opal_thread_and_fetch_64
#    define OPAL_ATOMIC_AND_FETCH64 opal_thread_and_fetch_64

#    define OPAL_THREAD_OR_FETCH64 opal_thread_or_fetch_64
#    define OPAL_ATOMIC_OR_FETCH64 opal_thread_or_fetch_64

#    define OPAL_THREAD_XOR_FETCH64 opal_thread_xor_fetch_64
#    define OPAL_ATOMIC_XOR_FETCH64 opal_thread_xor_fetch_64

#    define OPAL_THREAD_FETCH_ADD64 opal_thread_fetch_add_64
#    define OPAL_ATOMIC_FETCH_ADD64 opal_thread_fetch_add_64

#    define OPAL_THREAD_FETCH_AND64 opal_thread_fetch_and_64
#    define OPAL_ATOMIC_FETCH_AND64 opal_thread_fetch_and_64

#    define OPAL_THREAD_FETCH_OR64 opal_thread_fetch_or_64
#    define OPAL_ATOMIC_FETCH_OR64 opal_thread_fetch_or_64

#    define OPAL_THREAD_FETCH_XOR64 opal_thread_fetch_xor_64
#    define OPAL_ATOMIC_FETCH_XOR64 opal_thread_fetch_xor_64

#    define OPAL_THREAD_COMPARE_EXCHANGE_STRONG_64 opal_thread_compare_exchange_strong_64
#    define OPAL_ATOMIC_COMPARE_EXCHANGE_STRONG_64 opal_thread_compare_exchange_strong_64

#    define OPAL_THREAD_SWAP_64 opal_thread_swap_64
#    define OPAL_ATOMIC_SWAP_64 opal_thread_swap_64

/* thread local storage */
#if OPAL_C_HAVE__THREAD_LOCAL
#    define opal_thread_local      _Thread_local
#    define OPAL_HAVE_THREAD_LOCAL 1

#elif OPAL_C_HAVE___THREAD /* OPAL_C_HAVE__THREAD_LOCAL */
#    define opal_thread_local      __thread
#    define OPAL_HAVE_THREAD_LOCAL 1
#endif /* OPAL_C_HAVE___THREAD */

#if !defined(OPAL_HAVE_THREAD_LOCAL)
#    define OPAL_HAVE_THREAD_LOCAL 0
#endif /* !defined(OPAL_HAVE_THREAD_LOCAL) */

#endif /* OPAL_MCA_THREADS_THREAD_USAGE_H */