Home - OpenMPhttps://www.openmp.org/
C++ with OpenMP:
需要 -fopenmp 选项:
gcc -fopenmp anyname.c -o anyname.out
#include <iostream>
#include <omp.h>
int main() {
std::cout << "starting ..." << std::endl;
#pragma omp parallel for
for(int i = 0; i < 10; i++) {
//std::cout << "i=" << i << std::endl;
printf("i = %d, ThreadId=%d\n", i, omp_get_thread_num());
}
return 0;
}
#pragma omp parallel for 这条语句是用来指定后面的for循环语句变成并行执行的,for循环里的内容必须满足并行执行,即每次循环互不相干,后一次循环不依赖于前面的循环。
CMake with OpenMP:
Example: https://github.com/FrankKuiFang/playGround/tree/main/openMP
Notes for OpenMP:
Don’t to install OpenMP. OpenMP is a feature of the compiler. Check that the compiler you are using implements OpenMP.
OpenMP is used for parallelism within a (multi-core) computer while MPI is used for parallelism between computer.