14 template <
typename T,
typename indexT,
bool columnMajor>
16 if (vals !=
nullptr) {
19 if (innerIdx !=
nullptr) {
22 if (outerPtr !=
nullptr) {
25 if (metadata !=
nullptr) {
31 template <
typename T,
typename indexT,
bool columnMajor>
38 innerDim = mat.innerSize();
39 outerDim = mat.outerSize();
45 index_t =
sizeof(indexT);
49 vals = (T *)malloc(nnz *
sizeof(T));
50 innerIdx = (indexT *)malloc(nnz *
sizeof(indexT));
51 outerPtr = (indexT *)malloc((outerDim + 1) *
sizeof(indexT));
52 }
catch (std::bad_alloc &e) {
53 std::cerr <<
"Allocation failed: " << e.what() <<
'\n';
57 metadata =
new uint32_t[NUM_META_DATA];
59 metadata[1] = innerDim;
60 metadata[2] = outerDim;
63 metadata[5] = index_t;
66 memcpy(vals, mat.valuePtr(),
sizeof(T) * nnz);
67 memcpy(innerIdx, mat.innerIndexPtr(),
sizeof(indexT) * nnz);
68 memcpy(outerPtr, mat.outerIndexPtr(),
sizeof(indexT) * (outerDim + 1));
80 template <
typename T,
typename indexT,
bool columnMajor>
83 other.makeCompressed();
86 innerDim = other.innerSize();
87 outerDim = other.outerSize();
88 numRows = other.rows();
89 numCols = other.cols();
90 nnz = other.nonZeros();
93 index_t =
sizeof(indexT);
97 vals = (T *)malloc(nnz *
sizeof(T));
98 innerIdx = (indexT *)malloc(nnz *
sizeof(indexT));
99 outerPtr = (indexT *)malloc((outerDim + 1) *
sizeof(indexT));
100 }
catch (std::bad_alloc &e) {
101 std::cerr <<
"Allocation failed: " << e.what() <<
'\n';
105 metadata =
new uint32_t[NUM_META_DATA];
107 metadata[1] = innerDim;
108 metadata[2] = outerDim;
111 metadata[5] = index_t;
114 memcpy(vals, other.valuePtr(),
sizeof(T) * nnz);
115 memcpy(innerIdx, other.innerIndexPtr(),
sizeof(indexT) * nnz);
116 memcpy(outerPtr, other.outerIndexPtr(),
sizeof(indexT) * (outerDim + 1));
122 #ifdef IVSPARSE_DEBUG
128 template <
typename T,
typename indexT,
bool columnMajor>
134 template <
typename T,
typename indexT,
bool columnMajor>
135 template <u
int8_t compressionLevel2>
139 if constexpr (compressionLevel2 == 1) {
147 if constexpr (compressionLevel2 == 2) {
148 temp = other.
toCSC();
149 }
else if constexpr (compressionLevel2 == 3) {
150 temp = other.
toCSC();
157 #ifdef IVSPARSE_DEBUG
163 template <
typename T,
typename indexT,
bool columnMajor>
164 template <
typename T2,
typename indexT2>
166 T2 *vals, indexT2 *innerIndices, indexT2 *outerPtr, uint32_t num_rows, uint32_t num_cols, uint32_t nnz) {
168 #ifdef IVSPARSE_DEBUG
169 assert(vals !=
nullptr &&
"Values array is null");
170 assert(innerIndices !=
nullptr &&
"Inner indices array is null");
171 assert(outerPtr !=
nullptr &&
"Outer pointer array is null");
172 assert(num_rows > 0 &&
"Number of rows must be greater than 0");
173 assert(num_cols > 0 &&
"Number of columns must be greater than 0");
174 assert(nnz > 0 &&
"Number of nonzeros must be greater than 0");
178 if constexpr (columnMajor) {
191 index_t =
sizeof(indexT);
195 this->vals = (T *)malloc(nnz *
sizeof(T));
196 innerIdx = (indexT *)malloc(nnz *
sizeof(indexT));
197 this->outerPtr = (indexT *)malloc((outerDim + 1) *
sizeof(indexT));
198 }
catch (std::bad_alloc &e) {
199 std::cerr <<
"Allocation failed: " << e.what() <<
'\n';
203 metadata =
new uint32_t[NUM_META_DATA];
205 metadata[1] = innerDim;
206 metadata[2] = outerDim;
209 metadata[5] = index_t;
212 memcpy(this->vals, vals,
sizeof(T) * nnz);
213 memcpy(innerIdx, innerIndices,
sizeof(indexT) * nnz);
214 memcpy(this->outerPtr, outerPtr,
sizeof(indexT) * (outerDim + 1));
220 #ifdef IVSPARSE_DEBUG
226 template <
typename T,
typename indexT,
bool columnMajor>
230 if constexpr (columnMajor) {
231 innerDim = vec.getLength();
233 numRows = vec.getLength();
237 outerDim = vec.getLength();
239 numCols = vec.getLength();
245 index_t =
sizeof(indexT);
247 metadata =
new uint32_t[NUM_META_DATA];
249 metadata[1] = innerDim;
250 metadata[2] = outerDim;
253 metadata[5] = index_t;
262 outerPtr = (indexT *)malloc((outerDim + 1) *
sizeof(indexT));
263 }
catch (std::bad_alloc &e) {
264 std::cerr <<
"Allocation failed: " << e.what() <<
'\n';
273 vals = (T *)malloc(nnz *
sizeof(T));
274 innerIdx = (indexT *)malloc(nnz *
sizeof(indexT));
275 outerPtr = (indexT *)malloc((outerDim + 1) *
sizeof(indexT));
276 }
catch (std::bad_alloc &e) {
277 std::cerr <<
"Allocation failed: " << e.what() <<
'\n';
281 memcpy(vals, vec.getValues(),
sizeof(T) * nnz);
282 memcpy(innerIdx, vec.getInnerIndices(),
sizeof(indexT) * nnz);
289 #ifdef IVSPARSE_DEBUG
295 template <
typename T,
typename indexT,
bool columnMajor>
303 for (
size_t i = 1; i < vecs.size(); i++) { temp.
append(vecs[i]); }
311 #ifdef IVSPARSE_DEBUG
317 template <
typename T,
typename indexT,
bool columnMajor>
320 FILE *fp = fopen(filename,
"rb");
322 #ifdef IVSPARSE_DEBUG
325 std::cerr <<
"Error: Could not open file " << filename << std::endl;
331 metadata =
new uint32_t[NUM_META_DATA];
332 fread(metadata,
sizeof(uint32_t), NUM_META_DATA, fp);
333 innerDim = metadata[1];
334 outerDim = metadata[2];
337 index_t = metadata[5];
339 if constexpr (columnMajor) {
349 vals = (T *)malloc(nnz *
sizeof(T));
350 innerIdx = (indexT *)malloc(nnz *
sizeof(indexT));
351 outerPtr = (indexT *)malloc((outerDim + 1) *
sizeof(indexT));
352 }
catch (std::bad_alloc &e) {
353 std::cerr <<
"Error: Could not allocate memory for IVSparse matrix"
359 fread(vals,
sizeof(T), nnz, fp);
360 fread(innerIdx,
sizeof(indexT), nnz, fp);
361 fread(outerPtr,
sizeof(indexT), outerDim + 1, fp);
367 #ifdef IVSPARSE_DEBUG
375 template <
typename T,
typename indexT,
bool columnMajor>
376 template <
typename T2,
typename indexT2>
378 std::vector<std::tuple<indexT2, indexT2, T2>> &entries, uint32_t num_rows, uint32_t num_cols, uint32_t nnz) {
380 #ifdef IVSPARSE_DEBUG
381 assert(entries.size() > 0 &&
"Entries array is empty");
382 assert(num_rows > 0 &&
"Number of rows must be greater than 0");
383 assert(num_cols > 0 &&
"Number of columns must be greater than 0");
384 assert(nnz > 0 &&
"Number of nonzeros must be greater than 0");
387 if constexpr (columnMajor) {
401 index_t =
sizeof(indexT);
404 vals = (T *)malloc(nnz *
sizeof(T));
405 innerIdx = (indexT *)malloc(nnz *
sizeof(indexT));
406 outerPtr = (indexT *)calloc(outerDim + 1,
sizeof(indexT));
407 }
catch (std::bad_alloc &e) {
408 std::cerr <<
"Allocation failed: " << e.what() <<
'\n';
411 metadata =
new uint32_t[NUM_META_DATA];
414 metadata[1] = innerDim;
415 metadata[2] = outerDim;
418 metadata[5] = index_t;
421 std::sort(entries.begin(), entries.end(),
422 [](
const std::tuple<indexT2, indexT2, T2> &a,
423 const std::tuple<indexT2, indexT2, T2> &b) {
424 if (std::get<1>(a) == std::get<1>(b)) {
425 return std::get<0>(a) < std::get<0>(b);
427 return std::get<1>(a) < std::get<1>(b);
434 for (
size_t i = 0; i < entries.size(); i++) {
435 vals[i] = std::get<2>(entries[i]);
436 innerIdx[i] = std::get<0>(entries[i]);
439 if (std::get<1>(entries[i]) != OuterIndex) {
440 if (OuterIndex != -1) {
441 outerPtr[OuterIndex + 1] = count - 1;
443 OuterIndex = std::get<1>(entries[i]);
444 outerPtr[OuterIndex] = count - 1;
448 outerPtr[OuterIndex + 1] = count;
451 #ifdef IVSPARSE_DEBUG
Definition: CSC_SparseMatrix.hpp:24
void append(typename SparseMatrix< T, indexT, 1, columnMajor >::Vector &vec)
Definition: CSC_Methods.hpp:211
uint32_t nonZeros() const
Definition: IVSparse_Base_Methods.hpp:39
size_t byteSize() const
Definition: IVSparse_Base_Methods.hpp:42
Definition: IVCSC_SparseMatrix.hpp:29
IVSparse::SparseMatrix< T, indexT, 1, columnMajor > toCSC()
Definition: IVCSC_Methods.hpp:126
SparseMatrix()
Definition: IVCSC_SparseMatrix.hpp:99
~SparseMatrix()
Destroy the Sparse Matrix object.
Definition: IVCSC_Constructors.hpp:15