how can i use max funtion in arrayfun on GPU

8 views (last 30 days)
The following code makes an error.
Error using gpuArray/max. Too few arguments into max... blabla..
In Matlab manual, max function is supported in arrayfun. What should I do for this error?
If I remove the case of max, the code is valid. But as you can see, in case of max, even though b2 has no 100, the code makes an error.
function a = test_gpu6()
aa = rand(1000, 50, 'gpuArray');
b1 = gpuArray.colon(1, 1000)';
b2 = gpuArray([1 1 2 3 1]);
bb = arrayfun(@gpu_sin, b1, b2);
function a = gpu_sin(i, j)
if j == 1
a = sin(aa(i, 1));
elseif j == 2
a = cos(aa(i, 1));
elseif j == 3
a = tan(aa(i, 1));
elseif j == 100
a = max(aa(:, j));
end
end
end
I don't expect that "just use max(aa)" or something like that. Because this kind of programming is a kind of system.
I am looking forward to your brilliant answer. Thank you very much.

Accepted Answer

Edric Ellis
Edric Ellis on 5 Jul 2016
Inside the body of a gpuArray arrayfun call, the max function only operates in its two-input-argument form - i.e. where it returns the larger of the two input arguments.
  2 Comments
sungho Kang
sungho Kang on 6 Jul 2016
Hi, Edric. Thank you for your reply, and it's very helpful. But I am still curious.... that why the code makes an error, even though b2 has no 100. When the nested function is call, b2 has no 100 so the max case should not be calculated, I guess.
Joss Knight
Joss Knight on 13 Jul 2016
In order to run your arrayfun function on the GPU, MATLAB has to compile a new kernel with your MATLAB code. That means all of your code has to be compatible with this compilation, regardless of whether it is executed at runtime.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!