Changes

Jump to: navigation, search

GPU610/TeamKappa

4,112 bytes added, 21:13, 17 November 2015
Program 3: Serial pi Calculation - C version
I've decided to profile the calculate (estimate) of pi using a "dartboard" algorithm.
work in progress...The program is described as the follows:
* FILE: ser_pi_calc.c * DESCRIPTION: * Serial pi Calculation - C Version * This program calculates pi using a "dartboard" algorithm. See * Fox et al.(1988) Solving Problems on Concurrent Processors, vol.1 * page 207. * AUTHOR: unknown * REVISED: 02/23/12 Blaise Barney * Throw darts at board. Done by generating random numbers * between 0 and 1 and converting them to values for x and y * coordinates and then testing to see if they "land" in * the circle." If so, score is incremented. After throwing the * specified number of darts, pi is calculated. The computed value * of pi is returned as the value of this function, dboard.  ==== Source Code:file ====[https://computing.llnl.gov/tutorials/mpi/samples/Serial/C/ser_pi_calc.c link titlesource code]  ==== Profiling ====By default the argument 100000 darts and 100 rounds. using matrix@seneca I entered the following commands. compiled using g++ -O2 -g -pg -opi ser_pi_calc.cexecute pigprof: gprof -p -b bs > result.fltview result cat result.flt results: Float profile: Each sample counts as 0.01 seconds. % cumulative self self total time seconds seconds calls Ts/call Ts/call name 100.00 0.03 0.03 dboard(int) It appears it only calls the dboard(int) function==== Code Snippet ==== for (n = 1; n <= darts; n++) { /* generate random numbers for x and y coordinates */ r = (double)random()/cconst; x_coord = (2.0 * r) - 1.0; r = (double)random()/cconst; y_coord = (2.0 * r) - 1.0; /* if dart lands in circle, increment score */ if ((sqr(x_coord) + sqr(y_coord)) <= 1.0) score++; } /* calculate pi */ pi = 4.0 * (double)score/(double)darts; return(pi); } ==== Plans ====Using the serial Pi calculator/estimator, conduct a large amount of sampling which reaches the limit of my computer. Perhaps changing the number of round to one and and run the maximum sampling size my computer can handle. ==== Problem ====When using the code with visual studio, I had errors trying to compile it. It appears after some time searching online, the #include <time.h> function void srandom(unsigned seed); and random() is not come standard in all ANSI code so I tried the approach using srand(NULL) and rand(); and still failed resulting returns of 0.0000. zeroes. I modified the code for the random generate number and added report time kept idea the same into a simpler program. ==== Code ====<code><pre>#include <iostream>#include <ctime>#include <cstdlib>#include <chrono>using namespace std::chrono; double generateNumber() { const double randf = 1.0f / (double)RAND_MAX; return std::rand() * randf;}  void reportTime(const char* msg, steady_clock::duration span) { double nsecs = double(span.count()) * steady_clock::period::num / steady_clock::period::den; std::cout << msg << " - took - " << nsecs << " secs" << std::endl;}  int main(int argc, char* argv[]){ double dart = std::atoi(argv[1]); steady_clock::time_point ts, te;   double x, y; int i; int score = 0; double z; double pi;  ts = steady_clock::now(); //main loop for (i = 0; i<dart; ++i) { //get random points x = (double)generateNumber(); y = (double)generateNumber(); z = sqrt((x*x) + (y*y)); if (z <= 1) ++score; } pi = ((double)score / (double)dart)*4.0; // p = 4(m/n) std::cout << "After " << i << " throws, average pi is " << pi << std::endl; te = steady_clock::now(); reportTime("pi calculation", te - ts);}</pre></code> ==== Results, bottlenecks, benchmark ====It appears my computer's limitation is <code><pre>After 2147483647 throws, average pi is 3.14152pi calculation - took - 223.884 secs</pre></code>  {| class="wikitable" border="1"! n !! first !! second !! third !! average|-| 10000 || 0.0156s || 0.0156s || 0s || 0.0104s|-| 100000 || 0.1092s || 0.1092s || 0.1092s || 0.1092s|-| 1000000 || 1.0452s || 1.0296s || 1.0452s || 1.04s|-| 10000000 || 10.299s || 10.2542s || 10.4092s || 10.3208s|-| 100000000 || 102.19s || 103.553s || 103.728s || 103.157s|-| 2147483647 || 223.884s || 223.996s || 222.882s || 223.5873s|}
== Assignment 2 ==
13
edits

Navigation menu