Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6153525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:06:30+00:00 2026-05-23T20:06:30+00:00

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

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:06:31+00:00Added an answer on May 23, 2026 at 8:06 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So in jQuery, I have a global variable currentSubNav that stores a current visible
I have global variable fan_coil_ai_bi, and I use that variable to store some data
I have a script that reads of a global config file using Config::Tiny. Based
When you have a situation where Thread A reads some global variable and Thread
I'm faced with the problem that my applications global variable destructors are not called.
so I have this function which gets called multiple times during my program. //global
UPDATE : It doesn't have to be using static files. Is using a global
I have a variable whose scope needs to be global, because it needs to
I've got a function that wants to access a global variable, the name of
It happens lot of the time that when you report a bug to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.