2016-10-03 4 views
3

OpenMP와 OpenMPI를 모두 사용하는 프로그램을 작성 중입니다.OpenMP : PARALLEL 블록에서 MASTER와 FOR을 올바르게 중첩시키는 방법은 무엇입니까?

초기 노드에서 실행되는 프로세스의 경우 하나의 스레드가 다른 노드와 상호 작용하는 스케줄러와 계산을 수행하는 다른 노드와 작업하고 싶습니다.

코드 구조는 같다 :

int computation(...) 
{ 
    #pragma parallel for ..... 
} 

int main(...) 
{ 
    ... 
    if (mpi_rank == 0) // initial node 
    { 
     #pragma omp parallel 
     { 
      #pragma omp master 
      { 
       // task scheduling for other nodes 
      } 
      { 
       // WRONG: said 4 threads in total, this block will be executed for 
       // 3 times simultaneously, and the nested "for" in the function 
       // will spawn 4 threads each as well 
       // so ACTUALLY 3*4+1=13 threads here! 
       int computation(...); 
      } 
     } 
    } 
    else // other nodes 
    { 
     // get a task from node 0 scheduler by MPI 
     int computation(...); 
    } 
} 

내가 원하는 것은, 그렇게 만 4 개 스레드를 초기 노드에서, 스케줄러가 하나 개의 스레드를 받아, 하나 개의 연산 기능이 동시에 실행되는 점이다 동시에 사용됩니다.

나는 또한 시도

:

int computation(...) 
{ 
    register int thread_use = omp_get_max_threads(); // this is 4 
    if (rank == 0) 
    { 
     --thread_use; // if initial node, use 3 
    } 
    #pragma parallel for ..... num_threads(thread_use) 
} 

int main(...) 
{ 
    ... 
    if (mpi_rank == 0) // initial node 
    { 
     #pragma omp parallel 
     { 
      #pragma omp master 
      { 
       // task scheduling for other nodes 
      } 
      #pragma omp single 
      { 
       // WRONG: nest "for" can only use 1 thread 
       int computation(...); 
      } 
     } 
    } 
    else // other nodes 
    { 
     // get a task from node 0 scheduler by MPI 
     int computation(...); 
    } 
} 

... 또는

//other parts are the same as above 
if (mpi_rank == 0) // initial node 
{ 
    #pragma omp parallel num_threads(2) 
    { 
     #pragma omp master 
     { 
      // task scheduling for other nodes 
     } 
     { 
      // WRONG: nest "for" can only use 1 thread 
      int computation(...); 
     } 
    } 
} 

...하지만 그들 중 누구도 일했다.

내 목표를 달성하기 위해 OpenMP로 블록을 어떻게 배열해야합니까? 어떤 도움을 주시면 감사하겠습니다.

답변

3

우선 OpenMP에서 중첩 된 병렬 처리를 지정하려면 환경 변수 OMP_NESTEDtrue으로 설정해야합니다.

void compute_function() { 
    // Nested parallel region. Bottommost level 
    #pragma omp parallel 
    { 
     computation(); 
    } 
} 

더 많은 정보

OpenMP nested parallelism에 : scheduling_function()는 단일 스레드 기능입니다

// Parallel region. Topmost level 
#pragma omp parallel sections num_threads(2) 
{ 
    #pragma omp section 
    scheduling_function(); 

    #pragma omp section 
    compute_function(); 
} 

compute_function() 구조를 유사합니다

그런 다음, 가능한 구현은 다음과 같은 것이 같이 볼 수 있었다

+0

대단하다! 매우 감사합니다. –

+1

'OMP_NUM_THREADS' 환경 변수에 쉼표로 분리 된 값으로 각 중첩 레벨에서 사용되는 스레드의 수를 설정할 수 있다는 점을 고려하십시오. –