How to reshape a gpuArray in a mex file?

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

Jack Lee
Jack Lee on 16 Jan 2016
Edited: Jack Lee on 16 Jan 2016
Some discussion says that we shouldn't modify the input (passed by reference). http://www.mathworks.com/matlabcentral/newsreader/view_thread/296362
But my data is really large. It'd be ideal if I can reshape it in-place.

Sign in to comment.

Answers (2)

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.
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.

Asked:

on 15 Jan 2016

Answered:

on 18 Jan 2016

Community Treasure Hunt

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

Start Hunting!