【问题标题】:Parallel pooling on MATLAB for BifurcationMATLAB 上用于分叉的并行池
【发布时间】:2020-01-30 11:58:21
【问题描述】:

我对 MATLAB 上的并行池概念(我使用的是 2019 a 版本)和编码不熟悉。我将与您分享的这段代码可以在网上找到,我根据自己的需要做了一些修改。

问题陈述:我有一个非线性系统(罗斯勒方程),我必须绘制它的分岔图,我尝试使用 for 循环正常进行,但它的计算时间太多,我的电脑被绞死了几个次,所以我得到了一个建议,将我的代码并行池化,以解决这个问题。我试图学习如何在网上使用 MATLAB 进行并行池,但我仍然无法解决我的问题,因为仍然存在一些问题,因为我的代码中有 2 个 parfor 循环我在索引和分配方面遇到问题全局参数(请注意:此代码是为正常执行而编写的,不使用并行池)。

我在下面附上我的代码,如果我提到了很多代码行,请原谅。

clc;
a = 0.2; b = 0.2; global c;
crange = 1:0.05:90; % Range for parameter c
k = 0; tspan = 0:0.1:500; % Time interval for solving Rossler system
xmax = []; % A matrix for storing the sorted value of x1

for c = crange
f = @(t,x) [-x(2)-x(3); x(1)+a*x(2); b+x(3)*(x(1)-c)];
    x0 = [1 1 0]; % initial condition for Rossler system 
    k = k + 1;
    [t,x] = ode45(f,tspan,x0); % call ode() to solve Rossler system
    count = find(t>100); % find all the t values which is >10  
    x = x(count,:);
    j = 1; 
    n = length(x(:,1)); % find the length of vector x1(x in our problem)

    for i=2 : n-1
       % check for the min value in 1st column of sol matrix
       if (x(i-1,1)+eps) < x(i,1) && x(i,1) > (x(i+1,1)+eps)  
           xmax(k,j)=x(i,1); % Sorting the values of x1 in increasing order
           j=j+1;
       end 
    end

    % generating bifurcation map by plotting j-1 element of kth row each time
    if j>1
        plot(c,xmax(k,1:j-1),'k.','MarkerSize',1);
    end 

    hold on;
    index(k)=j-1;    
end
xlabel('Bifuracation parameter c');
ylabel('x max');
title('Bifurcation diagram for c'); 

【问题讨论】:

  • 你好阿基尔。这被投票关闭,因为需要提供重现问题的能力。我想您已经进行了一些调试以深入了解哪条线路出现故障。请指出故障发生在哪一行,以便更好地帮助社区为您提供帮助。
  • 这里有一些问题。 1) c 不需要是全局的。 2) k = k+1 特别不允许 parfor 循环,因为执行顺序会影响结果。 3) 你的内部for 循环似乎只是对x(:,1) 进行排序,为什么不使用sort? 4) 我按原样运行你的代码,只用了 150 秒,一个幼稚的并行 for 循环不太可能产生很大的不同,因为每次迭代大约需要 0.1 秒,所以开销很可能会抵消任何好处。
  • (续) 6) 我认为真正的问题是在您尝试显示绘图的循环之后,显示的点太多。我认为您可以使用不太精细的ctspan 向量,但仍然可以获得不错的结果。
  • @David:我的问题是为我使用的代码相同的另一个系统找到结果。提到的问题很简单,因为它有一个简单的流程。我设计的那个太复杂了,肯定需要并行处理。

标签: matlab parallel-processing


【解决方案1】:

这可以通过几个相对简单的步骤与parfor 兼容。首先,parforworkers 不能在屏幕上生成图形,所以我们需要改变一些东西来发出结果。在您的情况下,这并非完全微不足道,因为您的主要结果 xmax 以不完全统一的方式被分配 - 您在不同的循环迭代中分配了不同数量的元素。不仅如此,似乎无法预先预测xmax 需要多少列。

其次,您需要对循环迭代进行一些小改动以兼容parfor,这需要连续的整数循环迭代。

因此,主要的变化是让循环将单独的结果行写入我称为xmax_cell 的元胞数组。在parfor 循环之外,将其转换回矩阵形式很简单。

将所有这些放在一起,我们最终得到了这个,据我所知,它在 R2019b 中正常工作:

clc;
a = 0.2; b = 0.2;
crange = 1:0.05:90; % Range for parameter c
tspan = 0:0.1:500; % Time interval for solving Rossler system

% PARFOR loop outputs: a cell array of result rows ...
xmax_cell = cell(numel(crange), 1);
% ... and a track of the largest result row
maxNumCols = 0;

parfor k = 1:numel(crange)
    c = crange(k);
    f = @(t,x) [-x(2)-x(3); x(1)+a*x(2); b+x(3)*(x(1)-c)];
    x0 = [1 1 0]; % initial condition for Rossler system
    [t,x] = ode45(f,tspan,x0); % call ode() to solve Rossler system
    count = find(t>100); % find all the t values which is >10
    x = x(count,:);
    j = 1;
    n = length(x(:,1)); % find the length of vector x1(x in our problem)
    this_xmax = [];
    for i=2 : n-1
        % check for the min value in 1st column of sol matrix
        if (x(i-1,1)+eps) < x(i,1) && x(i,1) > (x(i+1,1)+eps)
            this_xmax(j) = x(i,1);
            j=j+1;
        end
    end

    % Keep track of what's the maximum number of columns
    maxNumCols = max(maxNumCols, numel(this_xmax));
    % Store this row into the output cell array.
    xmax_cell{k} = this_xmax;
end

% Fix up xmax - push each row into the resulting matrix.
xmax = NaN(numel(crange), maxNumCols);
for idx = 1:numel(crange)
    this_max = xmax_cell{idx};
    xmax(idx, 1:numel(this_max)) = this_max;
end

% Plot
plot(crange, xmax', 'k.', 'MarkerSize', 1)
xlabel('Bifuracation parameter c');
ylabel('x max');
title('Bifurcation diagram for c');

【讨论】:

    猜你喜欢
    • 2015-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多