-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmexcudaSparseSingleGPU.cu
188 lines (169 loc) · 5.94 KB
/
mexcudaSparseSingleGPU.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
/**************************************************************************
*
* Copyright 2022 the matRad development team.
*
**************************************************************************/
/*
Mex Function for Computing a sparse vector product with
compiling needs a matlab supported c/c++ compiler e.g. Microsoft Visual Studio C++ or MinGW64 and CUDA
compile with matlab: mexcuda 'mexcudaSparseSingleGPU.cu' ...
-I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.5\include" ...
-L"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.5\lib\x64" ...
NVCCFLAGS='"$NVCCFLAGS -Wno-deprecated-gpu-targets"' LDFLAGS='"$LDFLAGS -Wl,--no-as-needed"'...
-lcusparse -dynamic -v
look into compileAll and compileCUDA for more informations
run with matlab: ret_v = SparseSingleGPU(nrows, ncols, nnz, jc, ir, pr, trans, vector);
*look into SparseSingleGP for more informations
*/
// include matlabs api
#include "mex.h"
#include "class_handle.hpp"
//#include "gpu/mxGPUArray.h"
#include "matrix.h"
#include "sparseSingleGPU.cuh"
void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
//int initalizeMxGPU = mxInitGPU();
//if (initalizeMxGPU == MX_GPU_FAILURE)
// mexErrMsgTxt("Could not initialize GPU!");
// Get the command string
char cmd[64];
if (nrhs < 1 || mxGetString(prhs[0], cmd, sizeof(cmd)))
mexErrMsgTxt("First input should be a command string less than 64 characters long.");
// New
if (!strcmp("new", cmd)) {
// Check parameters
if (nlhs != 1)
mexErrMsgTxt("New: One output expected.");
// Return a handle to a new C++ instance
//if (nrhs == 1)
// plhs[0] = convertPtr2Mat<sparseSingleGPU>(new sparseSingleGPU());
//else if (nrhs == 2) {
if (nrhs == 2) {
if (!mxIsSparse(prhs[1]))
{
mexErrMsgIdAndTxt("MATLAB:sparseInternalOutput:invalidInputType",
"single sparse matrix can only be constructed from double sparse matrix.");
}
try
{
plhs[0] = convertPtr2Mat<sparseSingleGPU>(new sparseSingleGPU(prhs[1]));
}
catch (...)
{
mexErrMsgIdAndTxt("MATLAB:sparseInternalOutput:invalidInputType", "single sparse matrix could not be constructed from double.");
}
}
else
mexErrMsgTxt("New: Invalid Input.");
// We return now, as the object is constructed
return;
}
// For all other purposes, we need to pass the class handle as second argument.
//Check there is a second input, which should be the class instance handle
if (nrhs < 2)
mexErrMsgTxt("Second input should be a class instance handle.");
// Get the class instance pointer from the second input
sparseSingleGPU* sparseSingleGPU_instance = convertMat2Ptr<sparseSingleGPU>(prhs[1]);
if (!strcmp("nnz",cmd))
{
// Check parameters
if (nlhs < 0 || nlhs > 1)
mexErrMsgTxt("nnz: Unexpected arguments.");
mwSize nnz = sparseSingleGPU_instance->getNnz();
plhs[0] = mxCreateDoubleScalar((double) nnz);
return;
}
if (!strcmp("size",cmd))
{
// Check parameters
if (nlhs < 0 || nlhs > 1)
mexErrMsgTxt("size: Unexpected arguments.");
try {
mxArray* szArray = mxCreateDoubleMatrix(1,2,mxREAL);
double* pr = mxGetPr(szArray);
pr[0] = static_cast<double>(sparseSingleGPU_instance->getRows());
pr[1] = static_cast<double>(sparseSingleGPU_instance->getCols());
plhs[0] = szArray;
}
catch (...)
{
mexErrMsgTxt("size: Unexpected access violation.");
}
return;
}
/*
if (!strcmp("disp",cmd))
{
// Check parameters
if (nlhs < 0 || nlhs > 1)
mexErrMsgTxt("size: Unexpected arguments.");
try {
sparseSingleGPU_instance->disp();
}
catch (...)
{
mexErrMsgTxt("disp: Unexpected access violation.");
}
return;
}
*/
if (!strcmp("transpose",cmd))
{
if (nlhs < 0 || nlhs > 1 || nrhs > 2)
mexErrMsgTxt("transpose: Unexpected arguments.");
try {
plhs[0] = convertPtr2Mat<sparseSingleGPU>(sparseSingleGPU_instance->transpose());
}
catch(...)
{
mexErrMsgTxt("transpose: Transposing failed.");
}
return;
}
if (!strcmp("timesVec",cmd))
{
if (nlhs < 0 || nlhs > 1)
mexErrMsgTxt("timesVec: Unexpected arguments.");
try {
const mxSingle* vals = mxGetSingles(prhs[2]);
sparseSingleGPU::index_t n = mxGetNumberOfElements(prhs[2]);
mxArray* result = sparseSingleGPU_instance->timesVec(vals,n);
plhs[0] = result;
}
catch(...)
{
mexErrMsgTxt("timesVec: Product failed.");
}
return;
}
if (!strcmp("vecTimes",cmd))
{
if (nlhs < 0 || nlhs > 1)
mexErrMsgTxt("vecTimes: Unexpected arguments.");
try {
const mxSingle* vals = mxGetSingles(prhs[2]);
sparseSingleGPU::index_t n = mxGetNumberOfElements(prhs[2]);
mxArray* result = sparseSingleGPU_instance->vecTimes(vals,n);
plhs[0] = result;
}
catch(...)
{
mexErrMsgTxt("timesVec: Product failed.");
}
return;
}
// Delete
if (!strcmp("delete", cmd)) {
// Destroy the C++ object
destroyObject<sparseSingleGPU>(prhs[1]);
// Warn if other commands were ignored
if (nlhs != 0 || nrhs != 2)
mexWarnMsgTxt("Delete: Unexpected arguments ignored.");
return;
}
// Got here, so command not recognized
mexErrMsgTxt("Command not recognized.");
}