|
| 1 | +/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */ |
| 2 | +/* |
| 3 | + * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana |
| 4 | + * University Research and Technology |
| 5 | + * Corporation. All rights reserved. |
| 6 | + * Copyright (c) 2004-2007 The University of Tennessee and The University |
| 7 | + * of Tennessee Research Foundation. All rights |
| 8 | + * reserved. |
| 9 | + * Copyright (c) 2004-2006 High Performance Computing Center Stuttgart, |
| 10 | + * University of Stuttgart. All rights reserved. |
| 11 | + * Copyright (c) 2004-2005 The Regents of the University of California. |
| 12 | + * All rights reserved. |
| 13 | + * Copyright (c) 2007-2014 Cisco Systems, Inc. All rights reserved. |
| 14 | + * Copyright (c) 2014 Research Organization for Information Science |
| 15 | + * and Technology (RIST). All rights reserved. |
| 16 | + * Copyright (c) 2015-2016 Los Alamos National Security, LLC. All rights |
| 17 | + * reserved. |
| 18 | + * $COPYRIGHT$ |
| 19 | + * |
| 20 | + * Additional copyrights may follow |
| 21 | + * |
| 22 | + * $HEADER$ |
| 23 | + */ |
| 24 | + |
| 25 | +#if !defined(OPAL_THREAD_USAGE_H) |
| 26 | +#define OPAL_THREAD_USAGE_H |
| 27 | + |
| 28 | +#include "opal_config.h" |
| 29 | + |
| 30 | +#include "opal/sys/atomic.h" |
| 31 | +#include "opal/prefetch.h" |
| 32 | + |
| 33 | +OPAL_DECLSPEC extern bool opal_uses_threads; |
| 34 | + |
| 35 | +/** |
| 36 | + * Check and see if the process is using multiple threads. |
| 37 | + * |
| 38 | + * @retval true If the process may have more than one thread. |
| 39 | + * @retval false If the process only has a single thread. |
| 40 | + * |
| 41 | + * The value that this function returns is influenced by: |
| 42 | + * |
| 43 | + * - how MPI_INIT or MPI_INIT_THREAD was invoked, |
| 44 | + * - what the final MPI thread level was determined to be, |
| 45 | + * - whether the OMPI or MPI libraries are multi-threaded |
| 46 | + * |
| 47 | + * MPI_INIT and MPI_INIT_THREAD (specifically, back-end OMPI startup |
| 48 | + * functions) invoke opal_set_using_threads() to influence the value of |
| 49 | + * this function, depending on their situation. Some examples: |
| 50 | + * |
| 51 | + * - if MPI_INIT is invoked, and the ompi components in use are |
| 52 | + * single-threaded, this value will be false. |
| 53 | + * |
| 54 | + * - if MPI_INIT_THREAD is invoked with MPI_THREAD_MULTIPLE, we have |
| 55 | + * thread support, and the final thread level is determined to be |
| 56 | + * MPI_THREAD_MULTIPLE, this value will be true. |
| 57 | + * |
| 58 | + * - if the process is a single-threaded OMPI executable (e.g., mpicc), |
| 59 | + * this value will be false. |
| 60 | + * |
| 61 | + * Hence, this function will return false if there is guaranteed to |
| 62 | + * only be one thread in the process. If there is even the |
| 63 | + * possibility that we may have multiple threads, true will be |
| 64 | + * returned. |
| 65 | + */ |
| 66 | +#define opal_using_threads() opal_uses_threads |
| 67 | + |
| 68 | +/** |
| 69 | + * Set whether the process is using multiple threads or not. |
| 70 | + * |
| 71 | + * @param have Boolean indicating whether the process is using |
| 72 | + * multiple threads or not. |
| 73 | + * |
| 74 | + * @retval opal_using_threads The new return value from |
| 75 | + * opal_using_threads(). |
| 76 | + * |
| 77 | + * This function is used to influence the return value of |
| 78 | + * opal_using_threads(). If configure detected that we have thread |
| 79 | + * support, the return value of future invocations of |
| 80 | + * opal_using_threads() will be the parameter's value. If configure |
| 81 | + * detected that we have no thread support, then the retuen from |
| 82 | + * opal_using_threads() will always be false. |
| 83 | + */ |
| 84 | +static inline bool opal_set_using_threads(bool have) |
| 85 | +{ |
| 86 | + opal_uses_threads = have; |
| 87 | + return opal_using_threads(); |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +/** |
| 92 | + * Use an atomic operation for increment/decrement if opal_using_threads() |
| 93 | + * indicates that threads are in use by the application or library. |
| 94 | + */ |
| 95 | + |
| 96 | +static inline int32_t |
| 97 | +OPAL_THREAD_ADD32(volatile int32_t *addr, int delta) |
| 98 | +{ |
| 99 | + int32_t ret; |
| 100 | + |
| 101 | + if (OPAL_UNLIKELY(opal_using_threads())) { |
| 102 | + ret = opal_atomic_add_32(addr, delta); |
| 103 | + } else { |
| 104 | + ret = (*addr += delta); |
| 105 | + } |
| 106 | + |
| 107 | + return ret; |
| 108 | +} |
| 109 | + |
| 110 | +#if OPAL_HAVE_ATOMIC_MATH_64 |
| 111 | +static inline int64_t |
| 112 | +OPAL_THREAD_ADD64(volatile int64_t *addr, int delta) |
| 113 | +{ |
| 114 | + int64_t ret; |
| 115 | + |
| 116 | + if (OPAL_UNLIKELY(opal_using_threads())) { |
| 117 | + ret = opal_atomic_add_64(addr, delta); |
| 118 | + } else { |
| 119 | + ret = (*addr += delta); |
| 120 | + } |
| 121 | + |
| 122 | + return ret; |
| 123 | +} |
| 124 | +#endif |
| 125 | + |
| 126 | +static inline size_t |
| 127 | +OPAL_THREAD_ADD_SIZE_T(volatile size_t *addr, int delta) |
| 128 | +{ |
| 129 | + size_t ret; |
| 130 | + |
| 131 | + if (OPAL_UNLIKELY(opal_using_threads())) { |
| 132 | + ret = opal_atomic_add_size_t(addr, delta); |
| 133 | + } else { |
| 134 | + ret = (*addr += delta); |
| 135 | + } |
| 136 | + |
| 137 | + return ret; |
| 138 | +} |
| 139 | + |
| 140 | +/* BWB: FIX ME: remove if possible */ |
| 141 | +#define OPAL_CMPSET(x, y, z) ((*(x) == (y)) ? ((*(x) = (z)), 1) : 0) |
| 142 | + |
| 143 | +#if OPAL_HAVE_ATOMIC_CMPSET_32 |
| 144 | +#define OPAL_ATOMIC_CMPSET_32(x, y, z) \ |
| 145 | + (opal_using_threads() ? opal_atomic_cmpset_32(x, y, z) : OPAL_CMPSET(x, y, z)) |
| 146 | +#endif |
| 147 | +#if OPAL_HAVE_ATOMIC_CMPSET_64 |
| 148 | +#define OPAL_ATOMIC_CMPSET_64(x, y, z) \ |
| 149 | + (opal_using_threads() ? opal_atomic_cmpset_64(x, y, z) : OPAL_CMPSET(x, y, z)) |
| 150 | +#endif |
| 151 | +#if OPAL_HAVE_ATOMIC_CMPSET_32 || OPAL_HAVE_ATOMIC_CMPSET_64 |
| 152 | +#define OPAL_ATOMIC_CMPSET(x, y, z) \ |
| 153 | + (opal_using_threads() ? opal_atomic_cmpset(x, y, z) : OPAL_CMPSET(x, y, z)) |
| 154 | +#endif |
| 155 | +#if OPAL_HAVE_ATOMIC_CMPSET_32 || OPAL_HAVE_ATOMIC_CMPSET_64 |
| 156 | +#define OPAL_ATOMIC_CMPSET_PTR(x, y, z) \ |
| 157 | + (opal_using_threads() ? opal_atomic_cmpset_ptr(x, y, z) : OPAL_CMPSET(x, y, z)) |
| 158 | +#endif |
| 159 | + |
| 160 | +static inline void *opal_thread_swap_ptr (volatile void *ptr, void *newvalue) |
| 161 | +{ |
| 162 | + if (opal_using_threads ()) { |
| 163 | + return opal_atomic_swap_ptr (ptr, newvalue); |
| 164 | + } |
| 165 | + |
| 166 | + void *old = ((void **) ptr)[0]; |
| 167 | + ((void **) ptr)[0] = newvalue; |
| 168 | + |
| 169 | + return old; |
| 170 | +} |
| 171 | + |
| 172 | +#define OPAL_ATOMIC_SWAP_PTR(x, y) opal_thread_swap_ptr (x, y) |
| 173 | + |
| 174 | +#endif /* !defined(OPAL_THREAD_USAGE_H) */ |
0 commit comments