programing

섹션과 태스크 openmp의 차이

yoursource 2022. 8. 14. 12:50
반응형

섹션과 태스크 openmp의 차이

에서의 OpenMP의 차이점은 무엇입니까?

#pragma omp parallel sections
{
    #pragma omp section
    {
       fct1();
    }
    #pragma omp section
    {
       fct2();
    }
}

및 다음과 같습니다.

#pragma omp parallel 
{
    #pragma omp single
    {
       #pragma omp task
       fct1();
       #pragma omp task
       fct2();
    }
}

두 번째 코드가 정확한지 모르겠어요

태스크와 섹션의 차이는 코드가 실행되는 시간대에 있습니다. 있습니다.sections및 (단, (단,nowait을 사용하다) 모든 섹션이 실행될 때까지 스레드는 이 절을 떠나지 않습니다.

                 [    sections     ]
Thread 0: -------< section 1 >---->*------
Thread 1: -------< section 2      >*------
Thread 2: ------------------------>*------
...                                *
Thread N-1: ---------------------->*------

서 ★★★★N이 맞다sections두 개의 섹션으로 구성되며, 두 번째 섹션은 첫 번째 섹션보다 시간이 더 걸립니다.처음 2개의 스레드는 각각1개의 섹션을 실행합니다. 다른 것은요.N-2서는 「이러한 장벽」으로 됩니다).*를 참조해 주세요.

태스크는 가능한 경우 언제든지 소위 태스크 스케줄링 포인트에서 큐잉되고 실행됩니다.경우에 따라서는 실행 시 스레드 간의 작업 이동이 허용될 수 있습니다(수명 기간 중에도).이러한 작업을 Untied라고 하며 Untied 태스크가 한 스레드에서 실행을 시작한 후 일정 시점에 런타임에 의해 다른 스레드로 마이그레이션될 수 있습니다.

그러나 작업과 섹션은 여러 면에서 유사합니다.예를 들어, 다음의 2개의 코드 fragment는 기본적으로 같은 결과를 가져옵니다.

// sections
...
#pragma omp sections
{
   #pragma omp section
   foo();
   #pragma omp section
   bar();
}
...

// tasks
...
#pragma omp single nowait
{
   #pragma omp task
   foo();
   #pragma omp task
   bar();
}
#pragma omp taskwait
...

taskwait 닮다barrier그러나 태스크의 경우 대기 중인 모든 태스크가 실행될 때까지 현재 실행 흐름이 일시 중지됩니다.스케줄링 포인트입니다.즉, 스레드가 작업을 처리할 수 있도록 합니다.single하나의 스레드만으로 태스크를 작성하려면 construct가 필요합니다.single 각 됩니다.num_threads 그 、 nowait의 절single에게 구조물이 때까지 지시합니다.single됨).single구축).그래서 그들은...taskwait즉시 작업 처리를 시작합니다.

taskwait는 알기 쉽게 하기 위해 여기에 나타내는 명시적인 스케줄링 포인트입니다.명시적이든 암묵적이든 간에, 특히 장벽 동기화 내부에 암묵적인 스케줄링 포인트가 있다.따라서 위의 코드는 다음과 같이 간단하게 작성될 수도 있습니다.

// tasks
...
#pragma omp single
{
   #pragma omp task
   foo();
   #pragma omp task
   bar();
}
...

스레드가 3개일 경우 발생할 수 있는 시나리오 중 하나를 다음에 나타냅니다.

               +--+-->[ task queue ]--+
               |  |                   |
               |  |       +-----------+
               |  |       |
Thread 0: --< single >-|  v  |-----
Thread 1: -------->|< foo() >|-----
Thread 2: -------->|< bar() >|-----

여기에 표시하다| ... |스케줄링 포인트의 액션입니다.taskwait지시 또는 암묵적 장벽).기본적으로 스레드1그리고.2해당 시점에서 수행 중인 작업을 일시 중지하고 대기열에서 작업 처리를 시작합니다.모든 작업이 처리되면 스레드는 정상 실행 흐름을 재개합니다.스레드는1그리고.2스레드 전에 스케줄링 포인트에 도달할 수 있습니다.0를 종료했습니다.single구성하기 때문에 왼쪽은|는 정렬할 필요가 없습니다(위 그림에 나타나 있습니다).

