Raster memory layout#

This page defines cugeo-raster’s cross-language raster-memory vocabulary. It expands the compact memory rules in the public API contract; the pixel contract remains authoritative for sampling, nodata, validity, and numerical behavior.

Layout support does not imply that a decoder accepts that layout; check the capability matrix for decoder limits. The separate bounded resource API can create a pinned-host CUDA pool when the toolkit supports it.

One logical model, three common layouts#

Every raster is indexed logically as (row, column, band), regardless of its physical layout or the axis order displayed by a language binding. For a sample size S, the byte address of a logical sample is:

data + row * row_stride_bytes
     + column * pixel_stride_bytes
     + band * band_stride_bytes

data is the address of logical sample (0, 0, 0) for this view. It may point inside a larger allocation, for example at the first sample of a positive- stride crop. extent_bytes is the accessible forward byte range beginning at data; it is not necessarily the size of, or distance from, the owning allocation’s original base. No operation may access bytes before data merely because the owner has storage there.

The examples below use a 2 row x 3 column x 2 band UInt16 raster, so S = 2. r0c1b0, for example, means logical sample (0, 1, 0). Each bracket is one UInt16 sample.

BIP: band interleaved by pixel#

BIP stores all bands of a pixel together. Python presents it as CHW with strides (S, width*bands*S, bands*S) for tight packing.

row 0: [r0c0b0][r0c0b1] [r0c1b0][r0c1b1] [r0c2b0][r0c2b1]
row 1: [r1c0b0][r1c0b1] [r1c1b0][r1c1b1] [r1c2b0][r1c2b1]
             bands vary fastest, then columns, then rows ->

Tightly packed BIP has:

band_stride_bytes  = S                       = 2
pixel_stride_bytes = bands * S               = 4
row_stride_bytes   = width * pixel_stride    = 12
minimum extent                                  24 bytes

Pitched BIP retains the same band and pixel strides but permits row padding. With row_stride_bytes = 16:

row 0: 12 sample bytes | 4 padding bytes
row 1: 12 sample bytes | trailing allocation padding, if any
minimum extent = 16 + 8 + 2 + 2 = 28 bytes

A 32-byte allocation is natural for two complete 16-byte row slots, but the view needs only 28 accessible bytes because bytes after its final sample are not addressed. Row padding is pitch, not a layout conversion.

BIL: band interleaved by line#

BIL stores one row of one band, followed by the same row of the next band. Python presents it as CHW with strides (width*S, bands*width*S, S) for tight packing.

row 0: [r0c0b0][r0c1b0][r0c2b0] [r0c0b1][r0c1b1][r0c2b1]
row 1: [r1c0b0][r1c1b0][r1c2b0] [r1c0b1][r1c1b1][r1c2b1]
             columns vary fastest, then bands, then rows ->

Tightly packed BIL has:

pixel_stride_bytes = S                       = 2
band_stride_bytes  = width * S               = 6
row_stride_bytes   = bands * band_stride     = 12
minimum extent                                  24 bytes

A pitched BIL example uses band_stride_bytes = 8 and row_stride_bytes = 16. Each band line has two padding bytes. The minimum extent is 16 + 4 + 8 + 2 = 30 bytes; two complete row slots occupy 32 bytes.

BSQ: band sequential#

BSQ stores a complete band plane before the next. Its usual array presentation is CHW (channels, height, width).

band 0: [r0c0b0][r0c1b0][r0c2b0] [r1c0b0][r1c1b0][r1c2b0]
band 1: [r0c0b1][r0c1b1][r0c2b1] [r1c0b1][r1c1b1][r1c2b1]
             columns vary fastest, then rows, then bands ->

Tightly packed BSQ has:

pixel_stride_bytes = S                       = 2
row_stride_bytes   = width * S               = 6
band_stride_bytes  = height * row_stride     = 12
minimum extent                                  24 bytes

A pitched BSQ example uses row_stride_bytes = 8 and band_stride_bytes = 16. Each physical row has two padding bytes and each plane occupies two 8-byte row slots. The minimum extent is 8 + 4 + 16 + 2 = 30 bytes; two complete plane slots occupy 32 bytes.

