1 //------------------------------------------------------------------------------
2 // GB_mex_cumsum: cumulative using GB_cumsum
3 //------------------------------------------------------------------------------
4 
5 // SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
6 // SPDX-License-Identifier: Apache-2.0
7 
8 //------------------------------------------------------------------------------
9 
10 #include "GB_mex.h"
11 
12 #define USAGE "[p,k] = GB_mex_cumsum (c,nthreads,nmalloc)"
13 
mexFunction(int nargout,mxArray * pargout[],int nargin,const mxArray * pargin[])14 void mexFunction
15 (
16     int nargout,
17     mxArray *pargout [ ],
18     int nargin,
19     const mxArray *pargin [ ]
20 )
21 {
22     bool malloc_debug = GB_mx_get_global (true) ;
23 
24     // check inputs
25     if (nargin < 1 || nargin > 3 || nargout > 2)
26     {
27         mexErrMsgTxt ("Usage: " USAGE) ;
28     }
29 
30     if (!mxIsClass (pargin [0], "int64"))
31     {
32         mexErrMsgTxt ("c must be an int64 array") ;
33     }
34 
35     int64_t *c = mxGetData (pargin [0]) ;
36     int64_t n = (uint64_t) mxGetNumberOfElements (pargin [0]) ;
37 
38     int GET_SCALAR (1, int, nthreads, 1) ;
39 
40     int GET_SCALAR (2, int, nmalloc, 2) ;
41 
42     // make a copy of the input array (as a row vector)
43     pargout [0] = GB_mx_create_full (1, n+1, GrB_INT64) ;
44     int64_t *p = mxGetData (pargout [0]) ;
45     memcpy (p, c, n * sizeof (int64_t)) ;
46     p [n] = 0 ;
47 
48     // create the 2nd output, kresult, if requested
49     int64_t *kresult = NULL ;
50     if (nargout > 1)
51     {
52         pargout [1] = GB_mx_create_full (1, 1, GrB_INT64) ;
53         kresult = mxGetData (pargout [1]) ;
54     }
55 
56     if (!malloc_debug)
57     {
58         // normal usage
59         GB_cumsum (p, n, kresult, nthreads, NULL) ;
60     }
61     else
62     {
63         // test with malloc failures
64         // printf ("test cumsum with nmalloc: %d\n", nmalloc) ;
65         GB_Global_malloc_debug_set (true) ;
66         GB_Global_malloc_debug_count_set (nmalloc) ;
67         GB_cumsum (p, n, kresult, nthreads, NULL) ;
68         GB_Global_malloc_debug_set (false) ;
69     }
70 
71     // log the test coverage
72     GB_mx_put_global (true) ;
73 }
74 
75