pub struct SparseSynapticMap<const N: usize> {
pub row_ptr: Vec<usize>,
pub col_indices: Vec<u16>,
pub values: Vec<f32>,
}Expand description
Compressed Sparse Row representation of the synaptic weight matrix.
Generic over N (number of neurons) to support both small channel routers
and full 2048-neuron routing fabrics.
N must be at most MAX_SPARSE_NEURONS (65,536): target index
65,535 is representable as u16, target 65,536 is not. Constructors
and insertion APIs reject out-of-range or non-u16 indices rather than
truncating them into a different valid connection.
Fields§
§row_ptr: Vec<usize>Row pointers: row_ptr[i] gives the start index in col_indices/values
for neuron i. Length is N + 1.
col_indices: Vec<u16>Column indices: target neuron for each non-zero entry.
values: Vec<f32>Non-zero weight values.
Implementations§
Source§impl<const N: usize> SparseSynapticMap<N>
impl<const N: usize> SparseSynapticMap<N>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create an empty sparse synaptic map with N neurons.
§Panics
Panics if N exceeds MAX_SPARSE_NEURONS. Prefer
SparseSynapticMap::try_new for a recoverable error.
Sourcepub fn try_new() -> Result<Self>
pub fn try_new() -> Result<Self>
Fallible constructor that rejects an N larger than the u16
target address space.
Sourcepub fn from_dense(matrix: &[[f32; N]; N], sparsity_threshold: f32) -> Self
pub fn from_dense(matrix: &[[f32; N]; N], sparsity_threshold: f32) -> Self
Build from a dense weight matrix (for migration from dense representations).
§Panics
Panics if N exceeds MAX_SPARSE_NEURONS.
Sourcepub fn try_from_dense(
matrix: &[[f32; N]; N],
sparsity_threshold: f32,
) -> Result<Self>
pub fn try_from_dense( matrix: &[[f32; N]; N], sparsity_threshold: f32, ) -> Result<Self>
Fallible counterpart of SparseSynapticMap::from_dense.
Sourcepub fn from_adjacency(adjacency: &[Vec<Synapse>]) -> Self
pub fn from_adjacency(adjacency: &[Vec<Synapse>]) -> Self
Build from explicit adjacency lists.
§Panics
Panics if N is unsupported, adjacency.len() is not N, or any
stored target is >= N. Prefer SparseSynapticMap::try_from_adjacency.
Sourcepub fn try_from_adjacency(adjacency: &[Vec<Synapse>]) -> Result<Self>
pub fn try_from_adjacency(adjacency: &[Vec<Synapse>]) -> Result<Self>
Fallible counterpart of SparseSynapticMap::from_adjacency.
Sourcepub fn get_row(&self, row: usize) -> impl Iterator<Item = (u16, f32)> + '_
pub fn get_row(&self, row: usize) -> impl Iterator<Item = (u16, f32)> + '_
Get all synapses for a given neuron (row).
Sourcepub fn get_weight(&self, row: usize, col: usize) -> f32
pub fn get_weight(&self, row: usize, col: usize) -> f32
Get the weight from neuron row to neuron col. Returns 0.0 if not connected.
Sourcepub fn set_weight(
&mut self,
row: usize,
col: usize,
weight: f32,
sparsity_threshold: f32,
)
pub fn set_weight( &mut self, row: usize, col: usize, weight: f32, sparsity_threshold: f32, )
Update a single synaptic weight. Creates the connection if it doesn’t exist (when weight exceeds threshold) or removes it (when weight drops below).
§Panics
Panics if row or col is >= N, or if col cannot be represented
as u16. Prefer SparseSynapticMap::try_set_weight.
Sourcepub fn try_set_weight(
&mut self,
row: usize,
col: usize,
weight: f32,
sparsity_threshold: f32,
) -> Result<()>
pub fn try_set_weight( &mut self, row: usize, col: usize, weight: f32, sparsity_threshold: f32, ) -> Result<()>
Fallible counterpart of SparseSynapticMap::set_weight.
On error the map is left unchanged.
Sourcepub fn sparsity(&self) -> f32
pub fn sparsity(&self) -> f32
Sparsity ratio: fraction of zero entries in the full $N \times N$ matrix.
The denominator is computed in u64 so N = 65_536 does not overflow
usize on 32-bit targets (65_536² = 2³²).
Sourcepub fn to_dense(&self) -> [[f32; N]; N]
pub fn to_dense(&self) -> [[f32; N]; N]
Convert back to dense matrix (for debugging / interoperability).
Sourcepub fn to_gpu_arrays(&self) -> (Vec<u32>, Vec<u32>, Vec<f32>)
pub fn to_gpu_arrays(&self) -> (Vec<u32>, Vec<u32>, Vec<f32>)
Export as GPU-ready flat arrays for kernel launch. Returns (row_ptr, col_indices, values) as owned vectors.
§Panics
Panics if a row-pointer offset exceeds u32::MAX. Prefer
SparseSynapticMap::try_to_gpu_arrays.
Sourcepub fn try_to_gpu_arrays(&self) -> Result<(Vec<u32>, Vec<u32>, Vec<f32>)>
pub fn try_to_gpu_arrays(&self) -> Result<(Vec<u32>, Vec<u32>, Vec<f32>)>
Fallible counterpart of SparseSynapticMap::to_gpu_arrays.
Trait Implementations§
Source§impl<const N: usize> Clone for SparseSynapticMap<N>
impl<const N: usize> Clone for SparseSynapticMap<N>
Source§fn clone(&self) -> SparseSynapticMap<N>
fn clone(&self) -> SparseSynapticMap<N>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more