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

          Line data    Source code
       1             : //------------------------------------------------------------------------------
       2             : // LAGraph_SSaveSet: save a set of matrices to a *.lagraph 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             : // LAGraph_SSaveSet saves a set of matrices to a *.lagraph file.
      19             : // The file is created, written to with the JSON header and the serialized
      20             : // matrices, and then closed.  If using SuiteSparse:GraphBLAS, the highest
      21             : // level of compression is used (LZ4HC:9).
      22             : 
      23             : // Use LAGraph_SSLoadSet to load the matrices back in from the file.
      24             : 
      25             : // This method will not work without SuiteSparse:GraphBLAS, because the C API
      26             : // has no GrB* method for querying the GrB_Type (or its name as a string) of a
      27             : // matrix.
      28             : 
      29             : //------------------------------------------------------------------------------
      30             : 
      31             : #define LG_FREE_WORK                                \
      32             : {                                                   \
      33             :     fclose (f) ;                                    \
      34             :     f = NULL ;                                      \
      35             :     GrB_free (&desc) ;                              \
      36             :     LAGraph_SFreeContents (&Contents, nmatrices) ;  \
      37             : }
      38             : 
      39             : #define LG_FREE_ALL                                 \
      40             : {                                                   \
      41             :     LG_FREE_WORK ;                                  \
      42             : }
      43             : 
      44             : #include "LG_internal.h"
      45             : #include "LAGraphX.h"
      46             : 
      47             : //------------------------------------------------------------------------------
      48             : // LAGraph_SSaveSet
      49             : //------------------------------------------------------------------------------
      50             : 
      51           1 : int LAGraph_SSaveSet            // save a set of matrices from a *.lagraph file
      52             : (
      53             :     // inputs:
      54             :     char *filename,             // name of file to write to
      55             :     GrB_Matrix *Set,            // array of GrB_Matrix of size nmatrices
      56             :     GrB_Index nmatrices,        // # of matrices to write to *.lagraph file
      57             :     char *collection,           // name of this collection of matrices
      58             :     char *msg
      59             : )
      60             : {
      61             : 
      62             :     //--------------------------------------------------------------------------
      63             :     // check inputs
      64             :     //--------------------------------------------------------------------------
      65             : 
      66           1 :     LG_CLEAR_MSG ;
      67           1 :     FILE *f = NULL ;
      68             : 
      69           1 :     LAGraph_Contents *Contents = NULL ;
      70           1 :     GrB_Descriptor desc = NULL ;
      71             : 
      72           1 :     LG_ASSERT (filename != NULL && Set != NULL && collection != NULL,
      73             :         GrB_NULL_POINTER) ;
      74             : 
      75             :     #if LAGRAPH_SUITESPARSE
      76           1 :     GRB_TRY (GrB_Descriptor_new (&desc)) ;
      77           1 :     GRB_TRY (GxB_set (desc, GxB_COMPRESSION, GxB_COMPRESSION_LZ4HC + 9)) ;
      78             :     #endif
      79             : 
      80           1 :     f = fopen (filename, "wb") ;
      81           1 :     LG_ASSERT_MSG (f != NULL, -1001, "unable to create output file") ;
      82             : 
      83             :     //--------------------------------------------------------------------------
      84             :     // serialize all the matrices
      85             :     //--------------------------------------------------------------------------
      86             : 
      87             :     // allocate an Contents array of size nmatrices to hold the contents
      88           1 :     LG_TRY (LAGraph_Calloc ((void **) &Contents, nmatrices,
      89             :         sizeof (LAGraph_Contents), msg)) ;
      90             : 
      91          52 :     for (GrB_Index i = 0 ; i < nmatrices ; i++)
      92             :     {
      93             :         #if LAGRAPH_SUITESPARSE
      94             :         {
      95          51 :             GRB_TRY (GxB_Matrix_serialize (&(Contents [i].blob),
      96             :                 (GrB_Index *)&(Contents [i].blob_size), Set [i], desc)) ;
      97             :         }
      98             :         #else
      99             :         {
     100             :             GrB_Index estimate ;
     101             :             GRB_TRY (GrB_Matrix_serializeSize (&estimate, Set [i])) ;
     102             :             Contents [i].blob_size = estimate ;
     103             :             LAGRAPH_TRY (LAGraph_Malloc ((void **) &(Contents [i].blob),
     104             :                 estimate, sizeof (uint8_t), msg)) ;
     105             :             GRB_TRY (GrB_Matrix_serialize (Contents [i].blob,
     106             :                 (GrB_Index *) &(Contents [i].blob_size), Set [i])) ;
     107             :             LG_TRY (LAGraph_Realloc ((void **) &(Contents [i].blob),
     108             :                 (size_t) Contents [i].blob_size,
     109             :                 estimate, sizeof (uint8_t), msg)) ;
     110             :         }
     111             :         #endif
     112             :     }
     113             : 
     114             :     //--------------------------------------------------------------------------
     115             :     // write the header
     116             :     //--------------------------------------------------------------------------
     117             : 
     118           1 :     LG_TRY (LAGraph_SWrite_HeaderStart (f, collection, msg)) ;
     119          52 :     for (GrB_Index i = 0 ; i < nmatrices ; i++)
     120             :     {
     121             :         char typename [GxB_MAX_NAME_LEN] ;
     122          51 :         LG_TRY (LAGraph_Matrix_TypeName (typename, Set [i], msg)) ;
     123             :         char matrix_name [256] ;
     124          51 :         snprintf (matrix_name, 256, "A_%" PRIu64, i) ;
     125          51 :         LG_TRY (LAGraph_SWrite_HeaderItem (f, LAGraph_matrix_kind,
     126             :             matrix_name, typename, 0, Contents [i].blob_size, msg)) ;
     127             :     }
     128           1 :     LG_TRY (LAGraph_SWrite_HeaderEnd (f, msg)) ;
     129             : 
     130             :     //--------------------------------------------------------------------------
     131             :     // write all the blobs
     132             :     //--------------------------------------------------------------------------
     133             : 
     134          52 :     for (GrB_Index i = 0 ; i < nmatrices ; i++)
     135             :     {
     136          51 :         LG_TRY (LAGraph_SWrite_Item (f, Contents [i].blob,
     137             :             Contents [i].blob_size, msg)) ;
     138             :     }
     139             : 
     140             :     //--------------------------------------------------------------------------
     141             :     // free workspace and return result
     142             :     //--------------------------------------------------------------------------
     143             : 
     144           1 :     LG_FREE_WORK ;
     145           1 :     return (GrB_SUCCESS) ;
     146             : }

Generated by: LCOV version 1.14