This repository has been archived by the owner on Aug 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu_compute.odin
155 lines (130 loc) · 3.34 KB
/
cpu_compute.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
package compute
import "core:sync"
import "core:thread"
Compute_Dims :: distinct [3]u64
Workgroup_Args :: struct {
global_id: Compute_Dims,
workgroup_id: Compute_Dims,
workgroup_count: Compute_Dims,
workgroup_size: Compute_Dims,
tile_size: Compute_Dims,
}
Job :: struct {
fn: proc(Workgroup_Args, rawptr),
args: Workgroup_Args,
wg: ^sync.Wait_Group,
data: rawptr,
}
State :: enum u8 {
None = 0,
Terminate = 1,
}
Message :: union {
[]Job,
State,
}
Worker :: struct {
thread: ^thread.Thread,
channel: sync.Channel(Message, .Both),
}
@(thread_local, private="file") worker_idx: u64
local_worker_idx :: proc() -> u64 { return worker_idx }
worker_new :: proc(idx: u64) -> (worker: Worker) {
worker.channel = sync.channel_make(Message)
worker.thread = thread.create_and_start_with_poly_data2(
worker.channel,
idx,
proc(channel: sync.Channel(Message, .Both), idx: u64) {
worker_idx = idx
loop: for {
message := sync.channel_recv(channel)
switch m in message {
case []Job:
for job in m {
job.fn(job.args, job.data)
}
sync.wait_group_add(m[0].wg, -len(m))
case State:
if m == .Terminate {
break loop
}
}
}
},
)
return worker
}
worker_free :: proc(worker: ^Worker) {
thread.destroy(worker.thread)
sync.channel_destroy(worker.channel)
}
Compute_Group :: struct {
workers: [dynamic]Worker,
jobs: [dynamic]Job,
}
compute_group_new :: proc(worker_count := 16) -> (group: Compute_Group) {
for i in 0..<worker_count {
append(&group.workers, worker_new(u64(i)))
}
group.jobs = make([dynamic]Job)
return group
}
compute_group_free :: proc(group: ^Compute_Group) {
for worker, i in group.workers {
sync.channel_send(worker.channel, State.Terminate)
worker_free(&group.workers[i])
}
delete(group.jobs)
delete(group.workers)
}
compute :: proc(
group: ^Compute_Group,
total_size, workgroup_size: Compute_Dims,
fn: proc(Workgroup_Args, rawptr),
data: rawptr = nil)
{
assert(total_size.x > 0 && total_size.y > 0 && total_size.z > 0 &&
workgroup_size.x > 0 && workgroup_size.y > 0 && workgroup_size.z > 0,
)
wg: sync.Wait_Group
sync.wait_group_init(&wg)
defer sync.wait_group_destroy(&wg)
dispatches := 1 + ((total_size - 1) / workgroup_size)
dispatches_count := dispatches.x * dispatches.y * dispatches.z
reserve(&group.jobs, int(dispatches_count))
defer clear(&group.jobs)
for k in 0..<dispatches[2] {
for j in 0..<dispatches[1] {
for i in 0..<dispatches[0] {
global_id := workgroup_size * { i, j, k }
temp := global_id + workgroup_size
args := Workgroup_Args{
global_id,
{ i, j, k },
dispatches,
workgroup_size,
workgroup_size - (temp / total_size) * (temp % total_size),
}
append(&group.jobs, Job{ fn, args, &wg, data })
}
}
}
sync.wait_group_add(&wg, int(dispatches.x * dispatches.y * dispatches.z))
jobs_per_worker := len(group.jobs) / len(group.workers)
remainder_jobs := len(group.jobs) % len(group.workers)
for worker, i in group.workers {
begin_offset := remainder_jobs
end_offset := begin_offset
if i < remainder_jobs {
begin_offset = i
end_offset = begin_offset + 1
}
begin := i * jobs_per_worker + begin_offset
end := (i + 1) * jobs_per_worker + end_offset
assert(begin < end)
if begin < len(group.jobs) {
sync.channel_send(worker.channel, group.jobs[begin:end])
}
}
sync.wait_group_wait(&wg)
}