-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel_linear_conway.go
More file actions
190 lines (164 loc) · 3.14 KB
/
parallel_linear_conway.go
File metadata and controls
190 lines (164 loc) · 3.14 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"math/rand"
"sync"
"time"
)
type point struct{ x, y int }
type there struct{}
type Neighborhood struct {
sync.RWMutex
internal map[point]int
}
func NewNeighborhood() *Neighborhood {
return &Neighborhood{
internal: make(map[point]int),
}
}
func (n *Neighborhood) Inc(key point) {
n.Lock()
n.internal[key]++
n.Unlock()
}
func (n *Neighborhood) Dec(key point) {
n.Lock()
n.internal[key]--
if n.internal[key] == 0 {
delete(n.internal, key)
}
n.Unlock()
}
type Changelog struct {
sync.RWMutex
internal map[point]bool
}
func (c *Changelog) Set(p point, alive bool) {
c.Lock()
c.internal[p] = alive
c.Unlock()
}
func (c *Changelog) Reset() {
c.internal = make(map[point]bool)
}
const target int = 1e7
const size = 100
const gens int = target / size / size
const threads = 4
var alive = make(map[point]there)
var new_changes = &Changelog{}
var neighbors = NewNeighborhood()
var changed []point
var yes = there{}
func bound(n int) int {
return (size + n) % size
}
func wrap(p point) point {
p.x = bound(p.x)
p.y = bound(p.y)
return p
}
func set_alive(p point, b bool) {
if b {
alive[p] = yes
} else {
delete(alive, p)
}
}
func each_neighbor(p point, fn func(point)) {
for dx := -1; dx <= 1; dx++ {
for dy := -1; dy <= 1; dy++ {
if dx != 0 || dy != 0 {
fn(wrap(point{p.x + dx, p.y + dy}))
}
}
}
}
func update_neighbors(p point) {
_, on := alive[p]
if on {
each_neighbor(p, neighbors.Inc)
} else {
each_neighbor(p, neighbors.Dec)
}
}
func set(p point) {
alive[p] = yes
changed = append(changed, p)
// to cover solitary points
changed = append(changed, wrap(point{p.x + 1, p.y}))
update_neighbors(p)
}
func randomize_cells() {
for x := 0; x < size; x++ {
for y := 0; y < size; y++ {
p := point{x, y}
if rand.Intn(2) == 0 {
set(p)
}
}
}
}
func print_cells() {
print("\033[H\033[2J")
for y := 0; y < size && y < 20; y++ {
for x := 0; x < size && x < 20; x++ {
_, on := alive[point{x, y}]
if on {
print("o")
} else {
print(" ")
}
}
println()
}
}
func propagate_from(p point) {
each_neighbor(p, func(np point) {
_, lived := alive[np]
count := neighbors.internal[np]
lives := count == 3 || count == 2 && lived
if lives != lived {
new_changes.Set(np, lives)
}
})
}
func each_change(fn func(point)) {
var wg sync.WaitGroup
wg.Add(threads)
batch := len(changed) / threads
for t := 0; t < threads; t++ {
go func(start_cell int) {
for i := start_cell; i < start_cell+batch; i++ {
fn(changed[i])
}
wg.Done()
}(t * batch)
}
for i := batch * threads; i < len(changed); i++ {
fn(changed[i])
}
wg.Wait()
}
func implement_new_changes() {
changed = changed[:0]
for p, lives := range new_changes.internal {
set_alive(p, lives)
changed = append(changed, p)
}
}
func next_gen() {
new_changes.Reset()
each_change(propagate_from)
implement_new_changes()
each_change(update_neighbors)
}
func main() {
rand.Seed(time.Now().UnixNano())
randomize_cells()
start := time.Now()
for i := 0; i < gens; i++ {
next_gen()
}
seconds := time.Since(start).Seconds()
println("Go Efficiency in cellhz:", float64(gens*size*size)/seconds)
}