i am working on a core 2 duo machine and actually trying to find speedup of some program using openmp. can anybody suggest a sample code. Am using RedHat Linuc and gcc complier.
ANis
#include <stdlib.h>
#include <stdio.h>
#include <omp.h>
#define N 100000000
int main()
{
int i, *a;
long long sum = 0;
double t;
a = (int*)malloc(N*sizeof(int));
t = omp_get_wtime();
#pragma omp parallel for
for( i=0; i<N; ++i)
{ a[i] = i; }
#pragma omp parallel for reduction(+:sum)
for( i=0; i<N; ++i)
{ sum += a[i]; }
printf( "sum =%lld, t = %f\n", sum, omp_get_wtime()-t );
return 0;
}
$ gcc -fopenmp 1.c
$ OMP_NUM_THREADS=1 ./a.out
sum =4999999950000000, t = 0.982977
$ OMP_NUM_THREADS=2 ./a.out
sum =4999999950000000, t = 0.696786
$ OMP_NUM_THREADS=4 ./a.out
sum =4999999950000000, t = 0.280423
Return to Using OpenMP - The Book and Examples
Users browsing this forum: No registered users and 0 guests