그 실타래가 일어날 수도 있다.1의 처리를 완료할 수 있습니다.foo()다른 스레드가 태스크를 요청하기 전에 다른 스레드를 요청해야 합니다.둘 다foo()그리고.bar()는 같은 스레드에 의해 실행될 수 있습니다.

               +--+-->[ task queue ]--+
               |  |                   |
               |  |      +------------+
               |  |      |
Thread 0: --< single >-| v             |---
Thread 1: --------->|< foo() >< bar() >|---
Thread 2: --------------------->|      |---

또한 스레드 2가 너무 늦게 오면 선택한 스레드가 두 번째 작업을 실행할 수도 있습니다.

               +--+-->[ task queue ]--+
               |  |                   |
               |  |      +------------+
               |  |      |
Thread 0: --< single >-| v < bar() >|---
Thread 1: --------->|< foo() >      |---
Thread 2: ----------------->|       |---

경우에 따라서는 컴파일러 또는 OpenMP 런타임에 의해 태스크큐가 완전히 바이패스되어 태스크가 순차적으로 실행될 수 있습니다.

Thread 0: --< single: foo(); bar() >*---
Thread 1: ------------------------->*---
Thread 2: ------------------------->*---

지역 코드 내에 작업 스케줄링 지점이 없는 경우 OpenMP 런타임은 적절하다고 판단될 때마다 작업을 시작할 수 있습니다.를 들어, 모든 될 수 .parallel지역에 도달했습니다.

는 아니지만 OpenMP를 모두 .task ★★★★★★★★★★★★★★★★★」sections

섹션

int fib(int n)
{
    int i, j;
    if (n < 2)
        return n;
    else
    {
#pragma omp parallel sections       
{
#pragma omp section             
{
                i = fib(n - 1);
            }
#pragma omp section             
{
                j = fib(n - 2);
            }
        }
        printf("Current int %d is on thread %d \n", i + j, omp_get_thread_num());
        return i + j;
    }
}

int main()
{
    int n = 10;

#pragma omp parallel shared(n)  {
#pragma omp single      {
            printf("%d\n", omp_get_num_threads());
            printf("fib(%d) = %d\n", n, fib(n));
        }
    }
}

작업

#include <stdio.h>
#include <omp.h>
int fib(int n)
{
  int i, j;
  if (n<2)
    return n;
  else
    {
       #pragma omp task shared(i) firstprivate(n)
       i=fib(n-1);

       #pragma omp task shared(j) firstprivate(n)
       j=fib(n-2);

       #pragma omp taskwait
    printf("Current int %d is on thread %d \n", i + j, omp_get_thread_num());
       return i+j;
    }
}

int main()
{
  int n = 10;

  #pragma omp parallel shared(n)
  {
    #pragma omp single
    {
    printf("%d\n", omp_get_num_threads());
        printf ("fib(%d) = %d\n", n, fib(n));
    }
  }
}

섹션의 결과:

12
Current int 1 is on thread 0
Current int 2 is on thread 0
Current int 1 is on thread 0
Current int 3 is on thread 0
Current int 1 is on thread 0
Current int 2 is on thread 0
Current int 5 is on thread 0
Current int 1 is on thread 0
Current int 2 is on thread 0
Current int 1 is on thread 0
Current int 3 is on thread 0
Current int 8 is on thread 0
Current int 1 is on thread 0
Current int 2 is on thread 0
Current int 1 is on thread 0
Current int 3 is on thread 0
Current int 1 is on thread 0
Current int 2 is on thread 0
Current int 5 is on thread 0
Current int 13 is on thread 0
Current int 1 is on thread 0
Current int 2 is on thread 0
Current int 1 is on thread 0
Current int 3 is on thread 0
Current int 1 is on thread 0
Current int 2 is on thread 0
Current int 5 is on thread 0
Current int 1 is on thread 0
Current int 2 is on thread 0
Current int 1 is on thread 0
Current int 3 is on thread 0
Current int 8 is on thread 0
Current int 21 is on thread 0
Current int 1 is on thread 0
Current int 2 is on thread 0
Current int 1 is on thread 0
Current int 3 is on thread 0
Current int 1 is on thread 0
Current int 2 is on thread 0
Current int 5 is on thread 0
Current int 1 is on thread 0
Current int 2 is on thread 0
Current int 1 is on thread 0
Current int 3 is on thread 0
Current int 8 is on thread 0
Current int 1 is on thread 0
Current int 2 is on thread 0
Current int 1 is on thread 0
Current int 3 is on thread 0
Current int 1 is on thread 0
Current int 2 is on thread 0
Current int 5 is on thread 0
Current int 13 is on thread 0
Current int 34 is on thread 0
Current int 1 is on thread 0
Current int 2 is on thread 0
Current int 1 is on thread 0
Current int 3 is on thread 0
Current int 1 is on thread 0
Current int 2 is on thread 0
Current int 5 is on thread 0
Current int 1 is on thread 0
Current int 2 is on thread 0
Current int 1 is on thread 0
Current int 3 is on thread 0
Current int 8 is on thread 0
Current int 1 is on thread 0
Current int 2 is on thread 0
Current int 1 is on thread 0
Current int 3 is on thread 0
Current int 1 is on thread 0
Current int 2 is on thread 0
Current int 5 is on thread 0
Current int 13 is on thread 0
Current int 1 is on thread 0
Current int 2 is on thread 0
Current int 1 is on thread 0
Current int 3 is on thread 0
Current int 1 is on thread 0
Current int 2 is on thread 0
Current int 5 is on thread 0
Current int 1 is on thread 0
Current int 2 is on thread 0
Current int 1 is on thread 0
Current int 3 is on thread 0
Current int 8 is on thread 0
Current int 21 is on thread 0
Current int 55 is on thread 4
fib(10) = 55

