# for and parfor

1 visualizzazione (ultimi 30 giorni)
Nuchto il 4 Giu 2012
Simple question: why does FOR exist at all if we have PARFOR (which performs better and faster)?
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Geoff il 4 Giu 2012
That's a thought-provoking question.
"Faster" is not always a requirement in computing. Also, there are many algorithms or simple tasks that require a single thing to be done in sequential order, and cannot be broken apart. A contrived example would be writing bytes to a file. Parallelisation is inappropriate in this case.
I would end up wanting to specify special syntax to parfor that says "only use one thread, and do it in the right order"... But then, that special syntax would be summed up best by the word: for. The for-loop is a basic construct of just about every procedural language out there. If you take it away, you need a damned good reason.
In more simple terms... parfor and for are two different tools that have their own benefits, some of which overlap. Programmers should be able to choose the best tool for the job at hand.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Più risposte (2)

Honglei Chen il 4 Giu 2012
To use PARFOR, each iteration has to be independent to each other. This is not always possible. The following document may be useful.
##### 2 CommentiMostra NessunoNascondi Nessuno
Nuchto il 4 Giu 2012
But if I use parfor EVEN if each iteration is not independent, will it work (EVEN if it is no faster than FOR)?
Titus Edelhofer il 4 Giu 2012
No, it won't. Just try to enter
parfor i=2:5
x(i) = x(i-1) + x(i-2);
end
You will see (if you have the Parallel Computing Toolbox) that x and parfor go a red line indicating an error.

Accedi per commentare.

Thomas il 4 Giu 2012
to use 'parfor' you need the parallel computing toolbox, which is not useful in basic matlab. The 'for' can be used in basic matlab without the need for the parallel processing toolbox. The parallel processing toolbox is an extra addon and costs money..
As regards to speed, it is only faster if your code can be parallelized :)
In the par for all loops need to be independent of each other.
##### 2 CommentiMostra NessunoNascondi Nessuno
Honglei Chen il 4 Giu 2012
This is not entirely true as parfor is available in MATLAB, it just doesn't provide any benefit if you don't have Parallel Computing Toolbox, see
http://www.mathworks.com/help/techdoc/ref/parfor.html
Thomas il 4 Giu 2012
that was what I meant, that you do get it in matlab but is of no use without the PCT.. sorry if it came through as otherwise....

Accedi per commentare.

### Categorie

Scopri di più su Parallel for-Loops (parfor) in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by