Poplar and PopLibs
SparseStorageFormats.hpp
Go to the documentation of this file.
1// Copyright (c) 2020 Graphcore Ltd. All rights reserved.
6#ifndef _poplibs_popsparse_SparseStorageFormats_hpp_
7#define _poplibs_popsparse_SparseStorageFormats_hpp_
8
9#include <vector>
10
11namespace popsparse {
12
13struct Block {
14 // Number of columns in a block
15 std::size_t getNumColumnsInBlock() const { return blockDimensions[1]; }
16 // Number of rows in a block
17 std::size_t getNumRowsInBlock() const {
18 return blockDimensions[0];
19 ;
20 }
21 // Block size
22 std::size_t getBlockSize() const {
23 return blockDimensions[0] * blockDimensions[1];
24 }
25
26 // Block dimensions
27 std::array<std::size_t, 2> getBlockDimensions() const {
28 return blockDimensions;
29 }
30
31protected:
32 std::array<std::size_t, 2> blockDimensions;
33};
34
38template <typename T> struct COOMatrix : Block {
40 std::vector<T> nzValues;
41
43 std::vector<std::size_t> columnIndices;
44
46 std::vector<std::size_t> rowIndices;
47
48 COOMatrix(const std::vector<T> &nzValues,
49 const std::vector<std::size_t> &columnIndices,
50 const std::vector<std::size_t> &rowIndices,
51 const std::array<std::size_t, 2> &blockDimensions = {1, 1})
54 Block::blockDimensions = blockDimensions;
55 }
56
57 COOMatrix(std::vector<T> &&nzValues, std::vector<std::size_t> &&columnIndices,
58 std::vector<std::size_t> &&rowIndices,
59 const std::array<std::size_t, 2> &blockDimensions = {1, 1})
62 Block::blockDimensions = blockDimensions;
63 }
64
66 COOMatrix(std::size_t numNZValues,
67 const std::array<std::size_t, 2> &blockDimensions_ = {1, 1}) {
68 nzValues.resize(numNZValues);
69 rowIndices.resize(numNZValues);
70 columnIndices.resize(numNZValues);
71 Block::blockDimensions = blockDimensions_;
72 }
73
74 COOMatrix(const std::array<std::size_t, 2> &blockDimensions = {1, 1}) {
75 Block::blockDimensions = blockDimensions;
76 }
77 COOMatrix(const COOMatrix &) = default;
78};
79
85template <typename T> struct CSCMatrix : Block {
88 std::vector<T> nzValues;
89
93 std::vector<std::size_t> columnIndices;
94
97 std::vector<std::size_t> rowIndices;
98
99 CSCMatrix(const std::vector<T> &nzValues,
100 const std::vector<std::size_t> &columnIndices,
101 const std::vector<std::size_t> &rowIndices,
102 const std::array<std::size_t, 2> &blockDimensions = {1, 1})
105 Block::blockDimensions = blockDimensions;
106 }
107
108 CSCMatrix(std::vector<T> &&nzValues, std::vector<std::size_t> &&columnIndices,
109 std::vector<std::size_t> &&rowIndices,
110 const std::array<std::size_t, 2> &blockDimensions = {1, 1})
113 Block::blockDimensions = blockDimensions;
114 }
115
117 CSCMatrix(std::size_t numNZValues, std::size_t numColumns,
118 const std::array<std::size_t, 2> &blockDimensions_ = {1, 1}) {
119 nzValues.resize(numNZValues);
120 rowIndices.resize(numNZValues);
121 columnIndices.resize(numColumns + 1);
122 Block::blockDimensions = blockDimensions_;
123 }
124
125 CSCMatrix(const std::array<std::size_t, 2> &blockDimensions_ = {1, 1}) {
126 Block::blockDimensions = blockDimensions_;
127 }
128 CSCMatrix(const CSCMatrix &) = default;
129};
130
136template <typename T> struct CSRMatrix : Block {
138 std::vector<T> nzValues;
139
142 std::vector<std::size_t> columnIndices;
143
146 std::vector<std::size_t> rowIndices;
147
148 CSRMatrix(const std::vector<T> &nzValues,
149 const std::vector<std::size_t> &columnIndices,
150 const std::vector<std::size_t> &rowIndices,
151 const std::array<std::size_t, 2> &blockDimensions = {1, 1})
154 Block::blockDimensions = blockDimensions;
155 }
156
157 CSRMatrix(std::vector<T> &&nzValues, std::vector<std::size_t> &&columnIndices,
158 std::vector<std::size_t> &&rowIndices,
159 const std::array<std::size_t, 2> &blockDimensions = {1, 1})
162 Block::blockDimensions = blockDimensions;
163 }
164
165 // Constructor to allocate memory
166 CSRMatrix(std::size_t numNZValues, std::size_t numRows,
167 const std::array<std::size_t, 2> &blockDimensions_ = {1, 1}) {
168 nzValues.resize(numNZValues);
169 columnIndices.resize(numNZValues);
170 rowIndices.resize(numRows + 1);
171 Block::blockDimensions = blockDimensions_;
172 }
173
174 CSRMatrix(const std::array<std::size_t, 2> &blockDimensions_ = {1, 1}) {
175 Block::blockDimensions = blockDimensions_;
176 }
177 CSRMatrix(const CSRMatrix &) = default;
178};
179
180} // namespace popsparse
181#endif // _poplibs_popsparse_SparseStorageFormats_hpp_
@ Block
Sparsity is defined at a block level.
Support for sparse matrices.
Definition: codelets.hpp:8
Block Sparse matrix stored as coordinate (COO) or triplets format.
Definition: SparseStorageFormats.hpp:38
COOMatrix(std::size_t numNZValues, const std::array< std::size_t, 2 > &blockDimensions_={1, 1})
Constructor to allocate memory.
Definition: SparseStorageFormats.hpp:66
std::vector< std::size_t > rowIndices
Corresponding row indices for the non-zero values.
Definition: SparseStorageFormats.hpp:46
std::vector< T > nzValues
The non-zero values of the sparse matrix.
Definition: SparseStorageFormats.hpp:40
std::vector< std::size_t > columnIndices
Corresponding column indices for the non-zero values.
Definition: SparseStorageFormats.hpp:43
Sparse matrix stored in compressed sparse columns (CSC) format for a matrix of size [M x N].
Definition: SparseStorageFormats.hpp:85
std::vector< T > nzValues
The non-zero values of the sparse matrix.
Definition: SparseStorageFormats.hpp:88
std::vector< std::size_t > rowIndices
The row index of each block in nzValues.
Definition: SparseStorageFormats.hpp:97
CSCMatrix(std::size_t numNZValues, std::size_t numColumns, const std::array< std::size_t, 2 > &blockDimensions_={1, 1})
Constructor to allocate memory.
Definition: SparseStorageFormats.hpp:117
std::vector< std::size_t > columnIndices
Indices where non-zero values for each column block starts.
Definition: SparseStorageFormats.hpp:93
Sparse matrix stored in compressed sparse rows (CSR) format for a matrix of size [M x N].
Definition: SparseStorageFormats.hpp:136
std::vector< T > nzValues
The non-zero values of the sparse matrix.
Definition: SparseStorageFormats.hpp:138
std::vector< std::size_t > rowIndices
Indices where non-zero blocks of each row start.
Definition: SparseStorageFormats.hpp:146
std::vector< std::size_t > columnIndices
The column index of each block in nzValues.
Definition: SparseStorageFormats.hpp:142