OpenMP: running two functions in parallel, each with half a thread pool

I have a processor consumption function do_longthat 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 (machine dependent). How do I tell OpenMP that I want both do_long functions to run in parallel, and the N / 2 threads should loop in the first do_longand the other N / 2 should handle the second do_long?

+5
source share
1 answer

One approach is to do this 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;
}
+5
source

All Articles