1 //------------------------------------------------------------------------------
2 // GraphBLAS/Demo/Include/graphblas_demos.h: include file for all demo programs
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 #ifndef GRAPHBLAS_DEMOS_H
11 #define GRAPHBLAS_DEMOS_H
12 
13 //------------------------------------------------------------------------------
14 // manage compiler warnings
15 //------------------------------------------------------------------------------
16 
17 #if defined __INTEL_COMPILER
18 #pragma warning (disable: 58 167 144 177 181 186 188 589 593 869 981 1418 1419 1572 1599 2259 2282 2557 2547 3280 )
19 #elif defined __GNUC__
20 
21 // disable warnings for gcc 5.x and higher:
22 #if (__GNUC__ > 4)
23 // disable warnings
24 // #pragma GCC diagnostic ignored "-Wunknown-warning-option"
25 #pragma GCC diagnostic ignored "-Wint-in-bool-context"
26 #pragma GCC diagnostic ignored "-Wformat-truncation="
27 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
28 // enable these warnings as errors
29 #pragma GCC diagnostic error "-Wmisleading-indentation"
30 #endif
31 
32 #pragma GCC diagnostic ignored "-Wunknown-pragmas"
33 #if !defined ( __cplusplus )
34 #pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
35 #else
36 #pragma GCC diagnostic ignored "-Wwrite-strings"
37 #endif
38 #pragma GCC diagnostic ignored "-Wunused-variable"
39 #pragma GCC diagnostic ignored "-Wunused-result"
40 #pragma GCC diagnostic ignored "-Wunused-parameter"
41 #pragma GCC diagnostic ignored "-Wsign-compare"
42 #pragma GCC diagnostic ignored "-Wtype-limits"
43 
44 // enable these warnings as errors
45 #pragma GCC diagnostic error "-Wswitch-default"
46 #endif
47 
48 #ifndef GB_MICROSOFT
49 #if ( _MSC_VER && !__INTEL_COMPILER )
50 #define GB_MICROSOFT 1
51 #else
52 #define GB_MICROSOFT 0
53 #endif
54 #endif
55 
56 #include "GraphBLAS.h"
57 #include "simple_rand.h"
58 #include "simple_timer.h"
59 #include "usercomplex.h"
60 #include "prand.h"
61 
62 #ifdef MATLAB_MEX_FILE
63 #include "mex.h"
64 #include "matrix.h"
65 #define malloc  mxMalloc
66 #define free    mxFree
67 #define calloc  mxCalloc
68 #define realloc mxRealloc
69 #endif
70 
71 #undef MIN
72 #undef MAX
73 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
74 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
75 
76 GB_PUBLIC
77 GrB_Info bfs5m              // BFS of a graph (using vector assign & reduce)
78 (
79     GrB_Vector *v_output,   // v [i] is the BFS level of node i in the graph
80     const GrB_Matrix A,     // input graph, treated as if boolean in semiring
81     GrB_Index s             // starting node of the BFS
82 ) ;
83 
84 GB_PUBLIC
85 GrB_Info bfs5m_check        // BFS of a graph (using vector assign & reduce)
86 (
87     GrB_Vector *v_output,   // v [i] is the BFS level of node i in the graph
88     const GrB_Matrix A,     // input graph, treated as if boolean in semiring
89     GrB_Index s             // starting node of the BFS
90 ) ;
91 
92 GB_PUBLIC
93 GrB_Info bfs6               // BFS of a graph (using apply)
94 (
95     GrB_Vector *v_output,   // v [i] is the BFS level of node i in the graph
96     const GrB_Matrix A,     // input graph, treated as if boolean in semiring
97     GrB_Index s             // starting node of the BFS
98 ) ;
99 
100 GB_PUBLIC
101 GrB_Info bfs6_check         // BFS of a graph (using apply)
102 (
103     GrB_Vector *v_output,   // v [i] is the BFS level of node i in the graph
104     const GrB_Matrix A,     // input graph, treated as if boolean in semiring
105     GrB_Index s             // starting node of the BFS
106 ) ;
107 
108 GB_PUBLIC
109 GrB_Info read_matrix        // read a double-precision matrix
110 (
111     GrB_Matrix *A,          // handle of matrix to create
112     FILE *f,                // file to read the tuples from
113     bool make_symmetric,    // if true, return A as symmetric
114     bool no_self_edges,     // if true, then remove self edges from A
115     bool one_based,         // if true, input matrix is 1-based
116     bool boolean,           // if true, input is GrB_BOOL, otherwise GrB_FP64
117     bool printstuff         // if true, print status to stdout
118 ) ;
119 
120 GB_PUBLIC
121 GrB_Info mis                    // compute a maximal independent set
122 (
123     GrB_Vector *iset_output,    // iset(i) = true if i is in the set
124     const GrB_Matrix A,         // symmetric Boolean matrix
125     int64_t seed                // random number seed
126 ) ;
127 
128 GB_PUBLIC
129 GrB_Info mis_check              // compute a maximal independent set
130 (
131     GrB_Vector *iset_output,    // iset(i) = true if i is in the set
132     const GrB_Matrix A,         // symmetric Boolean matrix
133     int64_t seed                // random number seed
134 ) ;
135 
136 GB_PUBLIC
137 void mis_score  (void *result, const void *degree) ;
138 
139 GB_PUBLIC
140 void mis_score2 (void *result, const void *degree, const void *xrand) ;
141 
142 GB_PUBLIC
143 GrB_Info random_matrix      // create a random double-precision matrix
144 (
145     GrB_Matrix *A_output,   // handle of matrix to create
146     bool make_symmetric,    // if true, return A as symmetric
147     bool no_self_edges,     // if true, then do not create self edges
148     int64_t nrows,          // number of rows
149     int64_t ncols,          // number of columns
150     int64_t ntuples,        // number of entries (x2 if made symmetric)
151     int method,             // method to use: 0:setElement, 1:build
152     bool A_complex          // if true, create a Complex matrix
153 ) ;
154 
155 GB_PUBLIC
156 GrB_Info get_matrix         // get a matrix from stdin, or create random one
157 (
158     GrB_Matrix *A_output,   // matrix to create
159     int argc,               // command-line arguments
160     char **argv,
161     bool no_self_edges,     // if true, ensure the matrix has no self-edges
162     bool boolean,           // if true, file is read as GrB_BOOL, else GrB_FP64
163     bool spones             // if true, return all entries equal to 1
164 ) ;
165 
166 GB_PUBLIC
167 GrB_Info wathen             // construct a random Wathen matrix
168 (
169     GrB_Matrix *A_output,   // output matrix
170     int64_t nx,             // grid dimension nx
171     int64_t ny,             // grid dimension ny
172     bool scale,             // if true, scale the rows
173     int method,             // 0 to 3
174     double *rho_given       // nx-by-ny dense matrix, if NULL use random rho
175 ) ;
176 
177 GB_PUBLIC
178 GrB_Info triu               // C = triu (A,1)
179 (
180     GrB_Matrix *C_output,   // output matrix
181     const GrB_Matrix A      // input matrix, boolean or double
182 ) ;
183 
184 GB_PUBLIC
185 GrB_Info tricount           // count # of triangles
186 (
187     int64_t *ntri,          // # of triangles in the graph
188     const int method,       // 0 to 4, see above
189     const GrB_Matrix A,     // adjacency matrix for methods 0, 1, and 2
190     const GrB_Matrix E,     // edge incidence matrix for method 0
191     const GrB_Matrix L,     // L=tril(A) for methods 2, 4, and 4
192     const GrB_Matrix U,     // U=triu(A) for methods 2, 3, and 5
193     double t [2]            // t [0]: multiply time, t [1]: reduce time
194 ) ;
195 
196 GB_PUBLIC
197 GrB_Info isequal_type       // return GrB_SUCCESS if successful
198 (
199     bool *result,           // true if A == B, false if A != B or error
200     GrB_Matrix A,
201     GrB_Matrix B,
202     GrB_BinaryOp op         // should be GrB_EQ_<type>, for the type of A and B
203 ) ;
204 
205 GB_PUBLIC
206 GrB_Info isequal            // return GrB_SUCCESS if successful
207 (
208     bool *result,           // true if A == B, false if A != B or error
209     GrB_Matrix A,
210     GrB_Matrix B,
211     GrB_BinaryOp userop     // for A and B with user-defined types.  ignored
212                             // if A and B are of built-in types
213 ) ;
214 
215 //------------------------------------------------------------------------------
216 // page rank
217 //------------------------------------------------------------------------------
218 
219 // dpagerank computes an array of structs for its result
220 typedef struct
221 {
222     double pagerank ;   // the pagerank of a node
223     GrB_Index page ;    // the node number itself
224 }
225 PageRank ;
226 
227 // ipagerank computes an array of structs for its result
228 typedef struct
229 {
230     uint64_t pagerank ;     // the pagerank of a node
231     GrB_Index page ;        // the node number itself
232 }
233 iPageRank ;
234 
235 // using a standard semiring and FP64 arithmetic
236 GB_PUBLIC
237 GrB_Info dpagerank          // GrB_SUCCESS or error condition
238 (
239     PageRank **Phandle,     // output: pointer to array of PageRank structs
240     GrB_Matrix A
241 ) ;
242 
243 // like dpagerank but with user-defined type, operators, and semiring;
244 // also a stopping critirion
245 GB_PUBLIC
246 GrB_Info dpagerank2         // GrB_SUCCESS or error condition
247 (
248     PageRank **Phandle,     // output: pointer to array of PageRank structs
249     GrB_Matrix A,           // input graph, not modified
250     int itermax,            // max number of iterations
251     double tol,             // stop when norm (r-rnew,2) < tol
252     int *iters,             // number of iterations taken
253     GrB_Desc_Value method   // method to use for GrB_vxm (for testing only)
254 ) ;
255 
256 GB_PUBLIC
257 GrB_Info drowscale          // GrB_SUCCESS or error condition
258 (
259     GrB_Matrix *Chandle,    // output matrix C = rowscale (A)
260     GrB_Matrix A            // input matrix, not modified
261 ) ;
262 
263 GB_PUBLIC
264 GrB_Info ipagerank          // GrB_SUCCESS or error condition
265 (
266     iPageRank **Phandle,    // output: pointer to array of iPageRank structs
267     GrB_Matrix A            // input graph, not modified
268 ) ;
269 
270 GB_PUBLIC
271 GrB_Info irowscale          // GrB_SUCCESS or error condition
272 (
273     GrB_Matrix *Chandle,    // output matrix C = rowscale (A)
274     GrB_Matrix A            // input matrix, not modified
275 ) ;
276 
277 // multiplicative scaling factor for ipagerank, ZSCALE = 2^30
278 #define ZSCALE ((uint64_t) 1073741824)
279 
280 //------------------------------------------------------------------------------
281 // import/export test
282 //------------------------------------------------------------------------------
283 
284 GB_PUBLIC
285 GrB_Info import_test (GrB_Matrix *C_handle, int format, bool dump) ;
286 
287 //------------------------------------------------------------------------------
288 // CHECK: expr must be true; if not, return an error condition
289 //------------------------------------------------------------------------------
290 
291 // the #include'ing file must define the FREE_ALL macro
292 
293 #define CHECK(expr,info)                                                \
294 {                                                                       \
295     if (! (expr))                                                       \
296     {                                                                   \
297         /* free the result and all workspace, and return NULL */        \
298         FREE_ALL ;                                                      \
299         printf ("Failure: line %d file %s\n", __LINE__, __FILE__) ;     \
300         return (info) ;                                                 \
301     }                                                                   \
302 }
303 
304 //------------------------------------------------------------------------------
305 // OK  call a GraphBLAS method and check the result
306 //------------------------------------------------------------------------------
307 
308 // OK(method) is a macro that calls a GraphBLAS method and checks the status;
309 // if a failure occurs, it handles the error via the CHECK macro above, and
310 // returns the error status to the caller.
311 
312 #define OK(method)                                                      \
313 {                                                                       \
314     info = method ;                                                     \
315     if (!(info == GrB_SUCCESS || info == GrB_NO_VALUE))                 \
316     {                                                                   \
317         printf ("GraphBLAS error: %d\n", info) ;                        \
318         CHECK (false, info) ;                                           \
319     }                                                                   \
320 }
321 
322 #endif
323