1 //------------------------------------------------------------------------------
2 // GB_mex_assign_alias: C(I,J) = accum(C(I,J),C)
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 "C = GB_mex_assign_alias (C, accum, I, J, desc)"
13 
14 #define FREE_ALL                            \
15 {                                           \
16     GrB_Matrix_free_(&C) ;                  \
17     GrB_Descriptor_free_(&desc) ;           \
18     GB_mx_put_global (true) ;               \
19 }
20 
mexFunction(int nargout,mxArray * pargout[],int nargin,const mxArray * pargin[])21 void mexFunction
22 (
23     int nargout,
24     mxArray *pargout [ ],
25     int nargin,
26     const mxArray *pargin [ ]
27 )
28 {
29 
30     bool malloc_debug = GB_mx_get_global (true) ;
31     GrB_Matrix C = NULL ;
32     GrB_Descriptor desc = NULL ;
33     GrB_Index *I = NULL, ni = 0, I_range [3] ;
34     GrB_Index *J = NULL, nj = 0, J_range [3] ;
35     bool ignore ;
36 
37     // check inputs
38     if (nargout > 1 || nargin < 2 || nargin > 5)
39     {
40         mexErrMsgTxt ("Usage: " USAGE) ;
41     }
42 
43     // get C (make a deep copy)
44     #define GET_DEEP_COPY \
45         C = GB_mx_mxArray_to_Matrix (pargin [0], "C input", true, true) ;
46     #define FREE_DEEP_COPY GrB_Matrix_free_(&C) ;
47     GET_DEEP_COPY ;
48     if (C == NULL)
49     {
50         FREE_ALL ;
51         mexErrMsgTxt ("C failed") ;
52     }
53 
54     // get accum, if present
55     bool user_complex = (Complex != GxB_FC64) && (C->type == Complex) ;
56     GrB_BinaryOp accum ;
57     if (!GB_mx_mxArray_to_BinaryOp (&accum, pargin [1], "accum",
58         C->type, user_complex))
59     {
60         FREE_ALL ;
61         mexErrMsgTxt ("accum failed") ;
62     }
63 
64     // get I
65     if (!GB_mx_mxArray_to_indices (&I, PARGIN (2), &ni, I_range, &ignore))
66     {
67         FREE_ALL ;
68         mexErrMsgTxt ("I failed") ;
69     }
70 
71     // get J
72     if (!GB_mx_mxArray_to_indices (&J, PARGIN (3), &nj, J_range, &ignore))
73     {
74         FREE_ALL ;
75         mexErrMsgTxt ("J failed") ;
76     }
77 
78     // get desc
79     if (!GB_mx_mxArray_to_Descriptor (&desc, PARGIN (4), "desc"))
80     {
81         FREE_ALL ;
82         mexErrMsgTxt ("desc failed") ;
83     }
84 
85     // C(I,J) = accum (C(I,J),C)
86     METHOD (GrB_Matrix_assign_(C, NULL, accum, C, I, ni, J, nj, desc)) ;
87 
88     // return C to MATLAB as a struct and free the GraphBLAS C
89     pargout [0] = GB_mx_Matrix_to_mxArray (&C, "C output", true) ;
90 
91     FREE_ALL ;
92 }
93 
94