File: GB_mex_export.c

package info (click to toggle)
suitesparse-graphblas 7.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 67,112 kB
  • sloc: ansic: 1,072,243; cpp: 8,081; sh: 512; makefile: 506; asm: 369; python: 125; awk: 10
file content (367 lines) | stat: -rw-r--r-- 12,872 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//------------------------------------------------------------------------------
// GB_mex_export: test import/export
//------------------------------------------------------------------------------

// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

//------------------------------------------------------------------------------

#include "GB_mex.h"

#define USAGE "C = GB_mex_export (C, format, hyper, csc, dump, clear_nvec)"

// If Ap, Ah, etc are exported, then they are not NULL and not in the global
// memtable.  If they are not exported, they are NULL, and are part of the
// matrix, and in the global memtable.

#define FREE_WORK                                               \
{                                                               \
    GrB_Matrix_free_(&C) ;                                      \
    if (Ap != NULL) { mxFree (Ap) ; Ap = NULL ; }               \
    if (Ah != NULL) { mxFree (Ah) ; Ah = NULL ; }               \
    if (Ai != NULL) { mxFree (Ai) ; Ai = NULL ; }               \
    if (Aj != NULL) { mxFree (Aj) ; Aj = NULL ; }               \
    if (Ax != NULL) { mxFree (Ax) ; Ax = NULL ; }               \
}

#define FREE_ALL                                                \
{                                                               \
    FREE_WORK ;                                                 \
    GB_mx_put_global (true) ;                                   \
}

#define OK(method)                      \
{                                       \
    info = method ;                     \
    if (info != GrB_SUCCESS)            \
    {                                   \
        if (dump) printf ("method fail %d: %d\n", __LINE__, info) ; \
        FREE_WORK ;                     \
        return (info) ;                 \
    }                                   \
}

GrB_Vector v = NULL ;
GrB_Matrix C = NULL ;
GrB_Index *Ap = NULL ;
GrB_Index *Ah = NULL ;
GrB_Index *Ai = NULL ;
GrB_Index *Aj = NULL ;
GrB_Index nrows = 0 ;
GrB_Index ncols = 0 ;
GrB_Index nvals = 0 ;
GrB_Index nvec = 0 ;
GrB_Index Ai_size = 0 ;
GrB_Index Ax_size = 0 ;
bool iso = false ;
GrB_Index Ap_size = 0 ;
GrB_Index Aj_size = 0 ;
GrB_Index Ah_size = 0 ;
int64_t ignore = -1 ;
char *Ax = NULL ;
int format = 0 ;
bool jumbled = false ;
bool is_hyper = false ;
bool clear_nvec = false ;
bool is_csc = true ;
GrB_Info info = GrB_SUCCESS ;
GrB_Descriptor desc = NULL ;
bool dump = true ;
GrB_Type type = NULL ;
size_t asize = 0 ;
GrB_Info import_export (void) ;
GrB_Info import_export2 (void) ;

//------------------------------------------------------------------------------

