Question regarding calling the destructor of a broadcast variable in parfor.
1 view (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
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.
0 Comments
More Answers (0)
See Also
Categories
Find more on Parallel for-Loops (parfor) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!