File: GB_macrofy_monoid.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 (455 lines) | stat: -rw-r--r-- 17,117 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
//------------------------------------------------------------------------------
// GB_macrofy_monoid: build macros 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"

void GB_macrofy_monoid  // construct the macros for a monoid
(
    FILE *fp,           // File to write macros, assumed open already
    // inputs:
    bool C_iso,         // true if C is iso
    GrB_Monoid monoid,  // monoid to macrofy
    bool disable_terminal_condition,    // if true, a builtin monoid is assumed
                        // to be non-terminal.  For the (times, firstj, int64)
                        // semiring, times is normally a terminal monoid, but
                        // it's not worth exploiting in GrB_mxm.
    // output:
    const char **u_expression,
    const char **g_expression
)
{

    //--------------------------------------------------------------------------
    // get the monoid
    //--------------------------------------------------------------------------

    GrB_BinaryOp op = monoid->op ;
    const char *ztype_name = C_iso ? "void" : op->ztype->name ;
    int zcode = C_iso ? 0 : op->ztype->code ;
    size_t zsize = C_iso ? 0 : op->ztype->size ;
    GB_Opcode opcode = C_iso ? 0 : op->opcode ;

    if (C_iso)
    { 
        opcode = GB_ANY_binop_code ;
        zcode = 0 ;
    }
    else if (zcode == GB_BOOL_code)
    { 
        // rename the monoid
        opcode = GB_boolean_rename (opcode) ;
    }

    //--------------------------------------------------------------------------
    // create macros for the additive operator
    //--------------------------------------------------------------------------

    int add_ecode ;
    GB_enumify_binop (&add_ecode, opcode, zcode, false, false) ;
    GB_macrofy_binop (fp, "GB_ADD", false, false, true, false, false,
        add_ecode, C_iso, op, NULL, u_expression, g_expression) ;

    //--------------------------------------------------------------------------
    // create macros for the identity value
    //--------------------------------------------------------------------------

    bool has_byte ;
    uint8_t byte ;
    if (C_iso)
    { 
        // no values computed (C is iso)
        fprintf (fp, "#define GB_DECLARE_IDENTITY(z)\n") ;
        fprintf (fp, "#define GB_DECLARE_IDENTITY_CONST(z)\n") ;
    }
    else
    {
        int id_ecode ;
        GB_enumify_identity (&id_ecode, opcode, zcode) ;

        if (id_ecode <= 28)
        {
            // built-in monoid: a simple assignment
            const char *id_val = GB_macrofy_id (id_ecode, zsize,
                &has_byte, &byte) ;
            #define SLEN (256 + GxB_MAX_NAME_LEN)
            char id [SLEN] ;
            if (zcode == GB_FC32_code)
            { 
                snprintf (id, SLEN, "%s z = GxB_CMPLXF (%s,0)",
                    ztype_name, id_val) ;
            }
            else if (zcode == GB_FC64_code)
            { 
                snprintf (id, SLEN, "%s z = GxB_CMPLX (%s,0)",
                    ztype_name, id_val) ;
            }
            else
            { 
                snprintf (id, SLEN, "%s z = %s", ztype_name, id_val) ;
            }
            fprintf (fp, "#define GB_DECLARE_IDENTITY(z) %s\n", id) ;
            fprintf (fp, "#define GB_DECLARE_IDENTITY_CONST(z) const %s\n", id);
            if (has_byte)
            { 
                fprintf (fp, "#define GB_HAS_IDENTITY_BYTE 1\n") ;
                fprintf (fp, "#define GB_IDENTITY_BYTE 0x%02x\n", (int) byte) ;
            }
        }
        else
        { 
            // user-defined monoid:  all we have are the bytes
            GB_macrofy_bytes (fp, "IDENTITY", "z",
                ztype_name, (uint8_t *) (monoid->identity), zsize, true) ;
            fprintf (fp, "#define GB_DECLARE_IDENTITY_CONST(z) "
                "GB_DECLARE_IDENTITY(z)\n") ;
        }
    }

    //--------------------------------------------------------------------------
    // create macros for the terminal value and terminal conditions
    //--------------------------------------------------------------------------

    // GB_TERMINAL_CONDITION(z,zterminal) should return true if the value of z
    // has reached its terminal value (zterminal), or false otherwise.  If the
    // monoid is not terminal, then the macro should always return false.  The
    // ANY monoid should always return true.

    // GB_IF_TERMINAL_BREAK(z,zterminal) is a macro containing a full
    // statement.  If the monoid is never terminal, it becomes the empty
    // statement.  Otherwise, it checks the terminal condition and does a
    // "break" if true.

    // GB_DECLARE_TERMINAL_CONST(zterminal) declares the zterminal variable as
    // const.  It is empty if the monoid is not terminal.

    bool monoid_is_terminal = false ;

    int term_ecode ;
    if (C_iso)
    { 
        term_ecode = 18 ;
    }
    else
    { 
        GB_enumify_terminal (&term_ecode, opcode, zcode) ;
    }

    bool is_any_monoid = (term_ecode == 18) ;
    if (is_any_monoid || C_iso)
    { 
        // ANY monoid is terminal but with no specific terminal value
        fprintf (fp, "#define GB_IS_ANY_MONOID 1\n") ;
        monoid_is_terminal = true ;
    }
    else if (monoid->terminal == NULL)
    { 
        // monoid is not terminal (either built-in or user-defined)
        monoid_is_terminal = false ;
    }
    else if (term_ecode <= 28)
    {
        // built-in terminal monoid: terminal value is a simple assignment.
        // Its terminal condition can be ignored (for (times, firstj, int64),
        // for example) if disable_terminal_condition is true, but in that case
        // its terminal value must still be constructed for the query function.
        monoid_is_terminal = !disable_terminal_condition ;
        if (monoid_is_terminal)
        { 
            fprintf (fp, "#define GB_MONOID_IS_TERMINAL 1\n") ;
        }
        // there are no built-in terminal monoids, except ANY, handled above
        ASSERT (zcode != GB_FC32_code && zcode != GB_FC64_code) ;
        const char *term_value = GB_macrofy_id (term_ecode, zsize, NULL, NULL) ;
        fprintf (fp, "#define GB_DECLARE_TERMINAL_CONST(zterminal) "
            "const %s zterminal = ", ztype_name) ;
        fprintf (fp, "%s\n", term_value) ;
        if (monoid_is_terminal)
        { 
            fprintf (fp, "#define GB_TERMINAL_CONDITION(z,zterminal) "
                "((z) == %s)\n", term_value) ;
            fprintf (fp, "#define GB_IF_TERMINAL_BREAK(z,zterminal) "
                "if ((z) == %s) break\n", term_value) ;
        }
    }
    else
    { 
        // user-defined terminal monoid
        monoid_is_terminal = true ;
        fprintf (fp, "#define GB_MONOID_IS_TERMINAL 1\n") ;
        GB_macrofy_bytes (fp, "TERMINAL_CONST", "zterminal",
            ztype_name, monoid->terminal, zsize, false) ;
        fprintf (fp, "#define GB_TERMINAL_CONDITION(z,zterminal) "
            " (memcmp (&(z), &(zterminal), %d) == 0)\n", (int) zsize) ;
        fprintf (fp, "#define GB_IF_TERMINAL_BREAK(z,zterminal) "
            " if (memcmp (&(z), &(zterminal), %d) == 0) break\n", (int) zsize) ;
    }

    //--------------------------------------------------------------------------
    // determine the OpenMP #pragma omp reduction(redop:z) for this monoid
    //--------------------------------------------------------------------------

    // If not #define'd, omp/include/GB_monoid_shared_definitions.h defaults
    // to no #pragma.  The pragma is empty if the monoid is terminal, since
    // the simd reduction does not work with a 'break' in the loop.

    bool is_complex = (zcode == GB_FC32_code || zcode == GB_FC64_code) ;

    if (is_complex)
    { 
        fprintf (fp, "#define GB_Z_IS_COMPLEX 1\n") ;
    }

    if (!monoid_is_terminal && !is_complex)
    {
        char *redop = NULL ;
        if (opcode == GB_PLUS_binop_code)
        { 
            // #pragma omp simd reduction(+:z)
            redop = "+" ;
        }
        else if (opcode == GB_LXOR_binop_code || opcode == GB_BXOR_binop_code)
        { 
            // #pragma omp simd reduction(^:z)
            redop = "^" ;
        }
        else if (opcode == GB_TIMES_binop_code)
        { 
            // #pragma omp simd reduction(^:z)
            redop = "*" ;
        }
        if (redop != NULL)
        { 
            // The monoid has a "#pragma omp simd reduction(redop:z)" statement.
            // There are other OpenMP reductions that could be exploited, but
            // many are for terminal monoids (logical and bitwise AND, OR).
            // The min/max reductions are not exploited because they are
            // terminal monoids for integers.  For floating-point, the NaN
            // handling may differ, so they are not exploited here either.
            fprintf (fp, "#define GB_PRAGMA_SIMD_REDUCTION_MONOID(z) "
                "GB_PRAGMA_SIMD_REDUCTION (%s,z)\n", redop) ;
        }
    }

    //--------------------------------------------------------------------------
    // special cases
    //--------------------------------------------------------------------------

    bool is_integer = (zcode >= GB_INT8_code && zcode <= GB_UINT64_code) ;
    bool is_fp_real = (zcode == GB_FP32_code || zcode == GB_FP64_code) ;

    if (opcode == GB_PLUS_binop_code && zcode == GB_FC32_code)
    { 
        // PLUS_FC32 monoid
        fprintf (fp, "#define GB_IS_PLUS_FC32_MONOID 1\n") ;
    }
    else if (opcode == GB_PLUS_binop_code && zcode == GB_FC64_code)
    { 
        // PLUS_FC64 monoid
        fprintf (fp, "#define GB_IS_PLUS_FC64_MONOID 1\n") ;
    }
    else if (opcode == GB_MIN_binop_code && is_integer)
    { 
        // IMIN monoid (min with any integer type)
        fprintf (fp, "#define GB_IS_IMIN_MONOID 1\n") ;
    }
    else if (opcode == GB_MAX_binop_code && is_integer)
    { 
        // IMAX monoid (max with any integer type)
        fprintf (fp, "#define GB_IS_IMAX_MONOID 1\n") ;
    }
    else if (opcode == GB_MIN_binop_code && is_fp_real)
    { 
        // FMIN monoid (min with a real floating-point type)
        fprintf (fp, "#define GB_IS_FMIN_MONOID 1\n") ;
    }
    else if (opcode == GB_MAX_binop_code && is_fp_real)
    { 
        // FMAX monoid (max with a real floating-point type)
        fprintf (fp, "#define GB_IS_FMAX_MONOID 1\n") ;
    }

    // can ignore overflow in ztype when accumulating the result via the monoid
    // zcode == 0: only when C is iso
    bool ztype_ignore_overflow = (zcode == 0 ||
        zcode == GB_INT64_code || zcode == GB_UINT64_code ||
        zcode == GB_FP32_code  || zcode == GB_FP64_code ||
        zcode == GB_FC32_code  || zcode == GB_FC64_code) ;
    if (ztype_ignore_overflow && !is_any_monoid)
    { 
        // if the monoid is ANY, this is set to 1 by
        // monoid/include/GB_monoid_shared_definitions.h, so skip it here
        fprintf (fp, "#define GB_Z_IGNORE_OVERFLOW 1\n") ;
    }

    //--------------------------------------------------------------------------
    // create macros for atomics on the CPU
    //--------------------------------------------------------------------------

    fprintf (fp, "#define GB_Z_SIZE  %d\n", (int) zsize) ;
    fprintf (fp, "#define GB_Z_NBITS %d\n", 8 * (int) zsize) ;

    // atomic write
    bool has_atomic_write = false ;
    char *ztype_atomic = NULL ;
    if (zcode == 0)
    { 
        // C is iso (any_pair symbolic semiring)
        fprintf (fp, "#define GB_Z_ATOMIC_BITS 0\n") ;
    }
    else if (zsize == sizeof (uint8_t))
    { 
        // int8_t, uint8_t, and 8-bit user-defined types
        ztype_atomic = "uint8_t" ;
        has_atomic_write = true ;
        fprintf (fp, "#define GB_Z_ATOMIC_BITS 8\n") ;
    }
    else if (zsize == sizeof (uint16_t))
    { 
        // int16_t, uint16_t, and 16-bit user-defined types
        ztype_atomic = "uint16_t" ;
        has_atomic_write = true ;
        fprintf (fp, "#define GB_Z_ATOMIC_BITS 16\n") ;
    }
    else if (zsize == sizeof (uint32_t))
    { 
        // int32_t, uint32_t, float, and 32-bit user-defined types
        ztype_atomic = "uint32_t" ;
        has_atomic_write = true ;
        fprintf (fp, "#define GB_Z_ATOMIC_BITS 32\n") ;
    }
    else if (zsize == sizeof (uint64_t))
    { 
        // int64_t, uint64_t, double, float complex, and 64-bit user types
        ztype_atomic = "uint64_t" ;
        has_atomic_write = true ;
        fprintf (fp, "#define GB_Z_ATOMIC_BITS 64\n") ;
    }

    // atomic write for the ztype:  if GB_Z_ATOMIC_BITS is defined, then
    // GB_Z_HAS_ATOMIC_WRITE is #defined as 1 by
    // omp/include/GB_kernel_shared_definitions.h

    if (has_atomic_write && (zcode == GB_FC32_code || zcode == GB_UDT_code))
    { 
        // user-defined types of size 1, 2, 4, or 8 bytes can be written
        // atomically, but must use a pun with ztype_atomic.  float complex
        // should also ztype_atomic.
        fprintf (fp, "#define GB_Z_ATOMIC_TYPE %s\n", ztype_atomic) ;
    }

    // OpenMP atomic update support
    bool is_real = (zcode >= GB_BOOL_code && zcode <= GB_FP64_code) ;
    bool has_atomic_update = false ;
    int omp_atomic_version = 0 ;

    switch (opcode)
    {

        case GB_ANY_binop_code   : 
            // the ANY monoid is a special case.  It is done with an atomic
            // write, or no update at all.  The atomic write can be done for
            // float complex (64 bits) but not double complex (128 bits).
            // The atomic update is identical: just an atomic write.
            has_atomic_update = has_atomic_write ;
            omp_atomic_version = is_real ? 2 : 0 ;
            break ;

        case GB_LAND_binop_code  : 
        case GB_LOR_binop_code   : 
        case GB_LXOR_binop_code  : 
        case GB_BAND_binop_code  : 
        case GB_BOR_binop_code   : 
        case GB_BXOR_binop_code  : 
            // OpenMP 4.0 atomic, not on MS Visual Studio
            has_atomic_update = true ;
            omp_atomic_version = 4 ;
            break ;

        case GB_BXNOR_binop_code : 
        case GB_EQ_binop_code    : // LXNOR
        case GB_MIN_binop_code   : 
        case GB_MAX_binop_code   : 
            // these monoids can be done via atomic compare/exchange,
            // but not with an omp pragma
            has_atomic_update = true ;
            break ;

        case GB_PLUS_binop_code  : 
            // even complex can be done atomically
            has_atomic_update = true ;
            omp_atomic_version = 2 ;
            break ;

        case GB_TIMES_binop_code : 
            // real monoids can be done atomically, not double complex
            has_atomic_update = is_real || (zcode == GB_FC32_code) ;
            // only the real case has an omp pragma
            omp_atomic_version = is_real ? 2 : 0 ;
            break ;

        default : 
            // all other monoids, including user-defined, can be done atomically
            // via compare-and-swap, if z has size 1, 2, 4, or 8 bytes.
            // Otherwise, they must be done in a critical section.
            has_atomic_update = has_atomic_write ;
            omp_atomic_version = 0 ;
    }

    if (has_atomic_update)
    { 
        // the monoid can be done atomically
        fprintf (fp, "#define GB_Z_HAS_ATOMIC_UPDATE 1\n") ;
        if (omp_atomic_version == 4)
        { 
            // OpenMP 4.0 has an omp pragram but not OpenMP 2.0.
            fprintf (fp, "#define GB_Z_HAS_OMP_ATOMIC_UPDATE "
                "(!GB_COMPILER_MSC)\n") ;
        }
        else if (omp_atomic_version == 2)
        { 
            // this update has an omp pragma
            fprintf (fp, "#define GB_Z_HAS_OMP_ATOMIC_UPDATE 1\n") ;
        }
    }

    //--------------------------------------------------------------------------
    // create macros for the atomic CUDA operator, if available
    //--------------------------------------------------------------------------

    const char *a = NULL, *cuda_type = NULL ;
    bool user_monoid_atomically = false ;
    GB_enumify_cuda_atomic (&a, &user_monoid_atomically, &cuda_type,
        monoid, opcode, zsize, zcode) ;

    if (monoid == NULL || zcode == 0)
    { 
        // nothing to do: C is iso-valued.  For GrB_mxm only.
        ;
    }
    else if (user_monoid_atomically)
    { 
        // CUDA atomic for a user monoid
        fprintf (fp, "#define GB_Z_HAS_CUDA_ATOMIC_USER 1\n") ;
        fprintf (fp, "#define GB_Z_CUDA_ATOMIC_TYPE %s\n", cuda_type) ;
    }
    else if (a == NULL)
    { 
        // no CUDA atomics for this monoid
        ;
    }
    else
    { 
        // CUDA atomic available for a built-in monoid
        fprintf (fp, "#define GB_Z_HAS_CUDA_ATOMIC_BUILTIN 1\n") ;
        fprintf (fp, "#define GB_Z_CUDA_ATOMIC %s\n", a) ;
        fprintf (fp, "#define GB_Z_CUDA_ATOMIC_TYPE %s\n", cuda_type) ;
    }
}