N dimensional multiplication on gpu arrays
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
ahmet keles
le 7 Déc 2022
Commenté : Joss Knight
le 17 Déc 2022
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
Réponse acceptée
Chetan Bhavsar
le 7 Déc 2022
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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Get Started with GPU Coder dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!