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

17 Ansichten (letzte 30 Tage)
Merse Gaspar
Merse Gaspar am 25 Sep. 2019
Kommentiert: Edric Ellis am 26 Sep. 2019
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 Kommentar
Stephen23
Stephen23 am 25 Sep. 2019
"...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:

Melden Sie sich an, um zu kommentieren.

Antworten (1)

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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by