-
Notifications
You must be signed in to change notification settings - Fork 4
/
simple.odin
267 lines (221 loc) · 7.75 KB
/
simple.odin
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package jobs_example_simple
import common ".."
import jobs "../.."
import "core:fmt"
import "core:math/rand"
import "core:time"
main :: proc() {
jobs.initialize(num_worker_threads = -1, thread_proc = proc(_: rawptr) {
fmt.println("Hello from thread", jobs.current_thread_index())
for jobs.is_running() {
jobs.try_execute_queued_job()
}
})
common._profiler_init()
// sleep for a moment so all threads have time to init
time.sleep(time.Second)
{
section("simple")
g: jobs.Group
f: f32 = 1
a: int
for i in 0 ..< 10 {
jobs.dispatch(.Medium, jobs.make_job(&g, &f, proc(x: ^f32) {
common.profile_scope("A")
x^ = x^ * 2
fmt.println(x^)
}), jobs.make_job(&g, &a, proc(a: rawptr) {
common.profile_scope("B")
fmt.println(a)
}), jobs.make_job(&g, proc(_: rawptr) {
common.profile_scope("C")
fmt.println("Hey")
}))
}
jobs.wait(&g)
}
{
section("batches")
data := make([]int, 50)
for &x, i in data do x = i
g: jobs.Group
jobs.dispatch_batches(&g, data, 7, p = proc(b: ^jobs.Batch(int)) {
common.profile_scope("Batch")
fmt.printf(" %i: %i + %v (len %i)\n", b.index, b.offset, b.data, len(b.data))
})
jobs.wait(&g)
}
{
section("batches fixed")
data := make([]int, 50)
for &x, i in data do x = i
g: jobs.Group
jobs.dispatch_batches_fixed(&g, data, 7, p = proc(b: ^jobs.Batch(int)) {
common.profile_scope("Batch")
fmt.printf(" %i: %i + %v (len %i)\n", b.index, b.offset, b.data, len(b.data))
})
jobs.wait(&g)
}
{
section("balls")
Ball :: struct {
pos: [3]f32,
vel: [3]f32,
rad: f32,
damping: f32,
bounce_factor: f32,
some_data: [4]i32,
}
// hehe balls
balls := make([]Ball, 100000)
{
section("balls: singlethreaded")
update_balls(balls)
}
for batch_size in ([]int{1, 10, 100, 500, 1000, 5000, 10000}) {
section(fmt.tprintf("balls: batches of size %i", batch_size))
g: jobs.Group
jobs.dispatch_batches_fixed(&g, balls, batch_size = batch_size, p = proc(b: ^jobs.Batch(Ball)) {
common.profile_scope("Batch")
update_balls(b.data)
})
jobs.wait(&g)
}
update_balls :: proc(balls: []Ball) {
for &b in balls {
b.vel.y -= 9.81
b.vel /= 1.0 + b.damping
b.pos += b.vel
if b.pos.y - b.rad < 0.0 {
b.pos.y = b.rad
b.vel.y = abs(b.vel.y)
b.vel *= b.bounce_factor
}
// random stuff
for &d in b.some_data {
d *= d + 1
if d > 100 {
d -= 50
}
}
}
}
}
{
section("priorities")
g: jobs.Group
{
common.profile_scope("dispatch")
for i in 0 ..< 100 {
jobs.dispatch(.Low, jobs.make_job(&g, proc(_: rawptr) {
common.profile_scope("Low")
fmt.println("Low")
time.sleep(time.Microsecond * time.Duration(100 + rand.int31_max(100)))
}))
jobs.dispatch(.Medium, jobs.make_job(&g, proc(_: rawptr) {
common.profile_scope("Medium")
fmt.println("Medium")
time.sleep(time.Microsecond * time.Duration(100 + rand.int31_max(100)))
}))
jobs.dispatch(.High, jobs.make_job(&g, proc(_: rawptr) {
common.profile_scope("High")
fmt.println("High")
time.sleep(time.Microsecond * time.Duration(100 + rand.int31_max(100)))
}))
}
}
jobs.wait(&g)
}
{
// Measure how much useful code is executed
section("overhead")
Foo :: struct {
a, b, c: [3]f32,
data: [8]u64,
}
// hehe foos
foos := make([]Foo, 100000)
start: time.Tick
st_duration: time.Duration
{
start = section("foos: singlethreaded")
update_foos(foos)
st_duration = time.tick_since(start)
}
for batch_size in ([]int{1, 10, 100, 1000, 10000}) {
start = section(fmt.tprintf("foos: batches of size %i", batch_size))
g: jobs.Group
jobs.dispatch_batches_fixed(&g, foos, batch_size = batch_size, p = proc(b: ^jobs.Batch(Foo)) {
common.profile_scope("Batch")
update_foos(b.data)
})
jobs.wait(&g)
dur := time.tick_since(start)
faster := time.duration_microseconds(st_duration) / time.duration_microseconds(dur)
fmt.println(" ", faster, " x faster (target = ", jobs.num_threads(), " x)", sep = "")
}
update_foos :: proc(foos: []Foo) {
for &x in foos {
// do random crap
x.a *= 2
x.b += x.a
x.c = x.a.zzy * x.b.zxy
x.a += x.c * x.c * 2
for &d, i in x.data {
d += 2
d *= 2
d ~= 0xffaaffaa
d += u64(i % 2 == 0 ? x.a.z : x.b[i % 3])
}
}
}
}
{
section("nested")
g: jobs.Group
g2: jobs.Group
for i in 0 ..< 5 {
jobs.dispatch(.Medium, jobs.make_job(&g, proc(_: rawptr) {
common.profile_scope("A")
time.sleep(time.Millisecond * 200)
g: jobs.Group
for i in 0 ..< 5 {
jobs.dispatch(.Low, jobs.make_job(&g, proc(_: rawptr) {
common.profile_scope("A2")
time.sleep(time.Millisecond * 300)
}))
}
jobs.wait(&g)
}))
jobs.dispatch(.Medium, jobs.make_job(&g, &g2, proc(g: ^jobs.Group) {
common.profile_scope("B")
time.sleep(time.Millisecond * 500)
jobs.dispatch(.High, jobs.make_job(g, proc(_: rawptr) {
common.profile_scope("B2")
time.sleep(100)
}))
}))
}
jobs.wait(&g)
jobs.dispatch(.Medium, jobs.make_job(&g, &g2, proc(g: ^jobs.Group) {
common.profile_scope("Wait job")
jobs.wait(g)
time.sleep(time.Second)
fmt.println("done")
}))
jobs.wait(&g)
}
fmt.println("num_threads", jobs.num_threads())
common._profiler_shutdown()
jobs.shutdown()
}
_end_section :: proc(name: string, start: time.Tick) {
fmt.printf("%s: %v microseconds\n", name, time.duration_microseconds(time.tick_since(start)))
common.profile_end()
}
@(deferred_in_out = _end_section)
section :: proc(name: string) -> time.Tick {
fmt.println(name)
common.profile_begin(fmt.tprintf("Section %s", name))
return time.tick_now()
}