-
Notifications
You must be signed in to change notification settings - Fork 1
/
montecarlopi.go
63 lines (58 loc) · 1.42 KB
/
montecarlopi.go
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
// Author: Kevin Chalmers
// Date: 17/08/2017
package main
import (
"fmt"
"math"
"math/rand"
"os"
"time"
)
// montecarlopi estimates pi based on random point selection.
func montecarlopi(iterations int, master chan float64) {
s := rand.NewSource(time.Now().UnixNano())
r := rand.New(s)
in_circle := 0
for count := 0; count < iterations; count++ {
x := r.Float64()
y := r.Float64()
if x*x+y*y <= 1.0 {
in_circle++
}
}
master <- (4.0 * float64(in_circle)) / float64(iterations)
}
// experiment runs a monte carlo pi experiment for a number of threads.
func experiment(iterations int, threads int) {
f, err := os.Create(fmt.Sprintf("mcp-go-%d.csv", threads))
if err != nil {
fmt.Println("error creating file: ", err)
return
}
c := make(chan float64)
defer f.Close()
for index := 0; index < 100; index++ {
start := time.Now()
for t := 0; t < threads; t++ {
go montecarlopi(iterations/threads, c)
}
pi := 0.0
for t := 0; t < threads; t++ {
pi = pi + <-c
}
pi = pi / float64(threads)
total := time.Now().Sub(start).Nanoseconds()
fmt.Println(index, " ", pi, " ", total)
_, err := f.WriteString(fmt.Sprintf("%d\n", total))
if err != nil {
fmt.Println("error writing string: ", err)
}
}
}
func main() {
experiment(int(math.Pow(2, 24)), 1)
experiment(int(math.Pow(2, 24)), 2)
experiment(int(math.Pow(2, 24)), 4)
experiment(int(math.Pow(2, 24)), 8)
experiment(int(math.Pow(2, 24)), 16)
}