フィルターのクリア

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

1 回表示 (過去 30 日間)
wei zhang
wei zhang 2021 年 1 月 25 日
編集済み: Joss Knight 2021 年 1 月 25 日
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 Center および File ExchangeGPU 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