작업의 결과:

12
Current int 1 is on thread 3
Current int 2 is on thread 3
Current int 1 is on thread 8
Current int 2 is on thread 8
Current int 1 is on thread 8
Current int 1 is on thread 4
Current int 1 is on thread 11
Current int 1 is on thread 11
Current int 2 is on thread 11
Current int 3 is on thread 11
Current int 1 is on thread 11
Current int 2 is on thread 11
Current int 1 is on thread 11
Current int 1 is on thread 11
Current int 2 is on thread 11
Current int 3 is on thread 11
Current int 1 is on thread 11
Current int 2 is on thread 11
Current int 1 is on thread 11
Current int 1 is on thread 11
Current int 2 is on thread 11
Current int 3 is on thread 11
Current int 5 is on thread 11
Current int 8 is on thread 11
Current int 1 is on thread 8
Current int 2 is on thread 8
Current int 3 is on thread 8
Current int 5 is on thread 8
Current int 13 is on thread 8
Current int 1 is on thread 7
Current int 2 is on thread 7
Current int 1 is on thread 7
Current int 1 is on thread 7
Current int 1 is on thread 0
Current int 1 is on thread 0
Current int 2 is on thread 0
Current int 3 is on thread 0
Current int 1 is on thread 1
Current int 1 is on thread 6
Current int 2 is on thread 6
Current int 1 is on thread 9
Current int 2 is on thread 9
Current int 1 is on thread 2
Current int 2 is on thread 7
Current int 3 is on thread 7
Current int 5 is on thread 7
Current int 2 is on thread 5
Current int 5 is on thread 5
Current int 1 is on thread 5
Current int 2 is on thread 5
Current int 1 is on thread 5
Current int 1 is on thread 5
Current int 2 is on thread 5
Current int 3 is on thread 5
Current int 1 is on thread 5
Current int 2 is on thread 5
Current int 1 is on thread 5
Current int 1 is on thread 5
Current int 2 is on thread 5
Current int 3 is on thread 5
Current int 5 is on thread 5
Current int 1 is on thread 5
Current int 2 is on thread 5
Current int 1 is on thread 11
Current int 2 is on thread 11
Current int 1 is on thread 8
Current int 2 is on thread 8
Current int 5 is on thread 8
Current int 3 is on thread 1
Current int 8 is on thread 1
Current int 21 is on thread 1
Current int 1 is on thread 10
Current int 3 is on thread 10
Current int 8 is on thread 0
Current int 1 is on thread 4
Current int 3 is on thread 4
Current int 1 is on thread 9
Current int 3 is on thread 9
Current int 8 is on thread 9
Current int 3 is on thread 2
Current int 5 is on thread 3
Current int 13 is on thread 3
Current int 5 is on thread 6
Current int 13 is on thread 7
Current int 8 is on thread 10
Current int 21 is on thread 10
Current int 34 is on thread 3
Current int 55 is on thread 1
fib(10) = 55

컴퓨팅 자원을 분산하면서 섹션보다 작업이 훨씬 현명한 것 같습니다.

-------------------------------------------------------------------

이 질문에 대한 답을 찾으시는 분은 이 게시물 아래의 댓글을 봐주세요.

언급URL : https://stackoverflow.com/questions/13788638/difference-between-section-and-task-openmp

반응형