OpenMP:并行运行两个函数,每个函数占线程池的一半

我有一个CPU消耗函数do_long ,我需要在两个不同的数据集上运行。

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

我有N个线程可用(取决于机器)。 如何告诉OpenMP我想要do_long函数并行运行,N / 2线程应该在第一个do_long执行循环而另一个N / 2应该处理第二个do_long

一种方法是使用嵌套并行方法:

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