How to 'load' data inside a parfor loop in an effective way?

조회 수: 4 (최근 30일)
Merse Gaspar
Merse Gaspar 2019년 9월 25일
댓글: Edric Ellis 2019년 9월 26일
Let suppose the following general code structure.
parfor i = 1:10000
result(i) = myfunc(data(i));
end
or
parfor i = 1:10000
result(i) = myfunc(data(i).relevantpart);
end
I think, in both case all the data are copied into memory within each loop, and not just data(i) for loop i.
Am I right?
So I want to avoid this.
As far as I see, the only way to do this, if I have separate data variables, like data1, data2, data3, ..., data10000.
But this is very ugly, and to automatically genarate them outside the loop and use them inside the loop, you have to use the function 'eval', which is not recommended in general.
So what is the solution for this general data 'loading/using' problem?
  댓글 수: 1
Stephen23
Stephen23 2019년 9월 25일
"...you have to use the function 'eval', which is not recommended in general."
Not only is it a bad way to write code, eval doesn't really work inside parfor loops:

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

답변 (1개)

Matt J
Matt J 2019년 9월 25일
편집: Matt J 2019년 9월 25일
Am I right?
No. In the code you have shown, the struct array data will be sliced,
  댓글 수: 3
Matt J
Matt J 2019년 9월 25일
편집: Matt J 2019년 9월 25일
The Code Analyzer should warn you if you are broadcasting a non-sliced variable.
Edric Ellis
Edric Ellis 2019년 9월 26일
You can also view the amount of data sent to/from a parfor loop using ticBytes and tocBytes.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by