1 //------------------------------------------------------------------------------
2 // gb_get_mxargs: get input arguments to a GraphBLAS mexFunction
3 //------------------------------------------------------------------------------
4 
5 // SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
6 // SPDX-License-Identifier: GPL-3.0-or-later
7 
8 //------------------------------------------------------------------------------
9 
10 // gb_get_mxargs collects all the input arguments for the 12 foundational
11 // GraphBLAS operations.  The user-level view is described below.  For
12 // the private mexFunctions, the descriptor optionally appears as the last
13 // argument.  The matrix arguments are either MATLAB sparse or full matrices,
14 // GraphBLAS matrices.  To call the mexFunction, the opaque content of the
15 // GraphBLAS matrices has been extracted, so they appear here as GraphBLAS
16 // structs (a MATLAB struct G whose first field is always G.GraphBLAS).
17 
18 #include "gb_matlab.h"
19 
gb_get_mxargs(int nargin,const mxArray * pargin[],const char * usage,const mxArray * Matrix[4],int * nmatrices,const mxArray * String[2],int * nstrings,const mxArray * Cell[2],int * ncells,GrB_Descriptor * desc,base_enum_t * base,kind_enum_t * kind,GxB_Format_Value * fmt,int * sparsity)20 void gb_get_mxargs
21 (
22     // input:
23     int nargin,                 // # input arguments for mexFunction
24     const mxArray *pargin [ ],  // input arguments for mexFunction
25     const char *usage,          // usage to print, if too many args appear
26     // output:
27     const mxArray *Matrix [4],  // matrix arguments
28     int *nmatrices,             // # of matrix arguments
29     const mxArray *String [2],  // string arguments
30     int *nstrings,              // # of string arguments
31     const mxArray *Cell [2],    // cell array arguments
32     int *ncells,                // # of cell array arguments
33     GrB_Descriptor *desc,       // last argument is always the descriptor
34     base_enum_t *base,          // desc.base
35     kind_enum_t *kind,          // desc.kind
36     GxB_Format_Value *fmt,      // desc.format : by row or by col
37     int *sparsity               // desc.format : hypersparse/sparse/bitmap/full
38                                 // or 0 if not in the descriptor
39 )
40 {
41 
42     //--------------------------------------------------------------------------
43     // find the descriptor
44     //--------------------------------------------------------------------------
45 
46     (*desc) = NULL ;
47     (*kind) = KIND_GRB ;
48     (*fmt) = GxB_NO_FORMAT ;
49     (*sparsity) = 0 ;
50     (*base) = BASE_DEFAULT ;
51     if (nargin > 0)
52     {
53         (*desc) = gb_mxarray_to_descriptor (pargin [nargin-1], kind, fmt,
54             sparsity, base) ;
55     }
56     if ((*desc) != NULL)
57     {
58         // descriptor is present, remove it from further consideration
59         nargin-- ;
60     }
61 
62     //--------------------------------------------------------------------------
63     // find the remaining arguments
64     //--------------------------------------------------------------------------
65 
66     (*nmatrices) = 0 ;
67     (*nstrings) = 0 ;
68     (*ncells) = 0 ;
69 
70     for (int k = 0 ; k < nargin ; k++)
71     {
72         if (mxIsCell (pargin [k]))
73         {
74             // I or J index arguments
75             if ((*ncells) >= 2)
76             {
77                 ERROR ("only 2D indexing is supported") ;
78             }
79             Cell [(*ncells)++] = pargin [k] ;
80         }
81         else if (mxIsChar (pargin [k]))
82         {
83             // accum operator, unary op, binary op, monoid, or semiring
84             if ((*nstrings) >= 2)
85             {
86                 ERROR (usage) ;
87             }
88             String [(*nstrings)++] = pargin [k] ;
89         }
90         else
91         {
92             // a matrix argument is C, M, A, or B
93             if ((*nmatrices) >= 4)
94             {
95                 // at most 4 matrix inputs are allowed
96                 ERROR (usage) ;
97             }
98             Matrix [(*nmatrices)++] = pargin [k] ;
99         }
100     }
101 }
102 
103