Skip to content

Commit

Permalink
actually undo formatting changes in Coo.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
superwhiskers committed Jun 17, 2024
1 parent f2e5e04 commit 8a1522b
Showing 1 changed file with 19 additions and 88 deletions.
107 changes: 19 additions & 88 deletions resolve/matrix/Coo.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#include <cstring> // <-- includes memcpy
#include <iostream>
#include <iomanip>
#include <cassert>
#include <iomanip>

#include <resolve/utilities/logger/Logger.hpp>
#include "Coo.hpp"


namespace ReSolve
namespace ReSolve
{
using out = io::Logger;

Expand All @@ -18,15 +17,15 @@ namespace ReSolve
matrix::Coo::Coo(index_type n, index_type m, index_type nnz) : Sparse(n, m, nnz)
{
}

matrix::Coo::Coo(index_type n,
index_type m,
matrix::Coo::Coo(index_type n,
index_type m,
index_type nnz,
bool symmetric,
bool expanded) : Sparse(n, m, nnz, symmetric, expanded)
{
}

matrix::Coo::~Coo()
{
}
Expand Down Expand Up @@ -91,7 +90,7 @@ namespace ReSolve
if (((memspaceIn == memory::DEVICE)) && ((memspaceOut == memory::DEVICE))){ control = 3;}

if (memspaceOut == memory::HOST) {
//check if cpu data allocated
//check if cpu data allocated
if ((h_row_data_ == nullptr) != (h_col_data_ == nullptr)) {
out::error() << "In Coo::updateData one of host row or column data is null!\n";
}
Expand Down Expand Up @@ -151,15 +150,15 @@ namespace ReSolve
return -1;
}
return 0;
}
}

int matrix::Coo::updateData(index_type* row_data, index_type* col_data, real_type* val_data, index_type new_nnz, memory::MemorySpace memspaceIn, memory::MemorySpace memspaceOut)
{
this->destroyMatrixData(memspaceOut);
this->nnz_ = new_nnz;
int i = this->updateData(row_data, col_data, val_data, memspaceIn, memspaceOut);
return i;
}
}

int matrix::Coo::allocateMatrixData(memory::MemorySpace memspace)
{
Expand All @@ -169,20 +168,20 @@ namespace ReSolve

if (memspace == memory::HOST) {
this->h_row_data_ = new index_type[nnz_current];
std::fill(h_row_data_, h_row_data_ + nnz_current, 0);
std::fill(h_row_data_, h_row_data_ + nnz_current, 0);
this->h_col_data_ = new index_type[nnz_current];
std::fill(h_col_data_, h_col_data_ + nnz_current, 0);
std::fill(h_col_data_, h_col_data_ + nnz_current, 0);
this->h_val_data_ = new real_type[nnz_current];
std::fill(h_val_data_, h_val_data_ + nnz_current, 0.0);
std::fill(h_val_data_, h_val_data_ + nnz_current, 0.0);
owns_cpu_data_ = true;
owns_cpu_vals_ = true;
return 0;
}

if (memspace == memory::DEVICE) {
mem_.allocateArrayOnDevice(&d_row_data_, nnz_current);
mem_.allocateArrayOnDevice(&d_col_data_, nnz_current);
mem_.allocateArrayOnDevice(&d_val_data_, nnz_current);
mem_.allocateArrayOnDevice(&d_row_data_, nnz_current);
mem_.allocateArrayOnDevice(&d_col_data_, nnz_current);
mem_.allocateArrayOnDevice(&d_val_data_, nnz_current);
owns_gpu_data_ = true;
owns_gpu_vals_ = true;
return 0;
Expand All @@ -206,12 +205,12 @@ namespace ReSolve
out::error() << "In Coo::copyData one of host row or column data is null!\n";
}
if ((h_row_data_ == nullptr) && (h_col_data_ == nullptr)) {
h_row_data_ = new index_type[nnz_current];
h_col_data_ = new index_type[nnz_current];
h_row_data_ = new index_type[nnz_current];
h_col_data_ = new index_type[nnz_current];
owns_cpu_data_ = true;
}
if (h_val_data_ == nullptr) {
h_val_data_ = new real_type[nnz_current];
h_val_data_ = new real_type[nnz_current];
owns_cpu_vals_ = true;
}
mem_.copyArrayDeviceToHost(h_row_data_, d_row_data_, nnz_current);
Expand Down Expand Up @@ -247,7 +246,7 @@ namespace ReSolve

/**
* @brief Prints matrix data.
*
*
* @param out - Output stream where the matrix data is printed
*/
void matrix::Coo::print(std::ostream& out)
Expand All @@ -259,72 +258,4 @@ namespace ReSolve
<< h_val_data_[i] << "\n";
}
}

int matrix::Coo::expand()
{
if (is_symmetric_ && !is_expanded_) {
index_type* rows = getRowData(memory::HOST);
index_type* columns = getColData(memory::HOST);
real_type* values = getValues(memory::HOST);

if (rows == nullptr || columns == nullptr || values == nullptr) {
return 0;
}

// NOTE: this is predicated on the same define as that which disables
// assert(3), to avoid record-keeping where it is not necessary
#ifndef NDEBUG
index_type n_diagonal = 0;
#endif

// NOTE: so because most of the code here uses new/delete and there's no
// realloc(3) equivalent for that memory management scheme, we
// have to manually new/memcpy/delete, unfortunately
index_type* new_rows = new index_type[nnz_expanded_];
index_type* new_columns = new index_type[nnz_expanded_];
real_type* new_values = new real_type[nnz_expanded_];

index_type j = 0;
for (index_type i = 0; i < nnz_; i++) {
new_rows[j] = rows[i];
new_columns[j] = columns[i];
new_values[j] = values[i];

j++;

#ifndef NDEBUG
if (rows[i] == columns[i]) {
n_diagonal++;
} else {
#else
if (rows[i] != columns[i]) {
#endif
new_rows[j] = columns[i];
new_columns[j] = rows[i];
new_values[j] = values[i];

j++;
}
}

// NOTE: the effectiveness of this is probably questionable given that
// it occurs after we've already risked writing out-of-bounds, but
// i guess if that worked or we've over-allocated, this will catch
// something (in debug builds/release builds with asserts enabled)
assert(nnz_expanded_ == ((2 * nnz_) - n_diagonal));

if (destroyMatrixData(memory::HOST) != 0 ||
setMatrixData(new_rows, new_columns, new_values, memory::HOST) != 0) {
// TODO: make fallible
assert(false && "invalid state after coo matrix expansion");
return -1;
}

setNnz(nnz_expanded_);
setExpanded(true);
owns_cpu_data_ = owns_cpu_vals_ = true;
}

return 0;
}
} // namespace ReSolve

0 comments on commit 8a1522b

Please sign in to comment.