File: GB_atomics.h

package info (click to toggle)
suitesparse-graphblas 7.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 67,112 kB
  • sloc: ansic: 1,072,243; cpp: 8,081; sh: 512; makefile: 506; asm: 369; python: 125; awk: 10
file content (496 lines) | stat: -rw-r--r-- 19,636 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//------------------------------------------------------------------------------
// GB_atomics.h: definitions for atomic operations
//------------------------------------------------------------------------------

// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

//------------------------------------------------------------------------------

// All atomic operations used by SuiteSparse:GraphBLAS appear in this file.

// These atomic operations assume either an ANSI C11 compiler that supports
// OpenMP 3.1 or later, or Microsoft Visual Studio on 64-bit Windows (which
// only supports OpenMP 2.0).  SuiteSparse:GraphBLAS is not supported on 32-bit
// Windows.

#ifndef GB_ATOMICS_H
#define GB_ATOMICS_H
#include "GB.h"

//------------------------------------------------------------------------------
// atomic updates
//------------------------------------------------------------------------------

// Whenever possible, the OpenMP pragma is used with a clause (as introduced in
// OpenMP 3.1), as follow:
//
//      #pragma omp atomic [clause]
//
// where [clause] is read, write, update, or capture.
//
// Microsoft Visual Studio only supports OpenMP 2.0, which does not have the
// [clause].  Without the [clause], #pragma omp atomic is like 
// #pragma omp atomic update, but the expression can only be one of:
//      
//      x binop= expression
//      x++
//      ++x
//      x--
//      --x
//
// where binop is one of these operators:    + * - / & ^ | << >> 
//
// OpenMP 3.0 and later support additional options for the "update" clause,
// but SuiteSparse:GraphBLAS uses only this form:
//
//      x binop= expression
//
// where binop is:  + * & ^ |
//
// This atomic update is used for the PLUS, TIMES, LAND, LXOR, and LOR monoids,
// when applied to the built-in types.  For PLUS and TIMES, these are the 10
// types INTx, UINTx, FP32, FP64 (for x = 8, 16, 32, and 64).  For the boolean
// monoids, only the BOOL type is used.
//
// As a result, the atomic updates are the same for gcc and icc (which support
// OpenMP 3.0 or later) with the "update" clause.  For MS Visual Studio, the
// "update" clause is removed since it supports OpenMP 2.0.

#if ( _OPENMP >= 201307 )

    // OpenMP 4.0 or later
    // #define GB_ATOMIC_UPDATE GB_PRAGMA (omp atomic update seq_cst)
    #define GB_ATOMIC_UPDATE GB_PRAGMA (omp atomic update)

#elif ( _OPENMP >= 201107 )

    // OpenMP 3.1
    #define GB_ATOMIC_UPDATE GB_PRAGMA (omp atomic update)

#elif ( _OPENMP >= 199810 )

    // OpenMP 1.0 to 3.0: no optional clauses, "update" is assumed
    #define GB_ATOMIC_UPDATE GB_PRAGMA (omp atomic)

#else

    // no OpenMP at all
    #define GB_ATOMIC_UPDATE

#endif

//------------------------------------------------------------------------------
// atomic read and write
//------------------------------------------------------------------------------

// x86_64: no atomic read/write is needed.

// ARM, Power8/9, and others need the explicit atomic read/write.

// In Microsoft Visual Studio, simple reads and writes to properly aligned
// 64-bit values are already atomic on 64-bit Windows for any architecture
// supported by Windows (any Intel or ARM architecture). See:
// https://docs.microsoft.com/en-us/windows/win32/sync/interlocked-variable-access
// SuiteSparse:GraphBLAS is not supported on 32-bit Windows.  Thus, there
// is no need for atomic reads/writes when compiling GraphBLAS on Windows
// with MS Visual Studio.

#if GBX86

    // x86_64: no atomic read/write is needed.
    #define GB_ATOMIC_READ
    #define GB_ATOMIC_WRITE

#elif ( _OPENMP >= 201107 )

    // OpenMP 3.1 and later have atomic reads and writes
    #define GB_ATOMIC_READ    GB_PRAGMA (omp atomic read)
    #define GB_ATOMIC_WRITE   GB_PRAGMA (omp atomic write)

#else

    // OpenMP 3.0 or earlier, or no OpenMP at all
    #define GB_ATOMIC_READ
    #define GB_ATOMIC_WRITE

#endif

//------------------------------------------------------------------------------
// flush
//------------------------------------------------------------------------------

