并行度如何使用Matlab解决ODE参数扫描 [英] how does the parallelism solve the ODE parameter sweep using matlab

查看:226
本文介绍了并行度如何使用Matlab解决ODE参数扫描的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只想问一些有关我的代码的问题..从互联网上获取此代码,这可能与mathworks网站中的示例相同..i在MATLAB中模拟此代码,得到的结果是,我可以看到它花费的时间更少与串行计算相比,在使用并行计算(带有2个工人的matlabpool)时解决ODE.

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..

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

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

这是计算代码.

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..

推荐答案

Ode_Parallel_Computing_Core中的parfor循环可以提高速度.本质上,这两个工人正在同时求解函数ode_system给出的针对不同参数bGrid(ij), kGrid(ij)的微分方程.相比之下,传统的for循环会依次评估ij=1然后ij=2等的ode.

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.

这篇关于并行度如何使用Matlab解决ODE参数扫描的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