Question regarding calling the destructor of a broadcast variable in parfor.

4 views (last 30 days)
When I run PARfor script, with parfor having final loop index a number higher or equal than six times (nEnemies>=6), the destructor of Mage is called exactly 6 times while parfor executes. I do not understand why the destructor is even called, and much less why exactly 6 times.
If the parfor has the final loop index less or equal than six times (nEnemies<=6), the number of calls to Mage's destructor is equal to the value of the final loop index. I do not understand why the destructor is called.
As a side note, I know that doDamage method of Mage is not robust and can led to bugs related to Enemy's health, but it is not the point of this question.

Accepted Answer

Matt J
Matt J on 13 Oct 2024
Edited: Matt J on 13 Oct 2024
It is because you have 6 workers in your parpool. Therefore, a copy of Mage is sent to each worker, for a total of 6, during parfor, and those 6 are destroyed when parfor terminates.
When your loop has fewer N<6 iterations, only N of the workers have any work to do, and so only N object copies are broadcasted.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!