2016-02-01 66 views
1

通過C++列表/迭代循環,我想使用OpenMP並行的結構如下:並行化二爲使用OpenMP

for (auto it = data.begin(); it != data.end(); ++it) 
{ 
    for (vector<my_element>::iterator it2 = it->begin(); it2 != it->end(); ++it2) 
    { 
     // code here (it2->process) 
    } 
} 

我曾嘗試不同的方法,但我無法使其正常工作。你可以幫幫我嗎?。

我使用的是gcc 5.3.0,OpenMP 4.0。至今沒有成功,我已經試過的解決方案是:

解決方案1:

#pragma omp parallel 
#pragma omp single 
{ 
    for (auto it = data.begin(); it != data.end(); ++it) 
    { 
    #pragma omp task firstprivate(it) 
     for (vector<my_element>::iterator it2 = it->begin(); it2 != it->end(); ++it2) 
     { 
      // code here (it2->process) 
     } 
    #pragma omp taskwait 
    } 
} 

解決方案2:

#pragma omp parallel 
{ 
    for (auto it = data.begin(); it != data.end(); ++it) 
    { 
     #pragma omp single nowait 
     { 
      for (vector<my_element>::iterator it2 = it->begin(); it2 != it->end(); ++it2) 
      { 
      // code here (it2->process) 
      } 
     } 
    } 
} 
+0

是的,對不起,我只想得到一個通用的解決方案。我已經編輯過這個問題,包括我迄今爲止嘗試過的解決方案,但沒有成功 – Finfa811

+0

就這樣,我們清楚,你是想在第一個循環('data'的元素)還是內部循環(' it')? – NoseKnowsAll

+0

主要思想是試圖在第一個循環上並行化,因爲'data'元素的數量≈10^ 7。 'It'的元素<10。 – Finfa811

回答

1

我想你不應該叫omp taskwait

#pragma omp parallel 
{ 
    #pragma omp single 
    { 
     for (auto it = data.begin(); it != data.end(); ++it) 
     { 
     #pragma omp task firstprivate(it) 
      for (vector<my_element>::iterator it2 = it->begin(); it2 != it->end(); ++it2) 
      { 
      // code here (it2->process) 
      } 
     } 
    } 
} 
+0

該解決方案似乎正常工作。謝謝 – Finfa811

+0

有沒有什麼辦法可以在不同線程中運行第一個循環(沒有'single'用法)? – Finfa811

+0

@ Finfa811它運行在不同的線程中,併產生其餘線程工作的任務。如果沒有'single',所有的線程都會產生任務,而且沒有任何線程可以處理它們 – Radek