MATLAB PARALLEL COMPUTING TOOLBOX - S Uživatelská příručka Strana 32

  • Stažení
  • Přidat do mých příruček
  • Tisk
  • Strana
    / 656
  • Tabulka s obsahem
  • ŘEŠENÍ PROBLÉMŮ
  • KNIHY
  • Hodnocené. / 5. Na základě hodnocení zákazníků
Zobrazit stránku 31
2 Parallel for-Loops (parfor)
2-4
Create a parfor-Loop
The safest approach when creating a parfor-loop is to assume that iterations are
performed on different MATLAB workers in the parallel pool, so there is no sharing
of information between iterations. If you have a for-loop in which all iterations are
completely independent of each other, this loop is a good candidate for a parfor-loop.
Basically, if one iteration depends on the results of another iteration, these iterations are
not independent and cannot be evaluated in parallel, so you cannot easily reprogram the
loop into a parfor-loop.
The following example produces equivalent results, with a for-loop on the left, and a
parfor-loop on the right. Try typing each in your MATLAB Command Window:
clear A
for i = 1:8
A(i) = i;
end
A
1 2 3 4 5 6 7 8
clear A
parfor i = 1:8
A(i) = i;
end
A
1 2 3 4 5 6 7 8
Notice that each element of A is equal to its index. The parfor-loop works because
each element depends only upon its iteration of the loop, and upon no other iterations.
for-loops that merely repeat such independent tasks are ideally suited candidates for
parfor-loops.
Note If a parallel pool is not running, parfor creates a pool using your default cluster
profile, if your parallel preferences are set accordingly.
Related Examples
“Run Parallel for-Loops (parfor)” on page 1-4
“Run a Batch Parallel Loop” on page 1-9
“Convert Nested for-Loops to parfor” on page 2-48
More About
“Parallel Pools” on page 2-44
“Introduction to parfor” on page 2-2
Zobrazit stránku 31
1 2 ... 27 28 29 30 31 32 33 34 35 36 37 ... 655 656

Komentáře k této Příručce

Žádné komentáře