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#

Upper bound for the number of stored blocks, used for dimensioning launches. For compact matrices this is also the number of active non-zero blocks. See also nnz_sync().

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.

Type:

Array[int]

values#

Array of size at least equal to nnz containing block values.

Type:

Array[BlockType]

__init__()#

Methods

__init__()

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 non-zero block 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 non-zero block from the compressed row offsets.

Parameters:

out (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 non-zero block count. For padded matrices, this is the total row-capacity storage size, not necessarily the active non-zero 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.

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 a CPU APIC 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 non-zero 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.