是否可以通过采用meshgrid
、reshape
、线性索引和/或任何其他良好的优化技术来进一步优化以下脚本?
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;
提前道歉,因为这是一个非常具体的问题。然而,我正在努力提高状态空间模拟的计算效率,因此应用非常广泛。
谢谢!
我不熟悉并行工具箱,但你不能至少用加快均匀随机数的生成吗
A = [unifrnd(0.25, 0.75, [1 1 N1]), unifrnd(0.15, 0.45, [1 1 N1]); unifrnd(0.4, 1.2, [1 1 N1]), unifrnd(0.25, 0.75, [1 1 N1])];
等等?