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
?
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;
}
来源:https://stackoverflow.com/questions/7876156/openmp-run-two-functions-in-parallel-each-by-half-of-thread-pool