Multidimensional Array Optimization for Simulations
Show older comments
Is it possible to further optimise the following script by adopting meshgrid, reshape, linear indexing, and/or any other good optimisation techniques?
clear all
% tic;
N1 = 5000;
Nt = 2500;
u = randn(Nt,2);
A = zeros(2,2,N1);
B = zeros(2,2,N1);
C = zeros(2,2,N1);
D = zeros(2,2,N1);
X = zeros(2,1,N1);
Y = zeros(2,Nt,N1);
U = zeros(2,Nt,N1);
tic;
parfor i=1:N1
A(:,:,i) = [unifrnd(0.25,0.75),unifrnd(0.15,0.45);unifrnd(0.4,1.2),unifrnd(0.25,0.75)];
B(:,:,i) = [unifrnd(1,3),unifrnd(2.5,7.5);unifrnd(-22.5,-7.5),unifrnd(-1.95,-0.65)];
C(:,:,i) = [unifrnd(-1,1),unifrnd(-19.5,-6.5);unifrnd(0.5,1.5),unifrnd(-22.5,-7.5)];
D(:,:,i) = [unifrnd(0.1,0.3),unifrnd(-1,1);unifrnd(1,3),unifrnd(-1,1)];
U(:,:,i) = u';
X(:,:,i) = zeros(2,1);
Y(:,:,i) = zeros(2,Nt);
end
toc;
A1 = gpuArray(A);
B1 = gpuArray(B);
C1 = gpuArray(C);
D1 = gpuArray(D);
X3 = gpuArray(X);
U3 = gpuArray(U);
AX = gpuArray(zeros(2,Nt,N1));
X3update = zeros(2,Nt,N1);
X3update = gpuArray(X3update);
tic;
DU = pagefun(@mtimes, D1, U3(:,1:Nt,:));
BU = pagefun(@mtimes, B1, U3(:,1:Nt,:));
for j = 1:Nt
X3update = pagefun(@mtimes,A1,X3) + BU(:,j,:);
AX(:,j,:) = X3;
X3 = X3update;
end
CX = pagefun(@mtimes, C1, AX(:,1:Nt,:));
Y3 = CX + DU;
toc;
Apologies in advance as this is quite a specific question. However, I am trying to increase the computational efficiency of state-space simulations, so quite widely applicable.
Thanks!
4 Comments
José-Luis
on 16 Apr 2014
If you want to optimize I would recommend you to profile you code first.
Indu
on 16 Apr 2014
Jill Reese
on 16 Apr 2014
Note that tic/toc do not provide accurate timings for code containing gpuArrays as operations are executed asynchronously on the GPU. If at all possible, you should use:
gputimeit
Answers (0)
Categories
Find more on Parallel Computing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!