5

So I have outer parallel region with two inner parallel regions. Is it possible to put 2 threads into outer parallel and 4 threads into each inner ones? I made something like this, but it seems not work how i want it to. Any suggestions?

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;
}
}
NoseKnowsAll
  • 4,593
  • 2
  • 23
  • 44
Lobo
  • 183
  • 1
  • 2
  • 8

2 Answers2

3

This works in VS2012

example:

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

Output:

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
RyanP
  • 1,898
  • 15
  • 20
1

It is possible to set the number of threads on a loop with this:

#pragma parallel for num_threads(variable)

see also this post openmp difference beetween num_threads vs. omp_set_num_threads vs OMP_NUM_THREADS

Community
  • 1
  • 1
terence hill
  • 3,354
  • 18
  • 31
  • Yes i tried to put number of threads to inner regions like this, but they still get only two threads as outer region. – Lobo Nov 11 '15 at 16:56
  • This should work to set the number of threads working on the for(...) not on a region. – terence hill Nov 11 '15 at 17:04
  • Even if i put 4 threads into for parellel loops, omp_get_num_threads( ) - gives me same anserw (that only two threads are working in this loop). It seems that it depends from how many threads i set before outer region, but even if i set 8 of them they are all working in inner regions ( and i can't divide them to work into both of them in the same time) – Lobo Nov 11 '15 at 17:18
  • It is hard to answer, update your question, post a working code so that I can test it. – terence hill Nov 11 '15 at 20:13