Question

just want to ask some question regarding my code..got this code from internet and this might be the same example in mathworks website..i simulating this code in MATLAB and got the result which i can see that it takes less time to solve the ODE when using parallel computing (matlabpool with 2 workers) compared to serial computing..

my question is, can someone explain to me on how does the parallelism solve the ODE equation in matlab..below are the code that i use in parallel computing in matlab..

this code is the main code which will compile the calculation code and display code..

%  Main Coding
%  Initialize the k and b ranges.
%
  bVals = 0.1 : 0.05 : 5;
  kVals = 1.5 : 0.05 : 5;
%
%  Begin the parameter sweep.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'ODE_POOL\n' );
  fprintf ( 1, '  Sweep through sets of values of parameters B and K,\n' );
  fprintf ( 1, '  computing the solution of the ODE corresponding to each set.\n' );
  fprintf ( 1, '  For each solution X(T), determine the maximum value over time.\n' );
  fprintf ( 1, '  Construct a contour plot of XMAX(B,K).\n' );
  fprintf ( 1, '  Use the PARFOR command to carry out these computations in parallel.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Number of K values = %d\n', length ( kVals ) );
  fprintf ( 1, '  Number of B values = %d\n', length ( bVals ) );
  fprintf ( 1, '  Number of times the ODE must be solved = %d\n', ...
    length ( kVals ) * length ( bVals ) );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Begin computation\n' );

  matlabpool open local 2
%
%  Solve the ODE for every pair of K and B values and return the maximum
%  value over the time interval.
%
   tic
   peakVals = Ode_Parallel_Computing_Core ( bVals, kVals );
   toc

%   matlabpool close
%
%  Now display am image of the data.
%
  Ode_Parallel_Computing_Display ( bVals, kVals, peakVals );

  matlabpool close

this is the calculation code..

function peakVals = Ode_Parallel_Computing_Core ( bVals , kVals )
%
%  Form a grid of all pairs of K and B:
%
  [ kGrid, bGrid ] = meshgrid ( bVals , kVals );
%
%  Define an array to hold the results, and initialize it to NAN.
%
  peakVals = nan ( size ( kGrid ) );
%
%  Solve the ODE for every pair of K and B values.  (M is fixed at 5.)
%
  m = 5.0;

  parfor ij = 1 : numel(kGrid)
%
%  Solve the ODE over the time interval 0 <= T <= 25, with
%  initial conditions X(0) = 0, X'(0) = 1.
%
    [ T, Y ] = ode45 ( @(t,y) ode_system ( t, y, m, bGrid(ij), kGrid(ij) ), ...
      [0, 25],  [0, 1] );
%
%  Retrieve the maximum value achieved by this solution.
%
    peakVals(ij) = max ( Y(:,1) );

  end

  return
end

and this is display code which is use to display the graph..

function Ode_Parallel_Computing_Display ( bVals, kVals, peakVals )
figure ( 1 );

  surf ( bVals, kVals, peakVals, 'EdgeColor', 'Interp', 'FaceColor', 'Interp' );

  title ( 'Results of ODE Parameter Sweep With Parallel Computing' )

  xlabel ( 'Damping B' );
  ylabel ( 'Stiffness K' );
  zlabel ( 'Peak Displacement' );

  view ( 50, 30 )

%   filename = 'ode_display.png';
%   print ( '-dpng', 'ode_display.png' );
%   fprintf ( 1, '\n' );
%   fprintf ( 1, '  Plot saved as "%s".\n', 'ode_display.png' );

  return
end

these three code need to be open together in order to get the result..

i hope that someone can explain to me on how does it works..thanks..

Was it helpful?

Solution

The parfor loop in Ode_Parallel_Computing_Core is what is giving the speedup. Essentially the two workers are simultaneously solving the differential equation given by the function ode_system for different parameters bGrid(ij), kGrid(ij). In contrast, a traditional for loop would sequentially evaluate the ode for ij=1 then ij=2, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top