# include # include # include using namespace std; int main ( int argc, char *argv[] ) { int i, id, n = 1000; double total, total_local, x[1000]; for ( i = 0; i < n; i++ ) { x[i] = ( double ) rand ( ) / ( double ) RAND_MAX; } total = 0.0; # pragma omp parallel private ( id, total_local ) { id = omp_get_thread_num ( ); total_local = 0.0; # pragma omp for for ( i = 0; i < n; i++ ) { total_local = total_local + x[i]; } # pragma omp critical { total = total + total_local; } } cout << "Sum = " << total << "\n"; return 0; }