Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenMP: run two functions in parallel, each by half of thread pool

Tags:

c++

c

openmp

I have a CPU consuming function do_long that I need to run on two different datasets.

do_long(data1);
do_long(data2);

do_long() {
#pragma omp for
    for(...) {
        // do proccessing
    }
}

I have N threads available (depends on machine). How to tell OpenMP that I want that both do_long functions are run in parallel, and N/2 threads should perform the loop in first do_long and another N/2 should process second do_long?

like image 572
Jakub M. Avatar asked Oct 24 '11 13:10

Jakub M.


1 Answers

One approach is to do it using nested parallelism:

void do_long(int threads) {
#pragma omp parallel for num_threads(threads)
    for(...) {
        // do proccessing
    }
}


int main(){
    omp_set_nested(1);

    int threads = 8;
    int sub_threads = (threads + 1) / 2;

#pragma omp parallel num_threads(2)
    {
        int i = omp_get_thread_num();

        if (i == 0){
            do_long(data1, sub_threads);
        }
        if (i == 1 || omp_get_num_threads() != 2){
            do_long(data2, sub_threads);
        }
    }

    return 0;
}
like image 142
Mysticial Avatar answered Sep 22 '22 07:09

Mysticial