インターフェース
#include <omp.h>
int omp_get_num_threads();
動作中のスレッド数を返します。
parallelの中でparallel指定した場合は、omp_set_nested()を使わない限り、1が返されます。
サンプルプログラム
サンプルソースコードを示します。
#include <iostream>
#include <omp.h>
void process_sample()
{
std::cout << "ichigosample: in_parallel = "
<< omp_in_parallel() << std::endl;
std::cout << "ichigosample: num. threads = " << omp_get_num_threads()
<< " , thread number = " << omp_get_thread_num()
<< std::endl;
}
int main()
{
std::cout << "ichigosample: max. threads = "
<< omp_get_max_threads() << std::endl;
std::cout << "ichigosample: num. threads = "
<< omp_get_num_threads() << std::endl;
#pragma omp parallel
{
#pragma omp critical(crit_cout)
{
process_sample();
}
#pragma omp barrier
}
#pragma omp parallel num_threads(2)
{
#pragma omp critical(crit_cout)
{
process_sample();
}
#pragma omp barrier
}
return 0;
}
実行結果の1例を示します。
ichigosample: max. threads = 4
ichigosample: num. threads = 1
ichigosample: in_parallel = 1
ichigosample: num. threads = 4 , thread number = 0
ichigosample: in_parallel = 1
ichigosample: num. threads = 4 , thread number = 1
ichigosample: in_parallel = 1
ichigosample: num. threads = 4 , thread number = 3
ichigosample: in_parallel = 1
ichigosample: num. threads = 4 , thread number = 2
ichigosample: in_parallel = 1
ichigosample: num. threads = 2 , thread number = 0
ichigosample: in_parallel = 1
ichigosample: num. threads = 2 , thread number = 1
関連ページ
OpenMPの解説 目次