1 //------------------------------------------------------------------------------
2 // GB_mx_get_global: get the GraphBLAS thread-local storage from MATLAB
3 //------------------------------------------------------------------------------
4 
5 // SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
6 // SPDX-License-Identifier: Apache-2.0
7 
8 //------------------------------------------------------------------------------
9 
10 // Get the variable 'GraphBLAS_debug' from the MATLAB global workspace.
11 // If it doesn't exist, create it and set it to false.
12 
13 #include "GB_mex.h"
14 
GB_mx_get_global(bool cover)15 bool GB_mx_get_global       // true if doing malloc_debug
16 (
17     bool cover              // true if doing statement coverage
18 )
19 {
20 
21     //--------------------------------------------------------------------------
22     // get malloc debug
23     //--------------------------------------------------------------------------
24 
25     bool malloc_debug = false ;
26     bool *debug = NULL ;
27     const mxArray *debug_matlab = NULL ;
28     debug_matlab = mexGetVariablePtr ("global", "GraphBLAS_debug") ;
29     if (debug_matlab == NULL || mxIsEmpty (debug_matlab))
30     {
31         // doesn't exist; create it and set it to false
32         debug_matlab = GB_mx_create_full (1, 1, GrB_BOOL) ;
33         debug = (bool *) mxGetData (debug_matlab) ;
34         if (debug == NULL) mexErrMsgTxt ("debug_matlab null?!") ;
35         debug [0] = false ;
36         // copy it into the global workspace
37         mexPutVariable ("global", "GraphBLAS_debug", debug_matlab) ;
38     }
39     else
40     {
41         debug = (bool *) mxGetData (debug_matlab) ;
42         if (debug == NULL) mexErrMsgTxt ("debug_matlab null!") ;
43         malloc_debug = debug [0] ;
44         // if (malloc_debug) printf ("GraphBLAS malloc debug enabled\n") ;
45     }
46 
47     //--------------------------------------------------------------------------
48     // clear the time
49     //--------------------------------------------------------------------------
50 
51     GB_mx_clear_time ( ) ;
52 
53     //--------------------------------------------------------------------------
54     // get test coverage
55     //--------------------------------------------------------------------------
56 
57     #ifdef GBCOVER
58     if (cover) GB_cover_get ( ) ;
59     #endif
60 
61     //--------------------------------------------------------------------------
62     // initialize GraphBLAS
63     //--------------------------------------------------------------------------
64 
65     bool burble = GB_Global_burble_get ( ) ;            // save current burble
66     GB_Global_GrB_init_called_set (false) ;
67 //  GxB_init (GrB_NONBLOCKING, mxMalloc, NULL, NULL, mxFree, false) ;
68     GxB_init (GrB_NONBLOCKING, mxMalloc, mxCalloc, mxRealloc, mxFree, false) ;
69     ASSERT (GB_Global_nmalloc_get ( ) == 0) ;
70     GB_Global_abort_function_set (GB_mx_abort) ;
71     GB_Global_malloc_tracking_set (true) ;
72     GxB_Global_Option_set_(GxB_FORMAT, GxB_BY_COL) ;
73     GxB_Global_Option_set_(GxB_BURBLE, burble) ;        // restore the burble
74     GxB_Global_Option_set_(GxB_PRINTF, mexPrintf) ;
75 
76     //--------------------------------------------------------------------------
77     // get nthreads
78     //--------------------------------------------------------------------------
79 
80     int *nthreads = NULL ;
81     const mxArray *nthreads_matlab = NULL ;
82     nthreads_matlab = mexGetVariablePtr ("global", "GraphBLAS_nthreads") ;
83     if (nthreads_matlab == NULL || mxIsEmpty (nthreads_matlab))
84     {
85         // doesn't exist; create it and set it to 1
86         nthreads_matlab = GB_mx_create_full (1, 1, GrB_INT32) ;
87         nthreads = (int32_t *) mxGetData (nthreads_matlab) ;
88         if (nthreads == NULL) mexErrMsgTxt ("nthreads_matlab null?!") ;
89         nthreads [0] = 1 ;
90         // copy it into the global workspace
91         mexPutVariable ("global", "GraphBLAS_nthreads", nthreads_matlab) ;
92     }
93     else
94     {
95         nthreads = (int32_t *) mxGetData (nthreads_matlab) ;
96         if (nthreads == NULL) mexErrMsgTxt ("nthreads_matlab null!") ;
97     }
98 
99     GxB_Global_Option_set_(GxB_NTHREADS, nthreads [0]) ;
100 
101     //--------------------------------------------------------------------------
102     // get chunk
103     //--------------------------------------------------------------------------
104 
105     double *chunk = NULL ;
106     const mxArray *chunk_matlab = NULL ;
107     chunk_matlab = mexGetVariablePtr ("global", "GraphBLAS_chunk") ;
108     if (chunk_matlab == NULL || mxIsEmpty (chunk_matlab))
109     {
110         // doesn't exist; create it and set it to GB_CHUNK_DEFAULT
111         chunk_matlab = GB_mx_create_full (1, 1, GrB_FP64) ;
112         chunk = (double *) mxGetData (chunk_matlab) ;
113         if (chunk == NULL) mexErrMsgTxt ("chunk_matlab null?!") ;
114         chunk [0] = GB_CHUNK_DEFAULT ;
115         // copy it into the global workspace
116         mexPutVariable ("global", "GraphBLAS_chunk", chunk_matlab) ;
117     }
118     else
119     {
120         chunk = (double *) mxGetData (chunk_matlab) ;
121         if (chunk == NULL) mexErrMsgTxt ("chunk_matlab null!") ;
122     }
123 
124     GxB_Global_Option_set_(GxB_CHUNK, chunk [0]) ;
125 
126     //--------------------------------------------------------------------------
127     // get GraphBLAS_complex flag and allocate the complex type and operators
128     //--------------------------------------------------------------------------
129 
130     bool *builtin_complex = NULL ;
131     const mxArray *builtin_complex_matlab = NULL ;
132     builtin_complex_matlab =
133         mexGetVariablePtr ("global", "GraphBLAS_builtin_complex") ;
134     if (builtin_complex_matlab == NULL || mxIsEmpty (builtin_complex_matlab))
135     {
136         // doesn't exist; create it and set it to TRUE
137         builtin_complex_matlab = GB_mx_create_full (1, 1, GrB_BOOL) ;
138         builtin_complex = (bool *) mxGetData (builtin_complex_matlab) ;
139         if (builtin_complex == NULL)
140         {
141             mexErrMsgTxt ("builtin_complex_matlab null?!") ;
142         }
143         builtin_complex [0] = true ;
144         // copy it into the global workspace
145         mexPutVariable ("global", "GraphBLAS_builtin_complex",
146             builtin_complex_matlab) ;
147     }
148     else
149     {
150         builtin_complex = (bool *) mxGetData (builtin_complex_matlab) ;
151         if (builtin_complex == NULL)
152         {
153             mexErrMsgTxt ("builtin_complex_matlab null!") ;
154         }
155     }
156 
157     Complex_init (builtin_complex [0]) ;
158 
159     //--------------------------------------------------------------------------
160     // return malloc debug status
161     //--------------------------------------------------------------------------
162 
163     return (malloc_debug) ;
164 }
165 
166