Question regarding calling the destructor of a broadcast variable in parfor.
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Matt J
2024-10-13
编辑:Matt J
2024-10-13
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!