Problems with C++ Pointers in CUDA-Matlab compilation
Hey there, I have the following code-snippet:
double *f;
f = a_function(parameters...);
printf("%f", *(f+1));
loopAry(f, 5);
void loopAry(double *in, int size)
{
printf("%f\n", *(in+1));
for(int i = 0; i < size; i++)
{
printf("\nin[%d]=%f ", i, *(in+i));
}
}
(mex-file in matlab). Now the problem is: I have found two solutions before how to compile mex-files with CUDA in it and I figured out that when I compile it with the first method, the above code works as it should, and if I compile it with the second method, the code just doesn't run. Now I wonder, if the code contains anything suspicious what COULD eventually cause some problems?
Method one does the output (this is correct behaviour):
1.000000 1.000000
in[0]=1.000000
in[1]=1.000000
in[2]=1.000000
in[3]=1.000000
in[4]=1.000000
and the second does the following:
1.000000 0.000000
in[0]=0.000000
in[1]=90932971983710041000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
in[2]=909329719837100410000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000开发者_开发技巧00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
in[3]=90932971983710041000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
in[4]=90932971983710041000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
so there's something wrong with passing the data via pointer, since the output of the the second element (printf("%f", *(fVec0_+1));
) works as it is supposed to work... but NOT after calling loopArray()
...
To clarify the two compiling methods: FIRST (working) Execute this script: http://www-europe.mathworks.com/matlabcentral/fileexchange/25314-cuda-mex which makes a new m-file to compile like the usual mex-script (just with CUDA included) :-)
SECOND (not working) (found on http://forums.nvidia.com/index.php?showtopic=172175)
function nvc(filename)
options='-arch=sm_21';
options=[options ' --use_fast_math'];
txt=sprintf('"C:\\Program Files (x86)\\NVIDIA GPU Computing Toolkit\\CUDA\\v3.2\\bin\\nvcc" %s.cu %s -c -lcufft -lcudart -lcublas -lcuda --ptxas-options=-v -IJ:\\MATLAB32bitR2010b\\extern\\include\\',filename,options);
system(txt)
mex_options='-O'; % enable optimisation
n=getenv('CUDA_LIB_PATH');
mex(['-L' n],mex_options,'-lcudart','-lcufft','-lcublas','-lcuda',sprintf('%s.obj',filename));
delete(sprintf('%s.obj',filename));
EDIT This is the function which returns the pointer:
double *a_function(const mxArray *point)
{
double *dat = mxGetPr(point);
double vals[ 5 ] = {
dat[0]*dat[0]*dat[0],
dat[1]*dat[1]*dat[1],
dat[2]*dat[2]*dat[2],
dat[3]*dat[3]*dat[3],
dat[4]*dat[4]*dat[4]};
double *pnt = vals;
return pnt;
}
You declare the array as a local variable that will go out of scope at the end of the function. The f
pointer than points to the location where the array used to be but isn't anymore.
You should dynamically allocate the array:
double *vals = new double[5];
vals[0] = dat[0]*dat[0]*dat[0];
vals[1] = dat[1]*dat[1]*dat[1];
// ...
// Or use a for-loop for the initialization:
for (int i=0; i<5; i++)
vals[i] = dat[i]*dat[i]*dat[i];
When you are done with the array, delete it again:
delete[] f;
The function a_function
is returning an array that's allocated on its stack. As soon as a_function
returns, the array is invalid. Under some circumstances -- and compiler flags will affect this -- the array will appear to be partially or wholly available, but strictly it's garbage. You must allocate the array using new
if you need to return a pointer to it from a function.
精彩评论