Skip to content
Open
6 changes: 1 addition & 5 deletions examples/ExampleHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,10 @@ namespace ReSolve
ReSolve::vector::Vector* r,
ReSolve::vector::Vector* x)
{
assert(res_ != nullptr && "resetSystem should be called after setSystem");
A_ = A;
Comment on lines +145 to 146
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not touch this file for now. It needs more refactoring than this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change is very simple and is better than what we have currently. We can debate the error message, but the user should not be allowed to reset the system without first setting it.

r_ = r;
x_ = x;
if (res_ == nullptr)
{
res_ = new ReSolve::vector::Vector(A->getNumRows());
}

computeNorms();
}

Expand Down
1 change: 1 addition & 0 deletions resolve/LinSolverDirect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ namespace ReSolve
{
if (A == nullptr)
{
out::error() << "LinSolverDirect::setup: input matrix A is nullptr" << std::endl;
return 1;
}
A_ = A;
Expand Down
29 changes: 20 additions & 9 deletions resolve/LinSolverDirectCuSolverRf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ namespace ReSolve
*
* Sets up the cuSolverRf factorization for the given matrix A and its
* L and U factors. The permutation vectors P and Q are also set up.
* This function should not be called more than once for the same object.
*
* @param[in] A - pointer to the matrix A
* @param[in] L - pointer to the lower triangular factor L in CSR
Expand All @@ -75,31 +76,41 @@ namespace ReSolve
this->A_ = A;
index_type n = A_->getNumRows();

// Remember - P and Q are generally CPU variables!
// Factorization data is stored in the handle.
// If function is called again, destroy the old handle to get rid of old data.
if (setup_completed_)
{
cusolverRfDestroy(handle_cusolverrf_);
cusolverRfCreate(&handle_cusolverrf_);
out::error() << "LinSolverDirectCuSolverRf::setup should only be called one." << std::endl;
return 1;
}

if (d_P_ == nullptr)
{
mem_.allocateArrayOnDevice(&d_P_, n);
}
else
{
out::error() << "d_P_ should be nullptr on call to LinSolverDirectCuSolverRf::setup." << std::endl;
return 1;
}

if (d_Q_ == nullptr)
{
mem_.allocateArrayOnDevice(&d_Q_, n);
}

if (d_T_ != nullptr)
else
{
mem_.deleteOnDevice(d_T_);
out::error() << "d_Q_ should be nullptr on call to LinSolverDirectCuSolverRf::setup." << std::endl;
return 1;
}

mem_.allocateArrayOnDevice(&d_T_, n);
if (d_T_ == nullptr)
{
mem_.allocateArrayOnDevice(&d_T_, n);
}
else
{
out::error() << "d_T_ should be nullptr on call to LinSolverDirectCuSolverRf::setup" << std::endl;
return 1;
}

mem_.copyArrayHostToDevice(d_P_, P, n);
mem_.copyArrayHostToDevice(d_Q_, Q, n);
Expand Down
4 changes: 3 additions & 1 deletion resolve/LinSolverDirectKLU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ namespace ReSolve
if (Symbolic_ == nullptr)
{
out::error() << "Symbolic_ factorization failed with Common_.status = "
<< Common_.status << "\n";
<< Common_.status << std::endl;
return 1;
}
return 0;
Expand Down Expand Up @@ -167,6 +167,8 @@ namespace ReSolve

if (Numeric_ == nullptr)
{
out::error() << "Numeric_ factorization failed with Common_.status = "
<< Common_.status << std::endl;
return 1;
}
else
Expand Down
10 changes: 10 additions & 0 deletions resolve/LinSolverDirectRocSolverRf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,21 @@ namespace ReSolve
{
mem_.allocateArrayOnDevice(&d_P_, n);
}
else
{
out::error() << "d_P_ should be nullptr on call to LinSolverDirectRocSolverRf::setup." << std::endl;
return 1;
}

if (d_Q_ == nullptr)
{
mem_.allocateArrayOnDevice(&d_Q_, n);
}
else
{
out::error() << "d_Q_ should be nullptr on call to LinSolverDirectRocSolverRf::setup." << std::endl;
return 1;
}
mem_.copyArrayHostToDevice(d_P_, P, n);
mem_.copyArrayHostToDevice(d_Q_, Q, n);

Expand Down
1 change: 1 addition & 0 deletions resolve/LinSolverIterative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace ReSolve
{
if (A == nullptr)
{
out::error() << "LinSolverIterative::setup: input matrix A is nullptr" << std::endl;
return 1;
}
this->A_ = A;
Expand Down
15 changes: 14 additions & 1 deletion resolve/MemoryUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
#include <cstring> // <- declares `memcpy`

#include <resolve/resolve_defs.hpp>
#include <resolve/utilities/logger/Logger.hpp>

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

namespace memory
{
enum MemorySpace
Expand Down Expand Up @@ -86,12 +89,22 @@ namespace ReSolve
{
std::size_t arraysize = static_cast<std::size_t>(n) * sizeof(T);
*v = new T[arraysize];
return *v == nullptr ? 1 : 0;
if (*v == nullptr)
{
out::error() << "Memory allocation on host failed for size " << arraysize << " bytes." << std::endl;
return 1;
}
return 0;
}

template <typename T>
int deleteOnHost(T* v)
{
if (v == nullptr)
{
out::error() << "Trying to delete nullptr on host!" << std::endl;
return 1;
}
delete[] v;
v = nullptr;
return 0;
Expand Down
19 changes: 13 additions & 6 deletions resolve/SystemSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,6 @@ namespace ReSolve
vectorHandler_,
gs_);
}
else
{
// do nothing
}

return 0;
}
Expand All @@ -393,32 +389,43 @@ namespace ReSolve
factorizationSolver_->setup(A_);
return factorizationSolver_->analyze();
}
out::error() << "Analyze method not set to a valid method!" << std::endl;
return 1;
}

