Could I use the `mxGPUArray` format data in the cuda kernel function?

2 次查看(过去 30 天)
I am new in compiling the mexcuda. I am trying to use the mxGPUArray as inputs of cuda kernel. My sample.cu code is as below. It is a simple plus function. The inputs are two vectors in CPU. The output is one vector in CPU.
But it gives the error at the line of
% z[row] = x[row] * y[row] line error
error: expression must be a pointer to a complete object type
Must I use the cudaMemcpy function, with transfering the mxGPUArray* data to a double* format?
#include "mex.h"
#include "gpu/mxGPUArray.h"
__global__ void plus(mxGPUArray*x, mxGPUArray* y, mxGPUArray* z, int N)
{
int row = blockIdx.x*blockDim.x + threadIdx.x;
if (row < N)
{
z[row] = x[row] * y[row];% error is here
}
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, mxArray const *prhs[])
{
mxGPUArray* d_x = mxGPUCopyFromMxArray(prhs[0]);
mxGPUArray* d_y = mxGPUCopyFromMxArray(prhs[1]);
int N = (int)(mxGPUGetNumberOfElements(d_x));
mxGPUArray* d_z = mxGPUCreateGPUArray(mxGPUGetNumberOfDimensions(d_x),
mxGPUGetDimensions(d_x),
mxGPUGetClassID(d_x),
mxGPUGetComplexity(d_x),
MX_GPU_DO_NOT_INITIALIZE);
// =========================================================================
// gpu computing
// =========================================================================
int const threadsPerBlock = 256;
int blocksPerGrid;
blocksPerGrid = (N + threadsPerBlock - 1) / threadsPerBlock;
plus <<<blocksPerGrid, threadsPerBlock>>>(d_x, d_y, d_z,N);
plhs[0] = mxGPUCreateMxArrayOnCPU(d_z);
mxGPUDestroyGPUArray(d_x);
mxGPUDestroyGPUArray(d_y);
mxGPUDestroyGPUArray(d_z);
}
I don't want to do many copy steps. So I did not transfer the mxGPUArray into another double array. Any suggestion would be appreciated. Thank you.

回答(1 个)

Joss Knight
Joss Knight 2021-1-25
编辑:Joss Knight 2021-1-25
No. You have to get the GPU data pointer using the mxGPUGetData function.

类别

Help CenterFile Exchange 中查找有关 GPU CUDA and MEX Programming 的更多信息

产品


版本

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by