Skip to content

Commit 45a5554

Browse files
committed
Fix await: wake up device to finish work if it's "ready"; remove spurious ifs
1 parent 57d8c49 commit 45a5554

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

arrayjit/lib/backends.ml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,11 @@ module Multicore_backend (Backend : No_device_backend) : Backend = struct
213213
let d = device.state in
214214
if (not @@ is_idle device) && d.keep_spinning then (
215215
Mut.lock d.mut;
216-
if (not @@ is_idle device) && d.keep_spinning then (
217-
while (not @@ is_idle device) && d.keep_spinning do
218-
Stdlib.Condition.wait d.host_wait_for_idle d.mut
219-
done);
216+
while (not @@ is_idle device) && d.keep_spinning do
217+
(* If the device "is ready", it needs to be woken up first to finish the work. *)
218+
if d.is_ready then Stdlib.Condition.broadcast d.dev_wait_for_work;
219+
Stdlib.Condition.wait d.host_wait_for_idle d.mut
220+
done;
220221
Mut.unlock d.mut;
221222
Option.iter d.device_error ~f:(fun e ->
222223
Exn.reraise e @@ name ^ " device " ^ Int.to_string device.ordinal))
@@ -258,13 +259,12 @@ module Multicore_backend (Backend : No_device_backend) : Backend = struct
258259
match Queue.pop_opt state.queue with
259260
| None ->
260261
Mut.lock state.mut;
261-
if is_dev_queue_empty state && state.keep_spinning then (
262-
state.is_ready <- true;
263-
while is_dev_queue_empty state && state.keep_spinning do
264-
Stdlib.Condition.broadcast state.host_wait_for_idle;
265-
Stdlib.Condition.wait state.dev_wait_for_work state.mut
266-
done;
267-
state.is_ready <- false);
262+
state.is_ready <- true;
263+
Stdlib.Condition.broadcast state.host_wait_for_idle;
264+
while is_dev_queue_empty state && state.keep_spinning do
265+
Stdlib.Condition.wait state.dev_wait_for_work state.mut
266+
done;
267+
state.is_ready <- false;
268268
Mut.unlock state.mut
269269
| Some task -> Tnode.run task
270270
done

0 commit comments

Comments
 (0)