2011-12-03 43 views
4

我在使用OpenMP一開始,我只是編譯gcc -fopenmp openmp_c_helloworld.c下面的一段代碼:OpenMP的行爲檢測CPU和線程

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

int main (int argc, char *argv[]) { 
    int th_id, nthreads; 
    #pragma omp parallel private(th_id) 
    { 
    th_id = omp_get_thread_num(); 
    printf("Hello World from thread %d\n", th_id); 
    #pragma omp barrier 
    if (th_id == 0) { 
     nthreads = omp_get_num_threads(); 
     printf("There are %d threads\n",nthreads); 
    } 
    } 
    return EXIT_SUCCESS; 
} 

我只是一個四核Intel CPU超線程上運行的可執行文件我得到以下的輸出:

Hello World from thread 2 
Hello World from thread 0 
Hello World from thread 3 
Hello World from thread 1 
There are 4 threads 

從技術上講,我有我的可用CPU和4個CPU核心8線程,爲什麼OpenMP的顯示我只有4個線程?

回答

1

簡而言之,我認爲這是因爲OpenMP尋找CPU(核心)的數量而不是處理器線程的數量。 見this頁:`

實現默認 - 通常是一個節點上的CPU數量,雖然 它可以是動態的(見下子彈)。

你可以嘗試的一件事是設置你的程序中的線程數量等於處理器線程的數量,看看是否有性能改進(你必須創建自己的基準測試程序)。 在並行編程中,當工作線程的數量等於處理器線程的數量時,獲得了良好的性能。您還可以爲I/O保留一個或兩個額外的線程。