File: GB_mex_setElement.c

package info (click to toggle)
suitesparse 1%3A5.8.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 152,716 kB
  • sloc: ansic: 774,385; cpp: 24,213; makefile: 6,310; fortran: 1,927; java: 1,826; csh: 1,686; ruby: 725; sh: 535; perl: 225; python: 209; sed: 164; awk: 60
file content (277 lines) | stat: -rw-r--r-- 11,010 bytes parent folder | download
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
//------------------------------------------------------------------------------
// GB_mex_setElement: MATLAB interface for A(i,j) = x
//------------------------------------------------------------------------------

// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2020, All Rights Reserved.
// http://suitesparse.com   See GraphBLAS/Doc/License.txt for license.

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

// x = A (i,j), where i and j are zero-based.  If i and j arrays, then
// x (k) = A (i (k), j (k)) is done for all k.

// I and J and zero-based

#include "GB_mex.h"

#define USAGE "A = GB_mex_setElement (A, I, J, X, debug_wait)"

bool debug_wait = false ;

#define FREE_ALL                        \
{                                       \
    GB_MATRIX_FREE (&A) ;               \
    GB_mx_put_global (true, 0) ;        \
}

#if defined ( __GNUC__ )
#pragma GCC diagnostic ignored "-Wmissing-prototypes"
#endif

// set all elements of a matrix and return if an error is encountered
#define setEl(prefix,name,type)                                             \
GrB_Info set_ ## name                                                       \
(GrB_Matrix A, type *X, GrB_Index *I, GrB_Index *J, GrB_Index ni)           \
{                                                                           \
    for (int64_t k = 0 ; k < ni ; k++)                                      \
    {                                                                       \
        GrB_Info info = prefix ## Matrix_setElement_ ## name                \
            (A, AMPERSAND (X [k]), I [k], J [k]) ;                          \
        if (info != GrB_SUCCESS) return (info) ;                            \
    }                                                                       \
    if (debug_wait)                                                         \
    {                                                                       \
        return (GB_Matrix_wait (A, NULL)) ;                                 \
    }                                                                       \
    return (GrB_SUCCESS) ;                                                  \
}

// create all the local set_TYPE functions
#define AMPERSAND(x) x
setEl (GrB_, BOOL   , bool          ) ;
setEl (GrB_, INT8   , int8_t        ) ;
setEl (GrB_, UINT8  , uint8_t       ) ;
setEl (GrB_, INT16  , int16_t       ) ;
setEl (GrB_, UINT16 , uint16_t      ) ;
setEl (GrB_, INT32  , int32_t       ) ;
setEl (GrB_, UINT32 , uint32_t      ) ;
setEl (GrB_, INT64  , int64_t       ) ;
setEl (GrB_, UINT64 , uint64_t      ) ;
setEl (GrB_, FP32   , float         ) ;
setEl (GrB_, FP64   , double        ) ;
setEl (GxB_, FC32   , GxB_FC32_t    ) ;
setEl (GxB_, FC64   , GxB_FC64_t    ) ;
#undef  AMPERSAND
#define AMPERSAND(x) &x
setEl (GrB_, UDT    , GxB_FC64_t) ;
#undef  AMPERSAND


// set all elements of a vector and return if an error is encountered
#define vsetEl(prefix,name,type)                                            \
GrB_Info vset_ ## name                                                      \
(GrB_Matrix A, type *X, GrB_Index *I, GrB_Index ni)                         \
{                                                                           \
    GrB_Vector w = (GrB_Vector) A ;                                         \
    for (int64_t k = 0 ; k < ni ; k++)                                      \
    {                                                                       \
        GrB_Info info = prefix ## Vector_setElement_ ## name                \
            (w, AMPERSAND (X [k]), I [k]) ;                                 \
        if (info != GrB_SUCCESS) return (info) ;                            \
    }                                                                       \
    if (debug_wait)                                                         \
    {                                                                       \
        return (GB_Matrix_wait (A, NULL)) ;                                 \
    }                                                                       \
    return (GrB_SUCCESS) ;                                                  \
}

