2012-01-23 5 views
18

を返します。コマンドラインで渡される引数に基づいて使用するプロセッサの数。セット2にOMP_SET_NUM_THREADSを使用してスレッドの数()、しかしOMP_GET_NUM_THREADSは()私は、OpenMPを使用して、次のC/C++のコードを持っている1

しかし、私は次の出力を取得しています:なぜ2を返さないomp_get_num_threads()

argv[4]: 2 //OK 
nProcessors: 2 //OK 
omp_get_num_threads(): 1 //WTF?! 

!?


が指摘したように、私はので、関数が1を返し、シリアル地域でomp_get_num_threads()を呼んでいます。

#pragma omp parallel for private(i,j,tid,_hash) firstprivate(firstTime) reduction(+:nChunksDetected) 
    for(i=0;i<fileLen-CHUNKSIZE;i++){ 
     tid=omp_get_thread_num(); 
     printf("%d\n",tid); 
     int nThreads=omp_get_num_threads(); 
     printf("%d\n",nThreads); 
... 

出力:

0 //tid 
1 //nThreads - this should be 2! 
0 
1 
0 
1 
0 
1 
... 
+3

プログラムのシリアル部分からこれを呼び出していますか?その洞察力のための多くのおかげで - はい、スレッドの数は、1 – stephan

+0

彼のステファンは実際にある場合。私は別の関連する質問でOPを編集しました。 – Eamorr

+1

OpenMPを有効にしたインテル®IPPライブラリーを使用している場合、それらが衝突する場合があります。 –

答えて

26

omp_get_num_threads()コールリターン1のコードの連続切片に

しかし、私は、次の並列コードを有しています。

#include <iostream> 
#include <omp.h> 

int main (int argc, const char * argv[]) 
{ 
    int nProcessors = omp_get_max_threads(); 

    std::cout<<nProcessors<<std::endl; 

    omp_set_num_threads(nProcessors); 

    std::cout<<omp_get_num_threads()<<std::endl; 

#pragma omp parallel for 
    for(int i = 0; i < 5; i++){ 
     int tid = omp_get_thread_num(); 
     std::cout<<tid<<"\t tid"<<std::endl; 
     int nThreads = omp_get_num_threads(); 
     std::cout<<nThreads<<"\t nThreads"<<std::endl; 
    } 

    exit(0); 
} 

このコードが生成する:

1 
0 tid 
2 nThreads 
0 tid 
2 nThreads 
0 tid 
2 nThreads 
1 tid 
2 nThreads 
1 tid 
2 nThreads 

をだからここにあなたのコードは次のように見えるべきか、正しい値を取得するための並列コードを持っている必要がありますLink

を参照してください。あなたが開いMPのどちらかが有効になっていないと思われたり、ループは、OpenMP

によってparallizedできる形式ではありません10
+0

OKをインテルスレッドビルディングブロックを使用していますが、私は並列領域に入るとき、それはまだ私の編集を参照してください... 1を返します。多くのありがとう... – Eamorr

+0

あなたはどこであなたの平行領域を開始していますか、私は編集でそれが表示されません。 – tune2fs

+0

申し訳ありません、今すぐ更新しました... – Eamorr

8

間違った機能を使用しています。 omp_get_max_threadsを使用して、許可されたスレッドの最大数を確認してください。

0

それはすでにomp_get_num_threads()戻っていることが指摘されているコードの連続的なセクションで1。私たちは、並列セクションにある場合を除きしたがって、場合でもomp_set_num_threads()により、設定、1より大きいスレッドの総数は、omp_get_num_threads()への呼び出しは、1を返します。以下の例では、この点を明確にしようとしています。

#include <stdio.h> 

#include <omp.h> 

int main() { 

    const int maxNumThreads = omp_get_max_threads(); 

    printf("Maximum number of threads for this machine: %i\n", maxNumThreads); 

    printf("Not yet started a parallel Section: the number of threads is %i\n", omp_get_num_threads()); 

    printf("Setting the maximum number of threads...\n"); 
    omp_set_num_threads(maxNumThreads); 

    printf("Once again, not yet started a parallel Section: the number of threads is still %i\n", omp_get_num_threads()); 

    printf("Starting a parallel Section...\n"); 

#pragma omp parallel for 
    for (int i = 0; i < maxNumThreads; i++) { 
     int tid = omp_get_thread_num(); 
     printf("This is thread %i announcing that the number of launched threads is %i\n", tid, omp_get_num_threads()); 
    } 

}