threads and fork/join model of parallelism. threads are forked from the original proces/thread (master thread).threads are joined back to the original process and return to sequential execution.
master thread. This is shared data.#include <omp.h> -fopenmp flag.pragma is used to annotate
1
2
#pragma omp somedirective clause(value, othervalue)
parallel statement;
1
2
3
4
5
6
#pragma omp somedirective clause(value, othervalue)
{
parallel statement 1;
parallel statement 2;
...
}
molly csc466 directory, create a file called hello_omp.c with the following contents:c linenums="1" --8<-- "docs/csc466/lectures/data/openmp/hello_openmp.c"
1
2
3
4
5
gcc -o hello_omp hello_omp.c -fopenmp
export OMP_NUM_THREADS=4
./hello_omp
export OMP_NUM_THREADS=8
./hello_omp
omp.h to have libraries that support OpenMP.parallel region. Pay attention to how the curly bracket is setup, comparing to the other curly brackets. omp_get_thread_num gets the ID assigned to the thread and then assign it to a variable named tid of type int.omp_get_num_threads gets the value assigned to OMP_NUM_THREADS and return it to a variable named nthreads of type int.tid and nthreads.
1
export OMP_NUM_THREADS=4
nthreads=4.
csc466 directory, create a file called trapezoid.c with the following contents:c linenums="1" --8<-- "docs/csc466/lectures/data/openmp/trapezoid_openmp_1.c"
trapezoid.c.trapezoid.c so that it looks like below.c linenums="1" --8<-- "docs/csc466/lectures/data/openmp/trapezoid_openmp_2.c"
Alternate the trapezoid.c code so that the parallel region will invokes a function to calculate the partial sum.
```c linenums=”1” –8<– “docs/csc466/lectures/data/openmp/trapezoid_openmp_template.c”
c linenums="1" --8<-- "docs/csc466/lectures/data/openmp/trapezoid_openmp_3.c"
sum_series_1.c that takes a single integer N as a command line argument and calculate the sum of the first N non-negative integers.c linenums="1" --8<-- "docs/csc466/lectures/data/openmp/sum_series_1.c"
sum_series_2.c that takes a single integer N as a command line argument and calculate the sum of the first N non-negative integers.c linenums="1" --8<-- "docs/csc466/lectures/data/openmp/sum_series_2.c"
csc466 directory, create a file called trapezoid_time.c with the following contents:c linenums="1" --8<-- "docs/csc466/lectures/data/openmp/trapezoid_openmp_time.c"