mxGetDimensions returning incorrect values

I'm having a problem getting a mex file to run properly, and I was able to isolate the culprit as mxGetDimensions. For a matrix with dimensions MxNxL (in MATLAB), the function is returning the dimensions as Mx0xN. The relevant portion of the code follows. It's part of a code that will be run on the GPU but the problem occurs before I even do anything with the GPU.
#include "cuda.h"
#include "mex.h"
#include "math.h"
/*Main mex function*/
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
int L,N,M,dims,gridDim_y,i;
const int *dim_array;
double *vUoldr, *vUoldi, *vUnewr, *vUnewi;
float *fvUoldr, *fvUoldi;
/*GPU variables:*/
float *Uoldr_gpu, *Uoldi_gpu;
/*Find the dimensions of the input cube*/
dims = (int)mxGetNumberOfDimensions(prhs[0]);
dim_array = (const int*)mxGetDimensions(prhs[0]);
M = dim_array[0];
N = dim_array[1];
L = dim_array[2];
printf("%d %d %d %d\n",dims,M, N, L);
etc
}
In case something in my compilation is the issue here are the commands I'm using to compile:
!/usr/local/cuda/bin/nvcc -c -m64 -arch=sm_30 test.cu -Xcompiler -fPIC -I /Applications/MATLAB_R2011a.app/extern/include
mex -largeArrayDims test.o
and my MATLAB version is 2011a on Mac OS 10.7.4. Any ideas would be helpful as I'm sort of stumped at this point.

1 件のコメント

Jan
Jan 2012 年 9 月 21 日
編集済み: Jan 2012 年 9 月 21 日
@Jake: You use a meaningful title, useful tags (I add some others), you have spent time to isolate the problem and describe it clearly. The question contains the required code, though it is not formatted perfectly (I mark the code and hit the "{} Code" button for the last piece of perfection). You show the compile command, which can be useful for Mex problems and finally you mention the Matlab an OS version. Thanks for asking a good question! +1

サインインしてコメントする。

 採用された回答

Jan
Jan 2012 年 9 月 21 日
編集済み: Jan 2012 年 9 月 22 日

2 投票

This fails on 64 bit machines, which use 32 bit for type INT:
dims = (int)mxGetNumberOfDimensions(prhs[0]);
dim_array = (const int*) mxGetDimensions(prhs[0]);
Better and working under 32 and 64 bit:
const mwSize *dim_array, dims;
dims = mxGetNumberOfDimensions(prhs[0]);
dim_array = mxGetDimensions(prhs[0]); // No casting needed, thanks James

6 件のコメント

James Tursa
James Tursa 2012 年 9 月 21 日
編集済み: James Tursa 2012 年 9 月 21 日
Or just:
dim_array = mxGetDimensions(prhs[0]);
Since mxGetDimensions already returns a const mwSize * type, it doesn't need the explicit cast.
Jake
Jake 2012 年 9 月 21 日
ah! thanks! it probably would have taken me forever to come up with that on my own.
Bayes
Bayes 2018 年 6 月 19 日
編集済み: Bayes 2018 年 6 月 19 日
Hello,
In my mexfunction, I did use the following codes to get the dimensions of first input, but they still return wrong values. Is there any suggestion?
I am running on MATLAB2018a on macOS High Sierra 10.13.5.
Given input data testmat as first input argument
testmat(:,:,1) =
0 0.0200 0.0400 0.0600
0.0200 0 0.0200 0.0400
0.0400 0.0200 0 0.0200
0.0600 0.0400 0.0200 0
testmat(:,:,2) =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
and the following code:
mwSize nDim = mxGetNumberOfDimensions(prhs[0]);
mexPrintf("nDim:%g\n", nDim);
const mwSize* Dim = mxGetDimensions(prhs[0]);
for(mwIndex k=0; k<3; k++){
mexPrintf("Dim[%d]=%g\n", k+1, Dim[k]);
}
double* dist = (double*) mxGetData(prhs[0]);
mwSize n1=4, n2=4, n3=2;
for(mwIndex k=0; k<n3; k++){
for(mwIndex i=0; i<n1; i++){
for(mwIndex j=0; j<n2; j++){
mexPrintf(" %*g ", 4, dist[i+j*n1+k*n1*n2]);
}
mexPrintf("\n");
}
}
I got:
nDim:0
Dim[1]=0
Dim[2]=0
Dim[3]=0
0 0.02 0.04 0.06
0.02 0 0.02 0.04
0.04 0.02 0 0.02
0.06 0.04 0.02 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
If I run it my compiled C mex function again, the output for nDim and Dim will be different.
Jan
Jan 2018 年 6 月 20 日
'%g' is not the correct format to display a value of type mwSize.
If you have a C99 compiler use:
mexPrintf("Dim[%d]=%zu\n", k+1, Dim[k]);
With C89:
mexPrintf("Dim[%d]=%lu\n", k+1, (unsigned long) Dim[k]);
Bayes
Bayes 2018 年 6 月 20 日
Thank you very much. It works well. I should have paid more attention on it.
Artur MKRTCHYAN
Artur MKRTCHYAN 2021 年 5 月 28 日
Thanks a lot !!

サインインしてコメントする。

その他の回答 (1 件)

Malcolm Lidierth
Malcolm Lidierth 2012 年 9 月 21 日

0 投票

Is N correct? I am rusty with mex but with -largeArrayDims, mxGetDimensions returns size_t results. See MATLAB docs for mwSize

カテゴリ

製品

質問済み:

2012 年 9 月 20 日

コメント済み:

2021 年 5 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by