File: GB_mex_test16.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 (185 lines) | stat: -rw-r--r-- 6,952 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
//------------------------------------------------------------------------------
// GB_mex_test16: JIT error handling
//------------------------------------------------------------------------------

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

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

#include "GB_mex.h"
#include "GB_mex_errors.h"
#include "../Source/jitifyer/GB_stringify.h"

#define FREE_ALL ;
#define GET_DEEP_COPY ;
#define FREE_DEEP_COPY ;

void myfunc (float *z, const float *x) ;
void myfunc (float *z, const float *x) { (*z) = (*x) + 1 ; }

void mymult (float *z, const float *x, const float *y) ;
void mymult (float *z, const float *x, const float *y) { (*z) = (*x)*(*y) + 1 ;}

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

    //--------------------------------------------------------------------------
    // startup GraphBLAS
    //--------------------------------------------------------------------------

    GrB_Info info ;
    bool malloc_debug = GB_mx_get_global (true) ;

    //--------------------------------------------------------------------------
    // create some valid matrices
    //--------------------------------------------------------------------------

    uint64_t n = 4 ;
    GrB_Matrix A = NULL, B = NULL, C = NULL ;
    GrB_Vector v = NULL ;
    OK (GrB_Matrix_new (&A, GrB_FP32, n, n)) ;
    OK (GrB_Matrix_new (&C, GrB_FP32, n, n)) ;
    OK (GrB_Matrix_new (&B, GrB_FP32, n, n)) ;
    OK (GxB_set (A, GxB_SPARSITY_CONTROL, GxB_SPARSE)) ;
    OK (GxB_set (B, GxB_SPARSITY_CONTROL, GxB_BITMAP)) ;
    OK (GrB_assign (A, NULL, NULL, 1, GrB_ALL, n, GrB_ALL, n, NULL)) ;
    OK (GrB_assign (B, NULL, NULL, 1, GrB_ALL, n, GrB_ALL, n, NULL)) ;
    OK (GrB_Matrix_setElement (A, 0, 0, 0)) ;
    OK (GrB_Matrix_setElement (B, 0, 0, 0)) ;
    OK (GrB_Matrix_wait (A, GrB_MATERIALIZE)) ;
    OK (GrB_Matrix_wait (B, GrB_MATERIALIZE)) ;
    OK (GrB_Matrix_wait (C, GrB_MATERIALIZE)) ;

    //--------------------------------------------------------------------------
    // set the JIT to ON, and the Factory Kernels off
    //--------------------------------------------------------------------------

    int save_control ;
    bool save_factory = GB_factory_kernels_enabled ;
    GB_factory_kernels_enabled = false ;
    OK (GxB_Global_Option_get_INT32 (GxB_JIT_C_CONTROL, &save_control)) ;
    OK (GxB_Global_Option_set_INT32 (GxB_JIT_C_CONTROL, GxB_JIT_ON)) ;

    //--------------------------------------------------------------------------
    // try some methods that require the JIT
    //--------------------------------------------------------------------------

    OK (GxB_set (GxB_BURBLE, true)) ;
    GrB_Semiring s ;
    GrB_BinaryOp mult ;
    GrB_Monoid mon ;

    // user type with zero size
    GrB_Type MyType ;
    GrB_Info expected = GrB_INVALID_VALUE ;
    ERR (GxB_Type_new (&MyType, 0, NULL, NULL)) ;
    OK (GxB_Global_Option_set_INT32 (GxB_JIT_C_CONTROL, GxB_JIT_ON)) ;
    OK (GrB_Type_new (&MyType, sizeof (double))) ;
    OK (GxB_print (MyType, 3)) ;
    size_t size ;
    expected = GrB_NO_VALUE ;
    ERR (GB_user_type_jit (&size, MyType)) ;
    OK (GxB_Global_Option_set_INT32 (GxB_JIT_C_CONTROL, GxB_JIT_ON)) ;

    printf ("\nmacrofy type:\n") ;
    GB_macrofy_user_type (NULL, MyType) ;

    // user function with NULL pointer
    GrB_UnaryOp op ;
    expected = GrB_NULL_POINTER ;
    ERR (GxB_UnaryOp_new (&op, NULL, GrB_FP32, GrB_FP32, NULL, NULL)) ;
    OK (GxB_Global_Option_set_INT32 (GxB_JIT_C_CONTROL, GxB_JIT_ON)) ;

    OK (GrB_UnaryOp_new (&op, (GxB_unary_function) myfunc,
        GrB_FP32, GrB_FP32)) ;
    printf ("\nmacrofy op:\n") ;
    GB_macrofy_user_op (NULL, (GB_Operator) op) ;

    OK (GrB_BinaryOp_new (&mult, (GxB_binary_function) mymult,
        GrB_FP32, GrB_FP32, GrB_FP32)) ;
    OK (GxB_print (mult, 3)) ;

    OK (GrB_Monoid_new (&mon, mult, (float) 1)) ;
    OK (GxB_print (mult, 3)) ;

    OK (GrB_Semiring_new (&s, mon, mult)) ;
    OK (GxB_print (s, 3)) ;

    GB_jit_encoding e ;
    char *suffix ;
    uint64_t code = GB_encodify_mxm (&e, &suffix, 0, false, false, GxB_SPARSE,
        GrB_FP32, false, false, false, NULL, false, false, s, false, A, B) ;
    CHECK (code == UINT64_MAX) ;

    code = GB_encodify_reduce (&e, &suffix, GB_JIT_KERNEL_REDUCE, mon, A) ;
    CHECK (code == UINT64_MAX) ;

    code = GB_encodify_assign (&e, &suffix, /* kcode: */ 0, C,
        /* C_replace: */ false, /* I_is_32: */ false, /* J_is_32: */ false,
        /* Ikind: */ 0, /* Jkind: */ 0, /* M: */ NULL,
        /* Mask_comp: */ false, /* Mask_struct: */ false,
        /* accum: */ mult, A, /* scalar_type: */ NULL, /* S: */ NULL,
        /* assign_kind: */ 0) ;
    CHECK (code == UINT64_MAX) ;

    code = GB_encodify_build (&e, &suffix, 0, mult, GrB_FP32, GrB_FP32,
        true, true, true, true, false) ;
    CHECK (code == UINT64_MAX) ;

    //--------------------------------------------------------------------------
    // restore the JIT control, Factory Kernels, and renable the JIT
    //--------------------------------------------------------------------------

    OK (GxB_Global_Option_set_INT32 (GxB_JIT_C_CONTROL, save_control)) ;
    GB_factory_kernels_enabled = save_factory ;

    //--------------------------------------------------------------------------
    // GrB_select
    //--------------------------------------------------------------------------

    GrB_free (&C) ;
    OK (GrB_Matrix_new (&C, GrB_FP32, 2*n, 2*n)) ;
    expected = GrB_DIMENSION_MISMATCH ;
    ERR (GrB_select (C, NULL, NULL, GrB_TRIL, A, 0, NULL)) ;
    GrB_free (&C) ;

    //--------------------------------------------------------------------------
    // GrB_assign burble
    //--------------------------------------------------------------------------

    OK (GrB_Vector_new (&v, GrB_FP32, n)) ;
    OK (GrB_Matrix_new (&C, GrB_FP32, 2*n, 2*n)) ;
    OK (GxB_set (A, GxB_SPARSITY_CONTROL, GxB_BITMAP)) ;
    OK (GxB_set (C, GxB_SPARSITY_CONTROL, GxB_BITMAP)) ;
    OK (GrB_Row_assign_(A, NULL, NULL, v, 0, GrB_ALL, n, NULL)) ;
    OK (GrB_Col_assign_(A, NULL, NULL, v, GrB_ALL, n, 0, NULL)) ;
    uint64_t I [4] = {0,1,2,3} ;    // OK
    OK (GrB_assign (C, NULL, NULL, A, I, 4, I, 4, NULL)) ;
    OK (GxB_subassign (C, NULL, NULL, A, I, 4, I, 4, NULL)) ;

    //--------------------------------------------------------------------------
    // finalize GraphBLAS
    //--------------------------------------------------------------------------

    GrB_free (&A) ;
    GrB_free (&B) ;
    GrB_free (&C) ;
    GrB_free (&v) ;
    GrB_free (&MyType) ;
    GrB_free (&op) ;
    GrB_free (&mult) ;
    GrB_free (&s) ;
    GrB_free (&mon) ;

    OK (GxB_set (GxB_BURBLE, false)) ;
    GB_mx_put_global (true) ;
    printf ("\nGB_mex_test16:  all tests passed\n\n") ;
}