1 //------------------------------------------------------------------------------
2 // GB_mex_about3: still more basic tests
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 // Test lots of random stuff.  The function otherwise serves no purpose.
11 
12 #include "GB_mex.h"
13 #include "GB_mex_errors.h"
14 
15 #define USAGE "GB_mex_about3"
16 
17 int myprintf (const char *restrict format, ...) ;
18 
myprintf(const char * restrict format,...)19 int myprintf (const char *restrict format, ...)
20 {
21     printf ("[[myprintf:") ;
22     va_list ap ;
23     va_start (ap, format) ;
24     vprintf (format, ap) ;
25     va_end (ap) ;
26     printf ("]]") ;
27 }
28 
29 int myflush (void) ;
30 
myflush(void)31 int myflush (void)
32 {
33     printf ("myflush\n") ;
34     fflush (stdout) ;
35 }
36 
37 typedef int (* printf_func_t) (const char *restrict format, ...) ;
38 typedef int (* flush_func_t)  (void) ;
39 
mexFunction(int nargout,mxArray * pargout[],int nargin,const mxArray * pargin[])40 void mexFunction
41 (
42     int nargout,
43     mxArray *pargout [ ],
44     int nargin,
45     const mxArray *pargin [ ]
46 )
47 {
48 
49     GrB_Info info ;
50     GrB_Matrix C = NULL ;
51     GrB_Vector w = NULL ;
52     char *err ;
53 
54     //--------------------------------------------------------------------------
55     // startup GraphBLAS
56     //--------------------------------------------------------------------------
57 
58     bool malloc_debug = GB_mx_get_global (true) ;
59     FILE *f = fopen ("errlog4.txt", "w") ;
60     int expected = GrB_SUCCESS ;
61 
62     //--------------------------------------------------------------------------
63     // GxB_set/get for printf and flush
64     //--------------------------------------------------------------------------
65 
66     OK (GxB_Global_Option_set (GxB_BURBLE, true)) ;
67     OK (GrB_Matrix_new (&C, GrB_FP32, 10, 10)) ;
68 
69     printf ("\nBurble with standard printf/flush:\n") ;
70     GrB_Index nvals ;
71     OK (GrB_Matrix_nvals (&nvals, C)) ;
72     CHECK (nvals == 0) ;
73 
74     OK (GxB_Global_Option_set (GxB_PRINTF, myprintf)) ;
75     OK (GxB_Global_Option_set (GxB_FLUSH, myflush)) ;
76 
77     printf_func_t mypr ;
78     OK (GxB_Global_Option_get (GxB_PRINTF, &mypr)) ;
79     CHECK (mypr == myprintf) ;
80 
81     flush_func_t myfl ;
82     OK (GxB_Global_Option_get (GxB_FLUSH, &myfl)) ;
83     CHECK (myfl == myflush) ;
84 
85     printf ("\nBurble with myprintf/myflush:\n") ;
86     OK (GrB_Matrix_nvals (&nvals, C)) ;
87     CHECK (nvals == 0) ;
88     OK (GxB_Global_Option_set (GxB_BURBLE, false)) ;
89 
90     //--------------------------------------------------------------------------
91     // test GxB_set/get for free_pool_limit
92     //--------------------------------------------------------------------------
93 
94     int64_t free_pool_limit [64] ;
95     OK (GxB_Global_Option_set (GxB_MEMORY_POOL, NULL)) ;
96     OK (GxB_Global_Option_get (GxB_MEMORY_POOL, free_pool_limit)) ;
97     printf ("\ndefault memory pool limits:\n") ;
98     for (int k = 0 ; k < 64 ; k++)
99     {
100         if (free_pool_limit [k] > 0)
101         {
102             printf ("pool %2d: limit %ld\n", k, free_pool_limit [k]) ;
103         }
104     }
105     for (int k = 0 ; k < 64 ; k++)
106     {
107         free_pool_limit [k] = k ;
108     }
109     OK (GxB_Global_Option_set (GxB_MEMORY_POOL, free_pool_limit)) ;
110     OK (GxB_Global_Option_get (GxB_MEMORY_POOL, free_pool_limit)) ;
111     for (int k = 0 ; k < 3 ; k++)
112     {
113         CHECK (free_pool_limit [k] == 0) ;
114     }
115     for (int k = 3 ; k < 64 ; k++)
116     {
117         CHECK (free_pool_limit [k] == k) ;
118     }
119     for (int k = 0 ; k < 64 ; k++)
120     {
121         free_pool_limit [k] = 0 ;
122     }
123     OK (GxB_Global_Option_set (GxB_MEMORY_POOL, free_pool_limit)) ;
124     OK (GxB_Global_Option_get (GxB_MEMORY_POOL, free_pool_limit)) ;
125     for (int k = 0 ; k < 64 ; k++)
126     {
127         CHECK (free_pool_limit [k] == 0) ;
128     }
129 
130     //--------------------------------------------------------------------------
131     // GrB_reduce with invalid binary op
132     //--------------------------------------------------------------------------
133 
134     OK (GrB_Vector_new (&w, GrB_FP32, 10)) ;
135     info = GrB_Matrix_reduce_BinaryOp (w, NULL, NULL, GrB_LT_FP32, C, NULL) ;
136     CHECK (info == GrB_DOMAIN_MISMATCH) ;
137     const char *s ;
138     OK (GrB_error (&s, w)) ;
139     printf ("expected error: [%s]\n", s) ;
140     GrB_Vector_free_(&w) ;
141 
142     //--------------------------------------------------------------------------
143     // wrapup
144     //--------------------------------------------------------------------------
145 
146     GrB_Matrix_free_(&C) ;
147     GB_mx_put_global (true) ;
148     fclose (f) ;
149     printf ("\nGB_mex_about3: all tests passed\n\n") ;
150 }
151 
152