mirror of
https://github.com/hermitcore/libhermit.git
synced 2025-03-09 00:00:03 +01:00
add example, which creates a goroutine per core
This commit is contained in:
parent
0001229827
commit
dae1d39dd7
2 changed files with 24 additions and 8 deletions
|
@ -1 +1 @@
|
|||
Subproject commit d686639afb35650edf0d55647b7e8b30e8f1f989
|
||||
Subproject commit 07585ecfdbc69ec9cceadf4dcd0fdf1eb855d3e1
|
|
@ -32,17 +32,26 @@ import (
|
|||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func term(i int, step float64) (x float64) {
|
||||
x = (float64(i) + 0.5) * step
|
||||
x = 4.0 / (1.0 + x * x)
|
||||
var step float64
|
||||
|
||||
return
|
||||
func term(ch chan float64, start, end int) {
|
||||
var res float64
|
||||
|
||||
for i := start; i < end; i++ {
|
||||
x := (float64(i) + 0.5) * step
|
||||
res += 4.0 / (1.0 + x * x)
|
||||
}
|
||||
|
||||
ch <- res
|
||||
}
|
||||
|
||||
func main() {
|
||||
var num_steps int
|
||||
ch := make(chan float64)
|
||||
max_coroutines := runtime.NumCPU()
|
||||
|
||||
if len(os.Args) > 1 {
|
||||
num_steps, _ = strconv.Atoi(os.Args[1])
|
||||
|
@ -53,12 +62,19 @@ func main() {
|
|||
fmt.Println("num_steps = ", num_steps)
|
||||
|
||||
sum := float64(0)
|
||||
step := 1.0 / float64(num_steps)
|
||||
step = 1.0 / float64(num_steps)
|
||||
|
||||
start := time.Now()
|
||||
|
||||
for i := 0; i < num_steps; i++ {
|
||||
sum += term(i, step)
|
||||
for i := 0; i < max_coroutines; i++ {
|
||||
start := (num_steps / max_coroutines) * i
|
||||
end := (num_steps / max_coroutines) * (i+1)
|
||||
|
||||
go term(ch, start, end)
|
||||
}
|
||||
|
||||
for i := 0; i < max_coroutines; i++ {
|
||||
sum += <-ch
|
||||
}
|
||||
|
||||
elapsed := time.Since(start)
|
||||
|
|
Loading…
Add table
Reference in a new issue