#if defined ( _OPENMP )

    // All versions of OpenMP have the #pragma omp flush
    #define GB_OMP_FLUSH GB_PRAGMA (omp flush)

#else

    // no OpenMP at all
    #define GB_OMP_FLUSH

#endif

//------------------------------------------------------------------------------
// atomic capture
//------------------------------------------------------------------------------

// An atomic capture loads the prior value of the target into a thread-local
// result, and then overwrites the target with the new value.  The target is a
// value that is shared between threads.  The value and result arguments are
// thread-local.  SuiteSparse:GraphBLAS uses four atomic capture methods,
// defined below, of the form:
//
//      { result = target ; target = value ; }      for int64_t and int8_t
//      { result = target ; target |= value ; }     for int64_t
//      { result = target++ ; }                     for int64_t
//
// OpenMP 3.1 and later supports atomic captures with a "capture" clause: 
//
//      #pragma omp atomic capture
//      { result = target ; target = value ; }
//
// or with a binary operator
//
//      #pragma omp atomic capture
//      { result = target ; target binop= value ; }
//
// MS Visual Studio supports only OpenMP 2.0, and does not support any
// "capture" clause.  Thus, on Windows, _InterlockedExchange* and
// _InterlockedOr* functions are used instead, as described here:
//
// https://docs.microsoft.com/en-us/cpp/intrinsics/interlockedexchange-intrinsic-functions
// https://docs.microsoft.com/en-us/cpp/intrinsics/interlockedor-intrinsic-functions

#if ( _OPENMP >= 201307 )

    // OpenMP 4.0 or later
    // #define GB_ATOMIC_CAPTURE GB_PRAGMA (omp atomic capture seq_cst)
    #define GB_ATOMIC_CAPTURE GB_PRAGMA (omp atomic capture)

#elif ( _OPENMP >= 201107 )

    // OpenMP 3.1
    #define GB_ATOMIC_CAPTURE GB_PRAGMA (omp atomic capture)

#elif ( _OPENMP >= 199810 )

    // OpenMP 1.0 to 3.0: generate an intentional compile-time error if any
    // attempt is made to use the atomic capture.
    #define GB_ATOMIC_CAPTURE atomic capture not available

#else

    // no OpenMP at all
    #define GB_ATOMIC_CAPTURE

#endif

    //--------------------------------------------------------------------------
    // atomic capture for int64_t
    //--------------------------------------------------------------------------

    // int64_t result, target, value ;
    // do this atomically: { result = target ; target = value ; }

    #if GB_COMPILER_MSC

        #define GB_ATOMIC_CAPTURE_INT64(result, target, value)          \
        {                                                               \
            result = _InterlockedExchange64                             \
                ((int64_t volatile *) (&(target)), value) ;             \
        }

    #else

        #define GB_ATOMIC_CAPTURE_INT64(result, target, value)          \
        {                                                               \
            GB_ATOMIC_CAPTURE                                           \
            {                                                           \
                result = target ;                                       \
                target = value ;                                        \
            }                                                           \
        }

    #endif

    //--------------------------------------------------------------------------
    // atomic capture for int8_t
    //--------------------------------------------------------------------------

    // int8_t result, target, value ;
    // do this atomically: { result = target ; target = value ; }

    #if GB_COMPILER_MSC

        #define GB_ATOMIC_CAPTURE_INT8(result, target, value)           \
        {                                                               \
            result = _InterlockedExchange8                              \
                ((char volatile *) &(target), value) ;                  \
        }

    #else

        #define GB_ATOMIC_CAPTURE_INT8(result, target, value)           \
        {                                                               \
            GB_ATOMIC_CAPTURE                                           \
            {                                                           \
                result = target ;                                       \
                target = value ;                                        \
            }                                                           \
        }

    #endif

    //--------------------------------------------------------------------------
    // atomic capture with bitwise OR, for int64_t
    //--------------------------------------------------------------------------

    // int64_t result, target, value ;
    // do this atomically: { result = target ; target |= value ; }

    #if GB_COMPILER_MSC

        #define GB_ATOMIC_CAPTURE_INT64_OR(result, target, value)       \
        {                                                               \
            result = _InterlockedOr64                                   \
                ((int64_t volatile *) (&(target)), value) ;             \
        }

    #else

        #define GB_ATOMIC_CAPTURE_INT64_OR(result, target, value)       \
        {                                                               \
            GB_ATOMIC_CAPTURE                                           \
            {                                                           \
                result = target ;                                       \
                target |= value ;                                       \
            }                                                           \
        }

    #endif

    //--------------------------------------------------------------------------
    // atomic post-increment
    //--------------------------------------------------------------------------

    // Increment an int64_t value and return the value prior to being
    // incremented:
    //
    //      int64_t result = target++ ;
    //
    // See
    // https://docs.microsoft.com/en-us/cpp/intrinsics/interlockedincrement-intrinsic-functions?view=msvc-160
    // The MS Visual Studio version computes result = ++target, so result must
    // be decremented by one.

    #if GB_COMPILER_MSC

        #define GB_ATOMIC_CAPTURE_INC64(result,target)                  \
        {                                                               \
            result = _InterlockedIncrement64                            \
                ((int64_t volatile *) (&(target))) - 1 ;                \
        }

    #else

        #define GB_ATOMIC_CAPTURE_INC64(result,target)                  \
        {                                                               \
            GB_ATOMIC_CAPTURE                                           \
            result = (target)++ ;                                       \
        }

    #endif

