我不確定這裏發生了什麼事。
可能出現這種情況,您使用存根OpenMP庫進行編譯,該庫提供所有OpenMP庫API,但只能像序列模式那樣工作(例如,針對相應的英特爾編譯器開關,請參見this link)。
另一種可能性是在您的環境中將OMP_THREAD_LIMIT
環境變量設置爲1
。例如,見下面的代碼:
#include <iostream>
#include <omp.h>
int main() {
omp_set_num_threads(8);
#pragma omp parallel
#pragma omp single
std::cout << "Number of threads in the current parallel region is " << omp_get_num_threads() << std::endl;
return 0;
}
當與支持OpenMP和運行編譯,它給了我:
$ g++ -fopenmp nbthreads.cc -o nbthreads
$ ./nbthreads
Number of threads in the current parallel region is 8
$ OMP_THREAD_LIMIT=1 ./nbthreads
Number of threads in the current parallel region is 1
來自這兩種可能性
除此之外,我不知道。
編輯:感謝Z boson的 評論,我敢肯定,我已經得到了神祕的關鍵。
有了非常相同的代碼上文之一,這裏是我的本錢:
$ g++ -o nbthreads nbthreads.cc -lgomp
$ ./nbthreads
Number of threads in the current parallel region is 1
所以你根本錯誤地使用-lgomp
代替-fopenmp
在編譯/鏈接代碼。這給你一個相當於只有1個線程的OpenMP代碼,即使你明確要求更多。
究竟是什麼編譯器? – Jeff
gcc(Ubuntu 4.8.4-2ubuntu1〜14.04)4.8.4 –