Streams

A Stream is an asynchronous work queue. A stream issues operations to the device in enqueue order. It does not wait for an operation to complete before issuing the next one. Operations on different streams may run concurrently.

Every Map::execute() call goes through a stream. If you do not bind one explicitly, the Map uses its parent Job’s default stream.


Ordering and Completion

  • Issue order is guaranteed. Every task of an operation reaches the device before any task of a later operation on the same stream.
  • Completion order is not. A later operation can start while an earlier one is still running, and they may finish in either order.
  • Synchronize to express a dependency. When a Map reads another Map’s output, call synchronize() on the producer before executing the consumer. Launcher stage chains already do this between stages.
producer->execute(buffer);
producer->synchronize();  // waits for completion, not only for issue
consumer->execute(buffer);

Default Stream - One per Job

When you create a Job, PXL allocates a private default stream for it. Every Map built from that Job uses this default stream unless the Map explicitly binds another.

flowchart LR
    subgraph JobA[Job A]
        MapA1[Map a1] --> StreamA[default stream]
        MapA2[Map a2] --> StreamA
    end
    subgraph JobB[Job B]
        MapB1[Map b1] --> StreamB[default stream]
    end
    StreamA --> Device((Device))
    StreamB --> Device

Consequences:

  • Each Job submits work through its own default stream.
  • Maps built from the same Job share that Job’s default stream and are dispatched in enqueue order on the host side.

To give two Maps separate host submission queues, bind one Map to a custom stream as shown below.


Custom Streams

Create a stream with pxl::createStream() and bind it to a Map:

auto stream = pxl::createStream();

map->setStream(stream);             // bind a custom stream
map->execute(/* args */);
map->synchronize();

// Reset to the Job's default stream
map->setStream();

pxl::destroyStream(stream);

A stream can serve multiple executions and multiple Maps. The stream submits queued work in enqueue order. Wait for a Map to finish before changing its stream binding. Map::setStream() rejects changes while the Map is running.


In-flight Limit and Backpressure

A stream can hold only a bounded number of in-flight operations. When the queue is full, Map::execute() blocks until a slot frees up. The queue therefore limits how quickly the host can submit more work.

execute() returns an error if the stream stops or its consumer exits while submission waits. Check for any result other than Result::Success.


When to Add More Streams

Streams parallelize the host-side dispatch path. They do not multiply device throughput.

  • A Job’s device throughput is bounded by the number of Subs it owns. Adding more streams to the same Job does not exceed that bound.
  • Multiple streams help when host-side argument setup or kernel-launch overhead is the bottleneck and there is room for more host concurrency.
  • To increase device parallelism, create the Job with more Subs (Context::createJob(numSub)) or create more Jobs — not more streams.

Measure throughput before adding streams. A stream changes host submission concurrency, while Subs determine the available device cores. The runtime does not impose a streams-per-Sub ratio. Use xtop to compare host submission delays and device execution time for the workload.


→ Related: Programming Objects, Kernel Execution