我有一個C++類庫,使用omp進行並行化。我注意到我的問題,當它總是用完我的處理器上的所有核心沒有什麼omp_set_num_threads(threadCount)作爲輸入。omp_set_num_threads總是返回0,我無法獲得線程數與omp_get_thread_num()
所以investegating當我意識到讓NUM_THREADS工作的唯一方法是不使用OMP_SET_NUM_THREADS()方法,但設置NUM_THREADS的並行指令,像這樣:
#pragma omp parallel num_threads(4)
{
int i = omp_get_thread_num();
printf_s("Hello from thread %d\n", i);
}
在上述情況下,輸出是:
Hello from thread 0
Hello from thread 0
Hello from thread 0
Hello from thread 0
如果我改用以下:
omp_set_num_threads(4);
#pragma omp parallel //num_threads(4)
{
int i = omp_get_thread_num();
printf_s("Hello from thread %d\n", i);
}
輸出是
Hello from thread 0
Hello from thread 0
Hello from thread 0
Hello from thread 0
Hello from thread 0
Hello from thread 0
Hello from thread 0
Hello from thread 0
不過,我創建了一個類型的Win32控制檯應用程序的OMP_SET_NUM_THREADS作品的一個新項目,我得到的輸出:從線程2 你好從線程1
你好從線程0 你好 Hello from thread 3
我不得不認爲這與項目類型或項目中的某個參數有關。有誰知道它可能是什麼?
問候 理查德
我也要,我有/ OpenMP的標誌設置 – Richard 2010-11-03 14:12:30