Skip to content

Commit

Permalink
keep numeric zeros in matrix_data and conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
upsj committed Oct 18, 2021
1 parent 7a68b74 commit 41991f1
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 61 deletions.
29 changes: 12 additions & 17 deletions core/matrix/coo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,14 @@ template <typename ValueType, typename IndexType>
void Coo<ValueType, IndexType>::move_to(Csr<ValueType, IndexType>* result)
{
auto exec = this->get_executor();
auto tmp = Csr<ValueType, IndexType>::create(
exec, this->get_size(), this->get_num_stored_elements(),
result->get_strategy());
const auto nnz = this->get_num_stored_elements();
auto tmp = Csr<ValueType, IndexType>::create(exec, this->get_size(), nnz,
result->get_strategy());
tmp->values_ = std::move(this->values_);
tmp->col_idxs_ = std::move(this->col_idxs_);
exec->run(coo::make_build_row_ptrs(
this->get_const_row_idxs(), this->get_num_stored_elements(),
this->get_size()[0], tmp->get_row_ptrs()));
exec->run(coo::make_build_row_ptrs(this->get_const_row_idxs(), nnz,
this->get_size()[0],
tmp->get_row_ptrs()));
tmp->make_srow();
tmp->move_to(result);
}
Expand Down Expand Up @@ -204,20 +204,15 @@ void Coo<ValueType, IndexType>::move_to(Dense<ValueType>* result)
template <typename ValueType, typename IndexType>
void Coo<ValueType, IndexType>::read(const mat_data& data)
{
size_type nnz = 0;
for (const auto& elem : data.nonzeros) {
nnz += (elem.value != zero<ValueType>());
}
auto tmp = Coo::create(this->get_executor()->get_master(), data.size, nnz);
auto tmp = Coo::create(this->get_executor()->get_master(), data.size,
static_cast<size_type>(data.nonzeros.size()));
size_type elt = 0;
for (const auto& elem : data.nonzeros) {
auto val = elem.value;
if (val != zero<ValueType>()) {
tmp->get_row_idxs()[elt] = elem.row;
tmp->get_col_idxs()[elt] = elem.column;
tmp->get_values()[elt] = elem.value;
elt++;
}
tmp->get_row_idxs()[elt] = elem.row;
tmp->get_col_idxs()[elt] = elem.column;
tmp->get_values()[elt] = elem.value;
elt++;
}
this->copy_from(std::move(tmp));
}
Expand Down
14 changes: 5 additions & 9 deletions core/matrix/csr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,8 @@ template <typename ValueType, typename IndexType>
void Csr<ValueType, IndexType>::read(const mat_data& data)
{
size_type nnz = 0;
for (const auto& elem : data.nonzeros) {
nnz += (elem.value != zero<ValueType>());
}
auto tmp = Csr::create(this->get_executor()->get_master(), data.size, nnz,
auto tmp = Csr::create(this->get_executor()->get_master(), data.size,
static_cast<size_type>(data.nonzeros.size()),
this->get_strategy());
size_type ind = 0;
size_type cur_ptr = 0;
Expand All @@ -337,11 +335,9 @@ void Csr<ValueType, IndexType>::read(const mat_data& data)
break;
}
auto val = data.nonzeros[ind].value;
if (val != zero<ValueType>()) {
tmp->get_values()[cur_ptr] = val;
tmp->get_col_idxs()[cur_ptr] = data.nonzeros[ind].column;
++cur_ptr;
}
tmp->get_values()[cur_ptr] = val;
tmp->get_col_idxs()[cur_ptr] = data.nonzeros[ind].column;
++cur_ptr;
}
tmp->get_row_ptrs()[row + 1] = cur_ptr;
}
Expand Down
8 changes: 3 additions & 5 deletions core/matrix/ell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,9 @@ void Ell<ValueType, IndexType>::read(const mat_data& data)
size_type col = 0;
while (ind < n && data.nonzeros[ind].row == row) {
auto val = data.nonzeros[ind].value;
if (val != zero<ValueType>()) {
tmp->val_at(row, col) = val;
tmp->col_at(row, col) = data.nonzeros[ind].column;
col++;
}
tmp->val_at(row, col) = val;
tmp->col_at(row, col) = data.nonzeros[ind].column;
col++;
ind++;
}
for (auto i = col; i < num_stored_elements_per_row; i++) {
Expand Down
18 changes: 7 additions & 11 deletions core/matrix/hybrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void get_each_row_nnz(const matrix_data<ValueType, IndexType>& data,
current_row = elem.row;
nnz = 0;
}
nnz += (elem.value != zero<ValueType>());
nnz++;
}
row_nnz_val[current_row] = nnz;
}
Expand Down Expand Up @@ -266,12 +266,10 @@ void Hybrid<ValueType, IndexType>::read(const mat_data& data)
// coo_part
while (ind < n && data.nonzeros[ind].row == row) {
auto val = data.nonzeros[ind].value;
if (val != zero<ValueType>()) {
coo_vals[coo_ind] = val;
coo_col_idxs[coo_ind] = data.nonzeros[ind].column;
coo_row_idxs[coo_ind] = data.nonzeros[ind].row;
coo_ind++;
}
coo_vals[coo_ind] = val;
coo_col_idxs[coo_ind] = data.nonzeros[ind].column;
coo_row_idxs[coo_ind] = data.nonzeros[ind].row;
coo_ind++;
ind++;
}
}
Expand Down Expand Up @@ -305,10 +303,8 @@ void Hybrid<ValueType, IndexType>::write(mat_data& data) const
}

