Multiply three multidimensional arrays

Viewed 133

I have two multi-dimensional arrays, i.e. A3D and B3D. The array A3D has a dimension of 2 x 2 x n, while the array B3D has a dimension of m x 2 x n. Every 2D subarray in A3D is a symmetric matrix. For every i, I want to compute B3D[:,:,i]* A3D[:,:,i]* transpose(B3D[:,:,i]). The result is then stored in a multi-dimensional array. I tried the following Julia codes to accomplish the task. However, the computational time with my codes was around 4(s), which is quite slow. I am wondering whether the performance of my codes could be improved. Below are my Julia codes. Thanks for looking at my problem.

m = 100;
n = 30_000; # this could be a very large number.
A3D = rand(2,2,n);
[A3D[:,:,i] = Symmetric(A3D[:,:,i]) for i in 1:n];
B3D = rand(m,2,n);
res3D = zeros(m,m,n);
# approach 1
@time [res3D[:,:,i] = eB*eA*transpose(eB)
      for (eA, eB, i) in zip(eachslice(A3D,dims=3),eachslice(B3D,dims=3),1:n)]; 

UPDATE:

I added another approach to tackle my problem (see below). Approach 2 is a bit better than approach 1. But, can we even improve the performance of my code further?

# approach 2
@inbounds for i = 1:n
    res3D[:,:,i] = B3D[:,:,i]*A3D[:,:,i]*B3D[:,:,i]';
end
0 Answers
Related