// create all the local set_TYPE functions
#define AMPERSAND(x) x
vsetEl (GrB_, BOOL   , bool          ) ;
vsetEl (GrB_, INT8   , int8_t        ) ;
vsetEl (GrB_, UINT8  , uint8_t       ) ;
vsetEl (GrB_, INT16  , int16_t       ) ;
vsetEl (GrB_, UINT16 , uint16_t      ) ;
vsetEl (GrB_, INT32  , int32_t       ) ;
vsetEl (GrB_, UINT32 , uint32_t      ) ;
vsetEl (GrB_, INT64  , int64_t       ) ;
vsetEl (GrB_, UINT64 , uint64_t      ) ;
vsetEl (GrB_, FP32   , float         ) ;
vsetEl (GrB_, FP64   , double        ) ;
vsetEl (GxB_, FC32   , GxB_FC32_t    ) ;
vsetEl (GxB_, FC64   , GxB_FC64_t    ) ;
#undef  AMPERSAND
#define AMPERSAND(x) &x
vsetEl (GrB_, UDT    , GxB_FC64_t) ;
#undef  AMPERSAND

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

    bool malloc_debug = GB_mx_get_global (true) ;

    GrB_Matrix A = NULL ;
    GB_void *Y ;
    GrB_Type xtype ;
    GrB_Index *I = NULL, ni = 0, I_range [3] ;
    GrB_Index *J = NULL, nj = 0, J_range [3] ;
    bool is_list ;

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

    // get A (deep copy)
    #define GET_DEEP_COPY \
    A = GB_mx_mxArray_to_Matrix (pargin [0], "A input", true, true) ;
    #define FREE_DEEP_COPY GB_MATRIX_FREE (&A) ;
    GET_DEEP_COPY ;
    if (A == NULL)
    {
        FREE_ALL ;
        mexErrMsgTxt ("A failed") ;
    }

    // get I
    if (!GB_mx_mxArray_to_indices (&I, pargin [1], &ni, I_range, &is_list))
    {
        FREE_ALL ;
        mexErrMsgTxt ("I failed") ;
    }
    if (!is_list)
    {
        mexErrMsgTxt ("I is invalid; must be a list") ;
    }

    // get J
    if (!GB_mx_mxArray_to_indices (&J, pargin [2], &nj, J_range, &is_list))
    {
        FREE_ALL ;
        mexErrMsgTxt ("J failed") ;
    }
    if (!is_list)
    {
        mexErrMsgTxt ("J is invalid; must be a list") ;
    }

    if (ni != nj)
    {
        FREE_ALL ;
        mexErrMsgTxt ("I and J must be the same size") ;
    }

    // get X
    if (ni != mxGetNumberOfElements (pargin [3]))
    {
        FREE_ALL ;
        mexErrMsgTxt ("I and X must be the same size") ;
    }
    if (!(mxIsNumeric (pargin [3]) || mxIsLogical (pargin [3])))
    {
        FREE_ALL ;
        mexErrMsgTxt ("X must be a numeric or logical array") ;
    }
    if (mxIsSparse (pargin [3]))
    {
        FREE_ALL ;
        mexErrMsgTxt ("X cannot be sparse") ;
    }

    // get debug_wait (if true, to GB_Matrix_wait after setElements)
    GET_SCALAR (4, bool, debug_wait, false) ;

    if (mxIsComplex (pargin [3]))
    {
        xtype = Complex ;
        Y = mxGetComplexDoubles (pargin [3]) ;
    }
    else
    {
        Y = mxGetData (pargin [3]) ;
        xtype = GB_mx_Type (pargin [3]) ;
        if (xtype == NULL)
        {
            FREE_ALL ;
            mexErrMsgTxt ("X must be numeric") ;
        }
    }

    size_t s = 2 * sizeof (double) ;

    // A (i,j) = x, for a list of elements

    // the METHOD (...) macro is not used on each call to setElement, but
    // to all of them.  Thus, if any failure occurs, the computation is rolled
    // back to the very beginning, and another fresh, deep, copy of A is made,
    // and the sequence of setElements is tried again.  If a setElement fails
    // by running out of memory, it clears to whole matrix, so recovery cannot
    // be made.

    if (A->vdim == 1)
    {
        // test GrB_Vector_setElement
        switch (xtype->code)
        {
            case GB_BOOL_code   : METHOD (vset_BOOL   (A, (bool       *) Y, I, ni)) ; break ;
            case GB_INT8_code   : METHOD (vset_INT8   (A, (int8_t     *) Y, I, ni)) ; break ;
            case GB_INT16_code  : METHOD (vset_INT16  (A, (int16_t    *) Y, I, ni)) ; break ;
            case GB_INT32_code  : METHOD (vset_INT32  (A, (int32_t    *) Y, I, ni)) ; break ;
            case GB_INT64_code  : METHOD (vset_INT64  (A, (int64_t    *) Y, I, ni)) ; break ;
            case GB_UINT8_code  : METHOD (vset_UINT8  (A, (uint8_t    *) Y, I, ni)) ; break ;
            case GB_UINT16_code : METHOD (vset_UINT16 (A, (uint16_t   *) Y, I, ni)) ; break ;
            case GB_UINT32_code : METHOD (vset_UINT32 (A, (uint32_t   *) Y, I, ni)) ; break ;
            case GB_UINT64_code : METHOD (vset_UINT64 (A, (uint64_t   *) Y, I, ni)) ; break ;
            case GB_FP32_code   : METHOD (vset_FP32   (A, (float      *) Y, I, ni)) ; break ;
            case GB_FP64_code   : METHOD (vset_FP64   (A, (double     *) Y, I, ni)) ; break ;
            case GB_FC32_code   : METHOD (vset_FC32   (A, (GxB_FC32_t *) Y, I, ni)) ; break ;
            case GB_FC64_code   : METHOD (vset_FC64   (A, (GxB_FC64_t *) Y, I, ni)) ; break ;
            case GB_UDT_code    : METHOD (vset_UDT    (A, (void       *) Y, I, ni)) ; break ;
            default:
                FREE_ALL ;
                mexErrMsgTxt ("unsupported type") ;
        }
    }
    else
    {
        // test GrB_Matrix_setElement
        switch (xtype->code)
        {
            case GB_BOOL_code   : METHOD (set_BOOL   (A, (bool       *) Y, I, J, ni)) ; break ;
            case GB_INT8_code   : METHOD (set_INT8   (A, (int8_t     *) Y, I, J, ni)) ; break ;
            case GB_INT16_code  : METHOD (set_INT16  (A, (int16_t    *) Y, I, J, ni)) ; break ;
            case GB_INT32_code  : METHOD (set_INT32  (A, (int32_t    *) Y, I, J, ni)) ; break ;
            case GB_INT64_code  : METHOD (set_INT64  (A, (int64_t    *) Y, I, J, ni)) ; break ;
            case GB_UINT8_code  : METHOD (set_UINT8  (A, (uint8_t    *) Y, I, J, ni)) ; break ;
            case GB_UINT16_code : METHOD (set_UINT16 (A, (uint16_t   *) Y, I, J, ni)) ; break ;
            case GB_UINT32_code : METHOD (set_UINT32 (A, (uint32_t   *) Y, I, J, ni)) ; break ;
            case GB_UINT64_code : METHOD (set_UINT64 (A, (uint64_t   *) Y, I, J, ni)) ; break ;
            case GB_FP32_code   : METHOD (set_FP32   (A, (float      *) Y, I, J, ni)) ; break ;
            case GB_FP64_code   : METHOD (set_FP64   (A, (double     *) Y, I, J, ni)) ; break ;
            case GB_FC32_code   : METHOD (set_FC32   (A, (GxB_FC32_t *) Y, I, J, ni)) ; break ;
            case GB_FC64_code   : METHOD (set_FC64   (A, (GxB_FC64_t *) Y, I, J, ni)) ; break ;
            case GB_UDT_code    : METHOD (set_UDT    (A, (void       *) Y, I, J, ni)) ; break ;
            default:
                FREE_ALL ;
                mexErrMsgTxt ("unsupported type") ;
        }
    }

    // only do debug checks after adding lots of tuples
    if (ni > 1000) { ASSERT_MATRIX_OK (A, "A added pending tuples", GB0) ; }

    // return A to MATLAB as a struct and free the GraphBLAS A
    pargout [0] = GB_mx_Matrix_to_mxArray (&A, "A output", true) ;

    FREE_ALL ;
}