Source file parallel_scheduler.ml
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
open! Base
open Await
module Scheduler = Parallel_kernel.For_scheduler
module Result = Scheduler.Result
module Spawn = struct
type t =
{ create : Await.t @ local -> Parallel_kernel.t Concurrent.t @ local portable
@@ global
; spawn :
('r : value_or_null) 'a. ('r, 'a, Parallel_kernel.t) Concurrent.Scheduler.spawn_fn
@@ global
}
let thread =
let rec spawn
: type (r : value_or_null) a.
(r, a, Parallel_kernel.t) Concurrent.Scheduler.spawn_fn
=
fun scope #{ fn; name; affinity } resource ->
let task =
#{ Concurrent.Task.fn =
(fun scope () concurrent -> exclave_
fn scope Parallel_kernel.sequential (create (Concurrent.await concurrent)))
; name
; affinity
}
in
Concurrent_in_thread.scheduler.spawn scope task resource
and create await = exclave_
(Concurrent.create [@mode portable])
await
~scheduler:((Concurrent.Scheduler.create [@mode portable] [@alloc stack]) ~spawn)
in
{ create; spawn }
;;
let inject ~task ~subtask ~try_wake f r =
let r = (Obj.magic_many [@mode contended portable unique]) r in
let f parallel =
let r = (Obj.magic_unique [@mode contended portable]) r in
f parallel r
in
match Scheduler.root_exn f ~task ~subtask ~try_wake with
| root ->
task root;
Concurrent.Spawned
| exception (Out_of_fibers as exn) ->
let r = (Obj.magic_unique [@mode contended portable]) r in
let bt = Backtrace.Exn.most_recent () in
Concurrent.Failed (r, exn, bt)
;;
let fiber ~queues =
let task f = Work_deqs.inject queues f in
let subtask f = Work_deqs.push queues f in
let try_wake ~n = Work_deqs.try_wake queues ~n in
let rec spawn
: type (r : value_or_null) a.
(r, a, Parallel_kernel.t) Concurrent.Scheduler.spawn_fn
=
fun scope #{ fn; affinity = _; name = _ } r ->
match
inject
~task
~subtask
~try_wake
(fun parallel (token, r) ->
Concurrent.Scope.Token.use token ~f:(fun [@inline] terminator scope ->
let await = Scheduler.await parallel terminator in
fn scope parallel (create await) r [@nontail])
[@nontail])
(Concurrent.Scope.add scope, r)
with
| Spawned -> Spawned
| Failed ((token, r), exn, bt) ->
Concurrent.Scope.Token.drop token;
Failed (r, exn, bt)
and create await = exclave_
(Concurrent.create [@mode portable])
await
~scheduler:((Concurrent.Scheduler.create [@mode portable] [@alloc stack]) ~spawn)
in
exclave_ { create; spawn }
;;
end
let workers ~max_workers =
let workers =
let default = Multicore.max_domains () in
Option.value_map max_workers ~f:(Int.min default) ~default
in
if workers < 1 then invalid_arg "Parallel_scheduler: workers < 1";
workers
;;
let spawn_workers ~workers ~queues ~on_root scope =
let task i =
Concurrent.task
~name:[%string "Par_sched:%{i#Int}"]
~affinity:i
(fun _ cancellation _ concurrent ->
assert (Multicore.current_domain () = i);
Work_deqs.work
queues
~await:(Concurrent.await concurrent)
~cancellation [@nontail])
in
(Option.iter [@mode local]) on_root ~f:(fun on_root ->
Concurrent.Scheduler.spawn_daemon' on_root scope (task 0));
for i = 1 to workers - 1 do
Concurrent.Scheduler.spawn_daemon' Concurrent_in_thread.scheduler scope (task i)
done
;;
let with_workers ~workers ~on_root await f =
let queues = Work_deqs.create ~workers in
let%tydi { create; _ } = Spawn.fiber ~queues in
(Concurrent.Scope.with_ await () ~f:(fun await scope ->
spawn_workers ~workers ~queues ~on_root scope;
let concurrent = create await in
{ many = f concurrent }))
.many
;;
let with_parallel ?max_workers f =
let f = (Obj.magic_portable [@mode once]) f in
match workers ~max_workers with
| 1 -> f Parallel_kernel.sequential
| workers ->
let await =
(Await.Expert.create [@alloc stack])
~sync:Sync.blocking
~terminator:Terminator.unkillable
in
with_workers
~workers
~on_root:(Some Concurrent_in_thread.scheduler)
await
(fun concurrent ->
(Concurrent.spawn_join [@mode unique]) concurrent () ~f:(fun _ parallel _ ->
Result.Capsule.globalize
(Result.Capsule.try_with (fun () ->
(Scheduler.with_heartbeat (fun () -> { aliased_many = f parallel }))
.aliased_many)) [@nontail])
[@nontail])
|> Result.Capsule.unwrap_ok_exn
;;
let with_concurrent ?max_workers f =
let await =
(Await.Expert.create [@alloc stack])
~sync:Sync.blocking
~terminator:Terminator.unkillable
in
match workers ~max_workers with
| 1 when not (Basement.Stdlib_shim.runtime5 ()) ->
let%tydi { create; _ } = Spawn.thread in
f (create await) [@nontail]
| workers ->
with_workers
~workers
~on_root:(Some Concurrent_in_thread.scheduler)
await
(fun concurrent ->
Result.globalize
(Result.try_with (fun () ->
(Scheduler.with_heartbeat (fun () -> { aliased_many = f concurrent }))
.aliased_many)) [@nontail])
|> Result.ok_exn
;;
let scheduler ?max_workers ?on_root () =
match workers ~max_workers with
| 1 when not (Basement.Stdlib_shim.runtime5 ()) ->
let%tydi { spawn; _ } = Spawn.thread in
(Concurrent.Scheduler.create [@mode portable]) ~spawn
| workers ->
let workers, on_root =
match workers, on_root with
| n, None when Multicore.max_domains () > n -> n + 1, None
| _ -> workers, on_root
in
let queues = Work_deqs.create ~workers in
let%tydi { spawn; _ } = Spawn.fiber ~queues in
let scope = Concurrent.Scope.Global.create () ~on_exit:(fun _ _ -> ()) in
(match workers, on_root with
| 1, None ->
spawn_workers ~workers ~queues ~on_root:(Some Concurrent_in_thread.scheduler) scope
| _ -> spawn_workers ~workers ~queues ~on_root scope);
(Concurrent.Scheduler.create [@mode portable])
~spawn:(fun scope #{ fn; affinity; name } r ->
spawn
scope
#{ fn =
(fun scope ctx concurrent r ->
Scheduler.with_heartbeat (fun () -> fn scope ctx concurrent r [@nontail])
[@nontail])
; affinity
; name
}
r)
;;
let concurrent scheduler await f =
let concurrent = (Concurrent.create [@mode portable]) await ~scheduler in
f concurrent [@nontail]
;;
let parallel scheduler f =
let f = (Obj.magic_portable [@mode once]) f in
let await =
(Await.Expert.create [@alloc stack])
~sync:Sync.blocking
~terminator:Terminator.unkillable
in
let concurrent = (Concurrent.create [@mode portable]) await ~scheduler in
(Concurrent.spawn_join [@mode unique portable])
concurrent
()
~f:(fun _ (parallel : Parallel_kernel.t) concurrent ->
Result.Capsule.globalize
(Result.Capsule.try_with (fun () -> f parallel concurrent)) [@nontail])
|> Result.Capsule.unwrap_ok_exn
;;