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. The type is either derived
19 : // from GxB_Vector_type (if available) or assumed to be GrB_FP64 otherwise,
20 : // or passed in as a parameter.
21 :
22 : #include "LG_internal.h"
23 :
24 : #undef LG_FREE_WORK
25 : #define LG_FREE_WORK \
26 : { \
27 : LAGraph_Free ((void **) &I, NULL) ; \
28 : LAGraph_Free ((void **) &X, NULL) ; \
29 : }
30 :
31 : #undef LG_FREE_ALL
32 : #define LG_FREE_ALL LG_FREE_WORK
33 :
34 : //------------------------------------------------------------------------------
35 : // LG_Vector_Print_TYPE: print with the specified type
36 : //------------------------------------------------------------------------------
37 :
38 : #define LG_VECTOR_PRINT(suffix,ctype,gtype,fmt1,fmt2) \
39 : int LG_Vector_Print_ ## suffix \
40 : ( \
41 : GrB_Vector v, LAGraph_PrintLevel pr, FILE *f, char *msg \
42 : ) \
43 : { \
44 : LG_CLEAR_MSG ; \
45 : ctype *X = NULL ; \
46 : GrB_Index *I = NULL ; \
47 : LG_ASSERT (v != NULL, GrB_NULL_POINTER) ; \
48 : LG_ASSERT (f != NULL, GrB_NULL_POINTER) ; \
49 : int prl = (int) pr ; \
50 : if (prl <= 0) return (GrB_SUCCESS) ; \
51 : /* get basic properties */ \
52 : GrB_Index n, nvals ; \
53 : GRB_TRY (GrB_Vector_size (&n, v)) ; \
54 : GRB_TRY (GrB_Vector_nvals (&nvals, v)) ; \
55 : /* print header line */ \
56 : FPRINTF (f, "%s vector: n: %" PRIu64 " entries: %" PRIu64 \
57 : "\n", LG_XSTR (gtype), n, nvals) ; \
58 : if (prl <= 1) return (GrB_SUCCESS) ; \
59 : /* extract tuples */ \
60 : LG_TRY (LAGraph_Malloc ((void **) &I, nvals, sizeof (GrB_Index), msg)) ;\
61 : LG_TRY (LAGraph_Malloc ((void **) &X, nvals, sizeof (ctype), msg)) ; \
62 : GrB_Info info = GrB_Vector_extractTuples (I, X, &nvals, v) ; \
63 : LG_ASSERT_MSG (info != GrB_DOMAIN_MISMATCH, \
64 : GrB_NOT_IMPLEMENTED, "type not supported") ; \
65 : GRB_TRY (info) ; \
66 : /* determine the format */ \
67 : char *format = (prl <= 3) ? fmt1 : fmt2 ; \
68 : bool summary = (prl == 2 || prl == 4) && (nvals > LG_SHORT_LEN) ; \
69 : for (int64_t k = 0 ; k < nvals ; k++) \
70 : { \
71 : /* print the kth tuple */ \
72 : GrB_Index i = I [k] ; \
73 : ctype x = X [k] ; \
74 : FPRINTF (f, " (%" PRIu64 ") ", i) ; \
75 : FPRINTF (f, format, x) ; \
76 : FPRINTF (f, "\n") ; \
77 : if (summary && k > LG_SHORT_LEN) \
78 : { \
79 : /* quit early if a only a summary is requested */ \
80 : FPRINTF (f, " ...\n") ; \
81 : break ; \
82 : } \
83 : } \
84 : LG_FREE_WORK ; \
85 : return (GrB_SUCCESS) ; \
86 : }
87 :
88 114 : LG_VECTOR_PRINT (BOOL , bool , GrB_BOOL , "%d" , "%d" ) ;
89 66 : LG_VECTOR_PRINT (INT8 , int8_t , GrB_INT8 , "%d" , "%d" ) ;
90 66 : LG_VECTOR_PRINT (INT16 , int16_t , GrB_INT16 , "%d" , "%d" ) ;
91 553 : LG_VECTOR_PRINT (INT32 , int32_t , GrB_INT32 , "%" PRId32, "%" PRId32 ) ;
92 3317 : LG_VECTOR_PRINT (INT64 , int64_t , GrB_INT64 , "%" PRId64, "%" PRId64 ) ;
93 66 : LG_VECTOR_PRINT (UINT8 , uint8_t , GrB_UINT8 , "%d" , "%d" ) ;
94 66 : LG_VECTOR_PRINT (UINT16, uint16_t, GrB_UINT16, "%d" , "%d" ) ;
95 339 : LG_VECTOR_PRINT (UINT32, uint32_t, GrB_UINT32, "%" PRIu32, "%" PRIu32 ) ;
96 1594 : LG_VECTOR_PRINT (UINT64, uint64_t, GrB_UINT64, "%" PRIu64, "%" PRIu64 ) ;
97 88 : LG_VECTOR_PRINT (FP32 , float , GrB_FP32 , "%g" , "%0.7g" ) ;
98 980 : LG_VECTOR_PRINT (FP64 , double , GrB_FP64 , "%g" , "%0.15g") ;
99 : #if 0
100 : // would need to pass in an iscomplex flag to print creal(x) and cimag(x)
101 : LG_VECTOR_PRINT (FC32 , GxB_FC32_t, GxB_FC32, ...) ;
102 : LG_VECTOR_PRINT (FC64 , GxB_FC64_t, GxB_FC64, ...) ;
103 : #endif
104 :
105 : #undef LG_FREE_WORK
106 : #define LG_FREE_WORK ;
107 : #undef LG_FREE_ALL
108 : #define LG_FREE_ALL ;
109 :
110 : //------------------------------------------------------------------------------
111 : // LAGraph_Vector_Print: automatically determine the type
112 : //------------------------------------------------------------------------------
113 :
114 552 : int LAGraph_Vector_Print
115 : (
116 : // input:
117 : const GrB_Vector v, // vector to pretty-print to the file
118 : LAGraph_PrintLevel pr, // print level (0 to 5)
119 : FILE *f, // file to write it to, must be already open; use
120 : // stdout or stderr to print to those locations.
121 : char *msg
122 : )
123 : {
124 :
125 : //--------------------------------------------------------------------------
126 : // check inputs
127 : //--------------------------------------------------------------------------
128 :
129 552 : LG_CLEAR_MSG ;
130 552 : LG_ASSERT (v != NULL, GrB_NULL_POINTER) ;
131 552 : LG_ASSERT (f != NULL, GrB_NULL_POINTER) ;
132 :
133 : //--------------------------------------------------------------------------
134 : // determine the type
135 : //--------------------------------------------------------------------------
136 :
137 : GrB_Type type ;
138 : char typename [LAGRAPH_MAX_NAME_LEN] ;
139 551 : LG_TRY (LAGraph_Vector_TypeName (typename, v, msg)) ;
140 551 : LG_TRY (LAGraph_TypeFromName (&type, typename, msg)) ;
141 :
142 : //--------------------------------------------------------------------------
143 : // print the vector
144 : //--------------------------------------------------------------------------
145 :
146 551 : if (type == GrB_BOOL)
147 : {
148 8 : return (LG_Vector_Print_BOOL (v, pr, f, msg)) ;
149 : }
150 543 : else if (type == GrB_INT8)
151 : {
152 4 : return (LG_Vector_Print_INT8 (v, pr, f, msg)) ;
153 : }
154 539 : else if (type == GrB_INT16)
155 : {
156 4 : return (LG_Vector_Print_INT16 (v, pr, f, msg)) ;
157 : }
158 535 : else if (type == GrB_INT32)
159 : {
160 33 : return (LG_Vector_Print_INT32 (v, pr, f, msg)) ;
161 : }
162 502 : else if (type == GrB_INT64)
163 : {
164 317 : return (LG_Vector_Print_INT64 (v, pr, f, msg)) ;
165 : }
166 185 : else if (type == GrB_UINT8)
167 : {
168 4 : return (LG_Vector_Print_UINT8 (v, pr, f, msg)) ;
169 : }
170 181 : else if (type == GrB_UINT16)
171 : {
172 4 : return (LG_Vector_Print_UINT16 (v, pr, f, msg)) ;
173 : }
174 177 : else if (type == GrB_UINT32)
175 : {
176 23 : return (LG_Vector_Print_UINT32 (v, pr, f, msg)) ;
177 : }
178 154 : else if (type == GrB_UINT64)
179 : {
180 95 : return (LG_Vector_Print_UINT64 (v, pr, f, msg)) ;
181 : }
182 59 : else if (type == GrB_FP32)
183 : {
184 6 : return (LG_Vector_Print_FP32 (v, pr, f, msg)) ;
185 : }
186 53 : else if (type == GrB_FP64)
187 : {
188 52 : return (LG_Vector_Print_FP64 (v, pr, f, msg)) ;
189 : }
190 : #if 0
191 : else if (type == GxB_FC32)
192 : {
193 : return (LG_Vector_Print_FC32 (v, pr, f, msg)) ;
194 : }
195 : else if (type == GxB_FC32)
196 : {
197 : return (LG_Vector_Print_FC64 (v, pr, f, msg)) ;
198 : }
199 : #endif
200 : else
201 : {
202 1 : LG_ASSERT_MSG (false,
203 : GrB_NOT_IMPLEMENTED, "user-defined types not supported") ;
204 : return (GrB_SUCCESS) ;
205 : }
206 : }
|