Hello everyone,
I am using the setGlobal() and getGlobal() functions in a code that worked pretty well initially but doesn't work now that I'm trying to parrallelize it.
I noticed that the functions setGlobal() and getGlobal() struggle to work with the parfor loop. or example, when I run
setGlobalF([2 3 4])
% j = getGlobalF();
parfor i = 1:4
j = getGlobalF()
end
the j value is not assigned to [2 3 4].
Have you guys any ideas of why and how should I proceed ? Thanks !

댓글 수: 1

I forget to put the function, here it is
function r = getGlobalF
global F
r = F;
end
function setGlobalF(val)
global F
F = val;
end

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2022년 7월 13일

1 개 추천

Global and persistent variables are never copied to workers.
You need to do one of two things:
Note that any setGlobalF that you do on a worker will not affect the other workers. If you need to be able to affect the other workers, then you should either use spmd with labSend and labReceive or else use parallel.pool.DataQueue or parallel.pool.PollableDataQueue to send data from the workers to the client and then the client would send the updated values to the other workers.

댓글 수: 1

Quentin Reynard-Feytis
Quentin Reynard-Feytis 2022년 7월 13일
Thank you very much for your answer you made everything way much clearer :)

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

질문:

2022년 7월 13일

댓글:

2022년 7월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by