我需要并行执行两个 Matlab 函数。问题是从它们那里获得结果比执行要慢得多。
第一种方法:
spmd;
if labindex==1,
K=MatricaK(NP, NE, r, Kxx, N, h, 1); %K is 1000x1000 matrix
end;
if labindex==2,
F=Apkrovos(NP, NE, N, r, Ta, h, 1); %F is 1000x1 vector
end;
end;
%This part is quite fast, around 0.17s.
K=K{1};
F=F{2};
%This part is very slow, around 1.15s.
第二种方法:
parfor i=1:2
if i==1
K=MatricaK(NP, NE, r, Kxx, N, h, 1); %this way doesn't return K outside the loop, but very fast, around 0.15 for all loop
..
K{i}=MatricaK(NP, NE, r, Kxx, N, h, 1); %this works, but slow, around 1.5s
..
K = [K MatricaK(NP, NE, r, Kxx, N, h, 1)]; %also works, but slow, around 1.5s
...
end;
我怎样才能让结果快速返回?我发现MATLAB 上的 Parallel Programming 可以同时执行 3 个不同的函数,但速度没有任何问题。