GPU looping through matrices

조회 수: 2 (최근 30일)
Weixin Wang
Weixin Wang 2020년 8월 28일
댓글: Walter Roberson 2020년 8월 29일
Hello, I have a very basic question about GPU computing. Suppose I have a 3-by-3-by-1000 array, I want to loop through the third dimension and calculate every trace of the 3-by-3 matrices.
A = rand(3,3,1000);
trA = zeros(1000,1);
for n = 1:1000
trA(n) = trace(A(:,:,n));
end
Is there any way I can use GPU to do this? I know the "pagefun" can be used to loop through the third dimension, but the function it accepts is very limited, which does not include the trace function. Is it possible to even loop through the third dimension with a custom function from a matrix to a real number?
Thanks in advance.

채택된 답변

Walter Roberson
Walter Roberson 2020년 8월 28일
traceG = @(A) sum(pagefun(@triu, pagefun(@tril, A)),[1 2]);
  댓글 수: 4
Weixin Wang
Weixin Wang 2020년 8월 28일
Thanks a lot for this reply. So I guess there isn't a general solution, we have to invent a smart method for each specific function (in this example is trace), is this right?
Walter Roberson
Walter Roberson 2020년 8월 29일
The general solution is to loop using indexing.
However, indexing of GPU arrays is slow!! The instruction set does not have direct indexing in the sense of selecting only a single element and working on it: instead the set of instructions has to create a mask that each computation unit has that checks to see whether the address of its data is one of the ones that needs to be worked on, and if so then it does the operation, and if not then it sits idle until the next instruction. If you had (say) a 2^24 element array and wanted to select a single element from it, then the computation units for 2^24 minus 1 elements would say "Nope, that isn't me" and would idle and the one selected element would do the instruction.
Therefor it is significantly faster to vectorize, especially with longer vectors, doing work on as large a portion of the array as feasible. You can index: it just isn't efficient to do so. Smart methods like Edric's might at first look like they are doing a lot of extra work compared to what could be done on a classic system, but the work is being done in parallel over large portions of the array and that can be much faster than being selective on the data to work on.

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by