//------------------------------------------------------------------------------
// atomic compare-and-exchange
//------------------------------------------------------------------------------

// Atomic compare-and-exchange is used to implement the MAX, MIN and EQ
// monoids, for the fine-grain saxpy-style matrix multiplication.  Ideally,
// OpenMP would be used for these atomic operation, but they are not supported.
// So compiler-specific functions are used instead.

// In gcc, icc, and clang, the atomic compare-and-exchange function
// __atomic_compare_exchange computes the following, as a single atomic
// operation, where type_t is any 8, 16, 32, or 64 bit scalar type.  In
// SuiteSparse:GraphBLAS, type_t can be bool, int8_t, uint8_t, int16_t,
// uint16_t, int32_t, uint32_t, int64_t, uint64_t, float, or double.
//
//      bool __atomic_compare_exchange
//      (
//          type_t *target,         // input/output
//          type_t *expected,       // input/output
//          type_t *desired,        // input only, even though it is a pointer
//          bool weak,              // true, for SuiteSparse:GraphBLAS
//          int success_memorder,   // __ATOMIC_SEQ_CST is used here
//          int failure_memorder    // __ATOMIC_SEQ_CST is used here
//      )
//      {
//          bool result ;
//          if (*target == *expected)
//          {
//              *target = *desired ;
//              result = true ;
//          }
//          else
//          {
//              *expected = *target ;
//              result = false ;
//          }
//          return (result) ;
//      }
//
// The generic __atomic_compare_exchange function in gcc (also supported by
// icc) computes the above for any of these 8, 16, 32, or 64-bit scalar types
// needed in SuiteSparse:GraphBLAS.  SuiteSparse:GraphBLAS does not require the
// 'expected = target' assignment if the result is false.  It ignores the
// value of 'expected' after the operation completes.   The target, expected,
// and desired parameters are all provided as pointers:
//
// See https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html

// Microsoft Visual Studio provides similar but not identical functionality in
// the _InterlockedCompareExchange functions, but they are named differently
// for different types.  Only int8_t, int16_t, int32_t, and int64_t types are
// supported.  For the int64_t case, the following is performed atomically:
//
//      int64_t _InterlockedCompareExchange64
//      (
//          int64_t volatile *target,       // input/output
//          int64_t desired                 // input only
//          int64_t expected
//      )
//      {
//          int64_t result = *target ;
//          if (*target == expected)
//          {
//              target = desired ;
//          }
//          return (result) ;
//      }
//
// It does not assign "expected = target" if the test is false, but
// SuiteSparse:GraphBLAS does not require this action.  It does not return a
// boolean result, but instead returns the original value of (*target).
// However, this can be compared with the expected value to obtain the
// same boolean result as __atomic_compare_exchange.
//
// Type punning is used to extend these signed integer types to unsigned
// integers of the same number of bytes, and to float and double.

//------------------------------------------------------------------------------
// GB_PUN: type punning
//------------------------------------------------------------------------------

// With type punning, a value is treated as a different type, but with no
// typecasting.  The address of the variable is first typecasted to a (type *)
// pointer, and then the pointer is dereferenced.

#define GB_PUN(type,value) (*((type *) (&(value))))

