MATLAB Answers

Issues with doses/variants in parfor loop in SimBiology

3 views (last 30 days)
emjey
emjey on 12 Mar 2020
Commented: emjey on 20 Mar 2020
I have a question about runing the sbproj models in parfor loop
1. I load project and set up the solver:
sbioloadproject('modelName.sbproj','m1')
csObj = getconfigset(m1);
csObj.SolverType = 'sundials';
csObj.SolverOptions.AbsoluteToleranceScaling = 1;
csObj.SolverOptions.AbsoluteTolerance = 1E-16;
csObj.SolverOptions.RelativeTolerance = 1E-12;
csObj.MaximumWallClock = 60;
2. I define dose and variant:
dose1 = getdose(m1,'myDose1');
v1 = getvariant(m1,'myVariant1');
3. accelerate the model
sbioaccelerate(m1,csObj,v1,dose1);
4. simulate in parfor loop
m1ParConst = parallel.pool.Constant(m1);
parfor i = 1:r
[...]
% dose1 = getdose(m1ParConst.Value,'rela_3mgpkg_80kg_q2wx31');
% v1 = getvariant(m1,'myVariant1');
[t,y] = sbiosimulate(m1ParConst.Value,[],v1,dose1);
[...]
end
Question:
Q1. Is this correct? If I include dose1 and v1 definition (i.e. if I uncomment dose1 and v1 in parfor loop) I get an error:
Undefined function 'getdose' for input arguments of type 'double'.
Error in parallel_function>make_general_channel/channel_general (line 923)
O = F(C{:});
Error in remoteParallelFunction (line 46)
out = parallel.internal.pool.serialize(feval(channel, channelArgs{:}));
Q2: are the solver settings respected? If I try to include csOBj in 'sbiosimulate' call I get an error as well.
  2 Comments
emjey
emjey on 12 Mar 2020
R2018b, I have to check if I can switch to later version but for now I have this setup and it would be great if I can get it to work.
Update: I am installing R2019b now and keep you posted what happens.

Sign in to comment.

Answers (1)

Arthur Goldsipe
Arthur Goldsipe on 12 Mar 2020
First, what version of SimBiology are you using? Prior to R2019b, there was a problem that prevented objects from getting sent to workers properly. That is usually the cause of errors like "Undefined function 'getdose' for input arguments of type 'double'."
Second, it can be difficult to get details right with acceleration and parallelization when using sbioaccelerate and sbiosimulate. For example, sbioaccelerate is a "local" operation that is not preserved when transferring a model across workers. So if you want an accelerated model on a worker, you have to run sbioaccelerate on that worker.
As an alternative, I recommend using a SimFunction instead. It takes care of all of these details and is generally faster and more efficient. Let me show you some sample code. For now, I will assume you want to simulate varying the values of parameters p1 and p2, and look at the simulation results for y1 and y2. (You can also vary the dosing, or specify your alternate paramaeters using variants.)
1. Load project and set up the solver:
sbioloadproject('modelName.sbproj','m1')
csObj = getconfigset(m1);
csObj.SolverType = 'sundials';
csObj.SolverOptions.AbsoluteToleranceScaling = 1;
csObj.SolverOptions.AbsoluteTolerance = 1E-16;
csObj.SolverOptions.RelativeTolerance = 1E-12;
csObj.MaximumWallClock = 60;
tStop = csObj.StopTime;
2. Define dose and parameter values:
dose1 = getdose(m1,'myDose1');
doseTable = getTable(dose1);
p1 = [1;2;3];
p2 = [4;5;6];
3. Create the SimFunction (ignoring any warning about the dose):
func = createSimFunction(m1, ["p1", "p2"], ["y1", "y2"], dose1, "UseParallel", true);
4. Simulate in parallel:
[timeCell, yCell] = func([p1 p2], tStop, doseTable);
  9 Comments
emjey
emjey on 20 Mar 2020
Thank you, it works indeed for the example model. Acceleration brings the simulation time down to 20% of the default one!

Sign in to comment.

Communities

More Answers in the  SimBiology Community

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!