Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Mp nested parallelism

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;
}
}
like image 510
Lobo Avatar asked Sep 26 '22 05:09

Lobo


1 Answers

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
like image 160
RyanP Avatar answered Sep 30 '22 06:09

RyanP