-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathn-body-concurrent.v
61 lines (43 loc) · 1.04 KB
/
n-body-concurrent.v
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
module main
import solver
import animation
import benchmark
fn main() {
threads_count := solver.threads
delta := solver.delta
g := solver.gravity
iterations_count := solver.iterations
mut threads := []thread{cap: threads_count}
mut to_draw := [][]solver.Body{}
mut bodies := solver.initial_bodies.clone()
requests := chan solver.BodyRequest{cap: bodies.len}
results := chan solver.Body{cap: bodies.len}
for i in 0 .. bodies.len {
bodies[i].id = i
}
to_draw << bodies.clone()
mut bmark := benchmark.start()
for i in 0 .. threads_count {
threads << go solver.worker_n(requests, results, g, delta, i)
}
for _ in 0 .. iterations_count {
prev_state := bodies.clone()
for i in 0 .. bodies.len {
requests <- solver.BodyRequest{
body: prev_state[i]
previous_state: prev_state
id: prev_state[i].id
}
}
for _ in 0 .. bodies.len {
tmp := <- results
bodies[tmp.id] = tmp
}
to_draw << bodies.clone()
}
requests.close()
threads.wait()
results.close()
bmark.measure(@FN)
animation.start(to_draw)
}