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

조회 수: 2 (최근 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.

채택된 답변

Matt J
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개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by