所以我有两个内部平行区域的外部平行区域。是否可以将2个线程放入外部并行线程中,将4个线程放入每个内部线程中?我做了这样的事情,但似乎不是我想要它的工作。有什么建议吗?
start_r = omp_get_wtime();
omp_set_nested(1);
omp_set_num_threads(2);
#pragma omp parallel
{
printf("Thread %d executes the outer parallel region\n",omp_get_thread_num());
omp_set_num_threads(4);
#pragma omp parellel for private(i,j,color)schedule(guided, chunk) default(shared)
{
// Blur
for (int i = 1; i < x-1; i++)
for (int j = 1; j < y-1; j++)
for (int k = 0; k < 3; k++)
{
wynik = 0;
wynik = ((color[(i-1)][((j - 1))][k] +
color[(i-1)][j][k] +
color[(i-1)][(j + 1)][k] +
color[i][(j - 1)][k] +
color[i][j][k] +
color[i][(j + 1)][k] +
color[(i+1)][(j - 1)][k] +
color[(i+1)][j][k] +
color[(i+1)][(j + 1)][k])/9);
if (wynik>255)wynik = 255;
if (wynik<0)wynik = 0;
color2[i][j][k] = wynik;
}
stop_r = omp_get_wtime();
cout << "Wyostrzenie zejelo : " << (stop_r-start_r) <<" sekund"<< endl;
cout<<omp_get_nested( )<<endl;
cout<<"Ilość wątków dla rozmycia : "<<omp_get_num_threads( )<<endl;
printf("Thread %d executes the inner parallel region\n",omp_get_thread_num());
}
omp_set_num_threads(4);
#pragma omp parellel for schedule(guided, chunk) privat(i,j,color) default(shared)
{
// Sharp
for (int i = 1; i < x - 1; i++)
for (int j = 1; j < y - 1; j++)
for (int k = 0; k < 3; k++)
{
wynik = 0;
wynik = (color[(i-1)][(j - 1)][k] * (0) +
color[(i-1)][j][k] * (-1) +
color[(i-1)][(j + 1)][k] * (0) +
color[i][(j - 1)][k] * (-1) +
color[i][j][k] * 20 +
color[i][(j + 1)][k] * (-1) +
color[(i+1)][(j - 1)][k] * (0) +
color[(i+1)][j][k] * (-1) +
color[(i+1)][(j + 1)][k] * (0))/16;
wynik = wynik % 255;
color3[i][j][k] = wynik;
}
cout<<omp_get_nested( )<<endl;
cout<<"Ilość wątków dla wyostrzenia : "<<omp_get_num_threads( )<<endl;
printf("Thread %d executes the inner parallel region\n",omp_get_thread_num());
}
}
for (int j = 0; j < y; j++)
for (int i = 0; i < x; i++)
{
fwrite(color2[i][j], 1, 3, fp2);
fwrite(color3[i][j], 1, 3, fp3);
}
fclose(fp);
fclose(fp2);
fclose(fp3);
system("PAUSE");
return 0;
}
}
答案 0 :(得分:3)
这适用于VS2012
示例:
#include <iostream>
#include <omp.h>
int main()
{
omp_set_nested(2);
#pragma omp parallel num_threads( 2 )
{
int threadID1 = omp_get_thread_num();
#pragma omp parallel num_threads( 4 )
{
int threadID2 = omp_get_thread_num();
#pragma omp critical
{
std::cout << "tID1: " << threadID1 << std::endl;
std::cout << "tID2: " << threadID2 << std::endl;
std::cout << std::endl;
}
}
}
return EXIT_SUCCESS;
}
输出:
tID1: 0
tID2: 0
tID1: 0
tID2: 2
tID1: 0
tID2: 1
tID1: 0
tID2: 3
tID1: 1
tID2: 0
tID1: 1
tID2: 1
tID1: 1
tID2: 2
tID1: 1
tID2: 3
答案 1 :(得分:0)
可以使用以下方法设置循环上的线程数:
#pragma parallel for num_threads(variable)
另见这篇文章openmp difference beetween num_threads vs. omp_set_num_threads vs OMP_NUM_THREADS