Blender  V2.93
util_atomic.h
Go to the documentation of this file.
1 /*
2  * Copyright 2014 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __UTIL_ATOMIC_H__
18 #define __UTIL_ATOMIC_H__
19 
20 #ifndef __KERNEL_GPU__
21 
22 /* Using atomic ops header from Blender. */
23 # include "atomic_ops.h"
24 
25 # define atomic_add_and_fetch_float(p, x) atomic_add_and_fetch_fl((p), (x))
26 # define atomic_compare_and_swap_float(p, old_val, new_val) \
27  atomic_cas_float((p), (old_val), (new_val))
28 
29 # define atomic_fetch_and_inc_uint32(p) atomic_fetch_and_add_uint32((p), 1)
30 # define atomic_fetch_and_dec_uint32(p) atomic_fetch_and_add_uint32((p), -1)
31 
32 # define CCL_LOCAL_MEM_FENCE 0
33 # define ccl_barrier(flags) ((void)0)
34 
35 #else /* __KERNEL_GPU__ */
36 
37 # ifdef __KERNEL_OPENCL__
38 
39 /* Float atomics implementation credits:
40  * http://suhorukov.blogspot.in/2011/12/opencl-11-atomic-operations-on-floating.html
41  */
42 ccl_device_inline float atomic_add_and_fetch_float(volatile ccl_global float *source,
43  const float operand)
44 {
45  union {
46  unsigned int int_value;
47  float float_value;
48  } new_value;
49  union {
50  unsigned int int_value;
51  float float_value;
52  } prev_value;
53  do {
54  prev_value.float_value = *source;
55  new_value.float_value = prev_value.float_value + operand;
56  } while (atomic_cmpxchg((volatile ccl_global unsigned int *)source,
57  prev_value.int_value,
58  new_value.int_value) != prev_value.int_value);
59  return new_value.float_value;
60 }
61 
63  const float old_val,
64  const float new_val)
65 {
66  union {
67  unsigned int int_value;
68  float float_value;
69  } new_value, prev_value, result;
70  prev_value.float_value = old_val;
71  new_value.float_value = new_val;
72  result.int_value = atomic_cmpxchg(
73  (volatile ccl_global unsigned int *)dest, prev_value.int_value, new_value.int_value);
74  return result.float_value;
75 }
76 
77 # define atomic_fetch_and_add_uint32(p, x) atomic_add((p), (x))
78 # define atomic_fetch_and_inc_uint32(p) atomic_inc((p))
79 # define atomic_fetch_and_dec_uint32(p) atomic_dec((p))
80 # define atomic_fetch_and_or_uint32(p, x) atomic_or((p), (x))
81 
82 # define CCL_LOCAL_MEM_FENCE CLK_LOCAL_MEM_FENCE
83 # define ccl_barrier(flags) barrier(flags)
84 
85 # endif /* __KERNEL_OPENCL__ */
86 
87 # ifdef __KERNEL_CUDA__
88 
89 # define atomic_add_and_fetch_float(p, x) (atomicAdd((float *)(p), (float)(x)) + (float)(x))
90 
91 # define atomic_fetch_and_add_uint32(p, x) atomicAdd((unsigned int *)(p), (unsigned int)(x))
92 # define atomic_fetch_and_sub_uint32(p, x) atomicSub((unsigned int *)(p), (unsigned int)(x))
93 # define atomic_fetch_and_inc_uint32(p) atomic_fetch_and_add_uint32((p), 1)
94 # define atomic_fetch_and_dec_uint32(p) atomic_fetch_and_sub_uint32((p), 1)
95 # define atomic_fetch_and_or_uint32(p, x) atomicOr((unsigned int *)(p), (unsigned int)(x))
96 
97 ccl_device_inline float atomic_compare_and_swap_float(volatile float *dest,
98  const float old_val,
99  const float new_val)
100 {
101  union {
102  unsigned int int_value;
103  float float_value;
104  } new_value, prev_value, result;
105  prev_value.float_value = old_val;
106  new_value.float_value = new_val;
107  result.int_value = atomicCAS((unsigned int *)dest, prev_value.int_value, new_value.int_value);
108  return result.float_value;
109 }
110 
111 # define CCL_LOCAL_MEM_FENCE
112 # define ccl_barrier(flags) __syncthreads()
113 
114 # endif /* __KERNEL_CUDA__ */
115 
116 #endif /* __KERNEL_GPU__ */
117 
118 #endif /* __UTIL_ATOMIC_H__ */
Provides wrapper around system-specific atomic primitives, and some extensions (faked-atomic operatio...
#define ccl_device_inline
#define ccl_global
#define atomic_compare_and_swap_float(p, old_val, new_val)
Definition: util_atomic.h:26
#define atomic_add_and_fetch_float(p, x)
Definition: util_atomic.h:25