How to reshape a gpuArray in a mex file?
Show older comments
I have a variable x, a gpuArray. I call a mex function func(x). In the mex function, I want to reshape this variable.
The only mex function relevant seems to be "mxSetDimensions". I tried it in a way like this
mxSetDimensions((mxArray *) in[0], new_size, (size_t) 4 );
where "in" is the input argument of mexFunction(...)
It didn't work and corrupted my variable. After I call this mex file, I cannot even get the size of this variable.
>> size(x)
Error using gpuArray/size
Invalid number of objects.
Ideally, I don't want to duplicate the array, since this array maybe very large and this function is required to be as fast as possible. Why does one have to duplicate an array to reshape it? After reshaping it will be used later in the mex file.
Could someone help me? Thanks!
1 Comment
Answers (2)
Walter Roberson
on 16 Jan 2016
0 votes
What you have been passed is not the data for the gpuarray but rather data for a MATLAB object that refers to the GPU array; the actual data is on the GPU, no longer in user space.
I do not know enough about gpuarray to know if the GPU explicitly knows the size and so would have to be told about the reshape.
Edric Ellis
on 18 Jan 2016
0 votes
Walter is correct that the data in the mxArray is on the GPU. You cannot use mxSetDimensions on an mxArray that refers to an mxGPUArray, and unfortunately there is no other way to modify the dimensions of an mxGPUArray. Of course, inside your MEX function, you can treat the underlying pointer as being of any (compatible) dimensions you please.
Categories
Find more on GPU CUDA and MEX Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!