[Directive] sections

OpenMP에는 다양한 Directive들이 있는데, 이글에서는 sections 라는 directive를 소개하도록 하겠습니다. sections는 블럭으로 나누어진 코드들을 하나씩 쓰레드를 분산 시켜서 실행시키는 명령문 입니다. 블럭은 section 이라는 명령어로 구분하며 처음부분과 마지막 부분에는 쓰지 않아도 됩니다.

[Fortran 90]
    program Console
    implicit none
    !Variables
    integer thr_num
    integer omp_get_thread_num
   
    !$omp parallel sections num_threads(4) private(thr_num)
    thr_num = omp_get_thread_num()
    write(*,"(A17,I2)"), "Hello from thread ", thr_num
    !$omp section
    thr_num = omp_get_thread_num()
    write(*,"(A17,I2)"), "Hello from thread ", thr_num   
    !$omp section
    thr_num = omp_get_thread_num()
    write(*,"(A17,I2)"), "Hello from thread ", thr_num   
    !$omp section
    thr_num = omp_get_thread_num()
    write(*,"(A17,I2)"), "Hello from thread ", thr_num   
    !$omp end parallel sections
    end program Console

[Output]
Hello from thread 0
Hello from thread 1
Hello from thread 3
Hello from thread 2

위 코드에서 parallel Directive는 sections 와 한번에 쓰여졌는데, parallel은 다른 directive와 결합하여 한번에 쓸 수 있습니다. Clause들로는 num_threads와 private 가 쓰여졌으며, num_threads 는 쓰레드의 갯수를 세팅하고, private는 해당 변수를 각각의 쓰레드에 독립적으로 돌아 갈 수 있도록 합니다. thr_num 은 해당 쓰레드의 번호 이므로 각 쓰레드마다 독립적이어야 합니다. 중간에 나오는 "!$omp section" 구문은 블럭을 설정해 주는 코드로서, 이 구문들 사이가 하나의 블럭을 의미 합니다. 마지막으로 sections 는 다음과 같은 Clause 들을 지원합니다.

The sections directive supports the following OpenMP clauses:
출처 - https://msdn.microsoft.com/en-us/library/8k4b1177.aspx


댓글

이 블로그의 인기 게시물

[Linux, AIX] 사용자 계정 생성 및 설정

[AIX] rpm 설치와 rpm 으로 패키지 설치 및 삭제

Ubuntu 에서 Fortran 시작하기