GrB_Info import_export ( )
{

    OK (GB_Matrix_check (C, "C to export", GB0, stdout)) ;

    //--------------------------------------------------------------------------
    // export/import a vector
    //--------------------------------------------------------------------------

    if (GB_VECTOR_OK (C))
    {
        OK (GxB_Vector_export_CSC ((GrB_Vector *) (&C), &type, &nrows,
            &Ai, (void **) &Ax, &Ai_size, &Ax_size, &iso,
            &nvals, &jumbled, desc)) ;

        OK (GxB_Type_size (&asize, type)) ;

        if (dump)
        {
            printf ("export standard CSC vector: %llu-by-1, nvals %llu:\n",
                nrows, nvals) ;
            OK (GB_Type_check (type, "type", GxB_SUMMARY, stdout)) ;
            GB_Type_code code = type->code ;

            for (int64_t p = 0 ; p < nvals ; p++)
            {
                printf ("  row %llu value ", Ai [p]) ;
                GB_code_check (code, Ax + (iso ? 0:p)*asize, 5, stdout) ;
                printf ("\n") ;
            }
        }

        OK (GxB_Vector_import_CSC ((GrB_Vector *) (&C), type, nrows,
            &Ai, (void **) &Ax, Ai_size, Ax_size, iso, nvals, jumbled, desc)) ;

        return (GrB_SUCCESS) ;
    }

    //--------------------------------------------------------------------------
    // export/import a matrix
    //--------------------------------------------------------------------------

    switch (format)
    {

        //----------------------------------------------------------------------
        case 0 : 
        //----------------------------------------------------------------------

            OK (GxB_Matrix_export_CSR (&C, &type, &nrows, &ncols,
                    &Ap, &Aj, (void **) &Ax, &Ap_size, &Aj_size, &Ax_size, &iso,
                    &jumbled, desc)) ;

            OK (GxB_Type_size (&asize, type)) ;
            nvec = nrows ;

            if (dump)
            {
                printf ("\nexport standard CSR: %llu-by-%llu, Ax_size %llu:\n",
                    nrows, ncols, Ax_size) ;
                OK (GB_Type_check (type, "type", GxB_SUMMARY, stdout));
                GB_Type_code code = type->code ;

                for (int64_t i = 0 ; i < nrows ; i++)
                {
                    printf ("Row %lld\n", i) ;
                    for (int64_t p = Ap [i] ; p < Ap [i+1] ; p++)
                    {
                        printf ("  col %llu value ", Aj [p]) ;
                        GB_code_check (code, Ax + (iso ? 0:p)*asize,
                            5, stdout) ;
                        printf ("\n") ;
                    }
                }
            }

            OK (GxB_Matrix_import_CSR (&C, type, nrows, ncols,
                &Ap, &Aj, (void **) &Ax, Ap_size, Aj_size, Ax_size, iso,
                jumbled, desc)) ;

            OK (GB_Matrix_check (C, "C reimported",
                dump ? GxB_COMPLETE : GxB_SILENT, stdout)) ;
            break ;

        //----------------------------------------------------------------------
        case 1 : 
        //----------------------------------------------------------------------

            OK (GxB_Matrix_export_CSC (&C, &type, &nrows, &ncols,
                    &Ap, &Ai, (void **) &Ax, &Ap_size, &Ai_size, &Ax_size, &iso,
                    &jumbled, desc)) ;

            nvec = ncols ;
            OK (GxB_Type_size (&asize, type)) ;

            if (dump)
            {
                printf ("\nexport standard CSC: %llu-by-%llu, Ax_size %llu:\n",
                    nrows, ncols, Ax_size) ;
                OK (GB_Type_check (type, "type", GxB_SUMMARY, stdout));
                GB_Type_code code = type->code ;

                for (int64_t j = 0 ; j < ncols ; j++)
                {
                    printf ("Col %lld\n", j) ;
                    for (int64_t p = Ap [j] ; p < Ap [j+1] ; p++)
                    {
                        printf ("  row %llu value ", Ai [p]) ;
                        GB_code_check (code, Ax + (iso ? 0:p)*asize,
                            5, stdout) ;
                        printf ("\n") ;
                    }
                }
            }

            OK (GxB_Matrix_import_CSC (&C, type, nrows, ncols,
                &Ap, &Ai, (void **) &Ax, Ap_size, Ai_size, Ax_size, iso,
                jumbled, desc)) ;

            OK (GB_Matrix_check (C, "C reimported",
                dump ? GxB_COMPLETE : GxB_SILENT, stdout)) ;
            break ;

        //----------------------------------------------------------------------
        case 2 : 
        //----------------------------------------------------------------------

            OK (GxB_Matrix_export_HyperCSR (&C, &type, &nrows, &ncols,
                &Ap, &Ah, &Aj, (void **) &Ax,
                &Ap_size, &Ah_size, &Aj_size, &Ax_size, &iso,
                &nvec, &jumbled, desc)) ;

            OK (GxB_Type_size (&asize, type)) ;

            if (dump)
            {
                printf ("\nexport hyper CSR: %llu-by-%llu, Ax_size %llu, "
                    "nvec %llu:\n", nrows, ncols, Ax_size, nvec) ;
                OK (GB_Type_check (type, "type", GxB_SUMMARY, stdout));
                GB_Type_code code = type->code ;

                for (int64_t k = 0 ; k < nvec ; k++)
                {
                    int64_t i = Ah [k] ;
                    printf ("Row %lld\n", i) ;
                    for (int64_t p = Ap [k] ; p < Ap [k+1] ; p++)
                    {
                        printf ("  col %llu value ", Aj [p]) ;
                        GB_code_check (code, Ax + (iso ? 0:p)*asize,
                            5, stdout) ;
                        printf ("\n") ;
                    }
                }
            }

            OK (GxB_Matrix_import_HyperCSR (&C, type, nrows, ncols,
                &Ap, &Ah, &Aj, (void **) &Ax,
                Ap_size, Ah_size, Aj_size, Ax_size, iso,
                nvec, jumbled, desc)) ;

            OK (GB_Matrix_check (C, "C reimported",
                dump ? GxB_COMPLETE : GxB_SILENT, stdout));
            break ;

        //----------------------------------------------------------------------
        case 3 : 
        //----------------------------------------------------------------------

            OK (GxB_Matrix_export_HyperCSC (&C, &type, &nrows, &ncols,
                &Ap, &Ah, &Ai, (void **) &Ax,
                &Ap_size, &Ah_size, &Ai_size, &Ax_size, &iso,
                &nvec, &jumbled, desc)) ;

            OK (GxB_Type_size (&asize, type)) ;

            if (dump)
            {
                printf ("export hyper CSC: %llu-by-%llu, Ax_size %llu, "
                    "c %llu:\n", nrows, ncols, Ax_size, nvec) ;
                OK (GB_Type_check (type, "type", GxB_SUMMARY, stdout));
                GB_Type_code code = type->code ;

                for (int64_t k = 0 ; k < nvec ; k++)
                {
                    int64_t j = Ah [k] ;
                    printf ("Col %lld\n", j) ;
                    for (int64_t p = Ap [k] ; p < Ap [k+1] ; p++)
                    {
                        printf ("  row %llu value ", Ai [p]) ;
                        GB_code_check (code, Ax + (iso ? 0:p)*asize,
                            5, stdout) ;
                        printf ("\n") ;
                    }
                }
            }

            OK (GxB_Matrix_import_HyperCSC (&C, type, nrows, ncols,
                &Ap, &Ah, &Ai, (void **) &Ax,
                Ap_size, Ah_size, Ai_size, Ax_size, iso,
                nvec, jumbled, desc)) ;

            OK (GB_Matrix_check (C, "C reimported",
                dump ? GxB_COMPLETE : GxB_SILENT, stdout)) ;
            break ;

        //----------------------------------------------------------------------
        default : 
        //----------------------------------------------------------------------

            mexErrMsgTxt ("bad format") ;
            break ;
    }
    return (GrB_SUCCESS) ;
}

//------------------------------------------------------------------------------

GrB_Info import_export2 (void)
{
    OK (import_export ( )) ;
    OK (import_export ( )) ;
    return (GrB_SUCCESS) ;
}

//------------------------------------------------------------------------------

void mexFunction
(
    int nargout,
    mxArray *pargout [ ],
    int nargin,
    const mxArray *pargin [ ]
)
{

    bool malloc_debug = GB_mx_get_global (true) ;

    // check inputs
    if (nargout > 1 || nargin < 1 || nargin > 6)
    {
        mexErrMsgTxt ("Usage: " USAGE) ;
    }

    // get format for import/export
    GET_SCALAR (1, int, format, 0) ;

    // get hyper flag 
    GET_SCALAR (2, bool, is_hyper, false) ;

    // get csc flag 
    GET_SCALAR (3, bool, is_csc, true) ;

    // get dump flag 
    GET_SCALAR (4, bool, dump, false) ;

    // get clear_nvec flag 
    GET_SCALAR (5, bool, clear_nvec, false) ;

    // get C (make a deep copy)
    #define GET_DEEP_COPY \
    {                                                                       \
        C = GB_mx_mxArray_to_Matrix (pargin [0], "C input", true, true) ;   \
        if (!is_csc)                                                        \
        {                                                                   \
            /* convert C to CSR */                                          \
            GB_transpose_in_place (C, false, NULL) ;                        \
        }                                                                   \
        if (is_hyper && !GB_IS_FULL (C))                                    \
        {                                                                   \
            /* convert C to hypersparse */                                  \
            GB_convert_sparse_to_hyper (C, NULL) ;                          \
        }                                                                   \
    }
    #define FREE_DEEP_COPY GrB_Matrix_free_(&C) ;
    GET_DEEP_COPY ;
    if (C == NULL)
    {
        FREE_ALL ;
        mexErrMsgTxt ("C failed") ;
    }

    // import/export
    METHOD (import_export2 ( )) ;

    // return C
    pargout [0] = GB_mx_Matrix_to_mxArray (&C, "C export/import", false) ;

    FREE_ALL ;
}