int SystemSolver::factorize()
{
if (A_ == nullptr)
{
out::error() << "System matrix not set!\n";
return 1;
}
if (factorizationMethod_ == "klu")
{
is_solve_on_device_ = false;
return factorizationSolver_->factorize();
}
out::error() << "Factorization method not set to a valid method!" << std::endl;
return 1;
}

int SystemSolver::refactorize()
{
if (A_ == nullptr)
{
out::error() << "System matrix not set!\n";
return 1;
}
if (refactorizationMethod_ == "klu")
{
return factorizationSolver_->refactorize();
}

if (refactorizationMethod_ == "glu" || refactorizationMethod_ == "cusolverrf" || refactorizationMethod_ == "rocsolverrf")
{
is_solve_on_device_ = true;
return refactorizationSolver_->refactorize();
}

out::error() << "Refactorization method not set to a valid method!" << std::endl;
return 1;
}

Expand Down
11 changes: 5 additions & 6 deletions resolve/matrix/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,11 @@ namespace ReSolve
Logger::error() << "Empty input to updateArrayFromFile function ..." << std::endl;
return;
}

if (p_rhs == nullptr)
{
Logger::error() << "Null pointer to array in updateArrayFromFile function ..." << std::endl;
return;
}
real_type* rhs = *p_rhs;
std::stringstream ss;
std::string line;
Expand All @@ -399,11 +403,6 @@ namespace ReSolve
}
ss << line;
ss >> n >> m;

