File: GB_mex_reduce_to_GrB_Scalar.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 (230 lines) | stat: -rw-r--r-- 6,759 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
//------------------------------------------------------------------------------
// GB_mex_reduce_to_GrB_Scalar: S = accum(S,reduce_to_scalar(A))
//------------------------------------------------------------------------------

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

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

// Reduce a matrix or vector to a scalar

#include "GB_mex.h"

#define USAGE "S = GB_mex_reduce_to_GrB_Scalar (S, accum, reduce, A)"

#define FREE_ALL                        \
{                                       \
    GrB_Matrix_free_(&S) ;              \
    GrB_Matrix_free_(&A) ;              \
    if (reduce_monoid_allocated)        \
    {                                   \
        GrB_Monoid_free_(&reduce) ;     \
    }                                   \
    GB_mx_put_global (true) ;           \
}

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

    bool malloc_debug = GB_mx_get_global (true) ;
    GrB_Matrix A = NULL ;
    GrB_Matrix S = NULL ;
    GrB_Monoid reduce = NULL ;
    bool reduce_is_complex = false ;
    bool reduce_monoid_allocated = false ;

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

    // get the GrB_Scalar S as a GrB_Matrix
    #define GET_DEEP_COPY \
    S = GB_mx_mxArray_to_Matrix (pargin [0], "S input", true, true) ;
    #define FREE_DEEP_COPY GrB_Matrix_free_(&S) ;
    GET_DEEP_COPY ;
    if (S == NULL)
    {
        FREE_ALL ;
        mexErrMsgTxt ("S failed") ;
    }
    int64_t Snrows, Sncols ;
    GrB_Matrix_nrows (&Snrows, S) ;
    GrB_Matrix_ncols (&Sncols, S) ;
    if (Snrows != 1 || Sncols != 1)
    { 
        mexErrMsgTxt ("S must be a scalar") ;
    }
    GrB_Type stype = S->type ;

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

    // get reduce
    bool user_complex = (Complex != GxB_FC64) && (stype == Complex) ;
    GrB_BinaryOp reduceop ;
    if (!GB_mx_mxArray_to_BinaryOp (&reduceop, pargin [2], "reduceop",
        stype, user_complex) || reduceop == NULL) 
    {
        FREE_ALL ;
        mexErrMsgTxt ("reduceop failed") ;
    }

    // get the reduce monoid
    if (user_complex)
    {
        if (reduceop == Complex_plus)
        {
            reduce = Complex_plus_monoid ;
        }
        else if (reduceop == Complex_times)
        {
            reduce = Complex_times_monoid ;
        }
        else
        {
            FREE_ALL ;
            mexErrMsgTxt ("reduce failed") ;
        }
    }
    else
    {
        // create the reduce monoid
        if (!GB_mx_Monoid (&reduce, reduceop, malloc_debug))
        {
            FREE_ALL ;
            mexErrMsgTxt ("reduce failed") ;
        }
        reduce_monoid_allocated = true ;
    }

    // get accum, if present
    GrB_BinaryOp accum ;
    if (!GB_mx_mxArray_to_BinaryOp (&accum, pargin [1], "accum",
        stype, user_complex))
    {
        FREE_ALL ;
        mexErrMsgTxt ("accum failed") ;
    }

    // S = accum(S,A*B)

    if (reduceop == GrB_MIN_INT8
    ||  reduceop == GrB_MIN_INT16
    ||  reduceop == GrB_MIN_INT32
    ||  reduceop == GrB_MIN_INT64
    ||  reduceop == GrB_MIN_UINT8
    ||  reduceop == GrB_MIN_UINT16
    ||  reduceop == GrB_MIN_UINT32
    ||  reduceop == GrB_MIN_UINT64
    ||  reduceop == GrB_MIN_FP32
    ||  reduceop == GrB_MIN_FP64
    ||  reduceop == GrB_MAX_INT8
    ||  reduceop == GrB_MAX_INT16
    ||  reduceop == GrB_MAX_INT32
    ||  reduceop == GrB_MAX_INT64
    ||  reduceop == GrB_MAX_UINT8
    ||  reduceop == GrB_MAX_UINT16
    ||  reduceop == GrB_MAX_UINT32
    ||  reduceop == GrB_MAX_UINT64
    ||  reduceop == GrB_MAX_FP32
    ||  reduceop == GrB_MAX_FP64
    ||  reduceop == GrB_PLUS_INT8
    ||  reduceop == GrB_PLUS_INT16
    ||  reduceop == GrB_PLUS_INT32
    ||  reduceop == GrB_PLUS_INT64
    ||  reduceop == GrB_PLUS_UINT8
    ||  reduceop == GrB_PLUS_UINT16
    ||  reduceop == GrB_PLUS_UINT32
    ||  reduceop == GrB_PLUS_UINT64
    ||  reduceop == GrB_PLUS_FP32
    ||  reduceop == GrB_PLUS_FP64
    ||  reduceop == GxB_PLUS_FC32
    ||  reduceop == GxB_PLUS_FC64
    ||  reduceop == GrB_TIMES_INT8
    ||  reduceop == GrB_TIMES_INT16
    ||  reduceop == GrB_TIMES_INT32
    ||  reduceop == GrB_TIMES_INT64
    ||  reduceop == GrB_TIMES_UINT8
    ||  reduceop == GrB_TIMES_UINT16
    ||  reduceop == GrB_TIMES_UINT32
    ||  reduceop == GrB_TIMES_UINT64
    ||  reduceop == GrB_TIMES_FP32
    ||  reduceop == GrB_TIMES_FP64
    ||  reduceop == GxB_TIMES_FC32
    ||  reduceop == GxB_TIMES_FC64
    ||  reduceop == GxB_ANY_BOOL
    ||  reduceop == GxB_ANY_INT8
    ||  reduceop == GxB_ANY_INT16
    ||  reduceop == GxB_ANY_INT32
    ||  reduceop == GxB_ANY_INT64
    ||  reduceop == GxB_ANY_UINT8
    ||  reduceop == GxB_ANY_UINT16
    ||  reduceop == GxB_ANY_UINT32
    ||  reduceop == GxB_ANY_UINT64
    ||  reduceop == GxB_ANY_FP32
    ||  reduceop == GxB_ANY_FP64
    ||  reduceop == GxB_ANY_FC32
    ||  reduceop == GxB_ANY_FC64
    ||  reduceop == GrB_LOR
    ||  reduceop == GrB_LAND
    ||  reduceop == GrB_LXOR
    ||  reduceop == GrB_LXNOR
    ||  reduceop == GrB_BOR_UINT8
    ||  reduceop == GrB_BOR_UINT16
    ||  reduceop == GrB_BOR_UINT32
    ||  reduceop == GrB_BOR_UINT64
    ||  reduceop == GrB_BAND_UINT8
    ||  reduceop == GrB_BAND_UINT16
    ||  reduceop == GrB_BAND_UINT32
    ||  reduceop == GrB_BAND_UINT64
    ||  reduceop == GrB_BXOR_UINT8
    ||  reduceop == GrB_BXOR_UINT16
    ||  reduceop == GrB_BXOR_UINT32
    ||  reduceop == GrB_BXOR_UINT64
    ||  reduceop == GrB_BXNOR_UINT8
    ||  reduceop == GrB_BXNOR_UINT16
    ||  reduceop == GrB_BXNOR_UINT32
    ||  reduceop == GrB_BXNOR_UINT64)
    {
        // S = accum (S, reduce (A)) using a binary op
        if (GB_VECTOR_OK (A))
        {
            METHOD (GrB_Vector_reduce_BinaryOp_Scalar_((GrB_Scalar) S, accum, reduceop, (GrB_Vector) A, NULL)) ;
        }
        else
        {
            METHOD (GrB_Matrix_reduce_BinaryOp_Scalar_((GrB_Scalar) S, accum, reduceop, A, NULL)) ;
        }
    }
    else
    {
        // S = accum (S, reduce (A)) using a monoid
        if (GB_VECTOR_OK (A))
        {
            METHOD (GrB_Vector_reduce_Monoid_Scalar_((GrB_Scalar) S, accum, reduce, (GrB_Vector) A, NULL)) ;
        }
        else
        {
            METHOD (GrB_Matrix_reduce_Monoid_Scalar_((GrB_Scalar) S, accum, reduce, A, NULL)) ;
        }
    }

    // return S as struct
    pargout [0] = GB_mx_Matrix_to_mxArray (&S, "S", true) ;
    FREE_ALL ;
}