These recognized relationships are summarized below. Equality denotes tight packing; >= admits padding between the next larger units.

Class

pixel_stride_bytes

row_stride_bytes

band_stride_bytes

BIP

bands * S

>= width * pixel_stride

S

BIL

S

>= bands * band_stride

>= width * S

BSQ

S

>= width * S

>= height * row_stride

general strided

stride tuple does not match a recognized class

positive and sample-aligned

caller guarantees disjoint writable samples

Strides, not a caller-supplied label, are authoritative. A larger pitch does not change BIP, BIL, or BSQ into another layout. Conversely, a label cannot make incompatible strides belong to that class.

Extent, overflow, and writable injectivity#

For a nonempty positive-stride view, the minimum accessible extent is:

(height - 1) * row_stride_bytes
+ (width - 1) * pixel_stride_bytes
+ (bands - 1) * band_stride_bytes
+ S

Validation checks every multiplication and addition, including construction of the addressed pointer range, without wrapping size_t. The resulting minimum must not exceed extent_bytes. In ABI v1, dimensions are nonzero; data is non-null; samples are native-endian; and the pointer and every stride are aligned to S. Strides are positive and at least S. Zero, negative, element-misaligned, and backward-reaching views are rejected.

Extent safety is necessary but does not prove that writes are safe. For a writable view, the address map must not give the same or overlapping S-byte range to two distinct (row, column, band) tuples. For example, UInt8 with height=2, width=2, bands=1, row_stride=1, and pixel_stride=1 fits in three bytes, but (0, 1, 0) and (1, 0, 0) alias; it is not a valid writable view.

Writable samples occupy disjoint byte ranges. Inputs and outputs, separate outputs and in-flight operations must be disjoint where the operation forbids overlap. Read-only views retain their supported alias domain. Violations have no guaranteed result or diagnostic. Caller-chosen mutable Rust stride constructors are unsafe; their Safety sections specify this obligation. Checked extent, alignment, lifetime, codec integrity and resource admission remain enforced.

Interior logical origins#

Consider a tight UInt16 C3 BIP owner with width 8 and row stride 48 bytes. A 2 x 3 view beginning at owner coordinate (row=4, column=2) has:

data = owner_data + 4 * 48 + 2 * 6
width = 3, height = 2, bands = 3
row_stride = 48, pixel_stride = 6, band_stride = 2
minimum extent = 48 + 12 + 4 + 2 = 66 bytes

The descriptor’s extent_bytes must cover at least those 66 bytes forward from the view’s data. Supplying the owner’s total allocation size as though it began at the interior pointer is incorrect; the available forward extent must be reduced by the logical-origin offset.

Single-band classification#

For bands == 1, BIP and BSQ interleave order is physically unobservable: there is no second band to place beside a pixel or in another plane. A view with contiguous columns (pixel_stride_bytes = S) and an adequate row pitch must therefore not be presented as evidence of one interleave policy over the other. Current diagnostics classify the recognized case as single-band-packed and report row pitch separately. The unused band stride still has to satisfy ABI validation, but it does not affect any addressed sample when the band dimension is one.

Residency and ownership#

The shared vocabulary has three residency values:

Residency

Meaning

Current ABI v1 behavior

host

CPU-addressable pageable or otherwise ordinary host memory

host entry points borrow it synchronously

pinned-host

CPU-addressable host memory page-locked for CUDA transfer/integration

accepted by applicable host views/operations; the optional bounded resource API can create a toolkit-supported host-location CUDA pool

cuda-device

memory addressed in a specific CUDA device context

CUDA and accelerated-codec entry points borrow it asynchronously

The current cugeo_raster_raster record has no residency field. Residency is selected by the operation: passing a host pointer to a CUDA entry point, or a device pointer to a host entry point, does not relabel it. CUDA pointers are validated in their owning current context and are never dereferenced by host validation. pinned-host says that host pages are locked; it does not by itself promise device mapping, direct kernel access, asynchronous transfer, or zero copy.