if (rhs == nullptr)
{
rhs = new real_type[n];
}
real_type a;
index_type i = 0;
while (file >> a)
Expand Down
26 changes: 13 additions & 13 deletions resolve/vector/Vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -631,11 +631,11 @@ namespace ReSolve
*
* In case of multivectors, entire multivector is set to the constant.
*
* @param[in] C - Constant (real number)
* @param[in] memspace - Memory space of the data to be set to 0 (HOST or DEVICE)
* @param[in] constant - Constant (real number)
* @param[in] memspace - Memory space of the data to be set to constant (HOST or DEVICE)
*
*/
int Vector::setToConst(real_type C, memory::MemorySpace memspace)
int Vector::setToConst(real_type constant, memory::MemorySpace memspace)
{
using namespace ReSolve::memory;
switch (memspace)
Expand All @@ -646,7 +646,7 @@ namespace ReSolve
h_data_ = new real_type[n_capacity_ * k_];
owns_cpu_data_ = true;
}
mem_.setArrayToConstOnHost(h_data_, C, n_size_ * k_);
mem_.setArrayToConstOnHost(h_data_, constant, n_size_ * k_);
setHostUpdated(true);
setDeviceUpdated(false);
break;
Expand All @@ -656,7 +656,7 @@ namespace ReSolve
mem_.allocateArrayOnDevice(&d_data_, n_capacity_ * k_);
owns_gpu_data_ = true;
}
mem_.setArrayToConstOnDevice(d_data_, C, n_size_ * k_);
mem_.setArrayToConstOnDevice(d_data_, constant, n_size_ * k_);
setHostUpdated(false);
setDeviceUpdated(true);
break;
Expand All @@ -668,33 +668,33 @@ namespace ReSolve
* @brief set the data of a single vector in a multivector to a given constant.
*
* @param[in] j - Index of a vector in a multivector
* @param[in] C - Constant (real number)
* @param[in] constant - Constant (real number)
* @param[in] memspace - Memory space of the data to be set to 0 (HOST or DEVICE)
*
* @pre _j_ < _k_ i.e,, _j_ is smaller than the total number of vectors in multivector.
*/
int Vector::setToConst(index_type j, real_type C, memory::MemorySpace memspace)
int Vector::setToConst(index_type j, real_type constant, memory::MemorySpace memspace)
{
using namespace ReSolve::memory;
switch (memspace)
{
case HOST:
if (h_data_ == nullptr)
{
h_data_ = new real_type[n_capacity_ * k_];
owns_cpu_data_ = true;
out::error() << "Trying to set vector host values, but the values are not allocated!" << std::endl;
return 1;
}
mem_.setArrayToConstOnHost(&h_data_[n_size_ * j], C, n_size_);
mem_.setArrayToConstOnHost(&h_data_[n_size_ * j], constant, n_size_);
cpu_updated_[j] = true;
gpu_updated_[j] = false;
break;
case DEVICE:
if (d_data_ == nullptr)
{
mem_.allocateArrayOnDevice(&d_data_, n_capacity_ * k_);
owns_gpu_data_ = true;
out::error() << "Trying to set vector device values, but the values are not allocated!" << std::endl;
return 1;
}
mem_.setArrayToConstOnDevice(&d_data_[n_size_ * j], C, n_size_);
mem_.setArrayToConstOnDevice(&d_data_[n_size_ * j], constant, n_size_);
cpu_updated_[j] = false;
gpu_updated_[j] = true;
break;
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/matrix/MatrixHandlerTests.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
#include <resolve/matrix/Csc.hpp>
#include <resolve/matrix/Csr.hpp>
#include <resolve/matrix/MatrixHandler.hpp>
#include <resolve/utilities/logger/Logger.hpp>
#include <resolve/vector/Vector.hpp>
#include <resolve/workspace/LinAlgWorkspace.hpp>
#include <tests/unit/TestBase.hpp>

namespace ReSolve
{
using out = io::Logger;

namespace tests
{

Expand Down Expand Up @@ -616,6 +619,7 @@ namespace ReSolve
// Check if the matrix is valid
if (A == nullptr)
{
out::error() << "Matrix pointer is NULL in " << __func__ << std::endl;
return false;
}

Expand Down Expand Up @@ -697,6 +701,7 @@ namespace ReSolve
// Check if the matrix is valid
if (A == nullptr)
{
out::error() << "Matrix pointer is NULL in " << __func__ << std::endl;
return false;
}

Expand Down Expand Up @@ -780,6 +785,7 @@ namespace ReSolve
// Check if the vector is valid
if (scaled_vec == nullptr)
{
out::error() << "Vector pointer is NULL in " << __func__ << std::endl;
return false;
}

Expand Down
Loading