LCOV - code coverage report
Current view: top level - experimental/utility - LAGraph_SWrite.c (source / functions) Hit Total Coverage
Test: LAGraph code coverage report. Commit id: 3b461aa. Current time (UTC): 2024-01-25T16:04:32Z Lines: 33 33 100.0 %
Date: 2024-01-25 16:05:28 Functions: 4 4 100.0 %

          Line data    Source code
       1             : //------------------------------------------------------------------------------
       2             : // LAGraph_SWrite: write a sequence of serialized objects to a file
       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             : #include "LG_internal.h"
      19             : #include "LAGraphX.h"
      20             : 
      21             : #define FPRINT(params)                                  \
      22             : {                                                       \
      23             :     int result = fprintf params ;                       \
      24             :     LG_ASSERT_MSG (result >= 0, -1002, "file not written") ;  \
      25             : }
      26             : 
      27             : //------------------------------------------------------------------------------
      28             : // LAGraph_SWrite_HeaderStart
      29             : //------------------------------------------------------------------------------
      30             : 
      31         208 : int LAGraph_SWrite_HeaderStart  // write the first part of the JSON header
      32             : (
      33             :     FILE *f,                    // file to write to
      34             :     const char *name,           // name of this collection of matrices
      35             :     char *msg
      36             : )
      37             : {
      38             :     // check inputs
      39         208 :     LG_CLEAR_MSG ;
      40         208 :     LG_ASSERT (f != NULL && name != NULL, GrB_NULL_POINTER) ;
      41             : 
      42             :     // write the first part of the JSON header to the file
      43         206 :     FPRINT ((f, "{\n    \"LAGraph\": [%d,%d,%d],\n    \"GraphBLAS\": [ ",
      44             :         LAGRAPH_VERSION_MAJOR, LAGRAPH_VERSION_MINOR, LAGRAPH_VERSION_UPDATE)) ;
      45             : 
      46             :     #if LAGRAPH_SUITESPARSE
      47             : 
      48             :         // SuiteSparse:GraphBLAS v6.0.0 or later
      49             :         char *library ;
      50             :         int ver [3] ;
      51         206 :         GRB_TRY (GxB_get (GxB_LIBRARY_NAME, &library)) ;
      52         206 :         GRB_TRY (GxB_get (GxB_LIBRARY_VERSION, ver)) ;
      53         206 :         FPRINT ((f, "\"%s\", [%d,%d,%d] ],\n", library,
      54             :             ver [0], ver [1], ver [2])) ;
      55             : 
      56             :     #else
      57             : 
      58             :         // some other GraphBLAS library: call it "vanilla 1.0.0"
      59             :         FPRINT ((f, "\"%s\", [%d,%d,%d] ],\n", "vanilla", 1, 0, 0)) ;
      60             : 
      61             :     #endif
      62             : 
      63             :     // write name of this collection and start the list of items
      64         206 :     FPRINT ((f, "    \"%s\":\n    [\n", name)) ;
      65         206 :     return (GrB_SUCCESS) ;
      66             : }
      67             : 
      68             : //------------------------------------------------------------------------------
      69             : // LAGraph_SWrite_HeaderItem
      70             : //------------------------------------------------------------------------------
      71             : 
      72         259 : int LAGraph_SWrite_HeaderItem   // write a single item to the JSON header
      73             : (
      74             :     // inputs:
      75             :     FILE *f,                    // file to write to
      76             :     LAGraph_Contents_kind kind, // matrix, vector, or text
      77             :     const char *name,           // name of the matrix/vector/text; matrices from
      78             :                                 // sparse.tamu.edu use the form "Group/Name"
      79             :     const char *type,           // name of type of the matrix/vector
      80             :     // todo: vectors and text not yet supported by LAGraph_SWrite_HeaderItem
      81             :     int compression,            // text compression method
      82             :     GrB_Index blob_size,        // exact size of serialized blob for this item
      83             :     char *msg
      84             : )
      85             : {
      86             :     // check inputs
      87         259 :     LG_CLEAR_MSG ;
      88         259 :     LG_ASSERT (f != NULL, GrB_NULL_POINTER) ;
      89             : 
      90             :     // write the JSON information for this item
      91         257 :     FPRINT ((f, "        { \"")) ;
      92         257 :     switch (kind)
      93             :     {
      94         256 :         case LAGraph_matrix_kind :
      95         256 :             FPRINT ((f, "GrB_Matrix\": \"%s\", \"type\": \"%s", name, type)) ;
      96         256 :             break ;
      97             : 
      98             :         // todo: handle vectors and text
      99             : #if 0
     100             :         case LAGraph_vector_kind :
     101             :             FPRINT((f, "GrB_Vector\": \"%s\", \"type\": \"%s", name, type)) ;
     102             :             break ;
     103             : 
     104             :         case LAGraph_text_kind :
     105             :             FPRINT ((f, "text\": \"%s\", \"compression\": \"", name)) ;
     106             :             switch (compression)
     107             :             {
     108             :                 case -1   : FPRINT ((f, "none"   )) ; break ;
     109             :                 case 0    : FPRINT ((f, "default")) ; break ;
     110             :                 case 1000 : FPRINT ((f, "lz4"    )) ; break ;
     111             :                 case 2000 : FPRINT ((f, "lz4hc:0")) ; break ;
     112             :                 case 2001 : FPRINT ((f, "lz4hc:1")) ; break ;
     113             :                 case 2002 : FPRINT ((f, "lz4hc:2")) ; break ;
     114             :                 case 2003 : FPRINT ((f, "lz4hc:3")) ; break ;
     115             :                 case 2004 : FPRINT ((f, "lz4hc:4")) ; break ;
     116             :                 case 2005 : FPRINT ((f, "lz4hc:5")) ; break ;
     117             :                 case 2006 : FPRINT ((f, "lz4hc:6")) ; break ;
     118             :                 case 2007 : FPRINT ((f, "lz4hc:7")) ; break ;
     119             :                 case 2008 : FPRINT ((f, "lz4hc:8")) ; break ;
     120             :                 case 2009 : FPRINT ((f, "lz4hc:9")) ; break ;
     121             :                 default   : LG_ASSERT_MSG (false, GrB_INVALID_VALUE,
     122             :                     "invalid compression") ; break ;
     123             :             }
     124             :             break ;
     125             : #endif
     126             : 
     127           1 :         default :
     128           1 :             LG_ASSERT_MSG (false, GrB_INVALID_VALUE, "invalid kind") ;
     129             :             break ;
     130             :     }
     131             : 
     132         256 :     FPRINT ((f, "\", \"bytes\": %" PRIu64 " },\n", blob_size)) ;
     133         256 :     return (GrB_SUCCESS) ;
     134             : }
     135             : 
     136             : //------------------------------------------------------------------------------
     137             : // LAGraph_SWrite_HeaderEnd
     138             : //------------------------------------------------------------------------------
     139             : 
     140         207 : int LAGraph_SWrite_HeaderEnd    // write the end of the JSON header
     141             : (
     142             :     FILE *f,                    // file to write to
     143             :     char *msg
     144             : )
     145             : {
     146             :     // check inputs
     147         207 :     LG_CLEAR_MSG ;
     148         207 :     LG_ASSERT (f != NULL, GrB_NULL_POINTER) ;
     149             : 
     150             :     // finalize the JSON header string
     151         206 :     FPRINT ((f, "        null\n    ]\n}\n")) ;
     152             : 
     153             :     // write a final zero byte to terminate the JSON string
     154         206 :     fputc (0, f) ;
     155         206 :     return (GrB_SUCCESS) ;
     156             : }
     157             : 
     158             : //------------------------------------------------------------------------------
     159             : // LAGraph_SWrite_Item
     160             : //------------------------------------------------------------------------------
     161             : 
     162         258 : int LAGraph_SWrite_Item  // write the serialized blob of a matrix/vector/text
     163             : (
     164             :     // input:
     165             :     FILE *f,                // file to write to
     166             :     const void *blob,       // serialized blob from G*B_Matrix_serialize
     167             :     GrB_Index blob_size,    // exact size of the serialized blob
     168             :     char *msg
     169             : )
     170             : {
     171             :     // check inputs
     172         258 :     LG_CLEAR_MSG ;
     173         258 :     LG_ASSERT (f != NULL && blob != NULL, GrB_NULL_POINTER) ;
     174             : 
     175             :     // write the blob
     176         256 :     size_t blob_written = fwrite (blob, sizeof (uint8_t), blob_size, f) ;
     177         256 :     LG_ASSERT_MSG (blob_written == blob_size, -1001,
     178             :         "file not written properly") ;
     179         256 :     return (GrB_SUCCESS) ;
     180             : }

Generated by: LCOV version 1.14