Caller-owned allocations are the stable floor. A synchronous host call borrows them for the call. An asynchronous CUDA operation borrows every source, destination, auxiliary buffer, stream, and foreign owner until completion. Typed raster descriptors carry or unambiguously derive the residency value; they do not transfer ownership. Generic native allocator ownership is explicit and separate from raster-view ownership.

out wins#

Ordinary CUDA reads convert source-typed sampled device pixels into any native destination dtype through the shared pixel-finishing stage. Conversion retains the source scratch and whole output backing through completion; no decoded pixels cross host memory. Identity reads keep their direct codec paths.

Ordinary host TIFF fractional/clipped sampling and integer boundless nearest write into validated positive NumPy strides at any of the six native dtypes. Sampling retains source-type rounding before destination conversion. Sampling retains an integer decode ROI with its physical-image halo and origin; the output footprint stays fractional. Its budget charges decoded backing, band maps and the native decode plan. Integer storage-only conversion runs during bounded chunk scatter. Bounded explicit fills report a conservative mask-work high-water bound; their allocation count is unknown. Receipts report the staging; no NumPy pixel engine or implicit caller-output repacking runs.

Whenever an API accepts caller-provided out, its validated dimensions, dtype, strides, extent, and residency are the requested destination. A layout argument is an allocation preference only when cugeo-raster is allocating output; it cannot silently transpose, replace, or copy back into out.

A read plan tied to caller-owned out derives the destination layout class and pitch from out’s authoritative validated strides. It records that derived layout and plans against those exact strides. A caller-supplied layout label or allocation preference cannot override them: out wins. If no supported direct or explicit-conversion route reaches that destination, planning rejects the request rather than substituting another output allocation.

The only valid planning outcomes are:

  • produce directly into out;

  • consume out directly when it is an input to a compute stage;

  • report a named intermediate and explicit conversion into out; or

  • reject the request with an unsupported or invalid-layout result.

The APIs have no automatic backend router. Therefore a packed-only adapter rejects an incompatible out; it does not secretly allocate its preferred layout. Typed plans make the chosen direct or unsupported route inspectable. The explicit synchronous host layout-copy operation is not invoked implicitly by an I/O or compute entry point.

Examples:

  • Direct host read: native TIFF, including the qualified portable LZW/Deflate floor, can scatter samples into a valid caller-owned positive-stride host view, including BIP, BIL, BSQ, or an accepted general layout. Partial or endian-swapped source rows may use bounded staging, so “direct to out” does not by itself mean zero-copy from storage.

  • Direct compute: the general affine sampler reads and writes its validated positive-stride raster views without first imposing a preferred BIP/BSQ label. Eligible packed identity cases may use a pitched copy.

  • Accelerated restriction: the nvTIFF adapter’s qualified multi-region lane writes unchanged UInt8 RGB or UInt16 C3 full/odd-ROI/edge-ROI data from a classic little-endian stripped, uncompressed contiguous base IFD to packed interleaved device output. A compressed/tiled/overview source, BSQ, or arbitrary-stride out is unsupported in that lane. Scalar and prepared reads also accept classic little-endian tiled BlackIsZero UInt16 C1 Deflate with predictor 2 and tile dimensions that are multiples of 16. See the capability matrix for the supported nvTIFF profiles.

Explicit backend requests never fall back to another codec or to host execution. No auto policy chooses among routes; one may do so only when both the planned and the actual route are observable.

Explicit host layout copy#

The host operation copies unchanged logical samples between two validated caller-owned raster views. Rust and C accept host or pinned-host residency; Python accepts host NumPy arrays. Source and destination must have identical nonzero dimensions and dtype, positive sample-aligned strides, sufficient forward extents, and non-overlapping accessible spans. Both views are completely validated before the destination is modified. The caller guarantees disjoint input and output; aliasing is not checked. There is no backend fallback.

Strides remain authoritative. The operation supports layout-equivalent direct copies, BIP-to-BSQ and BSQ-to-BIP conversion, and valid general-strided host views. Single-band storage uses the physically equivalent direct-copy path. Specialized BIP/BSQ C3/C4/C8 paths and generic paths are observable in the receipt; their presence is an implementation fact rather than a wider semantic contract. The operation preserves native UInt8, UInt16, or Float32 sample bits and performs no dtype, color, radiometric, or endian conversion.

