Tim Mattson OMP Tutorial系列的学习笔记,油管和B站都能看到视频
Setup on OSX
- tutorial: https://iscinumpy.gitlab.io/post/omp-on-high-sierra/
- install OMP library:
brew install libomp
- compile
- C:
clang -Xpreprocessor -fopenmp hello_world.c -o helloworld -lomp
- C++:
clang++ -Xpreprocessor -fopenmp hello_world.cpp -o helloworld -lomp
- C:
How threads work
- programming shared memory computers
-
threads:
- “light weight” processes
- share Process state among multiple threads -> reduces cost of switching context
-
an instance of a program
- threads interact through reads/writes to a shared address space
- OS scheduler decides when to run which threads, interleaved for fairness
- synchronization to assure every legal order results in correct results
-
OpenMP overview
- a multi-threading, shared address model
- threads communicate by sharing variables
- unintended sharing of data causes race conditions
- race condition: when the program’s outcome changes as the threads are scheduled differently
- to control race condition: use synchronization
- synchronization is expensive