worksharing constructs
1
2
3
4
5
6
7
8
9
#pragma omp parallel
{
int threadnum = omp_get_thread_num(),
numthreads = omp_get_num_threads();
int low = N*threadnum/numthreads,
high = N*(threadnum+1)/numthreads;
for (int i=low; i<high; i++)
// do something with i
}
parallel for pragma
1
2
3
4
5
#pragma omp parallel
#pragma omp for
for (int i=0; i<N; i++) {
// do something with i
}
~~~c #pragma omp parallel { code1() #pragma omp for for (int i=0; i<N; i++) { code2() } code3() }
do and for pragmas do not create a team of threads: omp for or omp do directives need to be inside a parallel region.break, return, exit statements, or goto to a label outside the loop.continue (for C/C++) statement is allowed.private, and no changes to it inside the loop are allowed.
1
#pragma omp for schedule(...)
The schedules can be setup in a more fine-grained manner
Do we specify chunk size?
Create a file named for.c, compile and run
c linenums="1" --8<-- "docs/csc466/lectures/data/openmp/for.c"
1
2
gcc -o for for.c -fopenmp
./for 20 4
for_static.c, compile and runc linenums="1" --8<-- "docs/csc466/lectures/data/openmp/for_static.c"
1
2
gcc -o for_static for_static.c -fopenmp
./for 20 4
Create a file named for_dynamic.c, compile and run
c linenums="1" --8<-- "docs/csc466/lectures/data/openmp/for_dynamic.c"
1
2
gcc -o for_dynamic for_dynamic.c -fopenmp
./for 20 4
What is the difference?
for_static_rand.c, compile and runc linenums="1" --8<-- "docs/csc466/lectures/data/openmp/for_static_rand.c"
1
2
gcc -o for_static_rand for_static_rand.c -fopenmp
./for 20 4
for_dynamic_rand.c, compile and runc linenums="1" --8<-- "docs/csc466/lectures/data/openmp/for_dynamic_rand.c"
1
2
gcc -o for_dynamic_rand for_dynamic_rand.c -fopenmp
./for 20 4
nested_loops.c, compile and runc linenums="1" --8<-- "docs/csc466/lectures/data/openmp/nested_loops.c"
1
2
gcc -o nested_loop nested_loop.c -fopenmp
./for 2 4
nested_loops.c, compile and runc linenums="1" --8<-- "docs/csc466/lectures/data/openmp/nested_loops_collapse.c"
1
2
gcc -o nested_loops_collapse nested_loops_collapse.c -fopenmp
./for 2 4
c linenums="1" --8<-- "docs/csc466/lectures/data/openmp/collapse_3.c"
sum_series_for.c using the code below:c linenums="1" --8<-- "docs/csc466/lectures/data/openmp/sum_series_openmp_for.c"
openmp and select New File.sum_series_for_2.c as the file name and hits Enter.c linenums="1" --8<-- "docs/csc466/lectures/data/openmp/sum_series_openmp_for_2.c"
#pragma parallel for