Introduction to OpenMP

OpenMP overview

Target hardware and software
Fork/join model for threads
Basic requirements

Write, compile, and run an OpenMP program

OMP directives
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;
  ...
}
Hands-on: create hello_omp.c

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
What’s important?
1
export OMP_NUM_THREADS=4

Trapezoid

Overview
Trapezoidal
Implementation

c linenums="1" --8<-- "docs/csc466/lectures/data/openmp/trapezoid_openmp_1.c"

A bit more detailed

c linenums="1" --8<-- "docs/csc466/lectures/data/openmp/trapezoid_openmp_2.c"

Challenge 1:

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”

Solution

c linenums="1" --8<-- "docs/csc466/lectures/data/openmp/trapezoid_openmp_3.c"

Challenge 2:
Solution

c linenums="1" --8<-- "docs/csc466/lectures/data/openmp/sum_series_1.c"

Challenge 3:
Solution

c linenums="1" --8<-- "docs/csc466/lectures/data/openmp/sum_series_2.c"

Include timing

c linenums="1" --8<-- "docs/csc466/lectures/data/openmp/trapezoid_openmp_time.c"