4

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
1

1 Answer 1

9

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.

8
  • Ilieve. Thank you for this, but unfortunately your suggestion doesn't change the compilation error.
    – altroware
    Aug 8, 2013 at 8:33
  • Does your code have #include <iostream> and using namespace std;? Aug 8, 2013 at 8:52
  • Yes, of course. By the way, I tried to "share" cout and std::cout whithout results.
    – altroware
    Aug 8, 2013 at 9:04
  • Try to write #pragma omp parallel for default(none) shared(r, DIM_ENSEMBLE, system, current, cout) private(i, max_rate, time_increment, R, j, status) it works! Aug 9, 2013 at 0:11
  • @altroware, do you still get an error about cout or has it changed into an error about endl? The latter is also an external object and should be declared as shared. Aug 9, 2013 at 7:53

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.