The Python API is:

result = cugeo.raster.lowlevel.copy_layout(
    source,
    out=destination,
)

Python images use HW or CHW for every physical packing. Strides determine the physical class. When supplied, out defines shape, dtype, strides and residency; result.array is that exact object. With out=None, the host copy allocates a C-contiguous array with the source’s logical shape, retaining explicit C=1.

result.receipt records exact source/destination layout detail and residency, backend and selected path, logical/output/conversion/copy bytes, allocation count and bytes, zero host/device transfer counters, and native execution time. A BIP/BSQ conversion reports the logical payload in both copy_bytes and conversion_bytes; a layout-equivalent copy reports it only in copy_bytes. Native Rust/C operation-owned allocation counters are zero. Python reports its one output allocation when out=None, while a caller-owned out reports zero. Receipt allocation counters do not measure caller allocations, allocator metadata, or process RSS.

The host layout-copy benchmark documents the correctness-first measurement protocol and its qualification boundary.

What “zero-copy” means#

Zero-copy is meaningful only with a named producer-to-consumer boundary. For cugeo-raster, a zero-copy handoff means that the consumer reads the same caller- owned payload allocation and logical samples that the producer wrote, with no intervening payload relocation, repacking, layout conversion, dtype/color/ radiometric conversion, or host/device transfer. Metadata construction, descriptor arrays, codec scratch, and synchronization do not move that payload and are reported separately; their existence neither proves nor disproves the payload handoff.

Thus a qualified decoder writing pitched BIP into a caller device allocation, followed by a BIP-capable kernel reading that same pointer and strides on the same explicit stream, can be a zero-copy decode-to-compute handoff. Row padding does not defeat the claim. The claim does not mean the compressed file bytes were memory-mapped as pixels, that decoding performed no work, that the codec used no scratch, or that storage reached the GPU through GDS.

These are not zero-copy for the named handoff:

  • decoding into an internal BIP buffer and then using cudaMemcpy2D into the caller’s BIP buffer, even though layout and dtype are unchanged;

  • decoding BIP and transposing or repacking into BSQ, whether the conversion is explicit, hidden in a binding, or fused with another copy;

  • reading/decompressing into host memory and then copying the pixels to a device destination;

  • materializing a contiguous array from a strided Python view before calling native code, then copying results back;

  • calling device-resident output “zero-copy from storage” without evidence that there was no host staging or storage-to-device transfer.

A transpose view that changes only metadata can still be zero-copy as an array operation, but only if the downstream cugeo-raster operation accepts those exact strides and directly consumes the same allocation. If it materializes the view, the handoff is no longer zero-copy. Plans and receipts scope any zero-copy claim and separately report layout-conversion, H2D, D2H, and D2D-copy bytes.

Contract and boundaries#

Behavior includes public LayoutClass/Residency vocabulary, stride-derived layout classification (including single-band-packed), caller-guaranteed disjoint writable samples with positive sample-aligned strides, typed metadata/read-plan records, and equivalent safe Rust types. A read plan tied to caller-owned output derives layout from that output’s strides. Native uncompressed and qualified LZW/Deflate TIFF scatters into host BIP, BIL, BSQ, and accepted general strides. Explicit host layout copy supports that broad host stride contract. The additive CUDA layout-copy primitive supports positive sample-aligned strides, retaining direct and specialized BIP/BSQ paths and a generic strided path. A separate pitched-BIP CUDA-runtime owner is available. Complete-image CUDA writers may use the shared copy to pack incompatible encoder input into an admitted image-sized device allocation, reporting conversion, D2D bytes and managed device high water. Other I/O operations do not select implicit layout conversion. The narrow nvTIFF and nvImageCodec decode boundaries are listed in the capability matrix.

General pinned-host allocation, implicit codec-to-layout routing, arbitrary-stride accelerated decode, and GDS are absent. A separately gated experimental RMM 26.8 adapter is present; it is not a default, no automatic scheduling selects it, and it is not qualified for release packaging. Its evidence boundary is documented in bounded memory resources.