-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmexSphericalConvolution.cu
276 lines (246 loc) · 7.63 KB
/
mexSphericalConvolution.cu
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "mex.h"
#include "gpu/mxGPUArray.h"
#include "nnsphconv.hpp"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs,
mxArray const *prhs[])
{
float const *input, *filter, *bias, *derOutput;
float *output, *derInput, *derFilter, *derBias;
unsigned int Fin, Fout, Nin, Nout, Nfilt;
unsigned int const *map;
bool backMode, err = false;
char *errMsg;
mxGPUArray const *dev_input, *dev_filter, *dev_bias, *dev_derOutput;
mxGPUArray *dev_output, *dev_derInput, *dev_derFilter, *dev_derBias;
mwSize const *dims;
mwSize dims_4D[] = { 1, 1, 1, 1 }, dims_2D[] = { 1, 1 };
mwSize ndim;
mxInitGPU(); /* Initialize the MathWorks GPU API. */
/* -------------------------------------------------------------- */
/* Check the input arguments */
/* -------------------------------------------------------------- */
if (nrhs < 4) { mexErrMsgTxt("Not enough input arguments."); }
if (nrhs > 5) { mexErrMsgTxt("Too many inputs."); }
if (nrhs == 4) { backMode = false; }
if (nrhs == 5) { backMode = true; }
// get pointer to gpuArray input
dev_input = mxGPUCreateFromMxArray(prhs[0]);
if (mxGPUGetClassID(dev_input) == mxSINGLE_CLASS)
{
input = (float const *)mxGPUGetDataReadOnly(dev_input);
}
else
{
mexErrMsgTxt("Only single format of network input is supported.");
}
// get pointer to gpuArray filter
dev_filter = mxGPUCreateFromMxArray(prhs[1]);
if (mxGPUGetClassID(dev_filter) == mxSINGLE_CLASS)
{
filter = (float const *)mxGPUGetDataReadOnly(dev_filter);
}
else
{
mexErrMsgTxt("Only single format of network filter is supported.");
}
// get pointer to gpuArray bias
if (!backMode)
{
dev_bias = mxGPUCreateFromMxArray(prhs[2]);
if (mxGPUGetClassID(dev_bias) == mxSINGLE_CLASS)
{
bias = (float const *)mxGPUGetDataReadOnly(dev_bias);
}
else
{
mexErrMsgTxt("Only single format of network bias is supported.");
}
}
// get pointer to cpuArray map
if (mxGetClassID(prhs[3]) == mxUINT32_CLASS)
{
map = (unsigned int const *)mxGetData(prhs[3]);
}
else
{
mexErrMsgTxt("Only unsigned int format of network map is supported.");
}
// get pointer to gpuArray output derivative
if (backMode)
{
dev_derOutput = mxGPUCreateFromMxArray(prhs[4]);
if (mxGPUGetClassID(dev_derOutput) == mxSINGLE_CLASS)
{
derOutput = (float const *)mxGPUGetDataReadOnly(dev_derOutput);
}
else
{
mexErrMsgTxt("Only single format of network output derivative is supported.");
}
}
// parse the network input
ndim = mxGPUGetNumberOfDimensions(dev_input);
dims = mxGPUGetDimensions(dev_input);
if (ndim<3 || ndim>4)
{
mexErrMsgTxt("The network input must be a 4D matrix.");
}
else
{
Fin = dims[0] * dims[1] * dims[2];
Nin = (ndim == 3) ? 1 : dims[3];
}
// parse the network filter
ndim = mxGPUGetNumberOfDimensions(dev_filter);
dims = mxGPUGetDimensions(dev_filter);
if (ndim<3 || ndim>4)
{
mexErrMsgTxt("The network filter must be a 4D matrix.");
}
else
{
if ((dims[0] * dims[1]) != Fin)
{
mexErrMsgTxt("The input feature size of the network filter must equal " \
"that of the network input.");
}
Fout = dims[2];
Nfilt = (ndim == 3) ? 1 : dims[3];
}
// parse the network bias
if (!backMode)
{
if (mxGPUGetNumberOfElements(dev_bias) != Fout)
{
mexErrMsgTxt("Number of elements in network bias must equal the output feature size.");
}
}
// parse map from the network input to the network output
if (mxGetM(prhs[3]) != 4)
{
mexErrMsgTxt("The first dimension of map must be 4.");
}
int N = mxGetNumberOfElements(prhs[3]) / 4; // here we don't force map to be a 2D matrix
Nout = map[N * 4 - 2] + 1;
if (N != Nin & N != Nout)
{
mexErrMsgTxt("The second dimension of map must equal the number of network input" \
"in spherical convolution, and the number of network output in spherical deconvolution.");
}
// parse the network output derivative
if (backMode)
{
ndim = mxGPUGetNumberOfDimensions(dev_derOutput);
dims = mxGPUGetDimensions(dev_derOutput);
if (ndim<3 || ndim>4)
{
mexErrMsgTxt("The network output derivative must be a 4D matrix.");
}
else
{
if ((dims[0] * dims[1] * dims[2]) != Fout)
{
mexErrMsgTxt("Feature size of the network output derivative " \
"must equal the output feature size of the network filter.");
}
if ((ndim == 3 & Nout != 1) & (dims[3] != Nout))
{
mexErrMsgTxt("The number of the network output derivative " \
"must match the one given in map.");
}
}
}
/* -------------------------------------------------------------- */
/* Do the work*/
/* -------------------------------------------------------------- */
if (!backMode) // forward propagation
{
dims_4D[2] = Fout; dims_4D[3] = Nout;
dev_output = mxGPUCreateGPUArray(4, dims_4D, mxSINGLE_CLASS, mxREAL, MX_GPU_DO_NOT_INITIALIZE);
output = (float *)mxGPUGetData(dev_output);
if (Nin > Nout) // spherical convolution
{
SphericalConvolution op(map, Fin, Fout, Nin, Nout, Nfilt);
op.forward(output, input, filter, bias);
if (op.SUCCESS)
{
plhs[0] = mxGPUCreateMxArrayOnGPU(dev_output);
}
else
{
err = true;
errMsg = "The forward spherical convolution is failed.";
}
}
if(Nin <= Nout) // spherical deconvolution
{
SphericalDeconvolution op(map, Fin, Fout, Nin, Nout, Nfilt);
op.forward(output, input, filter, bias);
if (op.SUCCESS)
{
plhs[0] = mxGPUCreateMxArrayOnGPU(dev_output);
}
else
{
err = true;
errMsg = "The forward spherical deconvolution is failed.";
}
}
mxGPUDestroyGPUArray(dev_input);
mxGPUDestroyGPUArray(dev_filter);
mxGPUDestroyGPUArray(dev_bias);
mxGPUDestroyGPUArray(dev_output);
if (err) mexErrMsgTxt(errMsg);
}
else
{
dims_4D[2] = Fin; dims_4D[3] = Nin;
dev_derInput = mxGPUCreateGPUArray(4, dims_4D, mxSINGLE_CLASS, mxREAL, MX_GPU_DO_NOT_INITIALIZE);
derInput = (float *)mxGPUGetData(dev_derInput);
dims_4D[1] = Fin; dims_4D[2] = Fout; dims_4D[3] = Nfilt;
dev_derFilter = mxGPUCreateGPUArray(4, dims_4D, mxSINGLE_CLASS, mxREAL, MX_GPU_DO_NOT_INITIALIZE);
derFilter = (float *)mxGPUGetData(dev_derFilter);
dims_2D[1] = Fout;
dev_derBias = mxGPUCreateGPUArray(2, dims_2D, mxSINGLE_CLASS, mxREAL, MX_GPU_DO_NOT_INITIALIZE);
derBias = (float *)mxGPUGetData(dev_derBias);
if (Nin > Nout) // spherical convolution
{
SphericalConvolution op(map, Fin, Fout, Nin, Nout, Nfilt);
op.backward(derInput, derFilter, derBias, input, filter, derOutput);
if (op.SUCCESS)
{
plhs[0] = mxGPUCreateMxArrayOnGPU(dev_derInput);
plhs[1] = mxGPUCreateMxArrayOnGPU(dev_derFilter);
plhs[2] = mxGPUCreateMxArrayOnGPU(dev_derBias);
}
else
{
err = true;
errMsg = "The backward spherical convolution is failed.";
}
}
if (Nin <= Nout)// spherical deconvolution
{
SphericalDeconvolution op(map, Fin, Fout, Nin, Nout, Nfilt);
op.backward(derInput, derFilter, derBias, input, filter, derOutput);
if (op.SUCCESS)
{
plhs[0] = mxGPUCreateMxArrayOnGPU(dev_derInput);
plhs[1] = mxGPUCreateMxArrayOnGPU(dev_derFilter);
plhs[2] = mxGPUCreateMxArrayOnGPU(dev_derBias);
}
else
{
err = true;
errMsg = "The backward spherical deconvolution is failed.";
}
}
mxGPUDestroyGPUArray(dev_input);
mxGPUDestroyGPUArray(dev_filter);
mxGPUDestroyGPUArray(dev_derOutput);
mxGPUDestroyGPUArray(dev_derInput);
mxGPUDestroyGPUArray(dev_derFilter);
mxGPUDestroyGPUArray(dev_derBias);
if (err) mexErrMsgTxt(errMsg);
}
}