File: GB_enumify_cuda_atomic.c

package info (click to toggle)
suitesparse 1%3A7.10.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 254,920 kB
  • sloc: ansic: 1,134,743; cpp: 46,133; makefile: 4,875; fortran: 2,087; java: 1,826; sh: 996; ruby: 725; python: 495; asm: 371; sed: 166; awk: 44
file content (313 lines) | stat: -rw-r--r-- 10,568 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
//------------------------------------------------------------------------------
// GB_enumify_cuda_atomic: enumify the CUDA atomic for a monoid
//------------------------------------------------------------------------------

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

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

#include "GB.h"
#include "jitifyer/GB_stringify.h"

bool GB_enumify_cuda_atomic         // returns has_cheeseburger
(
    // output:
    const char **a,                 // CUDA atomic function name
    bool *user_monoid_atomically,   // true if user monoid has an atomic update
    const char **cuda_type,         // CUDA atomic type
    // input:
    GrB_Monoid monoid,  // monoid to query
    GB_Opcode add_opcode,
    size_t zsize,       // ztype->size
    int zcode           // ztype->code
)
{ 

    // All built-in monoids are handled, except for the double complex cases of
    // ANY and TIMES.  Those need to be done the same way user-defined monoids
    // are computed.

    (*a) = NULL ;
    (*user_monoid_atomically) = false ;
    (*cuda_type) = NULL ;
    bool has_cheeseburger = false ;

    switch (add_opcode)
    {

        // user defined monoid: can apply GB_ADD via atomicCAS if the ztype has
        // 32 or 64 bits
//      case  0 :
        case GB_USER_binop_code  :  // user defined binary op

            (*user_monoid_atomically) =
                (zsize == sizeof (uint32_t) ||
                 zsize == sizeof (uint64_t))  ;
            break ;

        // FIRST, ANY, SECOND: atomic write (not double complex)
//      case  1 :
//      case  2 :
        case GB_ANY_binop_code   :  // z = x or y, selected arbitrarily

            switch (zcode)
            {
                case GB_BOOL_code    : 
                case GB_INT8_code    : 
                case GB_UINT8_code   : 
                case GB_INT16_code   : 
                case GB_UINT16_code  : 
                case GB_INT32_code   : 
                case GB_UINT32_code  : 
                case GB_INT64_code   : 
                case GB_UINT64_code  : 
                case GB_FP32_code    : 
                case GB_FP64_code    : 
                case GB_FC32_code    : (*a) = "GB_cuda_atomic_write" ;
                default              : break ;
            }
            break ;

        // MIN (real only)
//      case  3 :
//      case  4 :
//      case  5 :
        case GB_MIN_binop_code   :  // z = min(x,y)

            switch (zcode)
            {
                case GB_INT8_code    : 
                case GB_UINT8_code   : 
                case GB_INT16_code   : 
                case GB_UINT16_code  : 
                case GB_INT32_code   : 
                case GB_UINT32_code  : 
                case GB_INT64_code   : 
                case GB_UINT64_code  : 
                case GB_FP32_code    : 
                case GB_FP64_code    : (*a) = "GB_cuda_atomic_min" ;
                default              : break ;
            }
            break ;

        // MAX (real only)
//      case  6 :
//      case  7 :
//      case  8 :
        case GB_MAX_binop_code   :  // z = max(x,y)

            switch (zcode)
            {
                case GB_INT8_code    : 
                case GB_UINT8_code   : 
                case GB_INT16_code   : 
                case GB_UINT16_code  : 
                case GB_INT32_code   : 
                case GB_UINT32_code  : 
                case GB_INT64_code   : 
                case GB_UINT64_code  : 
                case GB_FP32_code    : 
                case GB_FP64_code    : (*a) = "GB_cuda_atomic_max" ;
                default              : break ;
            }
            break ;

        // PLUS:  all types
//      case  9 :
//      case 10 :
//      case 11 :
        case GB_PLUS_binop_code  :  // z = x + y

            switch (zcode)
            {
                case GB_INT8_code    : 
                case GB_UINT8_code   : 
                case GB_INT16_code   : 
                case GB_UINT16_code  : 
                case GB_INT32_code   : 
                case GB_UINT32_code  : 
                case GB_INT64_code   : 
                case GB_UINT64_code  : 
                case GB_FP32_code    : 
                case GB_FP64_code    : 
                case GB_FC32_code    : 
                case GB_FC64_code    : (*a) = "GB_cuda_atomic_add" ;
                default              : break ;
            }
            break ;

        // TIMES: all real types, and float complex (but not double complex)
//      case 12 : 
//      case 14 : 
        case GB_TIMES_binop_code :  // z = x * y

            switch (zcode)
            {
                case GB_INT8_code    : 
                case GB_UINT8_code   : 
                case GB_INT16_code   : 
                case GB_UINT16_code  : 
                case GB_INT32_code   : 
                case GB_UINT32_code  : 
                case GB_INT64_code   : 
                case GB_UINT64_code  : 
                case GB_FP32_code    : 
                case GB_FP64_code    : 
                case GB_FC32_code    : (*a) = "GB_cuda_atomic_times" ;
                default              : break ;
            }
            break ;

        // BOR: z = (x | y), bitwise or,
        // logical LOR (via upscale to uint32_t and BOR)
//      case 17 :
//      case 19 :
        case GB_LOR_binop_code   :  // z = (x != 0) || (y != 0)
        case GB_BOR_binop_code   :  // z = (x | y), bitwise or

            switch (zcode)
            {
                case GB_BOOL_code    : 
                case GB_UINT8_code   : 
                case GB_UINT16_code  : 
                case GB_UINT32_code  : 
                case GB_UINT64_code  : (*a) = "GB_cuda_atomic_bor" ;
                default              : break ;
            }
            break ;

        // BAND: z = (x & y), bitwise and
        // logical LAND (via upscale to uint32_t and BAND)
//      case 18 :
//      case 20 :
        case GB_BAND_binop_code  :  // z = (x & y), bitwise and
        case GB_LAND_binop_code  :  // z = (x != 0) && (y != 0)

            switch (zcode)
            {
                case GB_BOOL_code    : 
                case GB_UINT8_code   : 
                case GB_UINT16_code  : 
                case GB_UINT32_code  : 
                case GB_UINT64_code  : (*a) = "GB_cuda_atomic_band" ;
                default              : break ;
            }
            break ;

        // BXOR: z = (x ^ y), bitwise xor, and boolean LXOR
//      case 16 :
//      case 21 :
        case GB_LXOR_binop_code  :  // z = (x != 0) != (y != 0)
        case GB_BXOR_binop_code  :  // z = (x ^ y), bitwise xor

            switch (zcode)
            {
                case GB_BOOL_code    : 
                case GB_UINT8_code   : 
                case GB_UINT16_code  : 
                case GB_UINT32_code  : 
                case GB_UINT64_code  : (*a) = "GB_cuda_atomic_bxor" ;
                default              : break ;
            }
            break ;

        // BXNOR: z = ~(x ^ y), bitwise xnor, and boolean LXNOR
//      case 15 :
//      case 22 :
        case GB_EQ_binop_code    :  // z = (x == y), is LXNOR for bool
        case GB_BXNOR_binop_code :  // z = ~(x ^ y), bitwise xnor

            switch (zcode)
            {
                case GB_BOOL_code    : 
                case GB_UINT8_code   : 
                case GB_UINT16_code  : 
                case GB_UINT32_code  : 
                case GB_UINT64_code  : (*a) = "GB_cuda_atomic_bxnor" ;
                default              : break ;
            }
            break ;

        // all other monoids
        default: break ;
    }

    //--------------------------------------------------------------------------
    // has_cheeseburger:  true if monoid can be done atomically in CUDA
    //--------------------------------------------------------------------------

    if (monoid == NULL || zcode == 0)
    { 

        //----------------------------------------------------------------------
        // C is iso: no values computed so no need for any CUDA atomics
        //----------------------------------------------------------------------

        has_cheeseburger = false ;

    }
    else if (*user_monoid_atomically)
    { 

        //----------------------------------------------------------------------
        // user-defined monoid with a type of 32 or 64 bits
        //----------------------------------------------------------------------

        if (zsize == sizeof (uint32_t))
        {
            (*cuda_type) = "unsigned int" ;
        }
        else // if (zsize == sizeof (uint64_t))
        {
            (*cuda_type) = "unsigned long long int" ;
        }
        (*a) = "GB_cuda_atomic_user" ;
        has_cheeseburger = true ;

    }
    else if ((*a) == NULL)
    { 

        //----------------------------------------------------------------------
        // no CUDA atomic available
        //----------------------------------------------------------------------

        // either built-in (GxB_ANY_FC64_MONOID or GxB_TIMES_FC64_MONOID),
        // or user-defined where the type is not 32 or 64 bits in size

        has_cheeseburger = false ;

    }
    else
    { 

        //----------------------------------------------------------------------
        // CUDA atomic available for a built-in monoid
        //----------------------------------------------------------------------

        // upscale 8-bit and 16-bit types to 32-bits,
        // all others use their native types
        switch (zcode)
        {
            case GB_INT8_code    : 
            case GB_INT16_code   : 
            case GB_INT32_code   : (*cuda_type) = "int32_t"    ; break ;
            case GB_INT64_code   : (*cuda_type) = "int64_t"    ; break ;
            case GB_BOOL_code    : 
            case GB_UINT8_code   : 
            case GB_UINT16_code  : 
            case GB_UINT32_code  : (*cuda_type) = "uint32_t"   ; break ;
            case GB_UINT64_code  : (*cuda_type) = "uint64_t"   ; break ;
            case GB_FP32_code    : (*cuda_type) = "float"      ; break ;
            case GB_FP64_code    : (*cuda_type) = "double"     ; break ;
            case GB_FC32_code    : (*cuda_type) = "GxB_FC32_t" ; break ;
            case GB_FC64_code    : (*cuda_type) = "GxB_FC64_t" ; break ;
            default :;
        }
        has_cheeseburger = true ;
    }

    return (has_cheeseburger) ;
}