Pergunta

I have a script which solves a system of differential equations for many parameters in a for loop. ( iterations are completely independent, but at the end of each iteration , a large matrix ( mat ) is modified according to the results of the computation ). Here is the code: (B is a matrix containing parameters)

   mat=zeros(20000,1);

   for n=1:20000         


         prop=B(n,:); % B is a (20000 * 2 ) matrix  that contains U and V parameters
         U=prop(1);
         V=prop(2);


         options=odeset('RelTol',1e-6,'AbsTol',1e-20);
         [T,X]=ode45(@acceleration,tspan,x0,options);

         rad=X(:,1);
         if max(rad)<radius   % radius is a constant
        mat(n)=1;

      end

      function xprime=acceleration(T,X)
      .
      .
      .
      end

First I tried to use parfor, but because the acceleration function (ode45 input) was defined as an inline function, (to achieve better performance) I couldn't do that .

Can I open 4 MATLAB sessions (my CPU has 4 cores) and run the code separately in each session , instead of modifying the code to implement acceleration as a separate function, and therefore , using parfor? Does it give 4X the performance of running on one session? (or does it give same performance as parallelized code ? - in parallel code I can't define inline functions-) (on Windows)

Foi útil?

Solução

If you're prepared do to the work of separating out the problem to run separately in 4 sessions, then reassemble the results, sure you can do that. In my experience (on Windows) it actually runs faster to run code in four separate sessions than to have a parfor loop with 4 workers. Not quite as fast as 4x performance of a single session, because the operating system will have other work to do... so for example if you have no other processor-heavy applications running, the OS itself might take up 25% of one core, leaving you with maybe 3.75x performance of a single session. However, this assumes you have enough memory for that not be the limiting factor.

If you wanted to do this regularly you might need to create some file-based signalling/data passing system.

This is obviously not as elegant as a parfor, but is workable for your situation, or if you can't afford the license fee for the parallel toolbox.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top