N dimensional multiplication on gpu arrays

조회 수: 5 (최근 30일)
ahmet keles
ahmet keles 2022년 12월 7일
댓글: Joss Knight 2022년 12월 17일
Is there a better way to compute N dimensional multiplication faster on gpu arrays? I am trying to write a custom deep learning layer which works with gpu array data. However this calculation takes too long for the layer to be meaningfull. Is there a way to do element wise multiplication like this one better on gpu arrays?
A=gpuArray(rand(20,20,16,1000));
B=gpuArray(rand(16,1000));
Z=zeros(size(A),'like',A);
tic
for i=1:16
for j=1:1000
Z(:,:,i,j)=A(:,:,i,j).*B(i,j);
end
end
toc
  댓글 수: 1
ahmet keles
ahmet keles 2022년 12월 7일
I have found a clever way to do it. Convert the small matrix into N dimensional array and multiply.
A=gpuArray(rand(20,20,16,10));
B=gpuArray(rand(16,10));
C=Matrisize(B,A);
Z=A.*C;
function [C]=Matrisize(X1,X2)
S=size(X2);
C=zeros(size(X2),'like',X2);
for i=1:S(1)
for j=1:S(2)
C(i,j,:,:)=X1;
end
end
end

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

채택된 답변

C B
C B 2022년 12월 7일
It is possible to improve the performance of the code you have provided by using the built-in element-wise multiplication operator .* in MATLAB, instead of the loop you are currently using. This operator can automatically take advantage of the parallel processing capabilities of the GPU, allowing for faster computation.
Here is an example of how you can use .* to perform element-wise multiplication of the A and B arrays on the GPU:
% Create gpuArray arrays
A = gpuArray(rand(20,20,16,1000));
B = gpuArray(rand(16,1000));
% Perform element-wise multiplication using .*
Z = A .* B;
Using this approach, the multiplication operation will be performed in parallel on the GPU, which should provide a significant performance improvement compared to using a loop.
Alternatively, you can use the built-in times function to perform element-wise multiplication on the GPU. This function has the same effect as the .* operator, but is written as a function rather than an operator. Here is an example of how to use times to perform element-wise multiplication on the GPU:
% Create gpuArray arrays
A = gpuArray(rand(20,20,16,1000));
B = gpuArray(rand(16,1000));
% Perform element-wise multiplication using times
Z = times(A, B);
Both of these approaches should provide significant performance improvements compared to using a loop to perform element-wise multiplication on the GPU.
  댓글 수: 2
ahmet keles
ahmet keles 2022년 12월 7일
Arrays have incompatible sizes for direct element wise multiplication thats the problem
Joss Knight
Joss Knight 2022년 12월 17일
B = gpuArray(rand(1,1,16,1000)) is what you need here.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by