parallel processing - Using spmd or parfor in Matlab -


i trying run experiments in parallel using matlab 2011b time-consuming. wondering if me 'translate' following block of generic (non-working) parfor code work in spmd code.

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; g=1:amountofoptions         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;                                             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);                        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 = 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 = 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 = 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;  

i've used parfor on spmd because it's more logical me. since parfor requires each iteration within loop independent of other iterations. it's easy encapsulating using following method.

% 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 = 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 

then can create encapsulating function fun accept parameters , process/reoutput them. have following definition/header:

function [startstockprice,strike,v,iv,sigma,riskfreerate,tn] = fun( startstockprice,strike,v,iv,sigma,riskfreerate,tn ); 

Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -