中图种学学计算机科学与术系 University of Science and Technology of China DEPARTMENT DF COMPUTE三巴 ENCE AND ECHNOLDD OpenMP的目标 标准性 简洁实用 使用方便 可移植性 国家高性能计算中心(合肥) 2021/2/3
国家高性能计算中心(合肥) 9 2021/2/3 OpenMP的目标 ▪ 标准性 ▪ 简洁实用 ▪ 使用方便 ▪ 可移植性
中图种学学计算机科学与术系 University of Science and Technology of China DEPARTMENT。 F COMPUTE三巴 ENCE AND ECHNOLDD OpenMP并行編程模型 基于线程的并行编程模型( Programming Model) penMP使用F。rk<Join并行执行模型 主线程 FORK F0RK 并行域 并行域 国家高性能计算中心(合肥) 2021/2/3 10
国家高性能计算中心(合肥) 10 2021/2/3 OpenMP并行编程模型 ▪ 基于线程的并行编程模型(Programming Model) ▪ OpenMP使用Fork-Join并行执行模型 F O R K J O I N F O R K J O I N 主线程 并行域 并行域
中图种学学计算机科学与术系 University of Science and Technology of China DEPARTMENT。 F COMPUTE三巴 ENCE AND ECHNOLDD peMP程序结构 基于 Fortran语言的 OpenMP程序的结构 PROGRAMHELLO INTEGER VARI VAR2 VAR3 I Serial code Beginning of parallel section. Fork a team of threads I Specify variable scoping ISOMP PARALLEL PRIVATE(VARI, VAR2) SHARED(VAR3) I Parallel section executed by all threads d All threads join master thread and disband I SOMP END PARALLEL I Resume serial code END 国家高性能计算中心(合肥) 2021/2/3
国家高性能计算中心(合肥) 11 2021/2/3 OpenMP程序结构 ▪ 基于Fortran语言的OpenMP程序的结构 PROGRAM HELLO INTEGER VAR1, VAR2, VAR3 !Serial code … !Beginning of parallel section. Fork a team of threads. !Specify variable scoping !$OMP PARALLEL PRIVATE(VAR1, VAR2) SHARED(VAR3) !Parallel section executed by all threads … !All threads join master thread and disband !$OMP END PARALLEL !Resume serial code … END
中图种学学计算机科学与术系 University of Science and Technology of China DEPARTMENT。 F COMPUTE三巴 ENCE AND ECHNOLDD OpenMP程存结构 基于c/c+语言的 OpenMP程序的结构 #f include <omp. h main 0{ int varl. var2. var3 /*Serial code*/ /Beginning of parallel section. Fork a team of threads*/ /Specify variable scoping*/ #pragma omp parallel private( varl, var2) shared( var3) /*Parallel section executed by all threads/ /*All threads join master thread and disband*/ /*Resume serial code */ 国家高性能计算中心(合肥) 2021/2/3 12
国家高性能计算中心(合肥) 12 2021/2/3 OpenMP程序结构 ▪ 基于c/c++语言的OpenMP程序的结构 #include <omp.h> main (){ int var1, var2, var3; /*Serial code*/ … /*Beginning of parallel section. Fork a team of threads*/ /*Specify variable scoping */ #pragma omp parallel private(var1, var2) shared(var3) { /*Parallel section executed by all threads*/ … /*All threads join master thread and disband*/ } /*Resume serial code */ … }
中图种学学计算机科学与术系 University of Science and Technology of China DEPARTMENT DF COMPUTE三巴 ENCE AND ECHNOLDD 一个简单的 OpenMP程存实例 基于c/C+语言的 OpenMP程序结构的一个具体实现 #include " omp. h int main( int argc, char argvD roc char buf 32] / Fork a team of threads * #pragma omp parallel private(nthreads, tid) Obtain and print thread id*/ tid=omp get thread numo; printf("Hello World from OMP thread %d\n", tid) Only master thread does this */ nthreads =omp get num threads; printf( " Number of threads %dln", nthreads); return 0 国家高性能计算中心(合肥) 2021/2/3 13
国家高性能计算中心(合肥) 13 2021/2/3 一个简单的OpenMP程序实例 ▪ 基于C/C++语言的OpenMP程序结构的一个具体实现 #include "omp.h" int main(int argc, char* argv[]) { int nthreads, tid; int nprocs; char buf[32]; /* Fork a team of threads */ #pragma omp parallel private(nthreads, tid) { /* Obtain and print thread id */ tid = omp_get_thread_num(); printf("Hello World from OMP thread %d\n", tid); /* Only master thread does this */ if (tid==0) { nthreads = omp_get_num_threads(); printf("Number of threads %d\n", nthreads); } } return 0; }