spmd, parfor, createParellel, Slower than the usual code without going in parallel

1 次查看(过去 30 天)
I have the following code which is quite computationally intensive. I tried to make it go in parallel. It turned out the computation time is much slower than that without going in parallel. Does anyone know what is wrong with my code? I have the following 2 examples that both showed this weird result. I tried replacing parfor with spmd and ran it. It produced the weird result too. Thank you very much.
1st:
% Conditional_pdf is a probability function that takes vectors as inputs and output
%a vector of elements as probability corresponding to each vector element.
% x_t is a vector
% x_t1 is a vector
tic;
F = @(x)Conditional_pdf(dt,x_t,x,sigma,beta);
parfor i = 1:length(x_t1)
ret(i) = mean(F(x_t1(i)));
end
toc;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2nd:
parfor pt=1:2
f0 = Fwd;
F = ones(N(pt),M)*f0(1,pt);
V = ones(N(pt),M)*ALPHA(pt);
for j=1:N(pt)
F(j+1,:) = F(j,:) + V(j,:).*abs(F(j,:)).^(BETA(j)).*dW_first(j,:).*sqrt(dt);
V(j+1,:) = V(j,:) + LAMBDA(j)*(LAMBDA_AVG(j) - V(j,:)).*dt + NU(j).*V(j,:).*dW_second(j,:).*sqrt(dt);
end
K = Strike(:,pt);
Fwd_Premium_call = zeros(length(K),1);
Fwd_Premium_put = zeros(length(K),1);
for k=1:length(K)
temp=sum(max(F(N(pt)+1,:)-Strike(k,pt),0));
temp1=sum(max(Strike(k,pt)-F(N(pt)+1,:),0));
Fwd_Premium_call(k) = temp/(M);
Fwd_Premium_put(k) = temp1/(M);
end
Price_call = exp(-LIBOR_d(:,pt).*T_future(:,pt)).*Fwd_Premium_call;
Price_put = exp(-LIBOR_d(:,pt).*T_future(:,pt)).*Fwd_Premium_put;
model_volatility_call = blkimpv( Fwd(:,pt), Strike(:,pt), LIBOR_d(:,pt), T_future(:,pt) - T_now(:,pt), Price_call, 10, 1e-10,{'call'});
model_volatility_put = blkimpv( Fwd(:,pt), Strike(:,pt), LIBOR_d(:,pt), T_future(:,pt) - T_now(:,pt), Price_put, 10, 1e-10,{'put'});
SABR_vol(:,pt) = [model_volatility_put(1); model_volatility_put(2); model_volatility_call(3); model_volatility_call(4); model_volatility_call(5)];
end

回答(1 个)

Edric Ellis
Edric Ellis 2014-1-6
There are several reasons why your code might not speed up when going parallel. The most likely is that you're using local workers only, and MATLAB is already doing a good job of multithreading your code - in which case, PARFOR and SPMD cannot speed things up for you. Check using Windows Task Manager, or UNIX "top" to see whether the non-PARFOR version of your code is able to run multithreaded.
  2 个评论
Puiyin
Puiyin 2014-1-6
Hello. I have a few questions.
Does matlab only allow me to use local workers only? Or do I need to pay extra for the virtual multithreading (does such a thing exist)?
So, you think the code written above is fine for multithreading?
Thanks
Edric Ellis
Edric Ellis 2014-1-6
MATLAB Distributed Computing Server is required to run workers on a cluster.
The easiest way to see if MATLAB is able to multithread your code is to run it and monitor the processor usage.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB Parallel Server 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by