Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go pi monte carlo #4

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ We're using four implementations of the algorithm:
- [x] [Rust](https://www.rust-lang.org/) version in [/rust](/rust).
- [x] [Python calling Rust with cffi](https://bheisler.github.io/post/calling-rust-in-python/) version in [/hybrid](/hybrid).
- [x] Python calling Rust with [WASM/WASI](https://wasi.dev/) version in [/wasm](/wasm).
- [x] [Go](https://golang.org) version in [/go](/go).

We're using the [hyperfine](https://github.com/sharkdp/hyperfine) benchmark tool.

Expand Down Expand Up @@ -168,5 +169,46 @@ Using WASM/WASI to call the Rust monte-carlo-pi loop from Python.

</details>

## Go

Run pi-monte-carlo algorithm in pure Go!

<details>
<summary>Click to expand!</summary>

### Build

```bash
go build .
```

### Execute

```shell
./pi-monte-carlo
```

### Benchmark

```shell
hyperfine -w 2 -m 10 ./pi-monte-carlo
```

Result (Ran on my macbook pro):

```shell
Benchmark #1: ./pi-monte-carlo
Time (mean ± σ): 51.8 ms ± 0.9 ms [User: 47.9 ms, System: 2.8 ms]
Range (min … max): 50.7 ms … 55.1 ms 50 runs
```

### Advantage

- Super fast (fastest benchmark from all experiments)
- Imperitive easy to understand code

### Disavantage

- Has to be compiled for specific OS and target platform

</details>
1 change: 1 addition & 0 deletions go/go-implementation/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pi-monte-carlo
3 changes: 3 additions & 0 deletions go/go-implementation/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/philips-labs/pi-python-rust/go/pi-monte-carlo

go 1.16
31 changes: 31 additions & 0 deletions go/go-implementation/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"fmt"
"math/rand"
"time"
)

func main() {
pi_est := montecarlo_pi(5000000)
fmt.Println(pi_est)
}

func montecarlo_pi(trials int) float64 {
hit := 0
s := rand.NewSource(time.Now().UnixNano())
r := rand.New(s)

for i := 0; i <= trials; i++ {
x := r.Float64()
y := r.Float64()

pos_on_board := x*x + y*y

if pos_on_board < 1.0 {
hit = hit + 1
}
}

return float64(hit) * 4.0 / float64(trials)
}
1 change: 1 addition & 0 deletions go/wasm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pi-monte-carlo
3 changes: 3 additions & 0 deletions go/wasm/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/philips-labs/pi-python-rust/go/pi-monte-carlo

go 1.16
36 changes: 36 additions & 0 deletions go/wasm/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"math/rand"
"time"
"syscall/js"
)

func main() {
c := make(chan struct{}, 0)
println("Go WebAssembly Initialized")

js.Global().Set("monteCarloPi", js.FuncOf(monteCarloPi))

<-c
}

func monteCarloPi(this js.Value, args []js.Value) interface{} {
trials := args[0].Int()
hit := 0
s := rand.NewSource(time.Now().UnixNano())
r := rand.New(s)

for i := 0; i <= trials; i++ {
x := r.Float64()
y := r.Float64()

pos_on_board := x*x + y*y

if pos_on_board < 1.0 {
hit = hit + 1
}
}

return float64(hit) * 4.0 / float64(trials)
}
Binary file added go/wasm/main.wasm
Binary file not shown.
6 changes: 6 additions & 0 deletions go/wasm/pi-monte-carlo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import wasmtime.loader

import main

pi_est = instance.exports.monteCarloPi(5_000_000)
print(pi_est)
2 changes: 1 addition & 1 deletion hybrid/pi-monte-carlo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn montecarlo_pi(trials: u32) -> f64 {
for _i in 1..trials {
let x: f64 = rng.gen::<f64>();
let y: f64 = rng.gen::<f64>();

// Shouldn't we do a sqrt over this?
let position_on_board: f64 = x * x + y * y;

Expand Down
2 changes: 1 addition & 1 deletion rust/pi-monte-carlo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn montecarlo_pi(trials: u32) -> f64 {
let mut hit: u32 = 0;
let mut rng = rand::thread_rng();

for _i in 1..trials {
for _i in 0..trials {
let x: f64 = rng.gen::<f64>();
let y: f64 = rng.gen::<f64>();

Expand Down
2 changes: 1 addition & 1 deletion wasm/pi_monte_carlo.wasm