1 //------------------------------------------------------------------------------
2 // GB_mex_assign_alias_mask_scalar: C<C,struct> = scalar
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_mask_scalar (C, scalar)"
13 
14 #define FREE_ALL                            \
15 {                                           \
16     GrB_Matrix_free_(&C) ;                  \
17     GrB_Matrix_free_(&S) ;                  \
18     GB_mx_put_global (true) ;               \
19 }
20 
21 GrB_Matrix C = NULL ;
22 GrB_Matrix S = NULL ;
23 GxB_Scalar scalar = NULL ;
24 GrB_Info assign_mask_scalar (void) ;
25 
assign_mask_scalar(void)26 GrB_Info assign_mask_scalar (void)
27 {
28     GrB_Info info ;
29     GrB_Index nrows, ncols ;
30     GrB_Matrix_nrows (&nrows, C) ;
31     GrB_Matrix_ncols (&ncols, C) ;
32     GrB_Type stype ;
33     GxB_Scalar_type (&stype, scalar) ;
34 
35     // some descriptors use GrB_REPLACE, just to test this option.
36     // It has no effect on the result.
37 
38     if (stype == GrB_BOOL)
39     {
40         bool y = *((bool *) (scalar->x)) ;
41         info = GrB_Matrix_assign_BOOL_
42             (C, C, NULL, y, GrB_ALL, nrows, GrB_ALL, ncols, GrB_DESC_S) ;
43     }
44     else if (stype == GrB_INT8)
45     {
46         int8_t y = *((int8_t *) (scalar->x)) ;
47         info = GrB_Matrix_assign_INT8_
48             (C, C, NULL, y, GrB_ALL, nrows, GrB_ALL, ncols, GrB_DESC_S) ;
49     }
50     else if (stype == GrB_INT16)
51     {
52         int16_t y = *((int16_t *) (scalar->x)) ;
53         info = GrB_Matrix_assign_INT16_
54             (C, C, NULL, y, GrB_ALL, nrows, GrB_ALL, ncols, GrB_DESC_S) ;
55     }
56     else if (stype == GrB_INT32)
57     {
58         int32_t y = *((int32_t *) (scalar->x)) ;
59         info = GrB_Matrix_assign_INT32_
60             (C, C, NULL, y, GrB_ALL, nrows, GrB_ALL, ncols, GrB_DESC_S) ;
61     }
62     else if (stype == GrB_INT64)
63     {
64         int64_t y = *((int64_t *) (scalar->x)) ;
65         info = GrB_Matrix_assign_INT64_
66             (C, C, NULL, y, GrB_ALL, nrows, GrB_ALL, ncols, GrB_DESC_S) ;
67     }
68     else if (stype == GrB_UINT8)
69     {
70         uint8_t y = *((uint8_t *) (scalar->x)) ;
71         info = GrB_Matrix_assign_UINT8_
72             (C, C, NULL, y, GrB_ALL, nrows, GrB_ALL, ncols, GrB_DESC_S) ;
73     }
74     else if (stype == GrB_UINT16)
75     {
76         uint16_t y = *((uint16_t *) (scalar->x)) ;
77         info = GrB_Matrix_assign_UINT16_
78             (C, C, NULL, y, GrB_ALL, nrows, GrB_ALL, ncols, GrB_DESC_RS) ;
79     }
80     else if (stype == GrB_UINT32)
81     {
82         uint32_t y = *((uint32_t *) (scalar->x)) ;
83         info = GrB_Matrix_assign_UINT32_
84             (C, C, NULL, y, GrB_ALL, nrows, GrB_ALL, ncols, GrB_DESC_RS) ;
85     }
86     else if (stype == GrB_UINT64)
87     {
88         uint64_t y = *((uint64_t *) (scalar->x)) ;
89         info = GrB_Matrix_assign_UINT64_
90             (C, C, NULL, y, GrB_ALL, nrows, GrB_ALL, ncols, GrB_DESC_RS) ;
91     }
92     else if (stype == GrB_FP32)
93     {
94         float y = *((float *) (scalar->x)) ;
95         info = GrB_Matrix_assign_FP32_
96             (C, C, NULL, y, GrB_ALL, nrows, GrB_ALL, ncols, GrB_DESC_RS) ;
97     }
98     else if (stype == GrB_FP64)
99     {
100         double y = *((double *) (scalar->x)) ;
101         info = GrB_Matrix_assign_FP64_
102             (C, C, NULL, y, GrB_ALL, nrows, GrB_ALL, ncols, GrB_DESC_S) ;
103     }
104     else if (stype == GxB_FC32)
105     {
106         GxB_FC32_t y = *((GxB_FC32_t *) (scalar->x)) ;
107         info = GxB_Matrix_assign_FC32_
108             (C, C, NULL, y, GrB_ALL, nrows, GrB_ALL, ncols, GrB_DESC_S) ;
109     }
110     else if (stype == GxB_FC64)
111     {
112         GxB_FC64_t y = *((GxB_FC64_t *) (scalar->x)) ;
113         info = GxB_Matrix_assign_FC64_
114             (C, C, NULL, y, GrB_ALL, nrows, GrB_ALL, ncols, GrB_DESC_RS) ;
115     }
116     else
117     {
118         mexErrMsgTxt ("unknown type") ;
119     }
120 
121     return (info) ;
122 }
123 
mexFunction(int nargout,mxArray * pargout[],int nargin,const mxArray * pargin[])124 void mexFunction
125 (
126     int nargout,
127     mxArray *pargout [ ],
128     int nargin,
129     const mxArray *pargin [ ]
130 )
131 {
132 
133     bool malloc_debug = GB_mx_get_global (true) ;
134 
135     // check inputs
136     if (nargout > 1 || nargin != 2)
137     {
138         mexErrMsgTxt ("Usage: " USAGE) ;
139     }
140 
141     // get C (make a deep copy)
142     #define GET_DEEP_COPY       \
143         C = GB_mx_mxArray_to_Matrix (pargin [0], "C input", true, true) ;   \
144         GxB_Matrix_Option_set (C, GxB_SPARSITY_CONTROL, C->sparsity) ;
145     #define FREE_DEEP_COPY      \
146         GrB_Matrix_free_(&C) ;
147     GET_DEEP_COPY ;
148     if (C == NULL)
149     {
150         FREE_ALL ;
151         mexErrMsgTxt ("C failed") ;
152     }
153 //  GxB_Matrix_fprint (C, "C", GxB_COMPLETE, NULL) ;
154 
155     // get scalar (shallow copy)
156     S = GB_mx_mxArray_to_Matrix (pargin [1], "scalar input", false, true) ;
157     if (S == NULL || S->magic != GB_MAGIC)
158     {
159         FREE_ALL ;
160         mexErrMsgTxt ("scalar failed") ;
161     }
162     GrB_Index snrows, sncols, snvals ;
163     GrB_Matrix_nrows (&snrows, S) ;
164     GrB_Matrix_ncols (&sncols, S) ;
165     GrB_Matrix_nvals (&snvals, S) ;
166     GxB_Format_Value fmt ;
167     GxB_Matrix_Option_get_(S, GxB_FORMAT, &fmt) ;
168     if (snrows != 1 || sncols != 1 || snvals != 1 || fmt != GxB_BY_COL)
169     {
170         FREE_ALL ;
171         mexErrMsgTxt ("scalar failed") ;
172     }
173     scalar = (GxB_Scalar) S ;
174 //  GrB_Info info = GxB_Scalar_fprint (scalar, "scalar", GxB_COMPLETE, NULL) ;
175 //  if (info != GrB_SUCCESS)
176 //  {
177 //      FREE_ALL ;
178 //      mexErrMsgTxt ("scalar failed") ;
179 //  }
180 
181     // C<C,struct> = scalar
182     METHOD (assign_mask_scalar ( )) ;
183 //  GxB_Matrix_fprint (C, "C output", GxB_COMPLETE, NULL) ;
184 
185     // return C to MATLAB as a struct and free the GraphBLAS C
186     pargout [0] = GB_mx_Matrix_to_mxArray (&C, "C output", true) ;
187 
188     FREE_ALL ;
189 }
190 
191