MATLAB:在并行模式下运行时,全局变量会发生什么? [英] MATLAB: What happens for a global variable when running in the parallel mode?

查看:1140
本文介绍了MATLAB:在并行模式下运行时,全局变量会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在并行模式下运行时发生全局变量会发生什么?



我有一个全局变量to_be_optimized_pa​​rameterIndexSet,它是一个应该使用gamultiobj进行优化的索引向量,我只在主脚本中设置了它的值其他)。

我的代码在串行模式下正常工作,但当切换到并行模式时(使用matlabpool open并为'gaoptimset'设置适当的值),所提到的全局变量变为空(= [])在健身功能,并导致此错误:

  ???错误使用==> parallel_function at 598 
错误==> PF_gaMultiFitness at 15 [THIS LINE:constants(to_be_optimized_pa​​rameterIndexSet)= individual;]
在赋值A(I)= B中,B和
中的元素数必须相同。

错误==> fcnvectorizer在17
parfor(i = 1:popSize)

错误==> gamultiobjMakeState at 52
Score =
fcnvectorizer(state.Population(initScoreProvided + 1:end,:),FitnessFcn,numObj,options.SerialUserFcn);

错误==> gamultiobjsolve在11
state = gamultiobjMakeState(GenomeLength,FitnessFcn,output.problemtype,options);

