altroware
altroware

Reputation: 980

Parallelize computation over ensemble with OpenMP

I would like to parallalize a c++ code with OpenMP. The code is not quite simple, but in my opinion the parallelization shoud not be hard, as I have an ensemble of independent systems, and i want to parellelize the for loop that spans over the std::vector which contains the systems.

The dimension of the ensamble is DIM_ENSEMBLE

Here is the interesting part of the code, with my attempt to parallelize. which of course doesn't work.

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

System is a struct which contain some std::vector

/* 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 

Here is a compilation error:

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

Upvotes: 4

Views: 2893

Answers (1)

Hristo Iliev
Hristo Iliev

Reputation: 74365

cout is an extern variable, declared in the standard C++ library, usually an instance of the output stream class (ostream), possibly a template type specialised with char. As any other C++ variable, its data sharing attribute in the scope of the OpenMP parallel region has to be either implicitly determined or explicitly specified. Given the default(none) clause, the implicit determination is switched off. As a result you have to explicitly declare the data sharing attribute of cout. The same applies to endl.

Problem resolution: add cout and endl (or possibly std::cout and std::endl) to the list of variables in the shared clause.

Upvotes: 10

Related Questions