Matrix multiply slices of 3d Matricies
Afficher commentaires plus anciens
Given two 3d matricies, A and B with
size(A) = (n, m, k)
and
size(B) = (m, p, k)
perform matrix multiplications on each slice obtained by fixing the last index, yielding a matrix C with
size(C) = (n, p, k).
To clarify, we would have
C(:, :, 1) = A(:, :, 1)*B(:, :, 1), ..., C(:, :, k) = A(:, :, k)*B(:, :, k).
I need to do this with gpuArrays in the most efficient manner possible.
Réponse acceptée
Plus de réponses (2)
James Tursa
le 14 Fév 2013
Modifié(e) : James Tursa
le 14 Fév 2013
If you are not restricted to gpuArrays you can do this:
C = mtimesx(A,B);
The MTIMESX function passes pointers to the slice data to BLAS library functions in the background, so it is pretty fast. You can find MTIMESX here:
MTIMESX is not yet multi-threaded across the third dimension (but an update is in the works). A nD matrix multiply multi-threaded on the third dimension called MMX can also be used:
C = MMX('mult', A, B);
MMX can be found here:
1 commentaire
Dan Ryan
le 14 Fév 2013
Azzi Abdelmalek
le 5 Fév 2013
Modifié(e) : Azzi Abdelmalek
le 5 Fév 2013
n=3;
m=4;
k=5;
p=2;
A=rand(n,m,k)
B=rand(m,p,k)
C=zeros(n,p,k)
for ii=1:k
C(:,:,ii)=A(:,:,ii)*B(:,:,ii)
end
7 commentaires
Sean de Wolski
le 5 Fév 2013
Could also use a parfor loop since each iteration is independent of others.
Dan Ryan
le 5 Fév 2013
Azzi Abdelmalek
le 5 Fév 2013
I don't see how you could do it without a for loop.
Sean de Wolski
le 5 Fév 2013
What's wrong with a loop?
Azzi Abdelmalek
le 5 Fév 2013
Modifié(e) : Azzi Abdelmalek
le 5 Fév 2013
bsxfun don't work with
n=3;
m=4;
k=5;
p=2;
A=rand(n,m,k)
B=rand(m,p,k)
C = bsxfun(@times, A, B);
Jill Reese
le 14 Fév 2013
Dan,
Can you elaborate on the sizes of m, n , k, and p that you are interested in? It would be useful to know a ballpark number for the size of problem you want to solve. Do you have many small page sizes, a few large pages, or something else?
Thanks,
Jill
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!