2013-08-07 35 views
2

我想用OpenMP對C++代碼進行並行化。代碼不是很簡單,但在我看來,並行化並不難,因爲我有一個獨立系統的集合,我想parellelize橫跨包含系統的std :: vector的for循環。使用OpenMP對整體進行並行化計算

的ensamble的尺寸爲DIM_ENSEMBLE

這裏是代碼的有趣的部分,我嘗試並行。這當然不起作用。

vector<Systems> system(DIM_ENSEMBLE); 
vector<double> current(100); 

系統是其中含有一些的std ::矢量

/* do things 
... 
*/ 

while (time < T){ 
/*154*/ #pragma omp parallel for default(none) shared(r, DIM_ENSEMBLE, system, current) private(i, max_rate, time_increment, R, j, status) 
    for (i =0; i< DIM_ENSEMBLE; i++) { 
    max_rate = function_A_of(i); 
    time_increment = function_B_of(r,max_rate); 
    R = function_C_of(r,max_rate); 
    j = function_D_of(System, i, R); 
    status = update_the_system(&system[0], i, time_increment, j, &current[0]); 
    if (status!=1) { 
     #pragma omp critical 
     { 
/*173*/ cout << "ERROR " << i << " " << time_increment << " " << j <<endl; 
     } 
    } 
    update_time(time); 
    } //end for loop 

/* now calculate some averages and distributions from the ensemble 
.... 
*/ 
} //end while loop 

這裏一個結構被編譯錯誤:

one-node-hirsch-parallel.cpp:173: error: ‘cout’ not specified in enclosing parallel 
    one-node-hirsch-parallel.cpp:154: error: enclosing parallel 
+0

在您的書名中,您寫了[OpenMP](http://en.wikipedia.org/wiki/OpenMP),並且在您編寫的問題中[Open MPI](http://en.wikipedia.org/wiki/Open_MPI )... –

+0

@Kyle_the_hacker。謝謝,完成。 – altroware

回答

6

coutextern變量,在標準C++庫聲明,通常是輸出流類(ostream)的一個實例,可能是專用於char的模板類型。與任何其他C++變量一樣,OpenMP parallel區域範圍內的數據共享屬性必須是隱式確定的或明確指定的。鑑於default(none)子句,隱式確定被關閉。因此,您必須明確聲明cout的數據共享屬性。這同樣適用於endl

問題解決方案:加coutendl(或可能std::coutstd::endl)至shared子句中的變量列表。

+0

Ilieve。謝謝你,但不幸的是你的建議不會改變編譯錯誤。 – altroware

+0

你的代碼有'#include '和'using namespace std;'嗎? –

+0

當然可以。順便說一下,我試圖「分享」cout和std :: cout而沒有結果。 – altroware