while (coo_ind < coo_nnz && coo_row_idxs[coo_ind] == row) {
if (coo_vals[coo_ind] != zero<ValueType>()) {
data.nonzeros.emplace_back(row, coo_col_idxs[coo_ind],
coo_vals[coo_ind]);
}
data.nonzeros.emplace_back(row, coo_col_idxs[coo_ind],
coo_vals[coo_ind]);
coo_ind++;
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/matrix/sellp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ size_type calculate_total_cols(const matrix_data<ValueType, IndexType>& data,
total_cols += slice_lengths[current_slice];
current_slice = elem.row / slice_size;
}
nonzeros_per_row += (elem.value != zero<ValueType>());
nonzeros_per_row++;
}
slice_lengths[current_slice] =
max(slice_lengths[current_slice], nonzeros_per_row);
Expand Down
13 changes: 4 additions & 9 deletions core/matrix/sparsity_csr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,9 @@ void SparsityCsr<ValueType, IndexType>::apply_impl(const LinOp* alpha,
template <typename ValueType, typename IndexType>
void SparsityCsr<ValueType, IndexType>::read(const mat_data& data)
{
size_type nnz = 0;
for (const auto& elem : data.nonzeros) {
nnz += (elem.value != zero<ValueType>());
}
auto tmp =
SparsityCsr::create(this->get_executor()->get_master(), data.size, nnz);
SparsityCsr::create(this->get_executor()->get_master(), data.size,
static_cast<size_type>(data.nonzeros.size()));
size_type ind = 0;
size_type cur_ptr = 0;
tmp->get_row_ptrs()[0] = cur_ptr;
Expand All @@ -114,10 +111,8 @@ void SparsityCsr<ValueType, IndexType>::read(const mat_data& data)
break;
}
auto val = data.nonzeros[ind].value;
if (val != zero<ValueType>()) {
tmp->get_col_idxs()[cur_ptr] = data.nonzeros[ind].column;
++cur_ptr;
}
tmp->get_col_idxs()[cur_ptr] = data.nonzeros[ind].column;
++cur_ptr;
}
tmp->get_row_ptrs()[row + 1] = cur_ptr;
}
Expand Down
8 changes: 3 additions & 5 deletions reference/matrix/hybrid_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,9 @@ void convert_to_csr(std::shared_ptr<const ReferenceExecutor> exec,
}
// Coo part (row should be ascending)
while (coo_idx < coo_nnz && coo_row[coo_idx] == row) {
if (coo_val[coo_idx] != zero<ValueType>()) {
csr_val[csr_idx] = coo_val[coo_idx];
csr_col_idxs[csr_idx] = coo_col[coo_idx];
csr_idx++;
}
csr_val[csr_idx] = coo_val[coo_idx];
csr_col_idxs[csr_idx] = coo_col[coo_idx];
csr_idx++;
coo_idx++;
}
csr_row_ptrs[row + 1] = csr_idx;
Expand Down
5 changes: 1 addition & 4 deletions reference/test/matrix/hybrid_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class Hybrid : public ::testing::Test {
: exec(gko::ReferenceExecutor::create()),
mtx1(Mtx::create(exec)),
mtx2(Mtx::create(exec)),
mtx3(Mtx::create(exec, gko::dim<2>{2, 3}, 2, 2, 2))
mtx3(Mtx::create(exec, gko::dim<2>{2, 3}, 2, 2, 1))
{
// clang-format off
mtx1 = gko::initialize<Mtx>({{1.0, 3.0, 2.0},
Expand All @@ -97,11 +97,8 @@ class Hybrid : public ::testing::Test {
ell_col[3] = 1;
// Set Coo values
coo_val[0] = 2.0;
coo_val[1] = 0.0;
coo_col[0] = 2;
coo_col[1] = 2;
coo_row[0] = 0;
coo_row[1] = 1;
}

void assert_equal_to_mtx(const Csr* m)
Expand Down

0 comments on commit 41991f1

Please sign in to comment.