在 Matlab 中使用 spmd 或 parfor
Using spmd or parfor in Matlab
我目前正在尝试使用 MATLAB 2011b 并行进行 运行 实验,这非常耗时。
我想知道是否有人可以帮助我 'translate' 将以下通用(非工作)parfor 代码块转换为可以在 spmd 代码中工作的代码。
amountOfOptions = 8;
startStockPrice = 60 + 40 * rand(1,amountOfOptions);
strike = 70 + 20 * rand(1,amountOfOptions);
v = 0.35 + 0.3 * rand(1,amountOfOptions);
IV = 0.25 + 0.1 * rand(1,amountOfOptions);
sigma = 0.15 + 0.65 * rand(1,amountOfOptions);
riskFreeRate = 0.05 + 0.1 * rand(1,amountOfOptions);
tn = fix(1 + 3 * rand(1,amountOfOptions));
tic;
for g=1:amountOfOptions
for i=1:10
N = i*5;
Cti = zeros(1,N);
Sti = zeros(1,N);
B = zeros(1,N);
d1_ti = zeros(1,N);
delta_t = zeros(1,N);
ctn = 0;
cmtn = 0;
result = 0;
t = (1:N)/N;
dt = 1/N;
c_mt0 = 0;
for j=1:10
B = sigma(g)*randn(1,N);
part1 = startStockPrice(g)*normcdf((log(startStockPrice(g)/strike(g))+(riskFreeRate(g)+(0.5*(IV(g))^2))*(tn))/(v(g)*sqrt(tn)),0,sigma(g));
part2 = exp(-riskFreeRate(g)*tn)*strike(g)*normcdf((log(startStockPrice(g)/strike(g))+(riskFreeRate(g)-(0.5*(IV(g))^2))*(tn))/(IV(g)*sqrt(tn)));
c_mt0 = part1 - part2;
Sti(1) = startStockPrice(g);
for j = 2:N-1
Sti(j)=Sti(j-1)*exp( (riskFreeRate(g)-dt*0.5*sigma(g)^2) * t(j)*dt + sigma(g)*B(j));
end
Sti(N) = Sti(N-1)*exp( (riskFreeRate(g)-dt*0.5*sigma(g)^2) * t(N)*dt + sigma(g)*B(N));
parfor i = 1:N-1
d1ti(i) = (log(Sti(i)/strike(g)) + (riskFreeRate(g) + v(g).^2/2) * (tn - t(i))) / (v(g) * sqrt(tn - t(i)));
end
parfor i = 1:N-1
Cti(i) = Sti(i).*normcdf((d1ti(i)),0,sigma(g)) - exp(-riskFreeRate(g).*(tn(g) - t(i))).*strike(g).*normcdf(((d1ti(i) - v(g)*sqrt(tn(g) - t(i)))) , 0 ,sigma(g));
end
if((Sti(N) - strike(g)) > 0)
ctn = Sti(N) - strike(g);
else
ctn = 0;
end
parfor i = 1:N-1
delta_t(i) = normcdf((d1ti(i)),0,sigma(g));
end
cmtn = ctn - c_mt0*exp(riskFreeRate(g)*tn(g));
result= cmtn + result;
end
result= result/10;
end
end
time = toc;
我一直使用 parfor 而不是 spmd,因为它对我来说更合乎逻辑。由于 parfor 要求循环内的每次迭代都独立于所有其他迭代。用下面的方法封装起来就这么简单
% Initial Variables
amountOfOptions = 8;
startStockPrice = 60 + 40 * rand(1,amountOfOptions);
strike = 70 + 20 * rand(1,amountOfOptions);
v = 0.35 + 0.3 * rand(1,amountOfOptions);
IV = 0.25 + 0.1 * rand(1,amountOfOptions);
sigma = 0.15 + 0.65 * rand(1,amountOfOptions);
riskFreeRate = 0.05 + 0.1 * rand(1,amountOfOptions);
tn = fix(1 + 3 * rand(1,amountOfOptions));
% Open Parpool
try
parpool;
catch
end
% Use parfor
parfor i = 1:amountOfOptions
[startStockPrice(i),strike(i),v(i),IV(i),sigma(i),riskFreeRate(i),tn(i)] = fun( startStockPrice(i),strike(i),v(i),IV(i),sigma(i),riskFreeRate(i),tn(i) );
end
然后您可以创建封装函数 fun
来接受所有参数和 process/reoutput 它们。它将具有以下 definition/header:
function [startStockPrice,strike,v,IV,sigma,riskFreeRate,tn] = fun( startStockPrice,strike,v,IV,sigma,riskFreeRate,tn );
我目前正在尝试使用 MATLAB 2011b 并行进行 运行 实验,这非常耗时。 我想知道是否有人可以帮助我 'translate' 将以下通用(非工作)parfor 代码块转换为可以在 spmd 代码中工作的代码。
amountOfOptions = 8;
startStockPrice = 60 + 40 * rand(1,amountOfOptions);
strike = 70 + 20 * rand(1,amountOfOptions);
v = 0.35 + 0.3 * rand(1,amountOfOptions);
IV = 0.25 + 0.1 * rand(1,amountOfOptions);
sigma = 0.15 + 0.65 * rand(1,amountOfOptions);
riskFreeRate = 0.05 + 0.1 * rand(1,amountOfOptions);
tn = fix(1 + 3 * rand(1,amountOfOptions));
tic;
for g=1:amountOfOptions
for i=1:10
N = i*5;
Cti = zeros(1,N);
Sti = zeros(1,N);
B = zeros(1,N);
d1_ti = zeros(1,N);
delta_t = zeros(1,N);
ctn = 0;
cmtn = 0;
result = 0;
t = (1:N)/N;
dt = 1/N;
c_mt0 = 0;
for j=1:10
B = sigma(g)*randn(1,N);
part1 = startStockPrice(g)*normcdf((log(startStockPrice(g)/strike(g))+(riskFreeRate(g)+(0.5*(IV(g))^2))*(tn))/(v(g)*sqrt(tn)),0,sigma(g));
part2 = exp(-riskFreeRate(g)*tn)*strike(g)*normcdf((log(startStockPrice(g)/strike(g))+(riskFreeRate(g)-(0.5*(IV(g))^2))*(tn))/(IV(g)*sqrt(tn)));
c_mt0 = part1 - part2;
Sti(1) = startStockPrice(g);
for j = 2:N-1
Sti(j)=Sti(j-1)*exp( (riskFreeRate(g)-dt*0.5*sigma(g)^2) * t(j)*dt + sigma(g)*B(j));
end
Sti(N) = Sti(N-1)*exp( (riskFreeRate(g)-dt*0.5*sigma(g)^2) * t(N)*dt + sigma(g)*B(N));
parfor i = 1:N-1
d1ti(i) = (log(Sti(i)/strike(g)) + (riskFreeRate(g) + v(g).^2/2) * (tn - t(i))) / (v(g) * sqrt(tn - t(i)));
end
parfor i = 1:N-1
Cti(i) = Sti(i).*normcdf((d1ti(i)),0,sigma(g)) - exp(-riskFreeRate(g).*(tn(g) - t(i))).*strike(g).*normcdf(((d1ti(i) - v(g)*sqrt(tn(g) - t(i)))) , 0 ,sigma(g));
end
if((Sti(N) - strike(g)) > 0)
ctn = Sti(N) - strike(g);
else
ctn = 0;
end
parfor i = 1:N-1
delta_t(i) = normcdf((d1ti(i)),0,sigma(g));
end
cmtn = ctn - c_mt0*exp(riskFreeRate(g)*tn(g));
result= cmtn + result;
end
result= result/10;
end
end
time = toc;
我一直使用 parfor 而不是 spmd,因为它对我来说更合乎逻辑。由于 parfor 要求循环内的每次迭代都独立于所有其他迭代。用下面的方法封装起来就这么简单
% Initial Variables
amountOfOptions = 8;
startStockPrice = 60 + 40 * rand(1,amountOfOptions);
strike = 70 + 20 * rand(1,amountOfOptions);
v = 0.35 + 0.3 * rand(1,amountOfOptions);
IV = 0.25 + 0.1 * rand(1,amountOfOptions);
sigma = 0.15 + 0.65 * rand(1,amountOfOptions);
riskFreeRate = 0.05 + 0.1 * rand(1,amountOfOptions);
tn = fix(1 + 3 * rand(1,amountOfOptions));
% Open Parpool
try
parpool;
catch
end
% Use parfor
parfor i = 1:amountOfOptions
[startStockPrice(i),strike(i),v(i),IV(i),sigma(i),riskFreeRate(i),tn(i)] = fun( startStockPrice(i),strike(i),v(i),IV(i),sigma(i),riskFreeRate(i),tn(i) );
end
然后您可以创建封装函数 fun
来接受所有参数和 process/reoutput 它们。它将具有以下 definition/header:
function [startStockPrice,strike,v,IV,sigma,riskFreeRate,tn] = fun( startStockPrice,strike,v,IV,sigma,riskFreeRate,tn );