2012-06-17 2 views
3

나는이 오래된 질문을하지만 대답은 온라인에서 나를 위해 작동하지 omp_get_num_threads 항상 GCC에 1을 반환있어, 코드는 다음과 같습니다 G ++ 4.4.5에서내가 (ICC에서 작동)

#include "stdio.h" 
#include "omp.h" 

main() 
{ 
    omp_set_num_threads(4); //initialise thread count for 4 core cpu                                
    int j; 
    printf ("%d\n", omp_get_max_threads()); 
    printf ("%d\n", omp_get_num_threads()); 
#pragma omp parallel for 
    for (int j=0; j<10; ++j) 
     { 
      printf ("%d\n", omp_get_num_threads()); 
      int threadNum; 
      threadNum = omp_get_thread_num(); 
      printf("This is thread %d\n", threadNum); 
     } 
    } 
    return 0; 
} 

, 리눅스 2.6. 32-5-AMD64, 그것은 생산 :

4 
1 
1 
This is thread 0 
1 
This is thread 0 
1 
This is thread 0 
1 
This is thread 0 
1 
This is thread 0 
1 
This is thread 0 
1 
This is thread 0 
1 
This is thread 0 
1 
This is thread 0 
1 
This is thread 0 

우리는 ICC 12.1.0로 이동하는 경우, 그것은 나에게 제공합니다

4 
1 
4 
This is thread 0 
4 
This is thread 0 
4 
This is thread 0 
4 
This is thread 1 
4 
This is thread 1 
4 
This is thread 1 
4 
This is thread 2 
4 
This is thread 2 
4 
This is thread 3 
4 
This is thread 3 

어떤 아이디어?

+0

어떤 컴파일러 플래그를 사용 했습니까? –

+0

또한 해당 코드는 컴파일되지 않습니다. 실제 코드를 게시하십시오. –

답변

7

컴파일 할 때 -fopenmp flag을 사용하는 것을 거의 잊어 버렸을 것입니다.

+1

나는 그 깃발을 잊지 않았다. 그러나 그것은 아직도 나를 위해 1이다. –

15

omp_get_num_threads()gcc으로 작동하지 않습니다.
내가 실행 스레드의 수를 계산하기 위해 내 자신의 루틴을 사용 : 리턴 omp_get_num_threads 프로그램의 순차적 섹션에서

#include <omp.h> 
#include <stdio.h> 

int omp_thread_count() { 
    int n = 0; 
    #pragma omp parallel reduction(+:n) 
    n += 1; 
    return n; 
} 

int main() { 
    printf("%d, %d\n",omp_thread_count(),omp_get_num_threads()); 
    return 0; 
} 
1

1. (https://gcc.gnu.org/onlinedocs/libgomp/omp_005fget_005fnum_005fthreads.html)

은 해당 omp_get_num_threads 것 병렬 섹션에 1 이외의 값을 반환하십시오.

gcc-4.9.1을 시도했습니다. 그래도 omp_get_num_threads()는 을 반환합니다.

icc와 gcc 사이의 스레드 수에 대한 인식이 다릅니다.

icc-10을 사용하면 maxNumCompThreads (2)를 사용하여 스레드 수를 지정할 수 있습니다.