[GSoC 2017] Coding period Week1 with gopy@CERN-HSF
Hi, all! On May 30, a coding period begins! In Korea, the semester is not over yet, so my blog posting has been slowed down a lot.
Now I am running for the 1st evaluation. On the 1st week, I added boilerplate code for CFFI support.
I and Sebastien discussed how to generate CFFI codes. There was a lot of unnecessary process in my draft, but thanks to his advice I was able to create a relatively clean generating process.
Finally, https://github.com/go-python/gopy/pull/93 was merged. So we can use gopy for a very simple case with PyPy
and Python3
.
Now we can run this kind of Go code by gopy
.
package calculatePi
import (
"math"
"math/rand"
"runtime"
"sync"
"time"
)
func monte_carlo_pi(reps int, result *int, wait *sync.WaitGroup) {
var x, y float64
count := 0
seed := rand.NewSource(time.Now().UnixNano())
random := rand.New(seed)
for i := 0; i < reps; i++ {
x = random.Float64() * 1.0
y = random.Float64() * 1.0
if num := math.Sqrt(x*x + y*y); num < 1.0 {
count++
}
}
*result = count
wait.Done()
}
func GetPI(samples int) float64 {
cores := runtime.NumCPU()
runtime.GOMAXPROCS(cores)
var wait sync.WaitGroup
counts := make([]int, cores)
wait.Add(cores)
for i := 0; i < cores; i++ {
go monte_carlo_pi(samples/cores, &counts[i], &wait)
}
wait.Wait()
total := 0
for i := 0; i < cores; i++ {
total += counts[i]
}
pi := (float64(total) / float64(samples)) * 4
return pi
}
And we can get performance enhancements by using gopy on Python compilers. On the week2, I worked on Remove generating builders and support Vars and Consts. I will talk about in the next post.
Thanks
Happy hacking