# include # include # include # include # include using namespace std; int main ( int argc, char *argv[] ); //****************************************************************************80 int main ( int argc, char *argv[] ) //****************************************************************************80 // // Purpose: // // MAIN is the main program for L1_NORM. // { int i; int num_threads; int n = 1000; double wtime; double wtime1; double wtime2; float *x; float x_l1_norm; # pragma omp parallel { num_threads = omp_get_num_threads ( ); } cout << "\n"; cout << "L1_NORM:\n"; cout << " C++ version\n"; cout << " Number of threads is " << num_threads << "\n"; x = new float[n]; // // Set X to random values. // for ( i = 0; i < n; i++ ) { x[i] = ( float ) rand ( ) / ( float ) RAND_MAX; } // // Compute the l1 norm sequentially. // wtime1 = omp_get_wtime ( ); x_l1_norm = 0.0; for ( i = 0; i < n; i++ ) { x_l1_norm = x_l1_norm + fabs ( x[i] ); } wtime2 = omp_get_wtime ( ); wtime = wtime2 - wtime1; cout << "\n"; cout << "Sequential calculation:\n"; cout << " L1_NORM = " << x_l1_norm << "\n"; cout << " Time = " << wtime << "\n"; // // Compute the l1 norm in parallel. // wtime1 = omp_get_wtime ( ); x_l1_norm = 0.0; # pragma omp parallel private ( i ) shared ( n, x ) reduction ( + : x_l1_norm ) # pragma omp for for ( i = 0; i < n; i++ ) { x_l1_norm = x_l1_norm + fabs ( x[i] ); } wtime2 = omp_get_wtime ( ); wtime = wtime2 - wtime1; cout << "\n"; cout << "Parallel calculation:\n"; cout << " L1_NORM = " << x_l1_norm << "\n"; cout << " Time = " << wtime << "\n"; delete [] x; return 0; }