E rror in ==> gamultiobj at 238
[x,fval,exitFlag,output,population,scores] = gamultiobjsolve(FitnessFcn,nvars,...

错误==> PF_GA_mainScript at 136
[x,fval,exitflag,output] = gamultiobj(@(individual)PF_gaMultiFitness(individual,initialConstants),...

导致:
用户提供的适应度函数评估失败。GA不能继续。

我已经检查了所有的代码,以确保我没有改变这个全局变量在其他地方。

我有一个四核处理器。



这个错误在哪里?有什么建议?



编辑1 :主要脚本中的MATLAB代码:

  clc 
clear
关闭所有

格式short g
全局simulation_duration%PF_gaMultiFitness将使用此变量
全局to_be_optimized_pa​​rameterIndexSet%PF_gaMultiFitness将使用这个变量
全局IC刺激Moment%PF_gaMultiFitness将使用这些v ariables

[initialConstants IC] = oldCICR_Constants; %初始化状态
to_be_optimized_pa​​rameterIndexSet = [21 22 23 24 25 26 27 28 17 20];
LB = [0.97667 0.38185 0.63529 0.046564 0.23207 0.87484 0.46014 0.0030636 0.46494 0.82407];
UB = [1.8486 0.68292 0.87129 0.87814 0.66982 1.3819 0.64562 0.15456 1.3717 1.8168];
PopulationSize = input('Population size?');
GaTimeLimit = input('GA time limit?(second)');
matlabpool打开
nGenerations = inf;
options = gaoptimset('PopulationSize',PopulationSize,'TimeLimit',GaTimeLimit,'Generations',nGenerations,...
'Vectorized','off','UseParallel','always');

[x,fval,exitflag,output] = gamultiobj(@(individual)PF_gaMultiFitness(individual,initialConstants)...
length(to_be_optimized_pa​​rameterIndexSet),[],[],[ ],[],LB,UB,选项);

matlabpool关闭

其他一些代码来显示结果...



适应函数的MATLAB代码PF_gaMultiFitness:

pre $ function objectives = PF_gaMultiFitness(个体,常量)
全局simulation_duration IC刺激元素to_be_optimized_pa​​rameterIndexSet
%此函数返回一个列中的多个目标和每个目标

常量(to_be_optimized_pa​​rameterIndexSet)=个体;
[smcState,〜,Time] = oldCICR_CompCore(常量,IC,simulation_duration,2);
targetValue = 1; %[uM]期望的[Ca] i峰值浓度
afterStimulus = smcState(Time> stimulusMoment,14); %刺激后的[Ca] i值
peak_Ca_value = max(afterStimulus); %smcState(:,14)是[Ca] i

if peak_Ca_value< 0.8 * targetValue
targets(1,1)= inf;

else
targets(1,1)= abs(peak_Ca_value - targetValue);
end

pkIDX = peakFinder(afterStimulus);
nPeaks = sum(pkIDX);
if nPeaks> 1
peakIndexes = find(pkIDX);
period = Time(peakIndexes(2)) - Time(peakIndexes(1));
目标(1,2)= 1e5 * 1 /期间;

elseif nPeaks == 1&& peak_Ca_value> 0.8 * targetValue
objectives(1,2)= 0;
else
targets(1,2)= inf;

end


end


解决方案

全局变量不会从MATLAB客户端传递给执行PARFOR循环体的工作人员。发送到循环体的唯一数据是程序文本中发生的变量。 这个博客条目可能会有所帮助。


What happens for a global variable when running in the parallel mode?

I have a global variable, "to_be_optimized_parameterIndexSet", which is a vector of indexes that should be optimized using gamultiobj and I have set its value only in the main script(nowhere else).

My code works properly in serial mode but when I switch to parallel mode (using "matlabpool open" and setting proper values for 'gaoptimset' ) the mentioned global variable becomes empty (=[]) in the fitness function and causes this error:

??? Error using ==> parallel_function at 598
Error in ==> PF_gaMultiFitness at 15 [THIS LINE: constants(to_be_optimized_parameterIndexSet) = individual;]
 In an assignment  A(I) = B, the number of elements in B and
 I must be the same.

Error in ==> fcnvectorizer at 17
        parfor (i = 1:popSize)

Error in ==> gamultiobjMakeState at 52
        Score =
        fcnvectorizer(state.Population(initScoreProvided+1:end,:),FitnessFcn,numObj,options.SerialUserFcn);

Error in ==> gamultiobjsolve at 11
state = gamultiobjMakeState(GenomeLength,FitnessFcn,output.problemtype,options);

E    rror in ==> gamultiobj at 238
[x,fval,exitFlag,output,population,scores] = gamultiobjsolve(FitnessFcn,nvars, ...

Error in ==> PF_GA_mainScript at 136
[x, fval, exitflag, output] = gamultiobj(@(individual)PF_gaMultiFitness(individual, initialConstants), ...

Caused by:
    Failure in user-supplied fitness function evaluation. GA cannot continue.

I have checked all the code to make sure I've not changed this global variable everywhere else.

I have a quad-core processor.

Where is the bug? any suggestion?

EDIT 1: The MATLAB code in the main script:

clc
clear
close all

format short g
global simulation_duration % PF_gaMultiFitness will use this variable
global to_be_optimized_parameterIndexSet % PF_gaMultiFitness will use this variable
global IC  stimulusMoment % PF_gaMultiFitness will use these variables

[initialConstants IC] = oldCICR_Constants; %initialize state
to_be_optimized_parameterIndexSet = [21    22    23    24    25    26    27    28    17    20];
LB = [ 0.97667      0.38185      0.63529     0.046564      0.23207      0.87484      0.46014    0.0030636   0.46494      0.82407 ];
UB = [1.8486      0.68292      0.87129      0.87814      0.66982       1.3819      0.64562      0.15456   1.3717       1.8168];
PopulationSize = input('Population size? ') ;
GaTimeLimit = input('GA time limit? (second)  ');
matlabpool open
nGenerations = inf;
options = gaoptimset('PopulationSize', PopulationSize, 'TimeLimit',GaTimeLimit, 'Generations', nGenerations, ...
    'Vectorized','off', 'UseParallel','always');

[x, fval, exitflag, output] = gamultiobj(@(individual)PF_gaMultiFitness(individual, initialConstants), ...
    length(to_be_optimized_parameterIndexSet),[],[],[],[],LB,UB,options);

matlabpool close

some other piece of code to show the results...

The MATLAB code of the fitness function, "PF_gaMultiFitness":

function objectives =PF_gaMultiFitness(individual, constants)
global simulation_duration IC stimulusMoment to_be_optimized_parameterIndexSet
%THIS FUNCTION RETURNS MULTI OBJECTIVES AND PUTS EACH OBJECTIVE IN A COLUMN

constants(to_be_optimized_parameterIndexSet) = individual;
[smcState , ~, Time]= oldCICR_CompCore(constants, IC, simulation_duration,2);
targetValue = 1; % [uM]desired [Ca]i peak concentration
afterStimulus = smcState(Time>stimulusMoment,14); % values of [Ca]i after stimulus
peak_Ca_value = max(afterStimulus); % smcState(:,14) is [Ca]i

if peak_Ca_value < 0.8 * targetValue
    objectives(1,1) = inf;

else
    objectives(1, 1) =  abs(peak_Ca_value - targetValue);
end

pkIDX = peakFinder(afterStimulus);
nPeaks = sum(pkIDX);
if nPeaks > 1
    peakIndexes = find(pkIDX);
    period = Time(peakIndexes(2)) - Time(peakIndexes(1));
    objectives(1,2)  = 1e5* 1/period;

elseif nPeaks ==   1 && peak_Ca_value > 0.8 * targetValue
    objectives(1,2) = 0;
else 
    objectives(1,2) = inf;

end


end

解决方案

Global variables do not get passed from the MATLAB client to the workers executing the body of the PARFOR loop. The only data that does get sent into the loop body are variables that occur in the text of the program. This blog entry might help.

这篇关于MATLAB:在并行模式下运行时,全局变量会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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