File: gbselect.c

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 (426 lines) | stat: -rw-r--r-- 14,521 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
//------------------------------------------------------------------------------
// gbselect: select entries from a GraphBLAS matrix
//------------------------------------------------------------------------------

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

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

// gbselect is an interface to GrB_Matrix_select and GxB_Matrix_select.

// Usage:

// C = gbselect (op, A)
// C = gbselect (op, A, desc)
// C = gbselect (op, A, b, desc)

// C = gbselect (Cin, accum, op, A, desc)
// C = gbselect (Cin, accum, op, A, b, desc)

// C = gbselect (Cin, M, op, A, desc)
// C = gbselect (Cin, M, op, A, b, desc)

// C = gbselect (Cin, M, accum, op, A, desc)
// C = gbselect (Cin, M, accum, op, A, b, desc)

// If Cin is not present then it is implicitly a matrix with no entries, of the
// right size (which depends on A, and the descriptor).  The type of Cin, if
// not present, is determined by the ztype of the accum, if present, or
// otherwise it has the same time as A.

// If op is '==' or '~=' and b is a NaN, and A has type GrB_FP32, GrB_FP64,
// GxB_FC32, or GxB_FC64, then a user-defined operator is used instead of
// GxB_EQ_THUNK, GxB_NE_THUNK, GrB_VALUEEQ* or GrB_VALUENE*.

// The 'tril', 'triu', 'diag', 'offdiag', and 2-input operators all require
// the b scalar.  The b scalar must not appear for the '*0' operators.

#include "gb_interface.h"

#define USAGE "usage: C = GrB.select (Cin, M, accum, op, A, b, desc)"

//------------------------------------------------------------------------------
// nan functions for GrB_IndexUnaryOp operators
//------------------------------------------------------------------------------

void gb_isnan32 (bool *z, const float *aij,
                 int64_t i, int64_t j, const void *thunk)
{ 
    (*z) = (isnan (*aij)) ;
}

void gb_isnan64 (bool *z, const double *aij,
                 int64_t i, int64_t j, const void *thunk)
{ 
    (*z) = (isnan (*aij)) ;
}

void gb_isnotnan32 (bool *z, const float *aij,
                    int64_t i, int64_t j, const void *thunk)
{ 
    (*z) = (!isnan (*aij)) ;
}

void gb_isnotnan64 (bool *z, const double *aij,
                    int64_t i, int64_t j, const void *thunk)
{ 
    (*z) = (!isnan (*aij)) ;
}

void gb_isnanfc32 (bool *z, const GxB_FC32_t *aij,
                   int64_t i, int64_t j, const void *thunk)
{ 
    (*z) = GB_cisnanf (*aij) ;
}

void gb_isnanfc64 (bool *z, const GxB_FC64_t *aij,
                   int64_t i, int64_t j, const void *thunk)
{ 
    (*z) = GB_cisnan (*aij) ;
}

void gb_isnotnanfc32 (bool *z, const GxB_FC32_t *aij,
                      int64_t i, int64_t j, const void *thunk)
{ 
    (*z) = !GB_cisnanf (*aij) ;
}

void gb_isnotnanfc64 (bool *z, const GxB_FC64_t *aij,
                      int64_t i, int64_t j, const void *thunk)
{ 
    (*z) = !GB_cisnan (*aij) ;
}

//------------------------------------------------------------------------------
// gbselect mexFunction
//------------------------------------------------------------------------------

