并行性如何使用matlab解决ODE参数扫描问题

时间:2013-12-25 05:09:45

标签: matlab parallel-processing ode

只是想问一些关于我的代码的问题..来自互联网的这个代码,这可能是mathworks网站中的相同示例..我在MATLAB中模拟这个代码并得到结果,我可以看到它需要更少的时间与串行计算相比,使用并行计算(matlabpool与2名工人)时解决ODE ..

我的问题是,有人可以向我解释并行性如何解决matlab中的ODE方程式。下面是我在matlab中并行计算中使用的代码。

这段代码是编译计算代码和显示代码的主要代码。

%  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

这是计算代码..

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

这是用于显示图形的显示代码..

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

这三个代码需要一起打开以获得结果..

我希望有人可以向我解释它是如何运作的......谢谢..

1 个答案:

答案 0 :(得分:1)

Ode_Parallel_Computing_Core中的parfor循环正在提供加速。基本上,这两个工人同时为不同的参数ode_system求解函数bGrid(ij), kGrid(ij)给出的微分方程。相反,传统的for循环会依次评估ij=1然后ij=2等的颂歌。