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 : if (f != NULL) fclose (f) ; \
34 : f = NULL ; \
35 : LAGraph_SFreeContents (&Contents, nmatrices) ; \
36 : }
37 :
38 : #define LG_FREE_ALL \
39 : { \
40 : LG_FREE_WORK ; \
41 : }
42 :
43 : #include "LG_internal.h"
44 : #include "LAGraphX.h"
45 :
46 : //------------------------------------------------------------------------------
47 : // LAGraph_SSaveSet
48 : //------------------------------------------------------------------------------
49 :
50 1 : int LAGraph_SSaveSet // save a set of matrices from a *.lagraph file
51 : (
52 : // inputs:
53 : char *filename, // name of file to write to
54 : GrB_Matrix *Set, // array of GrB_Matrix of size nmatrices
55 : GrB_Index nmatrices, // # of matrices to write to *.lagraph file
56 : char *collection, // name of this collection of matrices
57 : char *msg
58 : )
59 : {
60 :
61 : //--------------------------------------------------------------------------
62 : // check inputs
63 : //--------------------------------------------------------------------------
64 :
65 1 : LG_CLEAR_MSG ;
66 1 : FILE *f = NULL ;
67 :
68 1 : LAGraph_Contents *Contents = NULL ;
69 :
70 1 : LG_ASSERT (filename != NULL && Set != NULL && collection != NULL,
71 : GrB_NULL_POINTER) ;
72 :
73 1 : f = fopen (filename, "wb") ;
74 1 : LG_ASSERT_MSG (f != NULL, -1001, "unable to create output file") ;
75 :
76 : //--------------------------------------------------------------------------
77 : // serialize all the matrices
78 : //--------------------------------------------------------------------------
79 :
80 : // allocate an Contents array of size nmatrices to hold the contents
81 1 : LG_TRY (LAGraph_Calloc ((void **) &Contents, nmatrices,
82 : sizeof (LAGraph_Contents), msg)) ;
83 :
84 52 : for (GrB_Index i = 0 ; i < nmatrices ; i++)
85 : {
86 : #if LAGRAPH_SUITESPARSE
87 : {
88 51 : GRB_TRY (GxB_Matrix_serialize (&(Contents [i].blob),
89 : (GrB_Index *)&(Contents [i].blob_size), Set [i], NULL)) ;
90 : }
91 : #else
92 : {
93 : GrB_Index estimate ;
94 : GRB_TRY (GrB_Matrix_serializeSize (&estimate, Set [i])) ;
95 : Contents [i].blob_size = estimate ;
96 : LAGRAPH_TRY (LAGraph_Malloc ((void **) &(Contents [i].blob),
97 : estimate, sizeof (uint8_t), msg)) ;
98 : GRB_TRY (GrB_Matrix_serialize (Contents [i].blob,
99 : (GrB_Index *) &(Contents [i].blob_size), Set [i])) ;
100 : LG_TRY (LAGraph_Realloc ((void **) &(Contents [i].blob),
101 : (size_t) Contents [i].blob_size,
102 : estimate, sizeof (uint8_t), msg)) ;
103 : }
104 : #endif
105 : }
106 :
107 : //--------------------------------------------------------------------------
108 : // write the header
109 : //--------------------------------------------------------------------------
110 :
111 1 : LG_TRY (LAGraph_SWrite_HeaderStart (f, collection, msg)) ;
112 52 : for (GrB_Index i = 0 ; i < nmatrices ; i++)
113 : {
114 : char typename [LAGRAPH_MAX_NAME_LEN+1] ;
115 51 : LG_TRY (LAGraph_Matrix_TypeName (typename, Set [i], msg)) ;
116 : char matrix_name [256] ;
117 51 : snprintf (matrix_name, 256, "A_%" PRIu64, i) ;
118 51 : LG_TRY (LAGraph_SWrite_HeaderItem (f, LAGraph_matrix_kind,
119 : matrix_name, typename, 0, Contents [i].blob_size, msg)) ;
120 : }
121 1 : LG_TRY (LAGraph_SWrite_HeaderEnd (f, msg)) ;
122 :
123 : //--------------------------------------------------------------------------
124 : // write all the blobs
125 : //--------------------------------------------------------------------------
126 :
127 52 : for (GrB_Index i = 0 ; i < nmatrices ; i++)
128 : {
129 51 : LG_TRY (LAGraph_SWrite_Item (f, Contents [i].blob,
130 : Contents [i].blob_size, msg)) ;
131 : }
132 :
133 : //--------------------------------------------------------------------------
134 : // free workspace and return result
135 : //--------------------------------------------------------------------------
136 :
137 1 : LG_FREE_WORK ;
138 1 : return (GrB_SUCCESS) ;
139 : }
|