omp parallel vs. omp parallel for
These are equivalent.
#pragma omp parallel
spawns a group of threads, while #pragma omp for
divides loop iterations between the spawned threads. You can do both things at once with the fused #pragma omp parallel for
directive.
I don't think there is any difference, one is a shortcut for the other. Although your exact implementation might deal with them differently.
The combined parallel worksharing constructs are a shortcut for specifying a parallel construct containing one worksharing construct and no other statements. Permitted clauses are the union of the clauses allowed for the parallel and worksharing contructs.
Taken from http://www.openmp.org/mp-documents/OpenMP3.0-SummarySpec.pdf
The specs for OpenMP are here:
https://openmp.org/specifications/
Here is example of using separated parallel
and for
here. In short it can be used for dynamic allocation of OpenMP thread-private arrays before executing for
cycle in several threads.
It is impossible to do the same initializing in parallel for
case.
UPD: In the question example there is no difference between single pragma and two pragmas. But in practice you can make more thread aware behavior with separated parallel and for directives. Some code for example:
#pragma omp parallel
{
double *data = (double*)malloc(...); // this data is thread private
#pragma omp for
for(1...100) // first parallelized cycle
{
}
#pragma omp single
{} // make some single thread processing
#pragma omp for // second parallelized cycle
for(1...100)
{
}
#pragma omp single
{} // make some single thread processing again
free(data); // free thread private data
}
Although both versions of the specific example are equivalent, as already mentioned in the other answers, there is still one small difference between them. The first version includes an unnecessary implicit barrier, encountered at the end of the "omp for". The other implicit barrier can be found at the end of the parallel region. Adding "nowait" to "omp for" would make the two codes equivalent, at least from an OpenMP perspective. I mention this because an OpenMP compiler could generate slightly different code for the two cases.
There are obviously plenty of answers, but this one answers it very nicely (with source)
#pragma omp for
only delegates portions of the loop for different threads in the current team. A team is the group of threads executing the program. At program start, the team consists only of a single member: the master thread that runs the program.To create a new team of threads, you need to specify the parallel keyword. It can be specified in the surrounding context:
#pragma omp parallel { #pragma omp for for(int n = 0; n < 10; ++n) printf(" %d", n); }
and:
What are: parallel, for and a team
The difference between parallel, parallel for and for is as follows:
A team is the group of threads that execute currently. At the program beginning, the team consists of a single thread. A parallel construct splits the current thread into a new team of threads for the duration of the next block/statement, after which the team merges back into one. for divides the work of the for-loop among the threads of the current team.
It does not create threads, it only divides the work amongst the threads of the currently executing team. parallel for is a shorthand for two commands at once: parallel and for. Parallel creates a new team, and for splits that team to handle different portions of the loop. If your program never contains a parallel construct, there is never more than one thread; the master thread that starts the program and runs it, as in non-threading programs.
https://bisqwit.iki.fi/story/howto/openmp/