Line data Source code
1 : //------------------------------------------------------------------------------ 2 : // LAGraph_SLoadSet: load a set of matrices from 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_SLoadSet loads a set of GrB_Matrix objects from a *.lagraph file. 19 : // It returns a GrB_Matrix array of size nmatrices. In the future, it will 20 : // also return a set of GrB_Vectors, and a an array of uncompressed ascii 21 : // texts. The caller is responsible for freeing the output of this method, 22 : // via: 23 : 24 : // LAGraph_Free ((void **) &collection, NULL) ; 25 : // LAGraph_SFreeSet (&Set, nmatrices) ; 26 : 27 : // See also LAGraph_SRead, which just reads in the serialized objects and 28 : // does not convert them to their corresponding GrB_Matrix, GrB_Vector, or 29 : // uncompressed texts. 30 : 31 : //------------------------------------------------------------------------------ 32 : 33 : #define LG_FREE_WORK \ 34 : { \ 35 : if (f != NULL && f != stdin) fclose (f) ; \ 36 : f = NULL ; \ 37 : LAGraph_SFreeContents (&Contents, ncontents) ; \ 38 : } 39 : 40 : #define LG_FREE_ALL \ 41 : { \ 42 : LG_FREE_WORK ; \ 43 : LAGraph_SFreeSet (&Set, nmatrices) ; \ 44 : LAGraph_Free ((void **) &collection, NULL) ; \ 45 : } 46 : 47 : #include "LG_internal.h" 48 : #include "LAGraphX.h" 49 : 50 : //------------------------------------------------------------------------------ 51 : // LAGraph_SLoadSet 52 : //------------------------------------------------------------------------------ 53 : 54 5 : int LAGraph_SLoadSet // load a set of matrices from a *.lagraph file 55 : ( 56 : // input: 57 : char *filename, // name of file to read; NULL for stdin 58 : // outputs: 59 : GrB_Matrix **Set_handle, // array of GrB_Matrix of size nmatrices 60 : GrB_Index *nmatrices_handle, // # of matrices loaded from *.lagraph file 61 : // todo: handle vectors and text in LAGraph_SLoadSet 62 : // GrB_Vector **Set_handle, // array of GrB_Vector of size nvector 63 : // GrB_Index **nvectors_handle, // # of vectors loaded from *.lagraph file 64 : // char **Text_handle, // array of pointers to (char *) strings 65 : // GrB_Index **ntext_handle, // # of texts loaded from *.lagraph file 66 : char **collection_handle, // name of this collection of matrices 67 : char *msg 68 : ) 69 : { 70 : 71 : //-------------------------------------------------------------------------- 72 : // check inputs 73 : //-------------------------------------------------------------------------- 74 : 75 5 : LG_CLEAR_MSG ; 76 5 : FILE *f = stdin ; 77 5 : char *collection = NULL ; 78 5 : GrB_Matrix *Set = NULL ; 79 5 : LAGraph_Contents *Contents = NULL ; 80 5 : GrB_Index ncontents = 0 ; 81 5 : GrB_Index nmatrices = 0 ; 82 : // GrB_Index nvectors = 0 ; 83 : // GrB_Index ntexts = 0 ; 84 : 85 5 : LG_ASSERT (Set_handle != NULL && nmatrices_handle != NULL 86 : && collection_handle != NULL, GrB_NULL_POINTER) ; 87 : 88 : //-------------------------------------------------------------------------- 89 : // read the file 90 : //-------------------------------------------------------------------------- 91 : 92 4 : if (filename != NULL) 93 : { 94 4 : f = fopen (filename, "rb") ; 95 4 : LG_ASSERT_MSG (f != NULL, 96 : LAGRAPH_IO_ERROR, "unable to open input file") ; 97 : } 98 3 : LG_TRY (LAGraph_SRead (f, &collection, &Contents, &ncontents, msg)) ; 99 2 : if (filename != NULL) 100 : { 101 2 : fclose (f) ; 102 : } 103 2 : f = NULL ; 104 : 105 : //-------------------------------------------------------------------------- 106 : // count the matrices/vectors/texts in the Contents 107 : //-------------------------------------------------------------------------- 108 : 109 : // todo: for now, all Contents are matrices 110 2 : nmatrices = ncontents ; 111 : 112 : #if 0 113 : for (GrB_Index i = 0 ; i < ncontents ; i++) 114 : { 115 : switch (Contents [i].kind) 116 : { 117 : case LAGraph_matrix_kind : nmatrices++ ; break ; 118 : case LAGraph_vector_kind : nvectors++ ; break ; 119 : case LAGraph_text_kind : ntexts++ ; break ; 120 : default : LG_ASSERT_MSG (false, GrB_INVALID_VALUE, "unknown kind") ; 121 : } 122 : } 123 : if (nvectors > 0 || ntexts > 0) 124 : { 125 : // todo: handle vectors and texts 126 : printf ("Warning: %lu vectors and %lu texts ignored\n", 127 : nvectors, ntexts) ; 128 : } 129 : #endif 130 : 131 : //-------------------------------------------------------------------------- 132 : // convert all the matrices (skip vectors and text content for now) 133 : //-------------------------------------------------------------------------- 134 : 135 2 : LG_TRY (LAGraph_Calloc ((void **) &Set, nmatrices, sizeof (GrB_Matrix), 136 : msg)) ; 137 : 138 2 : GrB_Index kmatrices = 0 ; 139 54 : for (GrB_Index i = 0 ; i < ncontents ; i++) 140 : { 141 : // convert Contents [i] 142 52 : void *blob = Contents [i].blob ; 143 52 : size_t blob_size = Contents [i].blob_size ; 144 : 145 52 : if (Contents [i].kind == LAGraph_matrix_kind) 146 : { 147 : // convert Contents [i].typename to a GrB_Type ctype. 148 : // SuiteSparse:GraphBLAS allows this to be NULL for built-in types. 149 52 : GrB_Type ctype = NULL ; 150 52 : LG_TRY (LAGraph_TypeFromName (&ctype, Contents [i].type_name, msg)); 151 52 : GRB_TRY (GrB_Matrix_deserialize (&(Set [kmatrices]), ctype, blob, 152 : blob_size)) ; 153 52 : kmatrices++ ; 154 : } 155 : // todo: handle vectors and texts 156 : // else if (Content [i].kind == LAGraph_vector_kind) ... 157 : // else if (Content [i].kind == LAGraph_text_kind) ... 158 : 159 : // free the ith blob 160 52 : LAGraph_Free ((void **) &(Contents [i].blob), NULL) ; 161 : } 162 : 163 : //-------------------------------------------------------------------------- 164 : // free workspace and return result 165 : //-------------------------------------------------------------------------- 166 : 167 2 : LG_FREE_WORK ; 168 2 : (*Set_handle) = Set ; 169 2 : (*collection_handle) = collection ; 170 2 : (*nmatrices_handle) = nmatrices ; 171 2 : return (GrB_SUCCESS) ; 172 : }