#if GB_COMPILER_MSC

    //--------------------------------------------------------------------------
    // compare/exchange for MS Visual Studio
    //--------------------------------------------------------------------------

    // bool, int8_t, and uint8_t
    #define GB_ATOMIC_COMPARE_EXCHANGE_8(target, expected, desired)         \
    (                                                                       \
        GB_PUN (int8_t, expected) ==                                        \
            _InterlockedCompareExchange8 ((int8_t volatile *) (target),     \
                GB_PUN (int8_t, desired), GB_PUN (int8_t, expected))        \
    )

    // int16_t and uint16_t
    #define GB_ATOMIC_COMPARE_EXCHANGE_16(target, expected, desired)        \
    (                                                                       \
        GB_PUN (int16_t, expected) ==                                       \
            _InterlockedCompareExchange16 ((int16_t volatile *) (target),   \
            GB_PUN (int16_t, desired), GB_PUN (int16_t, expected))          \
    )

    // float, int32_t, and uint32_t
    #define GB_ATOMIC_COMPARE_EXCHANGE_32(target, expected, desired)        \
    (                                                                       \
        GB_PUN (int32_t, expected) ==                                       \
            _InterlockedCompareExchange ((int32_t volatile *) (target),     \
            GB_PUN (int32_t, desired), GB_PUN (int32_t, expected))          \
    )

    // double, int64_t, and uint64_t
    #define GB_ATOMIC_COMPARE_EXCHANGE_64(target, expected, desired)        \
    (                                                                       \
        GB_PUN (int64_t, expected) ==                                       \
            _InterlockedCompareExchange64 ((int64_t volatile *) (target),   \
            GB_PUN (int64_t, desired), GB_PUN (int64_t, expected))          \
    )

#else

    //--------------------------------------------------------------------------
    // compare/exchange for gcc, icc, and clang on x86 and Power8/9
    //--------------------------------------------------------------------------

    #include <stdatomic.h>

#if 0

    // the compare/exchange function is generic for any type
    #define GB_ATOMIC_COMPARE_EXCHANGE_X(target, expected, desired)     \
        atomic_compare_exchange_weak (target, &expected, desired)

    // the compare/exchange function is generic for any type
//  #define GB_ATOMIC_COMPARE_EXCHANGE_X(target, expected, desired)     \
//      __atomic_compare_exchange (target, &expected, &desired,         \
//          true, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)                   \


    // bool, int8_t, and uint8_t
    #define GB_ATOMIC_COMPARE_EXCHANGE_8(target, expected, desired)     \
            GB_ATOMIC_COMPARE_EXCHANGE_X(target, expected, desired)

    // int16_t and uint16_t
    #define GB_ATOMIC_COMPARE_EXCHANGE_16(target, expected, desired)    \
            GB_ATOMIC_COMPARE_EXCHANGE_X (target, expected, desired)

    // float, int32_t, and uint32_t
    #define GB_ATOMIC_COMPARE_EXCHANGE_32(target, expected, desired)    \
            GB_ATOMIC_COMPARE_EXCHANGE_X (target, expected, desired)

    // double, int64_t, and uint64_t
    #define GB_ATOMIC_COMPARE_EXCHANGE_64(target, expected, desired)    \
            GB_ATOMIC_COMPARE_EXCHANGE_X (target, expected, desired)

#else

    // bool, int8_t, and uint8_t
    #define GB_ATOMIC_COMPARE_EXCHANGE_8(target, expected, desired)     \
        atomic_compare_exchange_weak                                    \
            ((volatile _Atomic uint8_t  *) (target),                    \
            (uint8_t  *) (&(expected)), GB_PUN (uint8_t , desired)) 

    // int16_t and uint16_t
    #define GB_ATOMIC_COMPARE_EXCHANGE_16(target, expected, desired)    \
        atomic_compare_exchange_weak                                    \
            ((volatile _Atomic uint16_t *) (target),                    \
            (uint16_t *) (&(expected)), GB_PUN (uint16_t, desired)) 

    // float, int32_t, and uint32_t
    #define GB_ATOMIC_COMPARE_EXCHANGE_32(target, expected, desired)    \
        atomic_compare_exchange_weak                                    \
            ((volatile _Atomic uint32_t *) (target),                    \
            (uint32_t *) (&(expected)), GB_PUN (uint32_t, desired)) 

    // double, int64_t, and uint64_t
    #define GB_ATOMIC_COMPARE_EXCHANGE_64(target, expected, desired)    \
        atomic_compare_exchange_weak                                    \
            ((volatile _Atomic uint64_t *) (target),                    \
            (uint64_t *) (&(expected)), GB_PUN (uint64_t, desired)) 

#endif
#endif
#endif