void mexFunction
(
    int nargout,
    mxArray *pargout [ ],
    int nargin,
    const mxArray *pargin [ ]
)
{

    //--------------------------------------------------------------------------
    // check inputs
    //--------------------------------------------------------------------------

    gb_usage (nargin >= 2 && nargin <= 7 && nargout <= 2, USAGE) ;

    //--------------------------------------------------------------------------
    // find the arguments
    //--------------------------------------------------------------------------

    mxArray *Matrix [6], *String [2], *Cell [2] ;
    base_enum_t base ;
    kind_enum_t kind ;
    GxB_Format_Value fmt ;
    int nmatrices, nstrings, ncells, sparsity ;
    GrB_Descriptor desc ;
    gb_get_mxargs (nargin, pargin, USAGE, Matrix, &nmatrices, String, &nstrings,
        Cell, &ncells, &desc, &base, &kind, &fmt, &sparsity) ;

    CHECK_ERROR (nmatrices < 1 || nmatrices > 4 || nstrings < 1 || ncells > 0,
        USAGE) ;

    //--------------------------------------------------------------------------
    // get the select operator; determine the type and ithunk later
    //--------------------------------------------------------------------------

    int64_t ithunk = 0 ;
    GxB_SelectOp selop = NULL ;
    GrB_IndexUnaryOp idxunop = NULL ;
    bool thunk_required = false ; 
    bool op_is_positional = false ;

    gb_mxstring_to_selectop (&idxunop, &selop, &thunk_required,
        &op_is_positional, &ithunk, String [nstrings-1], NULL) ;

    //--------------------------------------------------------------------------
    // get the matrices
    //--------------------------------------------------------------------------

    GrB_Type atype, ctype = NULL ;
    GrB_Matrix C = NULL, M = NULL, A, b = NULL ;

    if (thunk_required)
    {
        if (nmatrices == 1)
        { 
            ERROR ("select operator input is missing") ;
        }
        else if (nmatrices == 2)
        { 
            A = gb_get_shallow (Matrix [0]) ;
            b = gb_get_shallow (Matrix [1]) ;
        }
        else if (nmatrices == 3)
        { 
            C = gb_get_deep    (Matrix [0]) ;
            A = gb_get_shallow (Matrix [1]) ;
            b = gb_get_shallow (Matrix [2]) ;
        }
        else // if (nmatrices == 4)
        { 
            C = gb_get_deep    (Matrix [0]) ;
            M = gb_get_shallow (Matrix [1]) ;
            A = gb_get_shallow (Matrix [2]) ;
            b = gb_get_shallow (Matrix [3]) ;
        }
    }
    else
    {
        if (nmatrices == 1)
        { 
            A = gb_get_shallow (Matrix [0]) ;
        }
        else if (nmatrices == 2)
        { 
            C = gb_get_deep    (Matrix [0]) ;
            A = gb_get_shallow (Matrix [1]) ;
        }
        else if (nmatrices == 3)
        { 
            C = gb_get_deep    (Matrix [0]) ;
            M = gb_get_shallow (Matrix [1]) ;
            A = gb_get_shallow (Matrix [2]) ;
        }
        else // if (nmatrices == 4)
        { 
            ERROR (USAGE) ;
        }
    }

    OK (GxB_Matrix_type (&atype, A)) ;
    if (C != NULL)
    { 
        OK (GxB_Matrix_type (&ctype, C)) ;
    }

    //--------------------------------------------------------------------------
    // finalize the select operator and ithunk
    //--------------------------------------------------------------------------

    ithunk = 0 ;
    GrB_Type btype = NULL ;
    if (b != NULL)
    {
        OK (GxB_Matrix_type (&btype, b)) ;
        if (op_is_positional)
        { 
            // get ithunk from the b scalar
            OK0 (GrB_Matrix_extractElement_INT64 (&ithunk, b, 0, 0)) ;
        }
    }

    gb_mxstring_to_selectop (&idxunop, &selop, &thunk_required,
        &op_is_positional, &ithunk, String [nstrings-1], atype) ;

    //--------------------------------------------------------------------------
    // get the accum operator
    //--------------------------------------------------------------------------

    GrB_BinaryOp accum = NULL ;
    if (nstrings > 1)
    { 
        // if accum appears, then Cin must also appear
        CHECK_ERROR (C == NULL, USAGE) ;
        accum = gb_mxstring_to_binop (String [0], ctype, ctype) ;
    }

    //--------------------------------------------------------------------------
    // construct C if not present on input
    //--------------------------------------------------------------------------

    // If C is NULL, then it is not present on input.
    // Construct C of the right size and type.

    if (C == NULL)
    { 
        // get the descriptor contents to determine if A is transposed
        GrB_Desc_Value in0 ;
        OK (GxB_Desc_get (desc, GrB_INP0, &in0)) ;
        bool A_transpose = (in0 == GrB_TRAN) ;

        // get the size of A
        GrB_Index anrows, ancols ;
        OK (GrB_Matrix_nrows (&anrows, A)) ;
        OK (GrB_Matrix_ncols (&ancols, A)) ;

        // determine the size of C
        GrB_Index cnrows = (A_transpose) ? ancols : anrows ;
        GrB_Index cncols = (A_transpose) ? anrows : ancols ;

        // C has the same type as A
        OK (GxB_Matrix_type (&ctype, A)) ;

        // create the matrix C and set its format and sparsity
        fmt = gb_get_format (cnrows, cncols, A, NULL, fmt) ;
        sparsity = gb_get_sparsity (A, NULL, sparsity) ;
        C = gb_new (ctype, cnrows, cncols, fmt, sparsity) ;
    }

    //--------------------------------------------------------------------------
    // handle the NaN case
    //--------------------------------------------------------------------------

    GrB_IndexUnaryOp nan_test = NULL ;
    GrB_Matrix b2 = b ;
    GrB_Matrix b3 = NULL, b4 = NULL ;

    if (op_is_positional)
    { 
        // construct a new int64 thunk scalar for positional ops
        OK (GrB_Matrix_new (&b3, GrB_INT64, 1, 1)) ;
        OK (GrB_Matrix_setElement_INT64 (b3, ithunk, 0, 0)) ;
        b2 = b3 ;
    }
    else if (b != NULL)
    {
        // check if b is NaN
        bool b_is_nan = false ;
        if (btype == GrB_FP32)
        { 
            float b_value = 0 ;
            OK0 (GrB_Matrix_extractElement_FP32 (&b_value, b, 0, 0)) ;
            b_is_nan = isnan (b_value) ;
        }
        else if (btype == GrB_FP64)
        { 
            double b_value = 0 ;
            OK0 (GrB_Matrix_extractElement_FP64 (&b_value, b, 0, 0)) ;
            b_is_nan = isnan (b_value) ;
        }
        else if (btype == GxB_FC32)
        { 
            GxB_FC32_t b_value = GxB_CMPLXF (0, 0) ;
            OK0 (GxB_Matrix_extractElement_FC32 (&b_value, b, 0, 0)) ;
            b_is_nan = GB_cisnanf (b_value) ;
        }
        else if (btype == GxB_FC64)
        { 
            GxB_FC64_t b_value = GxB_CMPLX (0, 0) ;
            OK0 (GxB_Matrix_extractElement_FC64 (&b_value, b, 0, 0)) ;
            b_is_nan = GB_cisnan (b_value) ;
        }

        if (b_is_nan)
        {
            // b is NaN; create a new nan_test operator if it should be used
            // instead of the built-in GxB_EQ_THUNK, GxB_NE_THUNK, GrB_VALUEEQ*
            // or GrB_VALUENE* operators.

            if (idxunop == GrB_VALUEEQ_FP32 ||
                selop == GxB_EQ_THUNK && atype == GrB_FP32)
            { 
                OK (GrB_IndexUnaryOp_new (&nan_test,
                    (GxB_index_unary_function) gb_isnan32,
                    GrB_BOOL, GrB_FP32, GrB_FP32)) ;
            }
            else if (idxunop == GrB_VALUEEQ_FP64 ||
                     selop == GxB_EQ_THUNK && atype == GrB_FP64)
            { 
                OK (GrB_IndexUnaryOp_new (&nan_test,
                    (GxB_index_unary_function) gb_isnan64,
                    GrB_BOOL, GrB_FP64, GrB_FP64)) ;
            }
            else if (idxunop == GxB_VALUEEQ_FC32 ||
                     selop == GxB_EQ_THUNK && atype == GxB_FC32)
            { 
                OK (GrB_IndexUnaryOp_new (&nan_test,
                    (GxB_index_unary_function) gb_isnanfc32,
                    GrB_BOOL, GxB_FC32, GxB_FC32)) ;
            }
            else if (idxunop == GxB_VALUEEQ_FC64 ||
                     selop == GxB_EQ_THUNK && atype == GxB_FC64)
            { 
                OK (GrB_IndexUnaryOp_new (&nan_test,
                    (GxB_index_unary_function) gb_isnanfc64,
                    GrB_BOOL, GxB_FC64, GxB_FC64)) ;
            }
            else if (idxunop == GrB_VALUENE_FP32 ||
                     selop == GxB_NE_THUNK && atype == GrB_FP32)
            { 
                OK (GrB_IndexUnaryOp_new (&nan_test,
                    (GxB_index_unary_function) gb_isnotnan32,
                    GrB_BOOL, GrB_FP32, GrB_FP32)) ;
            }
            else if (idxunop == GrB_VALUENE_FP64 ||
                     selop == GxB_NE_THUNK && atype == GrB_FP64)
            { 
                OK (GrB_IndexUnaryOp_new (&nan_test,
                    (GxB_index_unary_function) gb_isnotnan64,
                    GrB_BOOL, GrB_FP64, GrB_FP64)) ;
            }
            else if (idxunop == GxB_VALUENE_FC32 ||
                     selop == GxB_NE_THUNK && atype == GxB_FC32)
            { 
                OK (GrB_IndexUnaryOp_new (&nan_test,
                    (GxB_index_unary_function) gb_isnotnanfc32,
                    GrB_BOOL, GxB_FC32, GxB_FC32)) ;
            }
            else if (idxunop == GxB_VALUENE_FC64 ||
                     selop == GxB_NE_THUNK && atype == GxB_FC64)
            { 
                OK (GrB_IndexUnaryOp_new (&nan_test,
                    (GxB_index_unary_function) gb_isnotnanfc64,
                    GrB_BOOL, GxB_FC64, GxB_FC64)) ;
            }
        }

        if (nan_test != NULL)
        { 
            // use the new operator instead of the built-in one
            selop = NULL ;
            idxunop = nan_test ;
        }
    }

    //--------------------------------------------------------------------------
    // compute C<M> += select (A, b2)
    //--------------------------------------------------------------------------

    if (selop != NULL)
    { 
        OK1 (C, GxB_Matrix_select (C, M, accum, selop, A,
            (GrB_Scalar) b2, desc)) ;
    }
    else
    { 
        // typecast the b2 scalar to the idxunop->ytype
        GrB_Type ytype ;
        char ytype_name [GxB_MAX_NAME_LEN] ;
        OK (GxB_IndexUnaryOp_ytype_name (ytype_name, idxunop)) ;
        OK (GxB_Type_from_name (&ytype, ytype_name)) ;
        OK (GrB_Matrix_new (&b4, ytype, 1, 1)) ;
        OK (GrB_Matrix_assign (b4, NULL, NULL, b2, GrB_ALL, 1, GrB_ALL, 1,
            NULL)) ;
        OK1 (C, GrB_Matrix_select_Scalar (C, M, accum, idxunop, A,
            (GrB_Scalar) b4, desc)) ;
    }

    //--------------------------------------------------------------------------
    // free shallow copies
    //--------------------------------------------------------------------------

    OK (GrB_Matrix_free (&M)) ;
    OK (GrB_Matrix_free (&A)) ;
    OK (GrB_Matrix_free (&b)) ;
    OK (GrB_Matrix_free (&b3)) ;
    OK (GrB_Matrix_free (&b4)) ;
    OK (GrB_Descriptor_free (&desc)) ;
    OK (GrB_IndexUnaryOp_free (&nan_test)) ;

    //--------------------------------------------------------------------------
    // export the output matrix C
    //--------------------------------------------------------------------------

    pargout [0] = gb_export (&C, kind) ;
    pargout [1] = mxCreateDoubleScalar (kind) ;
    GB_WRAPUP ;
}