-
Notifications
You must be signed in to change notification settings - Fork 2
/
gmain.go
51 lines (49 loc) · 1020 Bytes
/
gmain.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
package main
import (
"os"
"fmt"
"strconv"
"sync"
"time"
"runtime"
)
func main() {
var wg sync.WaitGroup
n, _ := strconv.Atoi(os.Args[1]) /* Cycle length. */
r, _ := strconv.Atoi(os.Args[2]) /* Number of cycles. */
m, _ := strconv.Atoi(os.Args[3]) /* Number of rounds. */
p, _ := strconv.Atoi(os.Args[5]) /* Number of processors. */
if p > 0 {
runtime.GOMAXPROCS(p);
}
token := struct {}{}
cycles := make([][]chan struct {}, r)
for i, _ := range cycles {
cycles[i] = make([]chan struct {}, n)
for j, _ := range cycles[i] {
cycles[i][j] = make(chan struct{})
}
}
wg.Add(n * r)
start := time.Now()
for i, c := range cycles {
for j, q := range c {
go func(i int, j int, q chan struct{}) {
left := q
right := cycles[i][(j + 1) % n]
for d := 0; d < m; d++ {
if j == d % n {
right <- token
<- left
} else {
<- left
right <- token
}
}
wg.Done()
} (i, j, q)
}
}
wg.Wait()
fmt.Printf("%v\n", time.Since(start).Seconds())
}