# include # include # include # include # include int main ( int argc, char *argv[] ); /******************************************************************************/ int main ( int argc, char *argv[] ) /******************************************************************************/ /* Purpose: MAIN is the main program for L1_NORM. */ { int i; int j; 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 ( ); } printf ( "\n" ); printf ( "L1_NORM:\n" ); printf ( " C version\n" ); printf ( " Number of threads is %d\n", num_threads ); x = ( float * ) malloc ( n * n * sizeof ( float ) ); /* Set X to random values. */ for ( j = 0; j < n; j++ ) { for ( i = 0; i < n; i++ ) { x[i+j*n] = ( float ) rand ( ) / ( float ) RAND_MAX; } } x_l1_norm = ( float * ) malloc ( n * sizeof ( float ) ); /* Compute the l1 norm sequentially. */ wtime1 = omp_get_wtime ( ); for ( j = 0; j < n; j++ ) { x_l1_norm[j] = 0.0; for ( i = 0; i < n; i++ ) { x_l1_norm[j] = x_l1_norm[j] + fabs ( x[i+j*n] ); } } wtime2 = omp_get_wtime ( ); wtime = wtime2 - wtime1; printf ( "\n" ); printf ( "Sequential calculation:\n" ); printf ( " L1_NORM (first 4 ) = %f\n", x_l1_norm[0], x_l1_norm[1], x_l1_norm[2], x_l1_norm[3] ); printf ( " Time = %f\n", wtime ); /* Compute the l1 norm in parallel. */ wtime1 = omp_get_wtime ( ); # pragma omp parallel private ( i, j ) shared ( n, x, x_l1_norm ) # pragma omp for for ( j = 0; j < n; j++ ) { x_l1_norm[j] = 0.0; for ( i = 0; i < n; i++ ) { x_l1_norm[j] = x_l1_norm[j] + fabs ( x[i+j*n] ); } } wtime2 = omp_get_wtime ( ); wtime = wtime2 - wtime1; printf ( "\n" ); printf ( "Parallel calculation:\n" ); printf ( " L1_NORM (first 4 ) = %f\n", x_l1_norm[0], x_l1_norm[1], x_l1_norm[2], x_l1_norm[3] ); printf ( " Time = %f\n", wtime ); free ( x ); free ( x_l1_norm ); return 0; }