2015-05-18 91 views
2

我有一個遞歸程序,我想加快使用openMP。結構如下所示。如何在openMP中單個區域內並行for循環?

我對omp task不熟悉,剛剛從here瞭解到了一些東西。看起來我必須在omp single區域包裝buildTree

但是,我也想並行化buildTree內的for循環,我該如何實現呢?

int main() 
{ 
    #pragma omp parallel 
    { 
     #pragma omp single nowait 
     buildTree(); 
    } 
} 
void buildTree 
{ 
    if(endRecursion) 
     return; 
    for(int i = 0; i < problemSize; i++) 
    { 
     // I want to parallelize these code using omp for 
    } 

    if(problemSizeIsSmall) 
    { 
     buildTree(subProblemSize); // left subtree 
     buildTree(subProblemSize); // right subtree 
    } 
    else 
    { 
     #pragma omp task 
     { 
      buildTree(subProblemSize); // left subtree 
     } 
     #pragma omp task 
     { 
      buildTree(subProblemSize); // right subtree 
     } 
    } 
} 

回答

0

我認爲你可以在你的問題,請使用nested parallelism。 您的代碼看起來像這樣在您的main()

#pragma omp parallel for num_threads(2) 
buildTree(); 

buildTree()

omp_set_num_threads(4); // 4 or whatever number of threads you want 
#pragma omp parallel for 
for(int i = 0; i < problemSize; i++) 

檢查4.3節Example 4–2 Calls to OpenMP Routines Within Parallel Regionsfirst link更多細節