How can I know which worker performs each iteration of the parfor loop?
조회 수: 9 (최근 30일)
이전 댓글 표시
I want to know if it is possible to determine which worker performs each iteration of the parfor loop or if it is possible to assign each iteration to a worker.
댓글 수: 2
Thomas Ibbotson
2024년 5월 2일
We would usually advise against this, so it would be good to know why you want to do this. I will answer your question in detail below.
답변 (2개)
Thomas Ibbotson
2024년 5월 2일
You can find out which worker is running your code using the following function: https://uk.mathworks.com/help/parallel-computing/getcurrentworker.html
You can achieve more control over how iterations are divided amongst worker using parforOptions: https://uk.mathworks.com/help/parallel-computing/parforoptions.html
댓글 수: 0
Edric Ellis
2024년 5월 2일
편집: Edric Ellis
2024년 5월 2일
You can use getCurrentTask (if you're using a process pool rather than a thread pool). This has a field ID which tells you which task is operating, like this:
parfor i = 1:N
t = getCurrentTask();
fprintf('Iteration %d on task %d\n', i, t.ID);
end
It's not possible with parfor to assign iterations to workers. You can have more control with spmd - but we'd need to know more about what it is you're trying to achieve.
참고 항목
카테고리
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!