/
FLA_Matlab2C.c
executable file
·194 lines (160 loc) · 5.39 KB
/
FLA_Matlab2C.c
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
libflame
An object-based infrastructure for developing high-performance
dense linear algebra libraries.
Copyright (C) 2011, The University of Texas
libflame is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of
the License, or (at your option) any later version.
libflame is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with libflame; if you did not receive a copy, see
http://www.gnu.org/licenses/.
For more information, please contact us at flame@cs.utexas.edu or
send mail to:
Field G. Van Zee and/or
Robert A. van de Geijn
The University of Texas at Austin
Department of Computer Sciences
1 University Station C0500
Austin TX 78712
*/
/* ====================================================================================== */
/*
C file containing functions to convert things from Matlab FLAME to C FLAME.
*/
/* ====================================================================================== */
#include "mex.h"
#include "FLA_Matlab2C.h"
#include <string.h>
#include <stdio.h>
/* Check if the number of arguments with which the function is called is correct */
/* rrhs: required number of arguments */
/* srhs: supplied number of arguments */
void FLA_M2C_CheckNumArgs(int rrhs, int srhs) {
char err[200];
/* rrhs is the required number of arguments. If an additional optional argument is
specified, the routine is timed and the timing information returned in that argument. */
if (srhs != rrhs && srhs != rrhs+1) {
sprintf(err, "Wrong number of arguments: %d arguments supplied; %d or %d arguments required", srhs, rrhs, rrhs+1);
mexErrMsgTxt(err);
}
}
/* Convert FLAME attributes specified as a string in MATLAB to the numerical values
required when calling FLAME C functions. */
int FLA_M2C_ConvertAttribute(const mxArray *mobj) {
char *attr;
int value;
if (!mxIsChar(mobj)) {
mexErrMsgTxt("Char array expected.");
}
else {
/* Convert to C style string */
attr = mxArrayToString(mobj);
if (strcmp(attr, "FLA_NO_TRANSPOSE") == 0)
value = FLA_NO_TRANSPOSE;
else if (strcmp(attr, "FLA_TRANSPOSE") == 0)
value = FLA_TRANSPOSE;
else if (strcmp(attr, "FLA_CONJUGATE") == 0)
value = FLA_CONJUGATE;
else if (strcmp(attr, "FLA_NO_CONJUGATE") == 0)
value = FLA_NO_CONJUGATE;
else if (strcmp(attr, "FLA_CONJ_TRANSPOSE") == 0)
value = FLA_CONJ_TRANSPOSE;
else if (strcmp(attr, "FLA_LOWER_TRIANGULAR") == 0)
value = FLA_LOWER_TRIANGULAR;
else if (strcmp(attr, "FLA_UPPER_TRIANGULAR") == 0)
value = FLA_UPPER_TRIANGULAR;
else if (strcmp(attr, "FLA_NONUNIT_DIAG") == 0)
value = FLA_NONUNIT_DIAG;
else if (strcmp(attr, "FLA_UNIT_DIAG") == 0)
value = FLA_UNIT_DIAG;
else if (strcmp(attr, "FLA_ZERO_DIAG") == 0)
value = FLA_ZERO_DIAG;
else if (strcmp(attr, "FLA_LEFT") == 0)
value = FLA_LEFT;
else if (strcmp(attr, "FLA_RIGHT") == 0)
value = FLA_RIGHT;
else
value = -1;
mxFree(attr);
return value;
}
}
/* Convert Matlab array to C FLAME object */
FLA_Obj FLA_M2C_ConvertMxArray(const mxArray *mobj) {
int type;
FLA_Obj fobj;
if (!mxIsNumeric(mobj)) {
mexErrMsgTxt("Numeric data expected.");
}
else {
/* Determine data type of the matlab array. */
if (mxIsDouble(mobj))
if (mxIsComplex(mobj))
type = FLA_DOUBLE_COMPLEX;
else
type = FLA_DOUBLE;
else if (mxIsSingle(mobj))
if (mxIsComplex(mobj))
type = FLA_COMPLEX;
else
type = FLA_FLOAT;
else if (mxIsInt32(mobj))
type = FLA_INT;
else
mexErrMsgTxt("Data type not supported.");
fobj.datatype = type;
fobj.m = mxGetM(mobj);
fobj.n = mxGetN(mobj);
fobj.buffer = mxGetPr(mobj);
fobj.ldim = fobj.m;
return fobj;
}
}
/* Convert Matlab array to double */
double *FLA_M2C_ConvertDoublePtr(const mxArray *mobj) {
double *var = 0;
if (!mxIsDouble(mobj)) {
mexErrMsgTxt("Double precision variable expected.");
}
else if (mxGetM(mobj) != 1 || mxGetN(mobj) != 1) {
mexErrMsgTxt("Scalar variable expected.");
}
else {
var = mxGetPr(mobj);
}
return var;
}
/* Convert the attribute and FLA_Obj arguments */
void FLA_M2C_ConvertArgs(int nrhs, const mxArray *prhs[], int nint,
int attr[], FLA_Obj obj[]) {
char err[200];
int i, j;
/* First convert all the attributes */
/* Check if the argument is a string before calling the conversion function */
for (i=0; i<nint; i++) {
if (mxIsChar(prhs[i])) {
attr[i] = FLA_M2C_ConvertAttribute(prhs[i]);
}
else {
sprintf(err, "The %dth argument must be a FLAME attribute string.", i+1);
mexErrMsgTxt(err);
}
}
/* The remaining arguments are converted to FLA_Obj */
/* Check if the argument is numeric before calling the conversion function */
for (i=i; i<nrhs; i++) {
if (mxIsNumeric(prhs[i])) {
obj[i-nint] = FLA_M2C_ConvertMxArray(prhs[i]);
}
else {
sprintf(err, "The %dth argument must be numeric.", i+1);
mexErrMsgTxt(err);
}
}
}