Skip to content

Commit

Permalink
Serial C++ version of pi added
Browse files Browse the repository at this point in the history
  • Loading branch information
tomdeakin committed Aug 22, 2013
1 parent e59717b commit 4eda3f1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Exercises/Exercise01/Cpp/DeviceInfo
Exercises/Exercise03/Cpp/vadd
Exercises/Exercise04/Cpp/vadd
Exercises/Exercise05/Cpp/vadd
Exercises/Exercise08/Cpp/pi

Solutions/Exercise04/C/vadd_chain
Solutions/Exercise05/C/vadd_abc
Expand Down
15 changes: 15 additions & 0 deletions Exercises/Exercise08/Cpp/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CC=g++

CCFLAGS=-O3 -std=c++11 -std=gnu++11

LIBS = -lm

CPP_COMMON = ../../Cpp_common

INC = -I $(CPP_COMMON)

pi: pi.cpp
$(CC) $^ $(INC) $(CCFLAGS) $(LIBS) -o $@

clean:
rm -f pi
44 changes: 44 additions & 0 deletions Exercises/Exercise08/Cpp/pi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
This program will numerically compute the integral of
4/(1+x*x)
from 0 to 1. The value of this integral is pi -- which
is great since it gives us an easy way to check the answer.
The is the original sequential program. It uses the timer
from the OpenMP runtime library
History: Written by Tim Mattson, 11/99.
Ported to C++ by Tom Deakin, August 2013
*/

#include "util.hpp"

#include <cstdio>
static long num_steps = 100000000;
double step;
extern double wtime(); // returns time since some fixed past point (wtime.c)


int main ()
{
int i;
double x, pi, sum = 0.0;


step = 1.0/(double) num_steps;

util::Timer timer;

for (i=1;i<= num_steps; i++){
x = (i-0.5)*step;
sum = sum + 4.0/(1.0+x*x);
}

pi = step * sum;
double run_time = static_cast<double>(timer.getTimeMilliseconds()) / 1000.0;
printf("\n pi with %ld steps is %lf in %lf seconds\n", num_steps, pi, run_time);
}

0 comments on commit 4eda3f1

Please sign in to comment.