File: GB_mex_mxv_iterator.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 (173 lines) | stat: -rw-r--r-- 5,160 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
//------------------------------------------------------------------------------
// GB_mex_mxv_iterator: Y = A*X using an iterator
//------------------------------------------------------------------------------

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

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

// A and X must have the same type.  If boolean, the lor_land semiring is used.
// Otherwise, the plus_times semiring is used for the given type.

#include "GB_mex.h"
#include "GB_mex_errors.h"

#define USAGE "Y = GB_mex_mxv_iterator (A, X, kind)"

#define FREE_ALL                                    \
{                                                   \
    GrB_Vector_free_(&X) ;                          \
    GrB_Vector_free_(&Y) ;                          \
    GrB_Matrix_free_(&A) ;                          \
    GxB_Iterator_free (&iterator) ;                 \
    GB_mx_put_global (true) ;                       \
}

#define Assert(x)                                   \
{                                                   \
    if (!(x))                                       \
    {                                               \
        printf ("Failure at %d\n", __LINE__) ;      \
        mexErrMsgTxt ("fail") ;                     \
    }                                               \
}

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

    GrB_Info info ;
    bool malloc_debug = GB_mx_get_global (true) ;
    GrB_Vector Y = NULL, X = NULL ;
    GrB_Matrix A = NULL ;
    GxB_Iterator iterator = NULL ;

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

    // get A (shallow copy)
    A = GB_mx_mxArray_to_Matrix (pargin [0], "A input", false, true) ;
    if (A == NULL)
    {
        FREE_ALL ;
        mexErrMsgTxt ("A failed") ;
    }
    GrB_Index nrows, ncols ;
    OK (GrB_Matrix_nrows (&nrows, A)) ;
    OK (GrB_Matrix_ncols (&ncols, A)) ;
    GB_Global_print_one_based_set (0) ;

    // get X (shallow copy)
    X = GB_mx_mxArray_to_Vector (pargin [1], "X input", false, true) ;
    if (X == NULL)
    {
        FREE_ALL ;
        mexErrMsgTxt ("X failed") ;
    }
    // GxB_print (X, 3) ;

    GrB_Index n ;
    OK (GrB_Vector_size (&n, X)) ;
    if (n != ncols)
    {
        FREE_ALL ;
        mexErrMsgTxt ("X has the wrong size") ;
    }

    GrB_Type type = A->type ;
    if (type != X->type)
    {
        FREE_ALL ;
        mexErrMsgTxt ("A and X must have the same type") ;
    }

    // get kind
    int GET_SCALAR (2, int, kind, 0) ;
    bool use_macros = (kind <= 7) ;
    kind = kind % 8 ;

    // make sure X is full
    int sparsity ;
    OK (GxB_Vector_Option_get (X, GxB_SPARSITY_STATUS, &sparsity)) ;
    if (sparsity != GxB_FULL)
    {
        FREE_ALL ;
        mexErrMsgTxt ("X must be full") ;
    }

    bool user_complex = (Complex != GxB_FC64) && (type == Complex) ;

    // create Y and make it full: all zero but non-iso
    OK (GrB_Vector_new (&Y, type, nrows)) ;
    if (user_complex)
    {
        double z [2] ;
        z [0] = 0 ;
        z [1] = 0 ;
        OK (GrB_Vector_assign_UDT (Y, NULL, NULL, z, GrB_ALL, nrows, NULL)) ;
        z [0] = 1 ;
        OK (GrB_Vector_setElement_UDT (Y, z, 0)) ;
        z [0] = 0 ;
        OK (GrB_Vector_setElement_UDT (Y, z, 0)) ;
    }
    else
    {
        OK (GrB_Vector_assign_INT32 (Y, NULL, NULL, 0, GrB_ALL, nrows, NULL)) ;
        OK (GrB_Vector_setElement_INT32 (Y, 1, 0)) ;
        OK (GrB_Vector_setElement_INT32 (Y, 0, 0)) ;
    }
    OK (GrB_Vector_wait (Y, GrB_MATERIALIZE)) ;

    //--------------------------------------------------------------------------
    // Y += A*X using an iterator
    //--------------------------------------------------------------------------

    // create an iterator
    OK (GxB_Iterator_new (&iterator)) ;

    if (use_macros)
    {
        // use macros that are #define'd in GraphBLAS.h
        #include "Template/GB_mx_mxv_iterator_template.c"
    }
    else
    {
        // use functions whose prototypes are in GraphBLAS.h
        #include "GB_undef_iterator.h"
        #include "Template/GB_mx_mxv_iterator_template.c"
    }

    //--------------------------------------------------------------------------
    // error handling
    //--------------------------------------------------------------------------

    if (A->is_csc)
    {
        info = GxB_rowIterator_attach (iterator, A, NULL) ;
        Assert (info == GrB_NOT_IMPLEMENTED) ;
    }
    else
    {
        info = GxB_colIterator_attach (iterator, A, NULL) ;
        Assert (info == GrB_NOT_IMPLEMENTED) ;
    }

    //--------------------------------------------------------------------------
    // return Y as a struct and free the GraphBLAS Y
    //--------------------------------------------------------------------------

    // GxB_print (Y, 3) ;
    GB_Global_print_one_based_set (1) ;
    pargout [0] = GB_mx_Vector_to_mxArray (&Y, "Y output", true) ;
    FREE_ALL ;
}