﻿# rocBLAS stream switching: a handle carries workspace across the transition

Treat a rocBLAS handle stream change as a workspace-lifetime transition, checking ordinary and stream-ordered allocation rules before reusing or destroying a stream.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-rocblas-handle-stream-switch-workspace-transition/
Language: en

## Look beyond the stream field itself

A worker submits a matrix operation, changes its handle to another non-default stream and immediately submits more work. The handle can own temporary device storage shared by those submissions. Treating the setter as a harmless scheduling preference misses that resource relationship. Record the handle, its allocation mode and outstanding stream work before deciding whether the transition is safe for the exact library configuration in use.

## Follow the ordinary transition rule explicitly

The [rocBLAS programming guide](https://rocm.docs.amd.com/projects/rocBLAS/en/latest/how-to/Programmers_Guide.html) requires completion of earlier work when switching ordinary handle-managed storage between non-default streams. It also documents a stream-ordered allocation exception. Keep those cases distinct rather than turning one rule into an unconditional claim for every release and allocator. Do not assume that application buffers being separate also makes the handle's internal temporary allocation independent.

## Track association before destroying a stream

The [helper-function reference](https://rocmdocs.amd.com/projects/rocBLAS/en/latest/reference/helper-functions.html) defines handle stream setters and queries. Preserve the actual association during teardown, and update the handle through the documented sequence before destroying an old stream. A dangling association can affect later calls even after the original operation seemed successful. Check each relevant return code instead of investigating only the eventual failure in an unrelated matrix operation.

## Identify who manages temporary memory

The [device-memory allocation reference](https://rocm.docs.amd.com/projects/rocBLAS/en/latest/reference/memory-alloc.html) describes per-handle workspace management and user-controlled alternatives. Keep the active scheme visible in a reproduction because changing it changes which assumptions justify the stream transition. A copied handle identifier or reused allocation address is not proof that concurrent work has independent scratch storage or that a previous owner has completed using it.

## Test transition and arithmetic separately

Create a controlled test that alternates the selected streams while keeping matrix inputs fixed, and verify both completion and the intended teardown order. Repeat with the allocation mode explicitly recorded. The existing [stream-ordering guide](/journal/learn-stream-ordering-dependencies/) and [software reproducibility material](/journal/software-stack-reproducibility/) provide context. This separates a lifetime error during handle migration from a numerical or layout error, making the failure reproducible without relying on whichever scheduling order happened to hide it initially.
