-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
79 lines (67 loc) · 1.54 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include "bench_atomics.hpp"
#include "bench_mutex.hpp"
#include "bench_local.hpp"
#include "bench_shared.hpp"
#include <time.h>
static double my_time( void )
{
struct timespec tp;
if( 0 == clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp) )
{
return tp.tv_sec + (double)tp.tv_nsec / 1000000000.0;
}
else
{
std::cerr<<"Error reading current time"<<std::endl;
return 0;
}
}
int main( int numArgs, const char * args[] )
{
size_t count;
size_t nthreads;
benchmark * b;
if( numArgs != 4 )
{
std::cerr<<"Usage: "<<args[0]<<" mode numPoints numThreads"<<std::endl;
std::cerr<<"Modes: atomics mutex local shared"<<std::endl;
exit(2);
}
if(0){}
else if( 0 == strcmp( args[1], "atomics" ) )
{
b = new benchmark_atomics;
}
else if( 0 == strcmp( args[1], "mutex" ) )
{
b = new benchmark_mutex;
}
else if( 0 == strcmp( args[1], "local" ) )
{
b = new benchmark_local;
}
else if( 0 == strcmp( args[1], "shared" ) )
{
b = new benchmark_shared;
}
else
{
std::cerr<<"unknown mode"<<std::endl;
exit(3);
}
count = atoi(args[2]);
nthreads = atoi(args[3]);
std::cerr<<"Approximating the value of PI using "<<count<<" pseudorandomly placed points and "<<nthreads<<" threads"<<std::endl;
double time_start = my_time();
b->run( count, nthreads );
double time_end = my_time();
std::cout<<(time_end-time_start);
std::cerr<<"pi approx:"<< b->result() <<std::endl;
delete b;
//std::cerr<<"in circle:"<<in_circle<<" points"<<std::endl;
//std::cerr<<"in square:"<<in_square<<" points"<<std::endl;
}