Re:Efficient Jagged Arrays
by gautamcgoel on Monday July 03, 2023 @09:41AM
(#36572750)
Thanks for this comment, was super interesting! Apparently Matlab uses a similar format (Compressed Sparse Column) to represent sparse matrices.
Have you tried using precomputed byte offsets rather than indices for all the integers? Those would definitely speed up the non-power-of-2-sized case, but I'd be curious if they might speed this up a little bit too, since they should remove some bit shifting instructions.
Re:Efficient Jagged Arrays
by patagurbon on Tuesday July 04, 2023 @08:56AM
(#36586038)
You often need the indices for other operations (indexing into other arrays). CSC it's transpose CSR, and the doubly compressed forms DCSC/DCSR, are optimized for linear algebra for the most part.
I’m fairly sure anecdotes like this inform the conversation about structs of arrays versus arrays of structs.
There are a couple of languages that let you treat them the same way. Having list comprehensions (plural) over multidimensional data would be handy.
- a single array of floats with all the nonzero matrix entries, ordered row-by-row:
- a single array of ints with the corresponding column index: - a single array of ints, of length (m+1), with the start of each row in the two other arrays (the (m+1)th element stores nnz, which will be useful later): So to print a whole matrix, you do this: To a modern programmer, this may look very old-fashioned (Fortran-ish), but I have tried many alternatives (like grouping columns and values together in a struct, or using pointers), and in my numerical codes, nothing gets even close in terms of performance. It must be said that CSR and CSC (its transpose) are very compact in memory, which is often a bottleneck. Also, since so many sparse numerical codes in benchmarks have used this representation for decades, it could be that CPU designers make sure that it works well.[1] https://en.wikipedia.org/wiki/Sparse_matrix#Compressed_spars...