1

我目前正在尝试使用非常耗时的 MATLAB 2011b 并行运行实验。我想知道是否有人可以帮助我将以下通用(非工作)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; 
4

1 回答 1

0

我一直使用 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然后您可以创建将接受所有参数并处理/重新输出它们的封装函数。它将具有以下定义/标题:

function [startStockPrice,strike,v,IV,sigma,riskFreeRate,tn] = fun( startStockPrice,strike,v,IV,sigma,riskFreeRate,tn );
于 2015-04-22T16:32:27.027 回答