File: GB_mex_cumsum.c

package info (click to toggle)
suitesparse-graphblas 7.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 67,112 kB
  • sloc: ansic: 1,072,243; cpp: 8,081; sh: 512; makefile: 503; asm: 369; python: 125; awk: 10
file content (74 lines) | stat: -rw-r--r-- 2,062 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
//------------------------------------------------------------------------------
// GB_mex_cumsum: cumulative using GB_cumsum
//------------------------------------------------------------------------------

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

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

#include "GB_mex.h"

#define USAGE "[p,k] = GB_mex_cumsum (c,nthreads,malloc_debug_count)"

void mexFunction
(
    int nargout,
    mxArray *pargout [ ],
    int nargin,
    const mxArray *pargin [ ]
)
{
    bool malloc_debug = GB_mx_get_global (true) ;

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

    if (!mxIsClass (pargin [0], "int64"))
    {
        mexErrMsgTxt ("c must be an int64 array") ;
    }

    int64_t *c = mxGetData (pargin [0]) ;
    int64_t n = (uint64_t) mxGetNumberOfElements (pargin [0]) ;

    int GET_SCALAR (1, int, nthreads, 1) ;

    int GET_SCALAR (2, int, malloc_debug_count, 2) ;

    // make a copy of the input array (as a row vector)
    pargout [0] = GB_mx_create_full (1, n+1, GrB_INT64) ;
    int64_t *p = mxGetData (pargout [0]) ;
    memcpy (p, c, n * sizeof (int64_t)) ;
    p [n] = 0 ;

    // create the 2nd output, kresult, if requested
    int64_t *kresult = NULL ;
    if (nargout > 1)
    {
        pargout [1] = GB_mx_create_full (1, 1, GrB_INT64) ;
        kresult = mxGetData (pargout [1]) ;
    }

    if (!malloc_debug)
    {
        // normal usage
        GB_cumsum (p, n, kresult, nthreads, NULL) ;
    }
    else
    {
        // test with malloc failures
        // printf ("test cumsum with malloc_debug_count: %d\n", malloc_debug_count) ;
        GB_Global_malloc_debug_set (true) ;
        GB_Global_malloc_debug_count_set (malloc_debug_count) ;
        GB_cumsum (p, n, kresult, nthreads, NULL) ;
        GB_Global_malloc_debug_set (false) ;
    }

    // log the test coverage
    GB_mx_put_global (true) ;   
}