File: atomics.cl

package info (click to toggle)
pocl 6.0-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 25,304 kB
  • sloc: lisp: 149,513; ansic: 103,778; cpp: 54,947; python: 1,513; sh: 949; ruby: 255; pascal: 226; tcl: 180; makefile: 173; java: 72; xml: 49
file content (312 lines) | stat: -rw-r--r-- 7,288 bytes parent folder | download | duplicates (2)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* OpenCL built-in library: atomic operations

   Copyright (c) 2012 Universidad Rey Juan Carlos
   
   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:
   
   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.
   
   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   THE SOFTWARE.
*/

/*
This file implements old-style (pre OpenCL 2.0) atomics using 3.0 atomics.

Note: as explained in https://github.com/KhronosGroup/OpenCL-Docs/issues/353:

"In atomics prior to OpenCL 2.0 there was no definition of memory ordering and
scope and therefore old atomics behaved in implementation defined manner wrt
to these."

The reason for using explicit versions of 3.0 atomics below, is that
non-explicit versions require support for generic AS, memory_order_seq_cst, and
device-scope, while the explicit versions only require support for
device-scope.
*/

// Repeat the content of this file several times with different values
// for Q, T, and U:
#if !defined(Q)

#  define Q __global
#  include "atomics.cl"
#  undef Q

#  define Q __local
#  include "atomics.cl"
#  undef Q

#ifdef __opencl_c_generic_address_space
#  define Q __generic
#  include "atomics.cl"
#  undef Q
#endif


#elif !defined(T)

#  define T int
#  include "atomics.cl"
#  undef T

#  define T uint
#  include "atomics.cl"
#  undef T

#ifdef cl_khr_int64_base_atomics
#  define T long
#  include "atomics.cl"
#  undef T

#  define T ulong
#  include "atomics.cl"
#  undef T
#endif

// add & xchg is also supported for float & double as a special case
__attribute__((overloadable))
float atomic_xchg(volatile Q float *p, float val)
{
  int retval = atomic_xchg ((volatile Q int *)p, as_int(val));
  return as_float(retval);
}

__attribute__ ((overloadable)) float
atomic_add (volatile Q float *p, float val)
{
  union
  {
    uint u32;
    float f32;
  } next, expected, current;
  current.f32 = *p;

  do
    {
      expected.f32 = current.f32;
      next.f32 = expected.f32 + val;
      current.u32
          = atomic_cmpxchg ((volatile Q unsigned *)p, expected.u32, next.u32);
    }
  while (current.u32 != expected.u32);
  return current.f32;
}

__attribute__ ((overloadable)) float
atom_xchg (volatile Q float *p, float val)
{
  return atomic_xchg (p, val);
}

__attribute__ ((overloadable)) float
atom_add (volatile Q float *p, float val)
{
  return atomic_add (p, val);
}

#ifdef cl_khr_int64_base_atomics

__attribute__ ((overloadable)) double
atomic_xchg (volatile Q double *p, double val)
{
  ulong retval = atomic_xchg ((volatile Q ulong *)p, as_ulong (val));
  return as_double (retval);
}

__attribute__ ((overloadable)) double
atomic_add (volatile Q double *p, double val)
{
  union
  {
    ulong u64;
    double f64;
  } next, expected, current;
  current.f64 = *p;

  do
    {
      expected.f64 = current.f64;
      next.f64 = expected.f64 + val;
      current.u64
          = atomic_cmpxchg ((volatile Q ulong *)p, expected.u64, next.u64);
    }
  while (current.u64 != expected.u64);
  return current.f64;
}

__attribute__ ((overloadable)) double
atom_xchg (volatile Q double *p, double val)
{
  return atomic_xchg (p, val);
}

__attribute__ ((overloadable)) double
atom_add (volatile Q double *p, double val)
{
  return atomic_add (p, val);
}

#endif

#else

// basic

// read, add, store
__attribute__((overloadable))
T atomic_add(volatile Q T *p, T val)
{
  return __atomic_fetch_add (p, val, __ATOMIC_ACQ_REL);
}

