5

この古い質問がありますが、オンラインからの回答はありません。コードは次のとおりです。

#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;
}

G++ 4.4.5、linux 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

何か案は?

4

4 に答える 4

20

gccomp_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;
}
于 2012-11-11T04:52:31.530 に答える
7

コンパイル時に-fopenmpフラグを使用するのをほぼ確実に忘れていました。

于 2012-06-17T12:04:07.550 に答える
5

プログラムの順次セクションでは、omp_get_num_threads は 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() は 1 を返します。

スレッド数の認識は、icc と gcc で異なります。

icc-10 を使用すると、maxNumCompThreads(2) を使用してスレッド数を指定できます。

于 2015-10-31T09:12:46.450 に答える