Skip to content

Instantly share code, notes, and snippets.

@lhk
Created December 21, 2014 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lhk/f8fdd41f74d0ce29d5b0 to your computer and use it in GitHub Desktop.
Save lhk/f8fdd41f74d0ce29d5b0 to your computer and use it in GitHub Desktop.
A simple Benchmark based on Primes
//============================================================================
// 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;
}
@lhk
Copy link
Author

lhk commented Dec 21, 2014

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment