14 template <
typename T,
typename indexT,
bool columnMajor>
16 if (vals !=
nullptr) { free(vals); }
17 if (innerIdx !=
nullptr) { free(innerIdx); }
18 if (outerPtr !=
nullptr) { free(outerPtr); }
19 if (metadata !=
nullptr) {
delete[] metadata; }
23 template <
typename T,
typename indexT,
bool columnMajor>
30 innerDim = mat.innerSize();
31 outerDim = mat.outerSize();
37 index_t =
sizeof(indexT);
41 vals = (T *)malloc(nnz *
sizeof(T));
42 innerIdx = (indexT *)malloc(nnz *
sizeof(indexT));
43 outerPtr = (indexT *)malloc((outerDim + 1) *
sizeof(indexT));
44 }
catch (std::bad_alloc &e) { std::cerr <<
"Allocation failed: " << e.what() <<
'\n'; }
47 metadata =
new uint32_t[NUM_META_DATA];
49 metadata[1] = innerDim;
50 metadata[2] = outerDim;
53 metadata[5] = index_t;
56 memcpy(vals, mat.valuePtr(),
sizeof(T) * nnz);
57 memcpy(innerIdx, mat.innerIndexPtr(),
sizeof(indexT) * nnz);
58 memcpy(outerPtr, mat.outerIndexPtr(),
sizeof(indexT) * (outerDim + 1));
70 template <
typename T,
typename indexT,
bool columnMajor>
72 other.makeCompressed();
75 innerDim = other.innerSize();
76 outerDim = other.outerSize();
77 numRows = other.rows();
78 numCols = other.cols();
79 nnz = other.nonZeros();
82 index_t =
sizeof(indexT);
86 vals = (T *)malloc(nnz *
sizeof(T));
87 innerIdx = (indexT *)malloc(nnz *
sizeof(indexT));
88 outerPtr = (indexT *)malloc((outerDim + 1) *
sizeof(indexT));
89 }
catch (std::bad_alloc &e) { std::cerr <<
"Allocation failed: " << e.what() <<
'\n'; }
92 metadata =
new uint32_t[NUM_META_DATA];
94 metadata[1] = innerDim;
95 metadata[2] = outerDim;
98 metadata[5] = index_t;
101 memcpy(vals, other.valuePtr(),
sizeof(T) * nnz);
102 memcpy(innerIdx, other.innerIndexPtr(),
sizeof(indexT) * nnz);
103 memcpy(outerPtr, other.outerIndexPtr(),
sizeof(indexT) * (outerDim + 1));
115 template <
typename T,
typename indexT,
bool columnMajor>
119 template <
typename T,
typename indexT,
bool columnMajor>
120 template <u
int8_t compressionLevel2>
124 if constexpr (compressionLevel2 == 1) {
132 if constexpr (compressionLevel2 == 2) { temp = other.
toCSC(); }
133 else if constexpr (compressionLevel2 == 3) { temp = other.
toCSC(); }
145 template <
typename T,
typename indexT,
bool columnMajor>
146 template <
typename T2,
typename indexT2>
150 if constexpr (columnMajor) {
163 index_t =
sizeof(indexT);
167 this->vals = (T *)malloc(nnz *
sizeof(T));
168 innerIdx = (indexT *)malloc(nnz *
sizeof(indexT));
169 this->outerPtr = (indexT *)malloc((outerDim + 1) *
sizeof(indexT));
170 }
catch (std::bad_alloc &e) { std::cerr <<
"Allocation failed: " << e.what() <<
'\n'; }
173 metadata =
new uint32_t[NUM_META_DATA];
175 metadata[1] = innerDim;
176 metadata[2] = outerDim;
179 metadata[5] = index_t;
182 memcpy(this->vals, vals,
sizeof(T) * nnz);
183 memcpy(innerIdx, innerIndices,
sizeof(indexT) * nnz);
184 memcpy(this->outerPtr, outerPtr,
sizeof(indexT) * (outerDim + 1));
196 template <
typename T,
typename indexT,
bool columnMajor>
200 if constexpr (columnMajor) {
201 innerDim = vec.getLength();
203 numRows = vec.getLength();
207 outerDim = vec.getLength();
209 numCols = vec.getLength();
215 index_t =
sizeof(indexT);
217 metadata =
new uint32_t[NUM_META_DATA];
219 metadata[1] = innerDim;
220 metadata[2] = outerDim;
223 metadata[5] = index_t;
231 try { outerPtr = (indexT *)malloc((outerDim + 1) *
sizeof(indexT)); }
232 catch (std::bad_alloc &e) { std::cerr <<
"Allocation failed: " << e.what() <<
'\n'; }
240 vals = (T *)malloc(nnz *
sizeof(T));
241 innerIdx = (indexT *)malloc(nnz *
sizeof(indexT));
242 outerPtr = (indexT *)malloc((outerDim + 1) *
sizeof(indexT));
243 }
catch (std::bad_alloc &e) {
244 std::cerr <<
"Allocation failed: " << e.what() <<
'\n';
248 memcpy(vals, vec.getValues(),
sizeof(T) * nnz);
249 memcpy(innerIdx, vec.getInnerIndices(),
sizeof(indexT) * nnz);
262 template <
typename T,
typename indexT,
bool columnMajor>
269 for (
size_t i = 1; i < vecs.size(); i++) { temp.
append(vecs[i]); }
283 template <
typename T,
typename indexT,
bool columnMajor>
286 FILE *fp = fopen(filename,
"rb");
290 std::cerr <<
"Error: Could not open file " << filename << std::endl;
295 metadata =
new uint32_t[NUM_META_DATA];
296 fread(metadata,
sizeof(uint32_t), NUM_META_DATA, fp);
297 innerDim = metadata[1];
298 outerDim = metadata[2];
301 index_t = metadata[5];
303 if constexpr (columnMajor) {
313 vals = (T *)malloc(nnz *
sizeof(T));
314 innerIdx = (indexT *)malloc(nnz *
sizeof(indexT));
315 outerPtr = (indexT *)malloc((outerDim + 1) *
sizeof(indexT));
316 }
catch (std::bad_alloc &e) {
317 std::cerr <<
"Error: Could not allocate memory for IVSparse matrix" << std::endl;
322 fread(vals,
sizeof(T), nnz, fp);
323 fread(innerIdx,
sizeof(indexT), nnz, fp);
324 fread(outerPtr,
sizeof(indexT), outerDim + 1, fp);
338 template <
typename T,
typename indexT,
bool columnMajor>
339 template <
typename T2,
typename indexT2>
342 if constexpr (columnMajor) {
356 index_t =
sizeof(indexT);
359 vals = (T *)malloc(nnz *
sizeof(T));
360 innerIdx = (indexT *)malloc(nnz *
sizeof(indexT));
361 outerPtr = (indexT *)calloc(outerDim + 1,
sizeof(indexT));
362 }
catch (std::bad_alloc &e) {
363 std::cerr <<
"Allocation failed: " << e.what() <<
'\n';
366 metadata =
new uint32_t[NUM_META_DATA];
369 metadata[1] = innerDim;
370 metadata[2] = outerDim;
373 metadata[5] = index_t;
376 std::sort(entries.begin(), entries.end(), [](
const std::tuple<indexT2, indexT2, T2> &a,
const std::tuple<indexT2, indexT2, T2> &b)
378 if (std::get<1>(a) == std::get<1>(b)) {
379 return std::get<0>(a) < std::get<0>(b);
382 return std::get<1>(a) < std::get<1>(b);
388 for (
size_t i = 0; i < entries.size(); i++) {
389 vals[i] = std::get<2>(entries[i]);
390 innerIdx[i] = std::get<0>(entries[i]);
393 if (std::get<1>(entries[i]) != OuterIndex) {
394 if (OuterIndex != -1) { outerPtr[OuterIndex + 1] = count - 1; }
395 OuterIndex = std::get<1>(entries[i]);
396 outerPtr[OuterIndex] = count - 1;
400 outerPtr[OuterIndex + 1] = count;
Definition: CSC_SparseMatrix.hpp:24
void append(typename SparseMatrix< T, indexT, 1, columnMajor >::Vector &vec)
Definition: CSC_Methods.hpp:168
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, 1, columnMajor > toCSC()
Definition: IVCSC_Methods.hpp:92
SparseMatrix()
Definition: IVCSC_SparseMatrix.hpp:93
~SparseMatrix()
Destroy the Sparse Matrix object.
Definition: IVCSC_Constructors.hpp:15