root/oshmem/shmem/c/shmem_cswap.c

/* [<][>][^][v][top][bottom][index][help] */
   1 /*
   2  * Copyright (c) 2013      Mellanox Technologies, Inc.
   3  *                         All rights reserved.
   4  * $COPYRIGHT$
   5  *
   6  * Additional copyrights may follow
   7  *
   8  * $HEADER$
   9  */
  10 #include "oshmem_config.h"
  11 
  12 #include "oshmem/constants.h"
  13 #include "oshmem/include/shmem.h"
  14 #include "oshmem/include/shmemx.h"
  15 
  16 #include "oshmem/runtime/runtime.h"
  17 
  18 #include "oshmem/mca/atomic/atomic.h"
  19 
  20 /*
  21  * shmem_cswap performs an atomic conditional swap operation.
  22  * The conditional swap routines write value to address target on PE pe, and return the previous
  23  * contents of target. The replacement must occur only if cond is equal to target;
  24  * otherwise target is left unchanged. In either case, the routine must return the initial value
  25  * of target. The operation must be completed without the possibility of another process updating
  26  * target between the time of the fetch and the update.
  27  */
  28 #define DO_SHMEM_TYPE_ATOMIC_COMPARE_SWAP(ctx, type, target, cond, value, pe, out_value) do { \
  29         int rc = OSHMEM_SUCCESS;                                    \
  30         size_t size = 0;                                            \
  31                                                                     \
  32         RUNTIME_CHECK_INIT();                                       \
  33         RUNTIME_CHECK_PE(pe);                                       \
  34         RUNTIME_CHECK_ADDR(target);                                 \
  35                                                                     \
  36         size = sizeof(value);                                       \
  37         rc = MCA_ATOMIC_CALL(cswap(                                 \
  38             ctx,                                                    \
  39             (void*)target,                                          \
  40             (uint64_t*)&out_value,                                  \
  41             OSHMEM_ATOMIC_PTR_2_INT(&cond, sizeof(cond)),           \
  42             OSHMEM_ATOMIC_PTR_2_INT(&value, sizeof(value)),         \
  43             size,                                                   \
  44             pe));                                                   \
  45         RUNTIME_CHECK_RC(rc);                                       \
  46     } while (0)
  47 
  48 #define SHMEM_CTX_TYPE_ATOMIC_COMPARE_SWAP(type_name, type, prefix) \
  49     type prefix##_ctx##type_name##_atomic_compare_swap(shmem_ctx_t ctx, type *target, type cond, type value, int pe) \
  50     {                                                               \
  51         type out_value;                                             \
  52         DO_SHMEM_TYPE_ATOMIC_COMPARE_SWAP(ctx, type, target, cond, value,  \
  53                                    pe, out_value);                  \
  54         return out_value;                                           \
  55     }
  56 
  57 #define SHMEM_TYPE_ATOMIC_COMPARE_SWAP(type_name, type, prefix)     \
  58     type prefix##type_name##_atomic_compare_swap(type *target, type cond, type value, int pe) \
  59     {                                                               \
  60         type out_value;                                             \
  61         DO_SHMEM_TYPE_ATOMIC_COMPARE_SWAP(oshmem_ctx_default, type, target, \
  62                                    cond, value, pe, out_value);     \
  63         return out_value;                                           \
  64     }
  65 
  66 #if OSHMEM_PROFILING
  67 #include "oshmem/include/pshmem.h"
  68 #pragma weak shmem_ctx_uint_atomic_compare_swap = pshmem_ctx_uint_atomic_compare_swap
  69 #pragma weak shmem_ctx_ulong_atomic_compare_swap = pshmem_ctx_ulong_atomic_compare_swap
  70 #pragma weak shmem_ctx_ulonglong_atomic_compare_swap = pshmem_ctx_ulonglong_atomic_compare_swap
  71 #pragma weak shmem_ctx_int_atomic_compare_swap = pshmem_ctx_int_atomic_compare_swap
  72 #pragma weak shmem_ctx_long_atomic_compare_swap = pshmem_ctx_long_atomic_compare_swap
  73 #pragma weak shmem_ctx_longlong_atomic_compare_swap = pshmem_ctx_longlong_atomic_compare_swap
  74 
  75 #pragma weak shmem_int_atomic_compare_swap = pshmem_int_atomic_compare_swap
  76 #pragma weak shmem_long_atomic_compare_swap = pshmem_long_atomic_compare_swap
  77 #pragma weak shmem_longlong_atomic_compare_swap = pshmem_longlong_atomic_compare_swap
  78 #pragma weak shmem_uint_atomic_compare_swap = pshmem_uint_atomic_compare_swap
  79 #pragma weak shmem_ulong_atomic_compare_swap = pshmem_ulong_atomic_compare_swap
  80 #pragma weak shmem_ulonglong_atomic_compare_swap = pshmem_ulonglong_atomic_compare_swap
  81 
  82 #pragma weak shmem_int_cswap = pshmem_int_cswap
  83 #pragma weak shmem_long_cswap = pshmem_long_cswap
  84 #pragma weak shmem_longlong_cswap = pshmem_longlong_cswap
  85 
  86 #pragma weak shmemx_int32_cswap = pshmemx_int32_cswap
  87 #pragma weak shmemx_int64_cswap = pshmemx_int64_cswap
  88 #include "oshmem/shmem/c/profile/defines.h"
  89 #endif
  90 
  91 SHMEM_CTX_TYPE_ATOMIC_COMPARE_SWAP(_int, int, shmem)
  92 SHMEM_CTX_TYPE_ATOMIC_COMPARE_SWAP(_long, long, shmem)
  93 SHMEM_CTX_TYPE_ATOMIC_COMPARE_SWAP(_longlong, long long, shmem)
  94 SHMEM_CTX_TYPE_ATOMIC_COMPARE_SWAP(_uint, unsigned int, shmem)
  95 SHMEM_CTX_TYPE_ATOMIC_COMPARE_SWAP(_ulong, unsigned long, shmem)
  96 SHMEM_CTX_TYPE_ATOMIC_COMPARE_SWAP(_ulonglong, unsigned long long, shmem)
  97 SHMEM_TYPE_ATOMIC_COMPARE_SWAP(_int, int, shmem)
  98 SHMEM_TYPE_ATOMIC_COMPARE_SWAP(_long, long, shmem)
  99 SHMEM_TYPE_ATOMIC_COMPARE_SWAP(_longlong, long long, shmem)
 100 SHMEM_TYPE_ATOMIC_COMPARE_SWAP(_uint, unsigned int, shmem)
 101 SHMEM_TYPE_ATOMIC_COMPARE_SWAP(_ulong, unsigned long, shmem)
 102 SHMEM_TYPE_ATOMIC_COMPARE_SWAP(_ulonglong, unsigned long long, shmem)
 103 
 104 /* deprecated APIs */
 105 #define SHMEM_TYPE_CSWAP(type_name, type, prefix)                   \
 106     type prefix##type_name##_cswap(type *target, type cond, type value, int pe) \
 107     {                                                               \
 108         type out_value;                                             \
 109         DO_SHMEM_TYPE_ATOMIC_COMPARE_SWAP(oshmem_ctx_default, type, target, \
 110                                    cond, value, pe, out_value);     \
 111         return out_value;                                           \
 112     }
 113 
 114 SHMEM_TYPE_CSWAP(_int, int, shmem)
 115 SHMEM_TYPE_CSWAP(_long, long, shmem)
 116 SHMEM_TYPE_CSWAP(_longlong, long long, shmem)
 117 SHMEM_TYPE_CSWAP(_int32, int32_t, shmemx)
 118 SHMEM_TYPE_CSWAP(_int64, int64_t, shmemx)
 119 

/* [<][>][^][v][top][bottom][index][help] */