18#ifndef SPARSE_SOLVER_HPP
19#define SPARSE_SOLVER_HPP
26#include <tbb/parallel_sort.h>
27#define ezp_sort tbb::parallel_sort
29#define ezp_sort std::sort
38 sparse_coo_mat(
const IT n,
const IT nnz, IT*
const row, IT*
const col, DT*
const data)
45 auto is_valid()
const {
return row && col && data; }
50 const IT*
const row_idx;
51 const IT*
const col_idx;
58 bool operator()(
const IT
idx_a,
const IT
idx_b)
const {
64 template<
typename T>
bool approx_equal(
T x,
T y,
int ulp = 2)
65 requires(!std::numeric_limits<T>::is_integer)
66 {
return std::fabs(x -
y) <= std::numeric_limits<T>::epsilon() * std::fabs(x +
y) *
ulp || std::fabs(x -
y) < std::numeric_limits<T>::min(); }
71 IT *row_ptr, *col_idx;
74 std::vector<IT> row_storage, col_storage;
75 std::vector<DT> data_storage;
80 , row_ptr(
other.row_ptr)
81 , col_idx(
other.col_idx)
83 , row_storage(
other.row_storage)
84 , col_storage(
other.col_storage)
85 , data_storage(
other.data_storage) {
86 if(!row_storage.empty()) row_ptr = row_storage.data();
87 if(!col_storage.empty()) col_idx = col_storage.data();
88 if(!data_storage.empty()) data = data_storage.data();
95 sparse_csr_mat(
const IT n,
const IT nnz, IT*
const row_ptr, IT*
const col_idx, DT*
const data)
112 if(!
coo.is_valid())
return;
114 std::vector<IT2> index(nnz);
115 std::iota(index.begin(), index.end(),
IT2(0));
118 row_storage.resize(nnz);
119 col_storage.resize(nnz);
120 data_storage.resize(nnz);
122 for(
auto I = IT{0};
I < nnz; ++
I) {
123 row_storage[
I] =
coo.row[index[
I]];
124 col_storage[
I] =
coo.col[index[
I]];
125 data_storage[
I] =
coo.data[index[
I]];
130 row_ptr = row_storage.data();
131 col_idx = col_storage.data();
132 data = data_storage.data();
150 for(
auto I = IT{0};
I < nnz; ++
I) {
170 row_storage.resize(n + 1);
171 col_storage.resize(nnz);
172 data_storage.resize(nnz);
174 row_storage[0] = IT{0} +
shift;
175 row_storage[n] = nnz +
shift;
178 auto is_valid() {
return row_ptr && col_idx && data; }
Definition sparse_solver.hpp:49
Solver for general sparse matrices.
Definition sparse_solver.hpp:33
Definition sparse_solver.hpp:69
sparse_csr_mat(const sparse_coo_mat< DT2, IT2 > &coo, const bool one_based=false, const bool full=false)
Definition sparse_solver.hpp:109