Why numel() is not accepted with a gpuArray

pcGpu = gpuArray(5:100);
r = arrayfun( @numel, pcGpu );
simple line of code above, although numel is supported with gpuArray, I still get following feedback:
Function passed as first input argument contains unsupported or unknown function 'numel'.
For more information see Tips.
Am I doing sth. wrong?
Thanks, Song

댓글 수: 5

Why would you want to apply numel to each entry in a gpu Array? It would be asking for numel() of a scalar at each step, so the effect would be the same as ones(size(pcGpu), 'gpuArray')
Song Decn
Song Decn 2022년 9월 15일
Thanks Walter for the quick response.
Acutally what I really want to implement is to use GPU to execute a function containing numel(), like this:
function f = testFcn(a)
f = numel(a);
end
pcGpu = gpuArray(5:100);
% r = arrayfun( @numel, pcGpu );
r = arrayfun( @testFcn, pcGpu );
But still I get the following error:
Error using Untitled2 (line 24)
Function passed as first input argument contains unsupported or unknown function 'numel'.
For more information see Tips.
Error in 'testFcn' (line: 2)
So, does it mean that I can't call numel in a function for gpuArray?
I barely find examples for gpuArray calling in callback function.
Song Decn
Song Decn 2022년 9월 15일
편집: Walter Roberson 2022년 9월 15일
I tried another way:
pcGpu = gpuArray(5:100);
a = 1:10;
% r = arrayfun( @numel, pcGpu );
f = @(x)testFcn(a,x);
arrayfun( f, pcGpu );
function f = testFcn(a, pcGpu)
for i = 1:numel(a)
f = pcGpu + i;
end
end
still not working:
Error using Untitled2 (line 26)
Use of functional workspace is not supported.
For more information see Tips.
Are you applying numel to the entire array, or are you applying numel to each element of an array? When you use arrayfun then by definition the function is passed one element of the array.
arrayfun over a gpuArray does not support anonymous functions with captured variables.
for i = 1:numel(a)
f = pcGpu + i;
end
That overwrites all of f each iteration, so if you could get it to work, the result would be the same as if you had done
i = numel(a);
f = pcGpu + numel(a);
Reminder: you can pass numel(a) in as a parameter.

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2022년 9월 15일
편집: Walter Roberson 2022년 9월 15일

0 개 추천

numel is not supported inside any function run on a gpuArray by using arrayfun. The above link lists all supported functions.
Remember that arrayfun on a gpuArray runs all aspects on the gpu. Variables not specifically configured as gpuArray have to be converted to gpuArray internally because the entire function runs on the gpu.

댓글 수: 1

Song Decn
Song Decn 2022년 9월 18일
Thanks a lot Walter. I learned a lot and now have to rewrite my code now.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 GPU Computing in MATLAB에 대해 자세히 알아보기

제품

릴리스

R2020b

태그

질문:

2022년 9월 14일

댓글:

2022년 9월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by