1. Nested Parallel
개요
function
omp_set_nested()
: nested parallel 사용 여부 설정 가능omp_get_nested()
: nested parallel 지원 여부 결과 return주의
2. Example
C code
#include <stdio.h>
#include <omp.h>
int main()
{
int tid;
omp_set_nested(1); // nested parallel 사용
omp_set_num_threads(2);
#pargma omp parallel private(tid)
{
tid=omp_get_thread_num();
printf("thread id=%d\n", tid);
if (tid==1){
#pragma omp parallel private(tid)
{
tid = omp_get_thread_num();
printf("\t thread id=%d\n", tid);
}
}
}
}
Result
thread id=0
,thread id=1
출력\t thread id=0
,\t thread id=1
출력해석
omp_set_num_threads(2)
에서 Thread 0, Thread 1 생성if (tid==1)
에 의해 Thread 1에서 Thread 0, Thread 1이 다시 생성3. Additional Functions
function | 설명 |
---|---|
omp_set_nested(nested) | Nested Parallelism 활성화/비활성화 |
omp_get_nested() | Nested 설정 여부 return |
omp_get_level() | 현재 스레드의 level값 return |
omp_get_ancestor_thread_num(level) | 현재 스레드의 parent thread return |
omp_get_team_size(level) | 스레드 팀의 크기 return |
1. Example 1
C code
#include <stdio.h>
#include <omp.h>
int main()
{
int x,y,z;
omp_set_nested(1);
omp_set_num_threads(4);
#pragma omp parallel private(y)
{
// x: shared, y: private, z: shared
#pragma omp parallel num_threads(2) private(x)
{
// x: private, y: shared, z: shared
}
}
}
해석
2. Example 2
C code
#include <stdio.h>
#include <omp.h>
int main()
{
int x=1, y=10, z=20, tid;
omp_set_nested(1);
omp_set_num_threads(4);
#pragma omp parallel private(y, tid)
{
tid=omp_get_thread_num();
x++; y=12; z=22;
#pragma omp parallel num_threads(2) private(x, tid)
{
tid=omp_get_thread_num();
x=10; y++; z++;
printf("\t tid=%d x=%d y=%d z=%d\n",tid, x, y, z);
}
printf("tid=%d x=%d y=%d z=%d\n",tid, x, y, z);
}
printf("\n")
printf("x=%d y=%d z=%d\n"x, y, z);
}
Result
3. Example 2 - 해석
x: level 1에서는 shared, level 2에서는 private
x++
연산이 각 스레드 4개에서 이루어지며 x는 5가 됨x=10
에 의해 초기화가 되긴 하지만, nested parallel 영역이 끝나며 소멸x=5
만 남음y: level 1에서는 private, level 2에서는 shared
y++
연산 수행 -> 13, 14 반복 출력y=10
만 남음z: 모든 영역에서 shared
z=22
할당, level 2의 z++
연산이 shared var z에 모두 누적됨