How are Extrinsic Functions Handled When Executing Parallel Simulations with parsim() ?

Suppose a Simulink simulation that contains a Matlab Function block that calls another function declared as extrinsic with @doc:coder.extrinsic.
If multiple simulations are executed in parallel with @doc:parsim, a) does each worker get its own instance of the Matlab engine in which the extrinsic function is executed? Or b) do all of the workers share a common Matlab engine and share the extrinsic function? Seems like the latter could be problematic if the extrinsic function contains persistent data.
As best I can tell, @doc:parfor operates in accordance with (a), but I couldn't find anything on point in the doc for @doc:parsim.

3 commentaires

@Paul let me see if I have this right.
parpool(nworkers);
in = Simulink.SimulationInput(mdl);
out = parsim(in);
mdl has a MATLAB Function block that calls a function declared as extrinsic, thereby using a MATLAB engine (different than the workers).
Have you tried it and if so, aside from the workers, do you see 1 MATLAB Engine starting or multiple? I'm inclined to think only 1 Engine is started. Another way to test this is if MATLAB tried running two different functions that were both extrinsic (the calls would be serial, as MATLAB is), then only one Engine would be launched.
Paul
Paul le 21 Juin 2025
Modifié(e) : Paul le 22 Juin 2025
Hi Raymond,
Yes, mdl has a MATLAB Function block that calls a function declared as extrinsic. The question is whether or not the simulations on each worker are fully/completely/wholly indedpendent of each other and therefore the extrinsic function for each simulation is running on each worker. Refering to the diagram at parsim workflow, is the extrinsic function for each simulation executed on the worker or client session of Matlab?
Note that I'm assuming that the Parallel Computing Toolbox is installed. If it's not, then my understanding is that parsim is essentially a call to sim, and in this case the same extrinsic function will run as each simulation is executed serially, and therefore any persistent data in the extrinsic function will carry over from one simulation to the next.
I haven't tried testing any of this; I was hoping that the behavior in question is documented somewhere and I just couldn't find it.
"each simulation is executed serially, and therefore any persistent data in the extrinsic function will carry over from one simulation to the next."
Actually, if the simulation are executed serially, there should not be any problem with persistent data, at least based on my current understanding of how persistent data works in simulations.

Connectez-vous pour commenter.

Réponses (1)

Hello Paul,
Please find the information regarding parsim and extrinsic functions:
  • parsim distributes simulations to workers in a parallel pool.
  • Each worker is an independent MATLAB process with its own workspace and memory.
  • The coder.extrinsic function is executed by the MATLAB engine.
  • Therefore, since each worker is a separate MATLAB process, each worker uses its own instance of the MATLAB engine to execute the extrinsic function.

1 commentaire

Thanks for the answer. Is it based on the documentation or observation or something else?

Connectez-vous pour commenter.

Produits

Version

R2024b

Question posée :

le 22 Avr 2025

Commenté :

le 1 Août 2025

Community Treasure Hunt

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

Start Hunting!

Translated by