Created
December 21, 2014 12:52
-
-
Save lhk/f8fdd41f74d0ce29d5b0 to your computer and use it in GitHub Desktop.
A simple Benchmark based on Primes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//============================================================================ | |
// Name : Benchmark.cpp | |
// Author : Lars Klein | |
// Version : 0.3 | |
// Copyright : CC | |
// Description : A simple benchmark based on primes | |
//============================================================================ | |
#include <iostream> | |
#include <math.h> | |
#include "omp.h" | |
using namespace std; | |
int main() { | |
const int MAX_SIZE=1000000; | |
double walltime=omp_get_wtime(); | |
int i; | |
int j; | |
int prime; | |
int amount = 0; | |
int times=10; | |
for(int t=0;t<times;t++){ | |
amount=0; | |
# pragma omp parallel private ( i, j, prime ) | |
# pragma omp for reduction ( + : amount ) | |
for ( i = 2; i <= MAX_SIZE; i++ ){ | |
prime = 1; | |
for ( j = 2; j <= sqrt(i); j++ ){ | |
if ( i % j == 0 ){ | |
prime = 0; | |
break; | |
} | |
} | |
amount = amount + prime; | |
} | |
} | |
walltime=omp_get_wtime()-walltime; | |
walltime/=times; | |
cout<<"number of primes in 2.."<<MAX_SIZE<<" : "<<amount<<endl; | |
cout<<"the calculation took: "<<walltime<<endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
compile and run with
g++ -fopenmp Benchmark.cpp -o bench
export OMP_NUM_THREADS=2
./bench
Depending on the shell you use, you might have to replace
export OMP_NUM_THREADS=2
with
set -x OMP_NUM_THREADS=2