15 template <
typename T,
typename indexT,
bool columnMajor>
19 if (metadata !=
nullptr)
25 if (values !=
nullptr)
27 for (
size_t i = 0; i < outerDim; i++)
29 if (values[i] !=
nullptr)
36 if (counts !=
nullptr)
38 for (
size_t i = 0; i < outerDim; i++)
40 if (counts[i] !=
nullptr)
47 if (indices !=
nullptr)
49 for (
size_t i = 0; i < outerDim; i++)
51 if (indices[i] !=
nullptr)
59 if (valueSizes !=
nullptr)
63 if (indexSizes !=
nullptr)
70 template <
typename T,
typename indexT,
bool columnMajor>
80 outerDim = columnMajor ? numCols : numRows;
81 innerDim = columnMajor ? numRows : numCols;
87 compressCSC(mat.valuePtr(), mat.innerIndexPtr(), mat.outerIndexPtr());
91 template <
typename T,
typename indexT,
bool columnMajor>
105 nnz = mat.nonZeros();
108 compressCSC(mat.valuePtr(), mat.innerIndexPtr(), mat.outerIndexPtr());
112 template <
typename T,
typename indexT,
bool columnMajor>
116 template <
typename T,
typename indexT,
bool columnMajor>
117 template <u
int8_t otherCompressionLevel>
121 if constexpr (otherCompressionLevel == 2)
131 if constexpr (otherCompressionLevel == 1)
135 else if constexpr (otherCompressionLevel == 3)
152 template <
typename T,
typename indexT,
bool columnMajor>
153 template <
typename T2,
typename indexT2>
173 compressCSC(vals, innerIndices, outerPtr);
177 template <
typename T,
typename indexT,
bool columnMajor>
178 template <
typename T2,
typename indexT2>
204 index_t =
sizeof(indexT);
206 metadata =
new uint32_t[NUM_META_DATA];
208 metadata[1] = innerDim;
209 metadata[2] = outerDim;
212 metadata[5] = index_t;
217 values = (T **)malloc(
sizeof(T *) * outerDim);
218 counts = (indexT **)malloc(
sizeof(indexT *) * outerDim);
219 indices = (indexT **)malloc(
sizeof(indexT *) * outerDim);
220 valueSizes = (indexT *)malloc(
sizeof(indexT) * outerDim);
221 indexSizes = (indexT *)malloc(
sizeof(indexT) * outerDim);
223 catch (std::bad_alloc &ba)
225 std::cerr <<
"bad_alloc caught: " << ba.what() <<
'\n';
226 throw std::runtime_error(
"Error: Could not allocate memory");
230 std::sort(entries.begin(), entries.end(), [](
const std::tuple<indexT2, indexT2, T2> &a,
const std::tuple<indexT2, indexT2, T2> &b)
232 if (std::get<1>(a) == std::get<1>(b)) {
233 return std::get<0>(a) < std::get<0>(b);
235 return std::get<1>(a) < std::get<1>(b);
238 std::map<T2, std::vector<indexT2>> maps[outerDim];
241 for (
size_t i = 0; i < nnz; i++)
244 indexT2 row = std::get<0>(entries[i]);
245 indexT2 col = std::get<1>(entries[i]);
246 T2 val = std::get<2>(entries[i]);
249 if (maps[col].find(val) != maps[col].end())
253 maps[col][val].push_back(row);
258 maps[col][val] = std::vector<indexT2>{row};
265 #pragma omp parallel for
267 for (
size_t i = 0; i < outerDim; i++)
275 indices[i] =
nullptr;
281 size_t performanceVecSize = 0;
282 size_t numInidces = 0;
285 for (
auto &val : maps[i])
287 performanceVecSize++;
288 numInidces += val.second.size();
293 values[i] = (T *)malloc(
sizeof(T) * maps[i].size());
294 counts[i] = (indexT *)malloc(
sizeof(indexT) * maps[i].size());
295 indices[i] = (indexT *)malloc(
sizeof(indexT) * numInidces);
297 catch (std::bad_alloc &ba)
299 std::cerr <<
"bad_alloc caught: " << ba.what() <<
'\n';
300 throw std::runtime_error(
"Error: Could not allocate memory");
303 valueSizes[i] = maps[i].size();
304 indexSizes[i] = numInidces;
310 for (
auto &val : maps[i])
312 values[i][valIndex] = val.first;
313 counts[i][valIndex] = val.second.size();
315 for (
auto &indexVal : val.second)
317 indices[i][index] = indexVal;
335 template <
typename T,
typename indexT,
bool columnMajor>
342 numRows = vec.getLength();
350 numCols = vec.getLength();
356 index_t =
sizeof(indexT);
358 metadata =
new uint32_t[NUM_META_DATA];
360 metadata[1] = innerDim;
361 metadata[2] = outerDim;
364 metadata[5] = index_t;
369 values = (T **)malloc(
sizeof(T *));
370 counts = (indexT **)malloc(
sizeof(indexT *));
371 indices = (indexT **)malloc(
sizeof(indexT *));
372 valueSizes = (indexT *)malloc(
sizeof(indexT));
373 indexSizes = (indexT *)malloc(
sizeof(indexT));
375 catch (std::bad_alloc &ba)
377 std::cerr <<
"bad_alloc caught: " << ba.what() <<
'\n';
378 throw std::runtime_error(
"Error: Could not allocate memory");
382 if (vec.
byteSize() == 0) [[unlikely]]
386 indices[0] =
nullptr;
393 valueSizes[0] = vec.uniqueVals();
399 values[0] = (T *)malloc(
sizeof(T) * valueSizes[0]);
400 counts[0] = (indexT *)malloc(
sizeof(indexT) * valueSizes[0]);
401 indices[0] = (indexT *)malloc(
sizeof(indexT) * indexSizes[0]);
403 catch (std::bad_alloc &ba)
405 std::cerr <<
"bad_alloc caught: " << ba.what() <<
'\n';
406 throw std::runtime_error(
"Error: Could not allocate memory");
410 memcpy(values[0], vec.getValues(),
sizeof(T) * valueSizes[0]);
411 memcpy(counts[0], vec.getCounts(),
sizeof(indexT) * valueSizes[0]);
412 memcpy(indices[0], vec.getIndices(),
sizeof(indexT) * indexSizes[0]);
422 template <
typename T,
typename indexT,
bool columnMajor>
430 for (
size_t i = 1; i < vecs.size(); i++)
447 template <
typename T,
typename indexT,
bool columnMajor>
452 FILE *fp = fopen(filename,
"rb");
457 throw std::runtime_error(
"Error: Could not open file");
461 metadata =
new uint32_t[NUM_META_DATA];
462 fread(metadata,
sizeof(uint32_t), NUM_META_DATA, fp);
465 innerDim = metadata[1];
466 outerDim = metadata[2];
469 index_t = metadata[5];
470 numRows = columnMajor ? innerDim : outerDim;
471 numCols = columnMajor ? outerDim : innerDim;
474 if (metadata[0] != 2)
477 throw std::runtime_error(
"Error: Compression level of file does not match compression level of class");
483 values = (T **)malloc(
sizeof(T *) * outerDim);
484 counts = (indexT **)malloc(
sizeof(indexT *) * outerDim);
485 indices = (indexT **)malloc(
sizeof(indexT *) * outerDim);
486 valueSizes = (indexT *)malloc(
sizeof(indexT) * outerDim);
487 indexSizes = (indexT *)malloc(
sizeof(indexT) * outerDim);
489 catch (std::bad_alloc &ba)
491 std::cerr <<
"bad_alloc caught: " << ba.what() <<
'\n';
492 throw std::runtime_error(
"Error: Could not allocate memory");
496 for (
size_t i = 0; i < outerDim; i++)
498 fread(&valueSizes[i],
sizeof(indexT), 1, fp);
502 for (
size_t i = 0; i < outerDim; i++)
504 fread(&indexSizes[i],
sizeof(indexT), 1, fp);
508 for (
size_t i = 0; i < outerDim; i++)
512 values[i] = (T *)malloc(
sizeof(T) * valueSizes[i]);
514 catch (std::bad_alloc &ba)
516 std::cerr <<
"bad_alloc caught: " << ba.what() <<
'\n';
517 throw std::runtime_error(
"Error: Could not allocate memory");
519 fread(values[i],
sizeof(T), valueSizes[i], fp);
523 for (
size_t i = 0; i < outerDim; i++)
527 counts[i] = (indexT *)malloc(
sizeof(indexT) * valueSizes[i]);
529 catch (std::bad_alloc &ba)
531 std::cerr <<
"bad_alloc caught: " << ba.what() <<
'\n';
532 throw std::runtime_error(
"Error: Could not allocate memory");
534 fread(counts[i],
sizeof(indexT), valueSizes[i], fp);
538 for (
size_t i = 0; i < outerDim; i++)
542 indices[i] = (indexT *)malloc(
sizeof(indexT) * indexSizes[i]);
544 catch (std::bad_alloc &ba)
546 std::cerr <<
"bad_alloc caught: " << ba.what() <<
'\n';
547 throw std::runtime_error(
"Error: Could not allocate memory");
549 fread(indices[i],
sizeof(indexT), indexSizes[i], fp);
567 template <
typename T,
typename indexT,
bool columnMajor>
571 if constexpr (columnMajor)
585 index_t =
sizeof(indexT);
590 values = (T **)malloc(
sizeof(T *) * outerDim);
591 counts = (indexT **)malloc(
sizeof(indexT *) * outerDim);
592 indices = (indexT **)malloc(
sizeof(indexT *) * outerDim);
593 valueSizes = (indexT *)malloc(
sizeof(indexT) * outerDim);
594 indexSizes = (indexT *)malloc(
sizeof(indexT) * outerDim);
596 catch (std::bad_alloc &ba)
598 std::cerr <<
"bad_alloc caught: " << ba.what() <<
'\n';
599 throw std::runtime_error(
"Error: Could not allocate memory");
604 #pragma omp parallel for
606 for (
size_t i = 0; i < outerDim; i++)
610 if (maps[i].empty()) [[unlikely]]
614 indices[i] =
nullptr;
621 size_t numInidces = 0;
624 for (
auto &val : maps[i])
627 byteSize += (
sizeof(indexT) * val.second.size());
630 numInidces += val.second.size();
635 values[i] = (T *)malloc(
sizeof(T) * maps[i].size());
636 counts[i] = (indexT *)malloc(
sizeof(indexT) * maps[i].size());
637 indices[i] = (indexT *)malloc(
sizeof(indexT) * numInidces);
639 catch (std::bad_alloc &ba)
641 std::cerr <<
"bad_alloc caught: " << ba.what() <<
'\n';
642 throw std::runtime_error(
"Error: Could not allocate memory");
645 valueSizes[i] = maps[i].size();
646 indexSizes[i] = numInidces;
652 for (
auto &val : maps[i])
654 values[i][valIndex] = val.first;
655 counts[i][valIndex] = val.second.size();
657 for (
auto &indexVal : val.second)
659 indices[i][index] = indexVal;
669 metadata =
new uint32_t[NUM_META_DATA];
671 metadata[1] = innerDim;
672 metadata[2] = outerDim;
675 metadata[5] = index_t;
Definition: VCSC_SparseMatrix.hpp:22
void append(typename SparseMatrix< T, indexT, 2, columnMajor >::Vector &vec)
Definition: VCSC_Methods.hpp:233
uint32_t nonZeros() const
Definition: IVSparse_Base_Methods.hpp:34
size_t byteSize() const
Definition: IVSparse_Base_Methods.hpp:37
Definition: IVCSC_SparseMatrix.hpp:27
IVSparse::SparseMatrix< T, indexT, 2, columnMajor > toVCSC()
Definition: IVCSC_Methods.hpp:116
SparseMatrix()
Definition: IVCSC_SparseMatrix.hpp:93
~SparseMatrix()
Destroy the Sparse Matrix object.
Definition: IVCSC_Constructors.hpp:15