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: 7b9d2ee. Current time (UTC): 2025-06-03T21:57:17Z Lines: 29 29 100.0 %
Date: 2025-06-03 22:02:40 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.
      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 **) &J, NULL) ; \
      27             :     LAGraph_Free ((void **) &X, NULL) ; \
      28             : }
      29             : 
      30             : #undef  LG_FREE_ALL
      31             : #define LG_FREE_ALL LG_FREE_WORK
      32             : 
      33             : //------------------------------------------------------------------------------
      34             : // LG_Matrix_Print_TYPE: print with the specified type
      35             : //------------------------------------------------------------------------------
      36             : 
      37             : #define LG_MATRIX_PRINT(suffix,ctype,gtype,fmt1,fmt2)                       \
      38             : int LG_Matrix_Print_ ## suffix                                              \
      39             : (                                                                           \
      40             :     GrB_Matrix A, LAGraph_PrintLevel pr, FILE *f, char *msg                 \
      41             : )                                                                           \
      42             : {                                                                           \
      43             :     LG_CLEAR_MSG ;                                                          \
      44             :     ctype *X = NULL ;                                                       \
      45             :     GrB_Index *I = NULL, *J = NULL ;                                        \
      46             :     LG_ASSERT (A != NULL && f != NULL, GrB_NULL_POINTER) ;                  \
      47             :     int prl = (int) pr ;                                                    \
      48             :     if (prl <= 0) return (GrB_SUCCESS) ;                                    \
      49             :     /* get basic properties */                                              \
      50             :     GrB_Index nrows, ncols, nvals ;                                         \
      51             :     GRB_TRY (GrB_Matrix_nrows (&nrows, A)) ;                                \
      52             :     GRB_TRY (GrB_Matrix_ncols (&ncols, A)) ;                                \
      53             :     GRB_TRY (GrB_Matrix_nvals (&nvals, A)) ;                                \
      54             :     /* print header line */                                                 \
      55             :     FPRINTF (f, "%s matrix: %" PRIu64 "-by-%" PRIu64 " entries: %" PRIu64   \
      56             :         "\n", LG_XSTR (gtype), nrows, ncols, nvals) ;                       \
      57             :     if (prl <= 1) return (GrB_SUCCESS) ;                                    \
      58             :     /* extract tuples */                                                    \
      59             :     LG_TRY (LAGraph_Malloc ((void **) &I, nvals, sizeof (GrB_Index), msg)) ;\
      60             :     LG_TRY (LAGraph_Malloc ((void **) &J, nvals, sizeof (GrB_Index), msg)) ;\
      61             :     LG_TRY (LAGraph_Malloc ((void **) &X, nvals, sizeof (ctype), msg)) ;    \
      62             :     GrB_Info info = GrB_Matrix_extractTuples (I, J, X, &nvals, A) ;         \
      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             :         GrB_Index j = J [k] ;                                               \
      74             :         ctype     x = X [k] ;                                               \
      75             :         FPRINTF (f, "    (%" PRIu64 ", %" PRIu64 ")   ", i, j) ;            \
      76             :         FPRINTF (f, format, x) ;                                            \
      77             :         FPRINTF (f, "\n") ;                                                 \
      78             :         if (summary && k > LG_SHORT_LEN)                                    \
      79             :         {                                                                   \
      80             :             /* quit early if a only a summary is requested */               \
      81             :             FPRINTF (f, "    ...\n") ;                                      \
      82             :             break ;                                                         \
      83             :         }                                                                   \
      84             :     }                                                                       \
      85             :     LG_FREE_WORK ;                                                          \
      86             :     return (GrB_SUCCESS) ;                                                  \
      87             : }
      88             : 
      89        6823 : LG_MATRIX_PRINT (BOOL  , bool    , GrB_BOOL  , "%d"  , "%d"    ) ;
      90          99 : LG_MATRIX_PRINT (INT8  , int8_t  , GrB_INT8  , "%d"  , "%d"    ) ;
      91         112 : LG_MATRIX_PRINT (INT16 , int16_t , GrB_INT16 , "%d"  , "%d"    ) ;
      92        1933 : LG_MATRIX_PRINT (INT32 , int32_t , GrB_INT32 , "%" PRId32, "%" PRId32  ) ;
      93         503 : LG_MATRIX_PRINT (INT64 , int64_t , GrB_INT64 , "%" PRId64, "%" PRId64  ) ;
      94          63 : LG_MATRIX_PRINT (UINT8 , uint8_t , GrB_UINT8 , "%d"  , "%d"    ) ;
      95          63 : LG_MATRIX_PRINT (UINT16, uint16_t, GrB_UINT16, "%d"  , "%d"    ) ;
      96         220 : LG_MATRIX_PRINT (UINT32, uint32_t, GrB_UINT32, "%" PRIu32, "%" PRIu32  ) ;
      97         287 : LG_MATRIX_PRINT (UINT64, uint64_t, GrB_UINT64, "%" PRIu64, "%" PRIu64  ) ;
      98         251 : LG_MATRIX_PRINT (FP32  , float   , GrB_FP32  , "%g"  , "%0.7g" ) ;
      99        4808 : LG_MATRIX_PRINT (FP64  , double  , GrB_FP64  , "%g"  , "%0.15g") ;
     100             : // LG_MATRIX_PRINT (FC32  , GxB_FC32_t, GxB_FC32, ...) ;
     101             : // LG_MATRIX_PRINT (FC64  , GxB_FC64_t, GxB_FC64, ...) ;
     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_Matrix_Print: automatically determine the type
     110             : //------------------------------------------------------------------------------
     111             : 
     112         778 : int LAGraph_Matrix_Print
     113             : (
     114             :     // input:
     115             :     const GrB_Matrix A,     // matrix 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         778 :     LG_CLEAR_MSG ;
     128         778 :     LG_ASSERT (A != NULL && f != NULL, GrB_NULL_POINTER) ;
     129             : 
     130             :     //--------------------------------------------------------------------------
     131             :     // determine the type
     132             :     //--------------------------------------------------------------------------
     133             : 
     134             :     int32_t typecode ;
     135         778 :     GRB_TRY (GrB_get (A, &typecode, GrB_EL_TYPE_CODE)) ;
     136             : 
     137             :     //--------------------------------------------------------------------------
     138             :     // print the matrix
     139             :     //--------------------------------------------------------------------------
     140             : 
     141         778 :     switch (typecode)
     142             :     {
     143         334 :         case GrB_BOOL_CODE   : return (LG_Matrix_Print_BOOL (A, pr, f, msg)) ;
     144          12 :         case GrB_INT8_CODE   : return (LG_Matrix_Print_INT8 (A, pr, f, msg)) ;
     145          13 :         case GrB_INT16_CODE  : return (LG_Matrix_Print_INT16 (A, pr, f, msg)) ;
     146         102 :         case GrB_INT32_CODE  : return (LG_Matrix_Print_INT32 (A, pr, f, msg)) ;
     147          46 :         case GrB_INT64_CODE  : return (LG_Matrix_Print_INT64 (A, pr, f, msg)) ;
     148           8 :         case GrB_UINT8_CODE  : return (LG_Matrix_Print_UINT8 (A, pr, f, msg)) ;
     149           8 :         case GrB_UINT16_CODE : return (LG_Matrix_Print_UINT16 (A, pr, f, msg)) ;
     150           9 :         case GrB_UINT32_CODE : return (LG_Matrix_Print_UINT32 (A, pr, f, msg)) ;
     151          19 :         case GrB_UINT64_CODE : return (LG_Matrix_Print_UINT64 (A, pr, f, msg)) ;
     152          18 :         case GrB_FP32_CODE   : return (LG_Matrix_Print_FP32 (A, pr, f, msg)) ;
     153         208 :         case GrB_FP64_CODE   : return (LG_Matrix_Print_FP64 (A, pr, f, msg)) ;
     154             : //      case GxB_FC32_CODE   : return (LG_Matrix_Print_FC32 (A, pr, f, msg)) ;
     155             : //      case GxB_FC64_CODE   : return (LG_Matrix_Print_FC64 (A, pr, f, msg)) ;
     156           1 :         default              :
     157           1 :             LG_ASSERT_MSG (false,
     158             :                 GrB_NOT_IMPLEMENTED, "user-defined types not supported") ;
     159             :             return (GrB_NOT_IMPLEMENTED) ;
     160             :     }
     161             : }
     162             : 

Generated by: LCOV version 1.14