Line data Source code
1 : //------------------------------------------------------------------------------
2 : // LAGraph_Vector_Print: pretty-print a vector
3 : //------------------------------------------------------------------------------
4 :
5 : // LAGraph, (c) 2019-2022 by The LAGraph Contributors, All Rights Reserved.
6 : // SPDX-License-Identifier: BSD-2-Clause
7 : //
8 : // For additional details (including references to third party source code and
9 : // other files) see the LICENSE file or contact permission@sei.cmu.edu. See
10 : // Contributors.txt for a full list of contributors. Created, in part, with
11 : // funding and support from the U.S. Government (see Acknowledgments.txt file).
12 : // DM22-0790
13 :
14 : // Contributed by Timothy A. Davis, Texas A&M University
15 :
16 : //------------------------------------------------------------------------------
17 :
18 : // LAGraph_Vector_Print: pretty-print a vector.
19 :
20 : #include "LG_internal.h"
21 :
22 : #undef LG_FREE_WORK
23 : #define LG_FREE_WORK \
24 : { \
25 : LAGraph_Free ((void **) &I, NULL) ; \
26 : LAGraph_Free ((void **) &X, NULL) ; \
27 : }
28 :
29 : #undef LG_FREE_ALL
30 : #define LG_FREE_ALL LG_FREE_WORK
31 :
32 : //------------------------------------------------------------------------------
33 : // LG_Vector_Print_TYPE: print with the specified type
34 : //------------------------------------------------------------------------------
35 :
36 : #define LG_VECTOR_PRINT(suffix,ctype,gtype,fmt1,fmt2) \
37 : int LG_Vector_Print_ ## suffix \
38 : ( \
39 : GrB_Vector v, LAGraph_PrintLevel pr, FILE *f, char *msg \
40 : ) \
41 : { \
42 : LG_CLEAR_MSG ; \
43 : ctype *X = NULL ; \
44 : GrB_Index *I = NULL ; \
45 : LG_ASSERT (v != NULL, GrB_NULL_POINTER) ; \
46 : LG_ASSERT (f != NULL, GrB_NULL_POINTER) ; \
47 : int prl = (int) pr ; \
48 : if (prl <= 0) return (GrB_SUCCESS) ; \
49 : /* get basic properties */ \
50 : GrB_Index n, nvals ; \
51 : GRB_TRY (GrB_Vector_size (&n, v)) ; \
52 : GRB_TRY (GrB_Vector_nvals (&nvals, v)) ; \
53 : /* print header line */ \
54 : FPRINTF (f, "%s vector: n: %" PRIu64 " entries: %" PRIu64 \
55 : "\n", LG_XSTR (gtype), n, nvals) ; \
56 : if (prl <= 1) return (GrB_SUCCESS) ; \
57 : /* extract tuples */ \
58 : LG_TRY (LAGraph_Malloc ((void **) &I, nvals, sizeof (GrB_Index), msg)) ;\
59 : LG_TRY (LAGraph_Malloc ((void **) &X, nvals, sizeof (ctype), msg)) ; \
60 : GrB_Info info = GrB_Vector_extractTuples (I, X, &nvals, v) ; \
61 : LG_ASSERT_MSG (info != GrB_DOMAIN_MISMATCH, \
62 : GrB_NOT_IMPLEMENTED, "type not supported") ; \
63 : GRB_TRY (info) ; \
64 : /* determine the format */ \
65 : char *format = (prl <= 3) ? fmt1 : fmt2 ; \
66 : bool summary = (prl == 2 || prl == 4) && (nvals > LG_SHORT_LEN) ; \
67 : for (int64_t k = 0 ; k < nvals ; k++) \
68 : { \
69 : /* print the kth tuple */ \
70 : GrB_Index i = I [k] ; \
71 : ctype x = X [k] ; \
72 : FPRINTF (f, " (%" PRIu64 ") ", i) ; \
73 : FPRINTF (f, format, x) ; \
74 : FPRINTF (f, "\n") ; \
75 : if (summary && k > LG_SHORT_LEN) \
76 : { \
77 : /* quit early if a only a summary is requested */ \
78 : FPRINTF (f, " ...\n") ; \
79 : break ; \
80 : } \
81 : } \
82 : LG_FREE_WORK ; \
83 : return (GrB_SUCCESS) ; \
84 : }
85 :
86 114 : LG_VECTOR_PRINT (BOOL , bool , GrB_BOOL , "%d" , "%d" )
87 66 : LG_VECTOR_PRINT (INT8 , int8_t , GrB_INT8 , "%d" , "%d" )
88 66 : LG_VECTOR_PRINT (INT16 , int16_t , GrB_INT16 , "%d" , "%d" )
89 1312 : LG_VECTOR_PRINT (INT32 , int32_t , GrB_INT32 , "%" PRId32, "%" PRId32 )
90 3646 : LG_VECTOR_PRINT (INT64 , int64_t , GrB_INT64 , "%" PRId64, "%" PRId64 )
91 66 : LG_VECTOR_PRINT (UINT8 , uint8_t , GrB_UINT8 , "%d" , "%d" )
92 66 : LG_VECTOR_PRINT (UINT16, uint16_t, GrB_UINT16, "%d" , "%d" )
93 86 : LG_VECTOR_PRINT (UINT32, uint32_t, GrB_UINT32, "%" PRIu32, "%" PRIu32 )
94 1756 : LG_VECTOR_PRINT (UINT64, uint64_t, GrB_UINT64, "%" PRIu64, "%" PRIu64 )
95 562 : LG_VECTOR_PRINT (FP32 , float , GrB_FP32 , "%g" , "%0.7g" )
96 1137 : LG_VECTOR_PRINT (FP64 , double , GrB_FP64 , "%g" , "%0.15g")
97 : #if 0
98 : // would need to pass in an iscomplex flag to print creal(x) and cimag(x)
99 : LG_VECTOR_PRINT (FC32 , GxB_FC32_t, GxB_FC32, ...)
100 : LG_VECTOR_PRINT (FC64 , GxB_FC64_t, GxB_FC64, ...)
101 : #endif
102 :
103 : #undef LG_FREE_WORK
104 : #define LG_FREE_WORK ;
105 : #undef LG_FREE_ALL
106 : #define LG_FREE_ALL ;
107 :
108 : //------------------------------------------------------------------------------
109 : // LAGraph_Vector_Print: automatically determine the type
110 : //------------------------------------------------------------------------------
111 :
112 652 : int LAGraph_Vector_Print
113 : (
114 : // input:
115 : const GrB_Vector v, // vector to pretty-print to the file
116 : LAGraph_PrintLevel pr, // print level (0 to 5)
117 : FILE *f, // file to write it to, must be already open; use
118 : // stdout or stderr to print to those locations.
119 : char *msg
120 : )
121 : {
122 :
123 : //--------------------------------------------------------------------------
124 : // check inputs
125 : //--------------------------------------------------------------------------
126 :
127 652 : LG_CLEAR_MSG ;
128 652 : LG_ASSERT (v != NULL, GrB_NULL_POINTER) ;
129 652 : LG_ASSERT (f != NULL, GrB_NULL_POINTER) ;
130 :
131 : //--------------------------------------------------------------------------
132 : // determine the type
133 : //--------------------------------------------------------------------------
134 :
135 : int32_t typecode ;
136 651 : GRB_TRY (GrB_get (v, &typecode, GrB_EL_TYPE_CODE)) ;
137 :
138 : //--------------------------------------------------------------------------
139 : // print the vector
140 : //--------------------------------------------------------------------------
141 :
142 651 : switch (typecode)
143 : {
144 8 : case GrB_BOOL_CODE : return (LG_Vector_Print_BOOL (v, pr, f, msg)) ;
145 4 : case GrB_INT8_CODE : return (LG_Vector_Print_INT8 (v, pr, f, msg)) ;
146 4 : case GrB_INT16_CODE : return (LG_Vector_Print_INT16 (v, pr, f, msg)) ;
147 89 : case GrB_INT32_CODE : return (LG_Vector_Print_INT32 (v, pr, f, msg)) ;
148 338 : case GrB_INT64_CODE : return (LG_Vector_Print_INT64 (v, pr, f, msg)) ;
149 4 : case GrB_UINT8_CODE : return (LG_Vector_Print_UINT8 (v, pr, f, msg)) ;
150 4 : case GrB_UINT16_CODE : return (LG_Vector_Print_UINT16 (v, pr, f, msg)) ;
151 6 : case GrB_UINT32_CODE : return (LG_Vector_Print_UINT32 (v, pr, f, msg)) ;
152 106 : case GrB_UINT64_CODE : return (LG_Vector_Print_UINT64 (v, pr, f, msg)) ;
153 25 : case GrB_FP32_CODE : return (LG_Vector_Print_FP32 (v, pr, f, msg)) ;
154 62 : case GrB_FP64_CODE : return (LG_Vector_Print_FP64 (v, pr, f, msg)) ;
155 : // case GxB_FC32_CODE : return (LG_Vector_Print_FC32 (v, pr, f, msg)) ;
156 : // case GxB_FC64_CODE : return (LG_Vector_Print_FC64 (v, pr, f, msg)) ;
157 1 : default :
158 1 : LG_ASSERT_MSG (false,
159 : GrB_NOT_IMPLEMENTED, "user-defined types not supported") ;
160 : return (GrB_NOT_IMPLEMENTED) ;
161 : }
162 : }
163 :
|