mexcuda how to declare a matrix with size N given in input
1 回表示 (過去 30 日間)
古いコメントを表示
I am trying to calculate some harmonic components of a function mua on a [Nx Ny ] domain. I want to give in input the desired number of harmonics N, so that my output is a matrix An of size [N Nx Ny].
I can retrieve Nx and Ny from mxGPUGetNumberOfElements and mxGPUGetDimensions, but I can't find a way to access the value of N in my MexFunction, all I can get is an adress.
Here is a very simplified version of the mexFunction to illustrate my problem, where I hard coded N=16 in my declaration of matrix (Nnyz=16*NyNz;) to avoid memory issues , and printed the values of N and d_N:
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, mxArray const *prhs[])
{
mxGPUArray const *mua;
mxGPUArray const *N;
float const *d_mua;
int const *d_N;
mxGPUArray *An;
float *d_An;
int Np;
int const threadsPerBlock = 1024;
int blocksPerGrid;
mxInitGPU();
mua = mxGPUCreateFromMxArray(prhs[0]);
d_mua = (float const *)(mxGPUGetDataReadOnly(mua));
N = mxGPUCreateFromMxArray(prhs[1]);
d_N = (int const *)(mxGPUGetDataReadOnly(N));
mwSize NyNz;
mwSize Nnyz;
mwSize Ny;
NyNz= int(mxGPUGetNumberOfElements(mua));
Ny=*mxGPUGetDimensions(mua);
int Nz=NyNz/Ny;
Nnyz=16*NyNz;
mexPrintf("\n %i %i %i %i \n",N,d_N);
An = mxGPUCreateGPUArray(1,
&Nnyz,
mxSINGLE_CLASS,
mxREAL,
MX_GPU_INITIALIZE_VALUES);
d_An = (float *)(mxGPUGetData(An));
Np = (int)(mxGPUGetNumberOfElements(mua));
blocksPerGrid = (Np + threadsPerBlock - 1) / threadsPerBlock;
test_print<<<blocksPerGrid, threadsPerBlock>>>(d_mua,d_N,d_An,Np);
plhs[0] = mxGPUCreateMxArrayOnGPU(An);
mxGPUDestroyGPUArray(mua);
mxGPUDestroyGPUArray(N);
mxGPUDestroyGPUArray(An);
}
The printed result for N,d_N is:
2063388544 18874880.
Printing Ny and NyNz gave me the right dimensions .
0 件のコメント
回答 (1 件)
Joss Knight
2023 年 4 月 4 日
Hi Charlotte. If you just want to pass an ordinary scalar value to a mex function then use the ordinary Mex API rather than the mxGPU API. Pass a non-gpuArray scalar N to your function and read it into a variable using simple syntax like mxGetScalar . By calling mxGPUCreateFromMxArray you are creating a new scalar on the GPU. Now when you get the pointer to the data it's on the GPU! So you would have to copy it back again using cudaMemcpy or equivalent.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で GPU CUDA and MEX Programming についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!