필터 지우기
필터 지우기

How to use Sum and Dot function in GPU computation with arrayfun?

조회 수: 7 (최근 30일)
Viraj Gandhi
Viraj Gandhi 2022년 12월 28일
댓글: Joss Knight 2022년 12월 29일
I am trying to find a dot product of two arrays. The given code below is an example. I am new to GPU computing and any help is appreciated.
The main function is:
a = [1,2,3,4];
b = [5,6,7,8];
a=gpuArray(a);
b=gpuArray(b);
funOut = arrayfun(@protoDotProd,a,b);
The function where the dot product is below:
function funOut = protoDotProd(a,b)
cTemp = a+b; % Just an example. In reality, lots of operations
%% option1
% funOut = dot(a,cTemp);
%% option2
% funOutTemp = a.*cTemp;
% funOut = sum(funOutTemp);
%% option3
funOut = sum(a .* cTemp);
None of these options are working and giving me an error:
Error using dotTest
Function passed as first input argument contains unsupported or unknown function 'sum' (or 'dot' for option 1).
For more information see Tips.
Error in 'protoDotProd' (line: 6)

채택된 답변

Joss Knight
Joss Knight 2022년 12월 28일
You cannot perform vector operations in GPU arrayfun. The a and b arguments to your function are not the whole array, they are the scalar values of each element.
Do your element-wise operations ("lots of operations" in your words, followed by the element-wise multiplication) inside your arrayfun function and then do the sum.
Consult the documentation and examples for more info.
  댓글 수: 2
Viraj Gandhi
Viraj Gandhi 2022년 12월 29일
I see your point here in "The a and b arguments to your function are not the whole array, they are the scalar values of each element". arrayfun(@protoDotProd,a,b) is almost similar to write
for i=1:n
funOut(i) = protoDotProd(a(i),b(i));
end
Am I correct?
Thanks for your help.
Joss Knight
Joss Knight 2022년 12월 29일
Exactly correct, yes. Except that you can only perform scalar operations on those scalars (so you can't form an array by concatenating the two values for instance).

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 GPU Computing에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by