warp.sparse.BsrMatrix#

class warp.sparse.BsrMatrix[source]#

Untyped base class for BSR and CSR matrices.

Should not be constructed directly but through functions such as bsr_zeros().

nrow#

Number of rows of blocks.

Type:

int

ncol#

Number of columns of blocks.

Type:

int

nnz#

Host-side upper bound on the number of stored block slots used by sparse operations, including for dimensioning launches. Topology-changing operations may leave this larger than the active block count, even for compact matrices. The backing arrays may have additional allocated capacity. Call nnz_sync() before using nnz as an exact count.

Type:

int

offsets#

Array of size at least 1 + nrow such that the start and capacity end indices of row r are offsets[r] and offsets[r+1], respectively.

Type:

Array[int]

row_counts#

Optional array of size at least nrow containing the active block count of each row. Active blocks of row r are stored in offsets[r]:offsets[r] + row_counts[r]. For compact matrices, row_counts is None, in which case all storage in each row is active.

Type:

Array[int] | None

columns#

Array of size at least equal to nnz containing block column indices. Entries outside active row ranges are not part of the matrix and may be uninitialized.

Type:

Array[int]

values#

Array of size at least equal to nnz containing block values. Entries outside active row ranges are not part of the matrix and may be uninitialized. Active entries may also be uninitialized after topology-only or structure-only operations.

Type:

Array[BlockType]

Methods

clear_status()

Clear the asynchronous sparse status code.

copy_nnz_async()

Start the asynchronous transfer of offsets[nrow] from the device offsets array to host.

nnz_sync()

Synchronize the stored block upper bound from the device offsets array to the host.

notify_nnz_changed([nnz, nnz_capacity])

Notify the matrix that sparse storage metadata changed outside warp.sparse.

status_message()

Return a human-readable message for status_sync().

status_sync()

Return the asynchronous sparse status code, synchronizing if needed.

transpose()

Return a transposed copy of this matrix.

uncompress_rows([out])

Compute the row index for each stored block slot from the compressed row offsets.

Attributes

block_shape

Shape of the individual blocks.

block_size

Size of the individual blocks, i.e. number of rows per block times number of columns per block.

device

Device on which matrix arrays are allocated.

dtype

Data type for individual block values.

requires_grad

Read-only property indicating whether the matrix participates in adjoint computations.

scalar_type

Scalar type for individual block coefficients.

scalar_values

Access the values array as a 3d scalar array.

shape

Shape of the matrix, i.e. number of rows/columns of blocks times number of rows/columns per block.

property scalar_type: Scalar[source]#

Scalar type for individual block coefficients. For CSR matrices, this is the same as the block type.

property block_shape: tuple[int, int][source]#

Shape of the individual blocks.

property block_size: int[source]#

Size of the individual blocks, i.e. number of rows per block times number of columns per block.

property shape: tuple[int, int][source]#

Shape of the matrix, i.e. number of rows/columns of blocks times number of rows/columns per block.

property dtype: type[source]#

Data type for individual block values.

property device: Device[source]#

Device on which matrix arrays are allocated.

property requires_grad: bool[source]#

Read-only property indicating whether the matrix participates in adjoint computations.

property scalar_values: array[source]#

Access the values array as a 3d scalar array.

uncompress_rows(out=None)[source]#

Compute the row index for each stored block slot from the compressed row offsets.

The method writes nnz entries. When out is omitted, it allocates an array of that length. Entries outside active row ranges are -1. When out is provided, entries beyond nnz are unchanged. For a compact matrix, use nnz_sync() before treating the result as COO row data:

nnz = matrix.nnz_sync()
rows = matrix.uncompress_rows()[:nnz]
Parameters:

out (array) – Optional one-dimensional integer output array with at least nnz elements on the matrix’s device.

Returns:

The output array.

Return type:

array

nnz_sync()[source]#

Synchronize the stored block upper bound from the device offsets array to the host.

Ensures that any ongoing transfer of offsets[nrow] from the device offsets array to the host has completed, or, if none has been scheduled yet, starts a new transfer and waits for it to complete.

Then updates the host-side nnz upper bound to match offsets[nrow], and returns it. For compact matrices, this is the active stored block count. For padded matrices, this is the total row-capacity storage size, not necessarily the active block count.

See also notify_nnz_changed().

Raises:
  • RuntimeError – If called during a live CUDA graph capture because reading offsets[nrow] requires a device-to-host transfer.

  • NotImplementedError – If called during CPU graph capture after a topology update has been recorded. Query the count after replay instead.

Return type:

int

status_sync()[source]#

Return the asynchronous sparse status code, synchronizing if needed.

The status code is sticky: sparse operations can set it, but successful operations do not clear it. Use clear_status() before an operation when checking only that operation’s status.

Raises:
  • RuntimeError – If called during a live CUDA graph capture because reading the status requires a device-to-host transfer.

  • NotImplementedError – If called during CPU graph capture, because a status update recorded in the capture is applied only on replay. Query the status after graph replay.

Return type:

int

status_message()[source]#

Return a human-readable message for status_sync().

Return type:

str

clear_status()[source]#

Clear the asynchronous sparse status code.

Return type:

None

notify_nnz_changed(nnz=None, nnz_capacity=None)[source]#

Notify the matrix that sparse storage metadata changed outside warp.sparse.

Call this after modifying offsets or the host-side nnz upper bound directly. If assigning a new offsets array, also update row_counts for padded matrices, or set it to None for compact matrices.

Parameters:
  • nnz (int | None) – New stored block count upper bound. If omitted, read from offsets[nrow] unless nnz_capacity is provided. The caller is responsible for ensuring it is greater or equal to offsets[nrow].

  • nnz_capacity (int | None) – Optional storage pre-allocation size. If omitted, default to nnz. The caller is responsible for ensuring it is greater or equal to the true offsets[nrow] value.

Return type:

None

copy_nnz_async()[source]#

Start the asynchronous transfer of offsets[nrow] from the device offsets array to host.

Deprecated; prefer notify_nnz_changed() instead, which will make sure to resize arrays if necessary.

Return type:

None

transpose()[source]#

Return a transposed copy of this matrix.