forked from davemus/parallel-computings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sixth.c
121 lines (98 loc) · 2.6 KB
/
sixth.c
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
Author: IRLynx
Description:
This program calculates the volume of a hemisphere
using Monte-Carlo method. Pseudorandom numbers are
generated using LCG.
*/
#include "stdio.h"
#include "stdlib.h"
#include "stddef.h"
#include "stdint.h"
#include "math.h"
#include "limits.h"
#include "omp.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/mman.h"
/*
Macro _RAND_NEXT(NUM)
Generates a pseudorandom unsigned int number based on seed NUM
Implements the LCG
*/
#define _RAND_NEXT(NUM) ( ( ((unsigned int)(NUM)) * 1103515245 ) + 12345)
//Helper; must be ONLY statement on a line.
#define _RAND_SEED(VAR) VAR = _RAND_NEXT(VAR)
#define _SQ(EXPR) ( (EXPR)*(EXPR) )
/*
Macro _VEC_SQ(X,Y,Z)
Calculates the squared norm of vector (x,y,z)
*/
#define _VEC_SQ(X,Y,Z) ( _SQ(X) + _SQ(Y) + _SQ(Z))
/*
Macro RAND_NEXT(TYPE,NUM,LIM)
Generates a number of type TYPE in range [0,LIM]
using expression NUM as seed
*/
#define RAND_NEXT(TYPE,NUM,LIM) (((TYPE) _RAND_NEXT(NUM))/((TYPE)((long)UINT_MAX + 1)))*((TYPE) LIM)
#define R 2.0
#define NUM_POINTS 200000000
//Comment this to disable parallelism
#define OPTION_PARALLEL
//Uncomment this to use slow method for determining
//if a point is within a hemisphere
//#define OPTION_SLOW
extern int main(int argc,char *argv[]);
#ifdef OPTION_SLOW
double f(double x, double y);
#endif
int main(int argc,char *argv[])
{
double x, y, z;
int rand = 0;
int point_count = 0;
#ifdef OPTION_PARALLEL
#pragma omp parallel private(x,y,z,rand)
#endif
{
//Initial value for the LCG
#ifdef OPTION_PARALLEL
rand = omp_get_thread_num();
#endif
//Calculate the number of random points inside the hemisphere of radius R
#ifdef OPTION_PARALLEL
#pragma omp for reduction(+:point_count)
#endif
for( unsigned long i = 0; i < NUM_POINTS; i++ ) //size of long is exactly one machine word in linux
{
x = -((double)R) + 2*RAND_NEXT(double, rand, R);
_RAND_SEED(rand);
y = -((double)R) + 2*RAND_NEXT(double, rand, R);
_RAND_SEED(rand);
z = RAND_NEXT(double, rand, R);
_RAND_SEED(rand);
#ifndef OPTION_SLOW
if( _VEC_SQ(x,y,z) <= _SQ(R) ) //Checks if a point (x,y,z) is inside the hemisphere
{
point_count++;
}
#else
//Checks if a point (x,y,z) is inside the hemisphere.
//Very slow method, use only if the teacher asks you to do so.
if( fabs(f(x,y)) <= fabs(z) )
{
point_count++;
}
#endif
}
}
printf("The volume of hemisphere of radius %lf is %lf.\n",
(double)R, ((double)point_count)/NUM_POINTS*R*R*R*4);
return 0;
}
#ifdef OPTION_SLOW
double f(double x, double y)
{
return pow(pow(x,2.0)+pow(y,2.0),0.5);
}
#endif