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/op/op.h"
19 #include "oshmem/mca/atomic/atomic.h"
20 
21 /*
22  * These routines perform an atomic increment operation on a remote data object.
23  * The atomic increment routines replace the value of target with its value incremented by
24  * one. The operation must be completed without the possibility of another process updating
25  * target between the time of the fetch and the update.
26  */
27 #define DO_SHMEM_TYPE_ATOMIC_INC(ctx, type_name, type, target, pe) do { \
28         int rc = OSHMEM_SUCCESS;                                    \
29         size_t size = 0;                                            \
30         type value = 1;                                             \
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(add(                                   \
38             ctx,                                                    \
39             (void*)target,                                          \
40             value,                                                  \
41             size,                                                   \
42             pe));                                                   \
43         RUNTIME_CHECK_RC(rc);                                       \
44     } while (0)
45 
46 #define SHMEM_CTX_TYPE_ATOMIC_INC(type_name, type, prefix)          \
47     void prefix##_ctx##type_name##_atomic_inc(shmem_ctx_t ctx, type *target, int pe) \
48     {                                                               \
49         DO_SHMEM_TYPE_ATOMIC_INC(ctx, type_name, type, target, pe); \
50         return ;                                                    \
51     }
52 
53 #define SHMEM_TYPE_ATOMIC_INC(type_name, type, prefix)              \
54     void prefix##type_name##_atomic_inc(type *target, int pe)       \
55     {                                                               \
56         DO_SHMEM_TYPE_ATOMIC_INC(oshmem_ctx_default, type_name,     \
57                                  type, target, pe);                 \
58         return ;                                                    \
59     }
60 
61 #if OSHMEM_PROFILING
62 #include "oshmem/include/pshmem.h"
63 #pragma weak shmem_ctx_int_atomic_inc = pshmem_ctx_int_atomic_inc
64 #pragma weak shmem_ctx_long_atomic_inc = pshmem_ctx_long_atomic_inc
65 #pragma weak shmem_ctx_longlong_atomic_inc = pshmem_ctx_longlong_atomic_inc
66 #pragma weak shmem_ctx_uint_atomic_inc = pshmem_ctx_uint_atomic_inc
67 #pragma weak shmem_ctx_ulong_atomic_inc = pshmem_ctx_ulong_atomic_inc
68 #pragma weak shmem_ctx_ulonglong_atomic_inc = pshmem_ctx_ulonglong_atomic_inc
69 
70 #pragma weak shmem_int_atomic_inc = pshmem_int_atomic_inc
71 #pragma weak shmem_long_atomic_inc = pshmem_long_atomic_inc
72 #pragma weak shmem_longlong_atomic_inc = pshmem_longlong_atomic_inc
73 #pragma weak shmem_uint_atomic_inc = pshmem_uint_atomic_inc
74 #pragma weak shmem_ulong_atomic_inc = pshmem_ulong_atomic_inc
75 #pragma weak shmem_ulonglong_atomic_inc = pshmem_ulonglong_atomic_inc
76 
77 #pragma weak shmem_int_inc = pshmem_int_inc
78 #pragma weak shmem_long_inc = pshmem_long_inc
79 #pragma weak shmem_longlong_inc = pshmem_longlong_inc
80 
81 #pragma weak shmemx_int32_inc = pshmemx_int32_inc
82 #pragma weak shmemx_int64_inc = pshmemx_int64_inc
83 #include "oshmem/shmem/c/profile/defines.h"
84 #endif
85 
86 SHMEM_CTX_TYPE_ATOMIC_INC(_int, int, shmem)
87 SHMEM_CTX_TYPE_ATOMIC_INC(_long, long, shmem)
88 SHMEM_CTX_TYPE_ATOMIC_INC(_longlong, long long, shmem)
89 SHMEM_CTX_TYPE_ATOMIC_INC(_uint, unsigned int, shmem)
90 SHMEM_CTX_TYPE_ATOMIC_INC(_ulong, unsigned long, shmem)
91 SHMEM_CTX_TYPE_ATOMIC_INC(_ulonglong, unsigned long long, shmem)
92 SHMEM_TYPE_ATOMIC_INC(_int, int, shmem)
93 SHMEM_TYPE_ATOMIC_INC(_long, long, shmem)
94 SHMEM_TYPE_ATOMIC_INC(_longlong, long long, shmem)
95 SHMEM_TYPE_ATOMIC_INC(_uint, unsigned int, shmem)
96 SHMEM_TYPE_ATOMIC_INC(_ulong, unsigned long, shmem)
97 SHMEM_TYPE_ATOMIC_INC(_ulonglong, unsigned long long, shmem)
98 
99 #define SHMEM_TYPE_INC(type_name, type, prefix)                     \
100     void prefix##type_name##_inc(type *target, int pe)              \
101     {                                                               \
102         DO_SHMEM_TYPE_ATOMIC_INC(oshmem_ctx_default, type_name,     \
103                                  type, target, pe);                 \
104         return ;                                                    \
105     }
106 
107 SHMEM_TYPE_INC(_int, int, shmem)
108 SHMEM_TYPE_INC(_long, long, shmem)
109 SHMEM_TYPE_INC(_longlong, long long, shmem)
110 SHMEM_TYPE_INC(_int32, int32_t, shmemx)
111 SHMEM_TYPE_INC(_int64, int64_t, shmemx)
112