File: GB_mex_reduce_bool.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 (113 lines) | stat: -rw-r--r-- 2,838 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
//------------------------------------------------------------------------------
// GB_mex_reduce_bool: c = accum(c,reduce_to_scalar(A)) for boolean
//------------------------------------------------------------------------------

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

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

// Reduce a boolean matrix or vector to a boolean.

#include "GB_mex.h"

#define USAGE "result = GB_mex_reduce_bool (A, op, identity, terminal)"

#define FREE_ALL                        \
{                                       \
    GrB_Matrix_free_(&A) ;              \
    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_Info info ;
    GrB_Matrix A = NULL ;
    GrB_Monoid reduce = NULL ;
    GrB_BinaryOp reduceop = NULL ;

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

    #define GET_DEEP_COPY ;
    #define FREE_DEEP_COPY ;

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

    if (A->type != GrB_BOOL)
    {
        FREE_ALL ;
        mexErrMsgTxt ("A must be boolean") ;
    }

    // get the op (always boolean)
    if (!GB_mx_mxArray_to_BinaryOp (&reduceop, pargin [1], "reduceop",
        GrB_BOOL, false) || reduceop == NULL)
    {
        FREE_ALL ;
        mexErrMsgTxt ("reduceop failed") ;
    }

    // get the boolean identity value
    bool GET_SCALAR (2, bool, identity, true) ;

    // get the boolean terminal value, if any.  default is true
    bool terminal = true ;
    bool has_terminal = (nargin == 4) ;
    if (has_terminal)
    {
        GET_SCALAR (3, bool, terminal, true) ;
    }

    // create the reduce monoid
    if (has_terminal)
    {
        info = GxB_Monoid_terminal_new_BOOL_(&reduce, reduceop, identity, terminal) ;
    }
    else
    {
        info = GrB_Monoid_new_BOOL_(&reduce, reduceop, identity) ;
    }

    if (info != GrB_SUCCESS)
    {
        FREE_ALL ;
        mexErrMsgTxt ("monoid failed") ;
    }

    // reduce to a scalar
    bool result = false ;
    info = GrB_Matrix_reduce_BOOL_(&result, NULL, reduce, A, NULL) ;

    if (info != GrB_SUCCESS)
    {
        FREE_ALL ;
        mexErrMsgTxt ("GrB_reduce failed") ;
    }

    // return result as a boolean scalar
    pargout [0] = GB_mx_create_full (1, 1, GrB_BOOL) ;
    GB_void *p = mxGetData (pargout [0]) ;
    memcpy (p, &result, sizeof (bool)) ;

    FREE_ALL ;
}