LCOV - code coverage report
Current view: top level - src/utility - LAGraph_Matrix_Print.c (source / functions) Hit Total Coverage
Test: LAGraph code coverage report. Commit id: cc56ed4. Current time (UTC): 2024-08-30T17:14:30Z Lines: 39 39 100.0 %
Date: 2024-08-30 17:16:41 Functions: 12 12 100.0 %

          Line data    Source code
       1             : //------------------------------------------------------------------------------
       2             : // LAGraph_Matrix_Print:  pretty-print a matrix
       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_Matrix_Print:  pretty-print a matrix.  The type is either derived
      19             : // from GxB_Matrix_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 **) &J, NULL) ; \
      29             :     LAGraph_Free ((void **) &X, NULL) ; \
      30             : }
      31             : 
      32             : #undef  LG_FREE_ALL
      33             : #define LG_FREE_ALL LG_FREE_WORK
      34             : 
      35             : //------------------------------------------------------------------------------
      36             : // LG_Matrix_Print_TYPE: print with the specified type
      37             : //------------------------------------------------------------------------------
      38             : 
      39             : #define LG_MATRIX_PRINT(suffix,ctype,gtype,fmt1,fmt2)                       \
      40             : int LG_Matrix_Print_ ## suffix                                              \
      41             : (                                                                           \
      42             :     GrB_Matrix A, LAGraph_PrintLevel pr, FILE *f, char *msg                 \
      43             : )                                                                           \
      44             : {                                                                           \
      45             :     LG_CLEAR_MSG ;                                                          \
      46             :     ctype *X = NULL ;                                                       \
      47             :     GrB_Index *I = NULL, *J = NULL ;                                        \
      48             :     LG_ASSERT (A != NULL && f != NULL, GrB_NULL_POINTER) ;                  \
      49             :     int prl = (int) pr ;                                                    \
      50             :     if (prl <= 0) return (GrB_SUCCESS) ;                                    \
      51             :     /* get basic properties */                                              \
      52             :     GrB_Index nrows, ncols, nvals ;                                         \
      53             :     GRB_TRY (GrB_Matrix_nrows (&nrows, A)) ;                                \
      54             :     GRB_TRY (GrB_Matrix_ncols (&ncols, A)) ;                                \
      55             :     GRB_TRY (GrB_Matrix_nvals (&nvals, A)) ;                                \
      56             :     /* print header line */                                                 \
      57             :     FPRINTF (f, "%s matrix: %" PRIu64 "-by-%" PRIu64 " entries: %" PRIu64   \
      58             :         "\n", LG_XSTR (gtype), nrows, ncols, nvals) ;                       \
      59             :     if (prl <= 1) return (GrB_SUCCESS) ;                                    \
      60             :     /* extract tuples */                                                    \
      61             :     LG_TRY (LAGraph_Malloc ((void **) &I, nvals, sizeof (GrB_Index), msg)) ;\
      62             :     LG_TRY (LAGraph_Malloc ((void **) &J, nvals, sizeof (GrB_Index), msg)) ;\
      63             :     LG_TRY (LAGraph_Malloc ((void **) &X, nvals, sizeof (ctype), msg)) ;    \
      64             :     GrB_Info info = GrB_Matrix_extractTuples (I, J, X, &nvals, A) ;         \
      65             :     LG_ASSERT_MSG (info != GrB_DOMAIN_MISMATCH,                             \
      66             :         GrB_NOT_IMPLEMENTED, "type not supported") ;                        \
      67             :     GRB_TRY (info) ;                                                        \
      68             :     /* determine the format */                                              \
      69             :     char *format = (prl <= 3) ? fmt1 : fmt2 ;                               \
      70             :     bool summary = (prl == 2 || prl == 4) && (nvals > LG_SHORT_LEN) ;       \
      71             :     for (int64_t k = 0 ; k < nvals ; k++)                                   \
      72             :     {                                                                       \
      73             :         /* print the kth tuple */                                           \
      74             :         GrB_Index i = I [k] ;                                               \
      75             :         GrB_Index j = J [k] ;                                               \
      76             :         ctype     x = X [k] ;                                               \
      77             :         FPRINTF (f, "    (%" PRIu64 ", %" PRIu64 ")   ", i, j) ;            \
      78             :         FPRINTF (f, format, x) ;                                            \
      79             :         FPRINTF (f, "\n") ;                                                 \
      80             :         if (summary && k > LG_SHORT_LEN)                                    \
      81             :         {                                                                   \
      82             :             /* quit early if a only a summary is requested */               \
      83             :             FPRINTF (f, "    ...\n") ;                                      \
      84             :             break ;                                                         \
      85             :         }                                                                   \
      86             :     }                                                                       \
      87             :     LG_FREE_WORK ;                                                          \
      88             :     return (GrB_SUCCESS) ;                                                  \
      89             : }
      90             : 
      91        6322 : LG_MATRIX_PRINT (BOOL  , bool    , GrB_BOOL  , "%d"  , "%d"    ) ;
      92         100 : LG_MATRIX_PRINT (INT8  , int8_t  , GrB_INT8  , "%d"  , "%d"    ) ;
      93         113 : LG_MATRIX_PRINT (INT16 , int16_t , GrB_INT16 , "%d"  , "%d"    ) ;
      94         566 : LG_MATRIX_PRINT (INT32 , int32_t , GrB_INT32 , "%" PRId32, "%" PRId32  ) ;
      95         409 : LG_MATRIX_PRINT (INT64 , int64_t , GrB_INT64 , "%" PRId64, "%" PRId64  ) ;
      96          64 : LG_MATRIX_PRINT (UINT8 , uint8_t , GrB_UINT8 , "%d"  , "%d"    ) ;
      97          64 : LG_MATRIX_PRINT (UINT16, uint16_t, GrB_UINT16, "%d"  , "%d"    ) ;
      98         221 : LG_MATRIX_PRINT (UINT32, uint32_t, GrB_UINT32, "%" PRIu32, "%" PRIu32  ) ;
      99         288 : LG_MATRIX_PRINT (UINT64, uint64_t, GrB_UINT64, "%" PRIu64, "%" PRIu64  ) ;
     100         156 : LG_MATRIX_PRINT (FP32  , float   , GrB_FP32  , "%g"  , "%0.7g" ) ;
     101        4421 : LG_MATRIX_PRINT (FP64  , double  , GrB_FP64  , "%g"  , "%0.15g") ;
     102             : #if 0
     103             : LG_MATRIX_PRINT (FC32  , GxB_FC32_t, GxB_FC32, ...) ;
     104             : LG_MATRIX_PRINT (FC64  , GxB_FC64_t, GxB_FC64, ...) ;
     105             : #endif
     106             : 
     107             : #undef  LG_FREE_WORK
     108             : #define LG_FREE_WORK ;
     109             : #undef  LG_FREE_ALL
     110             : #define LG_FREE_ALL ;
     111             : 
     112             : //------------------------------------------------------------------------------
     113             : // LAGraph_Matrix_Print: automatically determine the type
     114             : //------------------------------------------------------------------------------
     115             : 
     116         738 : int LAGraph_Matrix_Print
     117             : (
     118             :     // input:
     119             :     const GrB_Matrix A,     // matrix to pretty-print to the file
     120             :     LAGraph_PrintLevel pr,  // print level (0 to 5)
     121             :     FILE *f,            // file to write it to, must be already open; use
     122             :                         // stdout or stderr to print to those locations.
     123             :     char *msg
     124             : )
     125             : {
     126             : 
     127             :     //--------------------------------------------------------------------------
     128             :     // check inputs
     129             :     //--------------------------------------------------------------------------
     130             : 
     131         738 :     LG_CLEAR_MSG ;
     132         738 :     LG_ASSERT (A != NULL && f != NULL, GrB_NULL_POINTER) ;
     133             : 
     134             :     //--------------------------------------------------------------------------
     135             :     // determine the type
     136             :     //--------------------------------------------------------------------------
     137             : 
     138             :     GrB_Type type ;
     139             :     char typename [LAGRAPH_MAX_NAME_LEN] ;
     140         738 :     LG_TRY (LAGraph_Matrix_TypeName (typename, A, msg)) ;
     141         738 :     LG_TRY (LAGraph_TypeFromName (&type, typename, msg)) ;
     142             : 
     143             :     //--------------------------------------------------------------------------
     144             :     // print the matrix
     145             :     //--------------------------------------------------------------------------
     146             : 
     147         738 :     if (type == GrB_BOOL)
     148             :     {
     149         325 :         return (LG_Matrix_Print_BOOL (A, pr, f, msg)) ;
     150             :     }
     151         413 :     else if (type == GrB_INT8)
     152             :     {
     153          12 :         return (LG_Matrix_Print_INT8 (A, pr, f, msg)) ;
     154             :     }
     155         401 :     else if (type == GrB_INT16)
     156             :     {
     157          13 :         return (LG_Matrix_Print_INT16 (A, pr, f, msg)) ;
     158             :     }
     159         388 :     else if (type == GrB_INT32)
     160             :     {
     161          82 :         return (LG_Matrix_Print_INT32 (A, pr, f, msg)) ;
     162             :     }
     163         306 :     else if (type == GrB_INT64)
     164             :     {
     165          43 :         return (LG_Matrix_Print_INT64 (A, pr, f, msg)) ;
     166             :     }
     167         263 :     else if (type == GrB_UINT8)
     168             :     {
     169           8 :         return (LG_Matrix_Print_UINT8 (A, pr, f, msg)) ;
     170             :     }
     171         255 :     else if (type == GrB_UINT16)
     172             :     {
     173           8 :         return (LG_Matrix_Print_UINT16 (A, pr, f, msg)) ;
     174             :     }
     175         247 :     else if (type == GrB_UINT32)
     176             :     {
     177           9 :         return (LG_Matrix_Print_UINT32 (A, pr, f, msg)) ;
     178             :     }
     179         238 :     else if (type == GrB_UINT64)
     180             :     {
     181          19 :         return (LG_Matrix_Print_UINT64 (A, pr, f, msg)) ;
     182             :     }
     183         219 :     else if (type == GrB_FP32)
     184             :     {
     185          15 :         return (LG_Matrix_Print_FP32 (A, pr, f, msg)) ;
     186             :     }
     187         204 :     else if (type == GrB_FP64)
     188             :     {
     189         203 :         return (LG_Matrix_Print_FP64 (A, pr, f, msg)) ;
     190             :     }
     191             :     #if 0
     192             :     else if (type == GxB_FC32)
     193             :     {
     194             :         return (LG_Matrix_Print_FC32 (A, pr, f, msg)) ;
     195             :     }
     196             :     else if (type == GxB_FC32)
     197             :     {
     198             :         return (LG_Matrix_Print_FC64 (A, pr, f, msg)) ;
     199             :     }
     200             :     #endif
     201             :     else
     202             :     {
     203           1 :         LG_ASSERT_MSG (false,
     204             :             GrB_NOT_IMPLEMENTED, "user-defined types not supported") ;
     205             :         return (GrB_SUCCESS) ;
     206             :     }
     207             : }

Generated by: LCOV version 1.14