MATLAB Answers

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

1 view (last 30 days)
wei zhang
wei zhang on 25 Jan 2021
Edited: Joss Knight on 25 Jan 2021
I am new in compiling the mexcuda. I am trying to use the mxGPUArray as inputs of cuda kernel. My 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),
// =========================================================================
// 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);
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.

Answers (1)

Joss Knight
Joss Knight on 25 Jan 2021
Edited: Joss Knight on 25 Jan 2021
No. You have to get the GPU data pointer using the mxGPUGetData function.




Community Treasure Hunt

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

Start Hunting!

Translated by