Openmp: Openmp Adds Constructs For Shared-Memory
Openmp: Openmp Adds Constructs For Shared-Memory
1
Compiling with OpenMP
2
Reflecting on Threads
#include <omp.h>
#include <stdio.h>
int main() {
#pragma omp parallel
printf("hello from %d of %d\n",
omp_get_thread_num(),
omp_get_num_threads());
} Copy
3
Running OpenMP Programs
4
OpenMP Directives
for
sections, section
barrier
exclusive
5
Creating Threads
6
Threads and Sharing
int main() {
int t, j, i;
#pragma omp parallel private(t, i) shared(j)
{
t = omp_get_thread_num();
printf("running %d\n", t);
for (i = 0; i < 1000000; i++)
j++; /* race! */
printf("ran %d\n", t);
}
printf("%d\n", j);
} Copy
7
Reduce
int t;
#pragma omp parallel reduction(+:t)
{
t = omp_get_thread_num() + 1;
printf("local %d\n", t);
}
printf("reduction %d\n", t); Copy
8
Work Sharing
9
Loop Workshare for Data Parallelism
11
Section Workshare for Task Parallelism
12
Other Patterns
13
Synchronization
14
OpenMP Documentation
http://www.openmp.org/
15