@@ -50,7 +50,8 @@ def __init__(self,
5050 stride : str ,
5151 region : List [AbstractInstruction ],
5252 lookahead : int = 2 ,
53- flags : FlagMode = FlagMode .OPTIONAL ):
53+ flags : FlagMode = FlagMode .OPTIONAL ,
54+ queue_depth : int = 1 ):
5455 super ().__init__ (context )
5556 self ._section_index = section_index
5657 self ._mode = mode
@@ -71,6 +72,18 @@ def __init__(self,
7172 self ._stage_depth : Optional [int ] = None
7273 # ids of leading region instructions emitted outside the flag guard
7374 self ._unguarded : set = set ()
75+ # `LAUNCHCTRL` only: how many cancel requests are kept in flight. One
76+ # is enough to hide the queue's own latency behind the body, because
77+ # the request for the next element is posted before the current one is
78+ # computed. It is *not* enough to prefetch the next element's data:
79+ # at depth one the next index arrives at the bottom of the iteration,
80+ # with no body left to overlap a transfer with. Two is the first
81+ # depth at which the index is known at the top.
82+ if mode is LoopMode .LAUNCHCTRL and queue_depth < 1 :
83+ raise InternalError (
84+ f'the work queue needs at least one request in flight, '
85+ f'got { queue_depth } ' )
86+ self ._queue_depth = queue_depth
7487 self ._is_ready = True
7588
7689 # -- structure ------------------------------------------------------- #
@@ -116,8 +129,16 @@ def uniform_scope(self) -> BarrierScope:
116129 guard. The tail block skips the body entirely, so a grid barrier there
117130 deadlocks.
118131
119- ``LAUNCHCTRL``: the queue hands out work per block, and the size guard
120- is the same.
132+ ``LAUNCHCTRL``: the *loop* is the exception -- its trip count is
133+ block-uniform. Every thread reads the same cancel response out of
134+ shared memory behind a barrier, so all of them leave on the same
135+ iteration, and the hand-off itself contains a ``__syncthreads``. What
136+ is still only ``SIMD``-uniform is the size guard *inside* it, which is
137+ per element exactly as in the other two modes. Answering ``GROUP``
138+ here would license a block barrier that ``_emit_body`` may then place
139+ under that guard, so the loop keeps the conservative answer until the
140+ guard is a construct ``verify`` can see; the barrier the queue needs
141+ for itself sits outside the guard where the emission puts it.
121142 """
122143 return BarrierScope .SIMD
123144
@@ -504,10 +525,30 @@ def gen_code(self, writer) -> None:
504525 def _structured_loop (self , writer ) -> bool :
505526 """Should this loop be an `Op.FOR` rather than Writer text?
506527
507- `PERSISTENT` only, for now. `LAUNCHCTRL` is a `while` with a break and
508- a queried successor, which is not a counted loop and wants its own
509- design; the remaining mode has no loop at all, so there is nothing to
510- put in the IR.
528+ `PERSISTENT` only, for now. The remaining mode has no loop at all, so
529+ there is nothing to put in the IR.
530+
531+ `LAUNCHCTRL` is the open one, and it is worth stating what it needs
532+ rather than that it needs something. Everything the IR would buy here
533+ hangs off one missing form: a loop whose successor is *queried*, not
534+ incremented. `for_` hands the body `loop.induction` because it
535+ computed it; the queue's next index is the result of a statement
536+ inside the body -- `cursor.next(queue)` -- and the loop ends when that
537+ statement answers -1. Give the builder that form and the rest follows
538+ at once: `batchId0` becomes a value, so the size guard becomes an
539+ `Op.IF` on it instead of `writer.If` over text and the body's reads of
540+ it are operands; the hand-off becomes an `Op.CALL` carrying
541+ `Effect.BARRIER` and its shared accesses, so the barrier it contains
542+ stops being invisible to every pass; and the region the passes walk
543+ becomes the whole loop rather than the part between the braces.
544+
545+ Without that form each of those is cosmetic, which is why this returns
546+ False rather than half of it. The cost is a name in `Op` -- a
547+ vocabulary deliberately closed -- plus the handle in `build.py`, the
548+ spelling and the `break` in `emit.py`, and the uniformity rule in
549+ `inspect.py`. Note also that the loop is *block*-uniform, which the
550+ counted loop is not, so the form can carry a stronger answer than
551+ `uniform_scope` gives today.
511552
512553 Not when a body is already open: `shared_body` nests, and a second
513554 one would put the loop inside the body it is meant to contain.
@@ -574,20 +615,41 @@ def gen_code_inner(self, writer) -> None:
574615 self ._advance_stage_counter (writer )
575616 elif self ._mode is LoopMode .LAUNCHCTRL :
576617 self ._declare_stage_counter (writer )
577- writer (f'__shared__ tensorforge::ClusterLaunchCtrl launchctrl;' )
578- writer (f'int phase = 0;' )
579- writer (f'launchctrl.init();' )
618+ self ._declare_windows_early (writer , list (self ._region ))
619+ depth = self ._queue_depth
620+ writer (f'__shared__ tensorforge::ClusterLaunchQueue<{ depth } > '
621+ f'launchQueue{ self ._section_index } ;' )
622+ # The cursor is a *local*: it is per-thread state, and every thread
623+ # of the block advances it identically. In shared memory the
624+ # parity would be one word 128 threads flip, which is a race whose
625+ # symptom is a wait for a phase that has already gone past.
626+ writer (f'tensorforge::ClusterLaunchCursor<{ depth } > '
627+ f'launchCursor{ self ._section_index } ;' )
628+ writer (f'launchCursor{ self ._section_index } '
629+ f'.start(launchQueue{ self ._section_index } );' )
580630 writer (f'size_t { self ._batch (0 )} = { self ._block_id ()} ;' )
581- with writer .While (f'true' ):
582- writer ('launchctrl.setupNext();' )
631+ with writer .While ('true' ):
583632 with writer .If (self ._size_guard ()):
584633 self ._emit_body (writer )
585634 self ._advance_stage_counter (writer )
586- writer ('const auto nextBlock = launchctrl.queryNext(phase);' )
587- with writer .If ('!nextBlock.has_value()' ):
635+ # Outside the size guard, deliberately. `next` contains the
636+ # block barrier that separates one element's shared memory
637+ # from the next one's, and the guard is per element: the rows
638+ # of a block hold different elements, so a barrier inside it
639+ # is reached by some rows and not others. That does not
640+ # reliably deadlock -- `bar.sync` pairs arrivals by barrier
641+ # *number*, so the rows that skipped rendezvous at the next
642+ # one instead and the loop limps on one barrier out of step,
643+ # reusing the tile an iteration early. Measured both ways: a
644+ # block barrier under the guard hangs outright once a flag
645+ # mask makes the rows disagree persistently.
646+ writer (f'const int nextBlock{ self ._section_index } = '
647+ f'launchCursor{ self ._section_index } '
648+ f'.next(launchQueue{ self ._section_index } );' )
649+ with writer .If (f'nextBlock{ self ._section_index } < 0' ):
588650 writer ('break;' )
589651 writer (f'{ self ._batch (0 )} = '
590- f'{ self ._block_id ("nextBlock.value() " )} ;' )
652+ f'{ self ._block_id (f "nextBlock{ self . _section_index } " )} ;' )
591653 else :
592654 writer (f'const size_t { self ._batch (0 )} = { self ._block_id ()} ;' )
593655 with writer .If (self ._size_guard ()):
0 commit comments