第四章 OpenMP 多线程编程

download 第四章  OpenMP 多线程编程

If you can't read please download the document

description

第四章 OpenMP 多线程编程. 主要内容. OpenMP 编程简介 OpenMP 多线程应用程序编程技术. 1. OpenMP 编程简介. 1.1 OpenMP 多线程编程发展概况. OpenMP 是一种面向共享内存多线程并行编程技术 OpenMP 具有良好的可移植性 支持多种编程语言 Fortran C/C++ 支持多种平台 www.openmp.org. OpenMP 最初是为共享内存的多处理器系统设计的并行编程方法,这与通过消息传递进行并行编程模型有很大的不同。. OpenMP 的支持环境. Intel 的 C++ 和 Fortran 编译器 - PowerPoint PPT Presentation

Transcript of 第四章 OpenMP 多线程编程

  • OpenMP

  • OpenMP OpenMP

  • 1. OpenMP

  • 1.1 OpenMP OpenMPOpenMP Fortran C/C++ www.openmp.org

  • OpenMP

  • OpenMP

    IntelC++FortranMicrosoftVisual Studio 2005gcc4.2

  • 1.2 OpenMP OpenMP OpenMPFork-JoinForkJoin

  • Fork-Join

  • OpenMP

  • OpenMP OpenMP C/C++OpenMP#pragma omp

  • Directiveparallel, for, parallel for, section, sections, single, master, critical, flush, ordered, atomic

  • OpenMPAPI omp.homp_get_thread_num()

  • ,,,.,.,,

  • 1.3 OpenMP Visual Studio .Net 2005OpenMP 2.0 /openmpOpenMP

  • OpenMP

  • #include "omp.h"

    int _tmain(int argc, _TCHAR* argv[]){printf("Hello from serial.\n");printf("Thread number=%d\n",omp_get_thread_num());

    #pragma omp parallel{printf("Hello from parallel. Thread number=%d\n", omp_get_thread_num());}printf("Hello from serial again.\n");

    getchar();return 0;}

  • 2. OpenMP

  • 2.1 OpenMP C/C++ #pragma omp parallel for [clause[clause]]for i = first ; i
  • #pragma omp parallel [clause[clause]]{#pragma omp for [clause[clause]]for i = first ; i
  • for for index = start ; index < end ; increment_exprbreak gotoreturn continue

  • m

  • shareprivateschedule if ordered copyin

  • OpenMP OpenMP

  • threadprivate

  • sharedprivarefirstprivatelastprivate

  • parallel for Privatefirstprivatelastprivatereduction

  • # pragma omp parallel for privatearx,ary,n reduction+:a,bfori=0;i
  • OpenMPC/C++

  • firstprivate lastprivate

  • #pragma omp parallel for Data Race

  • 2.2 #pragma omp parallel [clause[clause]]block

  • parallelparallel parallelbarrierjoin

  • threadprivate,copyinthreadprivate()copyin

  • threadprivate, copyin

  • #pragma omp parallel privatemyid{nthreads=omp_get_num_threads;myid=omp_get_thread_num;get_my_work_donemyid,nthreads;}

  • 2.3 OpenMP

  • OpenMP criticalatomic

  • critical #pragma omp critical [name]blockblock

  • #pragma omp atomicx ++

  • OpenMPbarrierordered sectionsmaster

  • barrier #pragma omp for#pragma omp single #pragma omp sections nowait

  • #pragma omp barrier #pragma omp parallel{initialization;#pragma omp barrierprocess;}

  • ordered ordered

  • OpenMPOpenMPforCPU

  • int i, j;int a[100][100] = {0};for ( i =0; i < 100; i++){for( j = i; j < 100; j++ ){ a[i][j] = i*j;}} 425i0i99100

  • for scheduleschedule(type[,size])

    type static dynamicguidedruntime ()size () sizesize

  • (static) parallel forschedulestaticntn/tsizen/t

  • #pragma omp parallel for schedule(static) for(i = 0; i < 10; i++ ) { printf("i=%d, thread_id=%d\n", i, omp_get_thread_num());}i=0, thread_id=0i=1, thread_id=0i=2, thread_id=0i=3, thread_id=0i=4, thread_id=0i=5, thread_id=1i=6, thread_id=1i=7, thread_id=1i=8, thread_id=1i=9, thread_id=1004159

  • #pragma omp parallel for schedule(static, 2) for(i = 0; i < 10; i++ ) { printf("i=%d, thread_id=%d\n", i, omp_get_thread_num());}i=0, thread_id=0i=1, thread_id=0i=4, thread_id=0i=5, thread_id=0i=8, thread_id=0i=9, thread_id=0i=2, thread_id=1i=3, thread_id=1i=6, thread_id=1i=7, thread_id=1010,2

  • (dynamic) sizesizesizesizesize

  • guidedguided guidedsizesize1

  • runtimerumtime runtimeOMP_SCHEDULE unixsetenvOMP_SCHEDULEsetenv OMP_SCHEDULEdynamic, 22windows||