__attribute__ ((overloadable)) T
atom_add (volatile Q T *p, T val)
{
  return __atomic_fetch_add (p, val, __ATOMIC_ACQ_REL);
}

// read, subtract, store
__attribute__((overloadable))
T atomic_sub(volatile Q T *p, T val)
{
  return __atomic_fetch_sub (p, val, __ATOMIC_ACQ_REL);
}

__attribute__ ((overloadable)) T
atom_sub (volatile Q T *p, T val)
{
  return __atomic_fetch_sub (p, val, __ATOMIC_ACQ_REL);
}

// read, increment, store
__attribute__((overloadable))
T atomic_inc(volatile Q T *p)
{
  return __atomic_fetch_add (p, (T)1, __ATOMIC_ACQ_REL);
}

__attribute__ ((overloadable)) T
atom_inc (volatile Q T *p)
{
  return __atomic_fetch_add (p, (T)1, __ATOMIC_ACQ_REL);
}

// read, decrement, store
__attribute__((overloadable))
T atomic_dec(volatile Q T *p)
{
  return __atomic_fetch_sub (p, (T)1, __ATOMIC_ACQ_REL);
}

__attribute__ ((overloadable)) T
atom_dec (volatile Q T *p)
{
  return __atomic_fetch_sub (p, (T)1, __ATOMIC_ACQ_REL);
}

__attribute__ ((overloadable)) T
atomic_and (volatile Q T *p, T val)
{
  return __atomic_fetch_and (p, val, __ATOMIC_ACQ_REL);
}

__attribute__ ((overloadable)) T
atom_and (volatile Q T *p, T val)
{
  return __atomic_fetch_and (p, val, __ATOMIC_ACQ_REL);
}

__attribute__ ((overloadable)) T
atomic_or (volatile Q T *p, T val)
{
  return __atomic_fetch_or (p, val, __ATOMIC_ACQ_REL);
}

__attribute__ ((overloadable)) T
atom_or (volatile Q T *p, T val)
{
  return __atomic_fetch_or (p, val, __ATOMIC_ACQ_REL);
}

__attribute__ ((overloadable)) T
atomic_xor (volatile Q T *p, T val)
{
  return __atomic_fetch_xor (p, val, __ATOMIC_ACQ_REL);
}

__attribute__ ((overloadable)) T
atom_xor (volatile Q T *p, T val)
{
  return __atomic_fetch_xor (p, val, __ATOMIC_ACQ_REL);
}

/**********************************************************************/

// read, swap, store
__attribute__ ((overloadable)) T
atomic_xchg (volatile Q T *p, T val)
{
  return __atomic_exchange_n (p, val, __ATOMIC_ACQ_REL);
}

__attribute__ ((overloadable)) T
atom_xchg (volatile Q T *p, T val)
{
  return __atomic_exchange_n (p, val, __ATOMIC_ACQ_REL);
}

// read, store
__attribute__((overloadable))
T atomic_cmpxchg(volatile Q T *p, T cmp, T val)
{
  __atomic_compare_exchange_n (p, &cmp, val, false, __ATOMIC_ACQ_REL,
                               __ATOMIC_ACQUIRE);
  return cmp;
}

__attribute__ ((overloadable)) T
atom_cmpxchg (volatile Q T *p, T cmp, T val)
{
  __atomic_compare_exchange_n (p, &cmp, val, false, __ATOMIC_ACQ_REL,
                               __ATOMIC_ACQUIRE);
  return cmp;
}

__attribute__ ((overloadable)) T
atomic_min (volatile Q T *p, T val)
{
  return __atomic_fetch_min (p, val, __ATOMIC_ACQ_REL);
}

__attribute__ ((overloadable)) T
atomic_max (volatile Q T *p, T val)
{
  return __atomic_fetch_max (p, val, __ATOMIC_ACQ_REL);
}


__attribute__ ((overloadable)) T
atom_min (volatile Q T *p, T val)
{
  return atomic_min (p, val);
}

__attribute__ ((overloadable)) T
atom_max (volatile Q T *p, T val)
{
  return atomic_max (p, val);
}

#endif