Skip to content

Commit

Permalink
Improve error message
Browse files Browse the repository at this point in the history
  • Loading branch information
joaoleal committed Feb 12, 2020
1 parent 3399515 commit 56009f7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
61 changes: 32 additions & 29 deletions include/cppad/cg/model/functor_generic_model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ namespace cg {
* This class is not thread-safe and it should not be used simultaneously in
* different threads.
* Multiple instances of this class for the same model from the same model
* library object can be used simulataneously in different threads.
* library object can be used simultaneously in different threads.
*
* @author Joao Leal
*/
template<class Base>
class FunctorGenericModel : public GenericModel<Base> {
protected:
static constexpr const char* ERROR_LIBRARY_NOT_READY = "The model library is not ready. The model library that"
" provided this model might have been closed or deleted.";
protected:
bool _isLibraryReady;
/// the model name
Expand Down Expand Up @@ -124,7 +127,7 @@ class FunctorGenericModel : public GenericModel<Base> {
}

std::vector<bool> JacobianSparsityBool() override {
CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Dynamic library closed")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_jacobianSparsity != nullptr, "No Jacobian sparsity function defined in the dynamic library")

unsigned long const* row, *col;
Expand All @@ -140,7 +143,7 @@ class FunctorGenericModel : public GenericModel<Base> {
}

std::vector<std::set<size_t> > JacobianSparsitySet() override {
CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Model library is not ready (possibly closed)")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_jacobianSparsity != nullptr, "No Jacobian sparsity function defined in the dynamic library")

unsigned long const* row, *col;
Expand All @@ -157,7 +160,7 @@ class FunctorGenericModel : public GenericModel<Base> {

void JacobianSparsity(std::vector<size_t>& equations,
std::vector<size_t>& variables) override {
CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Model library is not ready (possibly closed)")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_jacobianSparsity != nullptr, "No Jacobian sparsity function defined in the dynamic library")

unsigned long const* row, *col;
Expand All @@ -177,7 +180,7 @@ class FunctorGenericModel : public GenericModel<Base> {
}

std::vector<bool> HessianSparsityBool() override {
CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Model library is not ready (possibly closed)")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_hessianSparsity != nullptr, "No Hessian sparsity function defined in the dynamic library")

unsigned long const* row, *col;
Expand All @@ -193,7 +196,7 @@ class FunctorGenericModel : public GenericModel<Base> {
}

std::vector<std::set<size_t> > HessianSparsitySet() override {
CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Model library is not ready (possibly closed)")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_hessianSparsity != nullptr, "No Hessian sparsity function defined in the dynamic library")

unsigned long const* row, *col;
Expand All @@ -210,7 +213,7 @@ class FunctorGenericModel : public GenericModel<Base> {

void HessianSparsity(std::vector<size_t>& rows,
std::vector<size_t>& cols) override {
CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Model library is not ready (possibly closed)")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_hessianSparsity != nullptr, "No Hessian sparsity function defined in the dynamic library")

unsigned long const* row, *col;
Expand All @@ -229,7 +232,7 @@ class FunctorGenericModel : public GenericModel<Base> {
}

std::vector<bool> HessianSparsityBool(size_t i) override {
CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Model library is not ready (possibly closed)")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_hessianSparsity2 != nullptr, "No Hessian sparsity function defined in the dynamic library")

unsigned long const* row, *col;
Expand All @@ -245,7 +248,7 @@ class FunctorGenericModel : public GenericModel<Base> {
}

std::vector<std::set<size_t> > HessianSparsitySet(size_t i) override {
CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Model library is not ready (possibly closed)")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_hessianSparsity2 != nullptr, "No Hessian sparsity function defined in the dynamic library")

unsigned long const* row, *col;
Expand All @@ -262,7 +265,7 @@ class FunctorGenericModel : public GenericModel<Base> {

void HessianSparsity(size_t i, std::vector<size_t>& rows,
std::vector<size_t>& cols) override {
CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Model library is not ready (possibly closed)")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_hessianSparsity2 != nullptr, "No Hessian sparsity function defined in the dynamic library")

unsigned long const* row, *col;
Expand Down Expand Up @@ -295,7 +298,7 @@ class FunctorGenericModel : public GenericModel<Base> {
/// calculate the dependent values (zero order)
void ForwardZero(ArrayView<const Base> x,
ArrayView<Base> dep) override {
CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Model library is not ready (possibly closed)")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_zero != nullptr, "No zero order forward function defined in the dynamic library")
CPPADCG_ASSERT_KNOWN(_in.size() == 1, "The number of independent variable arrays is higher than 1,"
" please use the variable size methods")
Expand All @@ -311,7 +314,7 @@ class FunctorGenericModel : public GenericModel<Base> {

void ForwardZero(const std::vector<const Base*> &x,
ArrayView<Base> dep) override {
CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Model library is not ready (possibly closed)")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_zero != nullptr, "No zero order forward function defined in the dynamic library")
CPPADCG_ASSERT_KNOWN(_in.size() == x.size(), "The number of independent variable arrays is invalid")
CPPADCG_ASSERT_KNOWN(dep.size() == _m, "Invalid dependent array size")
Expand All @@ -326,7 +329,7 @@ class FunctorGenericModel : public GenericModel<Base> {
CppAD::vector<bool>& vy,
ArrayView<const Base> tx,
ArrayView<Base> ty) override {
CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Model library is not ready (possibly closed)")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_zero != nullptr, "No zero order forward function defined in the dynamic library")
CPPADCG_ASSERT_KNOWN(_in.size() == 1, "The number of independent variable arrays is higher than 1,"
" please use the variable size methods")
Expand Down Expand Up @@ -361,7 +364,7 @@ class FunctorGenericModel : public GenericModel<Base> {
/// calculate entire Jacobian
void Jacobian(ArrayView<const Base> x,
ArrayView<Base> jac) override {
CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Model library is not ready (possibly closed)")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_jacobian != nullptr, "No Jacobian function defined in the dynamic library")
CPPADCG_ASSERT_KNOWN(_in.size() == 1, "The number of independent variable arrays is higher than 1,"
" please use the variable size methods")
Expand All @@ -384,7 +387,7 @@ class FunctorGenericModel : public GenericModel<Base> {
void Hessian(ArrayView<const Base> x,
ArrayView<const Base> w,
ArrayView<Base> hess) override {
CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Model library is not ready (possibly closed)")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_hessian != nullptr, "No Hessian function defined in the dynamic library")
CPPADCG_ASSERT_KNOWN(_in.size() == 1, "The number of independent variable arrays is higher than 1,"
" please use the variable size methods")
Expand All @@ -408,7 +411,7 @@ class FunctorGenericModel : public GenericModel<Base> {
ArrayView<Base> ty) override {
const size_t k = 1;

CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Model library is not ready (possibly closed)")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_forwardOne != nullptr, "No forward one function defined in the dynamic library")
CPPADCG_ASSERT_KNOWN(tx.size() >= (k + 1) * _n, "Invalid tx size")
CPPADCG_ASSERT_KNOWN(ty.size() >= (k + 1) * _m, "Invalid ty size")
Expand All @@ -426,7 +429,7 @@ class FunctorGenericModel : public GenericModel<Base> {
void ForwardOne(ArrayView<const Base> x,
size_t tx1Nnz, const size_t idx[], const Base tx1[],
ArrayView<Base> ty1) override {
CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Model library is not ready (possibly closed)")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_sparseForwardOne != nullptr, "No sparse forward one function defined in the dynamic library")
CPPADCG_ASSERT_KNOWN(_forwardOneSparsity != nullptr, "No forward one sparsity function defined in the dynamic library")
CPPADCG_ASSERT_KNOWN(x.size() >= _n, "Invalid x size")
Expand Down Expand Up @@ -472,7 +475,7 @@ class FunctorGenericModel : public GenericModel<Base> {
const size_t k = 0;
const size_t k1 = k + 1;

CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Model library is not ready (possibly closed)")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_reverseOne != nullptr, "No reverse one function defined in the dynamic library")
CPPADCG_ASSERT_KNOWN(tx.size() >= k1 * _n, "Invalid tx size")
CPPADCG_ASSERT_KNOWN(ty.size() >= k1 * _m, "Invalid ty size")
Expand All @@ -492,7 +495,7 @@ class FunctorGenericModel : public GenericModel<Base> {
void ReverseOne(ArrayView<const Base> x,
ArrayView<Base> px,
size_t pyNnz, const size_t idx[], const Base py[]) override {
CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Model library is not ready (possibly closed)")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_sparseReverseOne != nullptr, "No sparse reverse one function defined in the dynamic library")
CPPADCG_ASSERT_KNOWN(_reverseOneSparsity != nullptr, "No reverse one sparsity function defined in the dynamic library")
CPPADCG_ASSERT_KNOWN(x.size() >= _n, "Invalid x size")
Expand Down Expand Up @@ -538,7 +541,7 @@ class FunctorGenericModel : public GenericModel<Base> {
const size_t k = 1;
const size_t k1 = k + 1;

CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Model library is not ready (possibly closed)")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_reverseTwo != nullptr, "No sparse reverse two function defined in the dynamic library")
CPPADCG_ASSERT_KNOWN(_in.size() == 1, "The number of independent variable arrays is higher than 1")
CPPADCG_ASSERT_KNOWN(tx.size() >= k1 * _n, "Invalid tx size")
Expand All @@ -561,7 +564,7 @@ class FunctorGenericModel : public GenericModel<Base> {
size_t tx1Nnz, const size_t idx[], const Base tx1[],
ArrayView<Base> px2,
ArrayView<const Base> py2) override {
CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Model library is not ready (possibly closed)")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_sparseReverseTwo != nullptr, "No sparse reverse two function defined in the dynamic library")
CPPADCG_ASSERT_KNOWN(_reverseTwoSparsity != nullptr, "No reverse two sparsity function defined in the dynamic library")
CPPADCG_ASSERT_KNOWN(x.size() >= _n, "Invalid x size")
Expand Down Expand Up @@ -607,7 +610,7 @@ class FunctorGenericModel : public GenericModel<Base> {

void SparseJacobian(ArrayView<const Base> x,
ArrayView<Base> jac) override {
CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Model library is not ready (possibly closed)")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_sparseJacobian != nullptr, "No sparse jacobian function defined in the dynamic library")
CPPADCG_ASSERT_KNOWN(_in.size() == 1, "The number of independent variable arrays is higher than 1,"
" please use the variable size methods")
Expand Down Expand Up @@ -640,7 +643,7 @@ class FunctorGenericModel : public GenericModel<Base> {
std::vector<Base>& jac,
std::vector<size_t>& row,
std::vector<size_t>& col) override {
CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Model library is not ready (possibly closed)")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_sparseJacobian != nullptr, "No sparse Jacobian function defined in the dynamic library")
CPPADCG_ASSERT_KNOWN(_in.size() == 1, "The number of independent variable arrays is higher than 1,"
" please use the variable size methods")
Expand Down Expand Up @@ -669,7 +672,7 @@ class FunctorGenericModel : public GenericModel<Base> {
ArrayView<Base> jac,
size_t const** row,
size_t const** col) override {
CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Model library is not ready (possibly closed)")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_sparseJacobian != nullptr, "No sparse Jacobian function defined in the dynamic library")
CPPADCG_ASSERT_KNOWN(_in.size() == 1, "The number of independent variable arrays is higher than 1,"
" please use the variable size methods")
Expand All @@ -696,7 +699,7 @@ class FunctorGenericModel : public GenericModel<Base> {
ArrayView<Base> jac,
size_t const** row,
size_t const** col) override {
CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Model library is not ready (possibly closed)")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_sparseJacobian != nullptr, "No sparse Jacobian function defined in the dynamic library")
CPPADCG_ASSERT_KNOWN(_in.size() == x.size(), "The number of independent variable arrays is invalid")
CPPADCG_ASSERT_KNOWN(_missingAtomicFunctions == 0, "Some atomic functions used by the compiled model have not been specified yet")
Expand Down Expand Up @@ -725,7 +728,7 @@ class FunctorGenericModel : public GenericModel<Base> {
void SparseHessian(ArrayView<const Base> x,
ArrayView<const Base> w,
ArrayView<Base> hess) override {
CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Model library is not ready (possibly closed)")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_sparseHessian != nullptr, "No sparse Hessian function defined in the dynamic library")
CPPADCG_ASSERT_KNOWN(x.size() == _n, "Invalid independent array size")
CPPADCG_ASSERT_KNOWN(w.size() == _m, "Invalid multiplier array size")
Expand Down Expand Up @@ -759,7 +762,7 @@ class FunctorGenericModel : public GenericModel<Base> {
std::vector<Base>& hess,
std::vector<size_t>& row,
std::vector<size_t>& col) override {
CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Model library is not ready (possibly closed)")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_sparseHessian != nullptr, "No sparse Hessian function defined in the dynamic library")
CPPADCG_ASSERT_KNOWN(x.size() == _n, "Invalid independent array size")
CPPADCG_ASSERT_KNOWN(w.size() == _m, "Invalid multiplier array size")
Expand Down Expand Up @@ -792,7 +795,7 @@ class FunctorGenericModel : public GenericModel<Base> {
ArrayView<Base> hess,
size_t const** row,
size_t const** col) override {
CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Model library is not ready (possibly closed)")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_sparseHessian != nullptr, "No sparse Hessian function defined in the dynamic library")
CPPADCG_ASSERT_KNOWN(_in.size() == 1, "The number of independent variable arrays is higher than 1,"
" please use the variable size methods")
Expand Down Expand Up @@ -821,7 +824,7 @@ class FunctorGenericModel : public GenericModel<Base> {
ArrayView<Base> hess,
size_t const** row,
size_t const** col) override {
CPPADCG_ASSERT_KNOWN(_isLibraryReady, "Model library is not ready (possibly closed)")
CPPADCG_ASSERT_KNOWN(_isLibraryReady, ERROR_LIBRARY_NOT_READY)
CPPADCG_ASSERT_KNOWN(_sparseHessian != nullptr, "No sparse Hessian function defined in the dynamic library")
CPPADCG_ASSERT_KNOWN(_in.size() == x.size(), "The number of independent variable arrays is invalid")
CPPADCG_ASSERT_KNOWN(w.size() == _m, "Invalid multiplier array size")
Expand Down
5 changes: 2 additions & 3 deletions include/cppad/cg/model/model_library.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/* --------------------------------------------------------------------------
* CppADCodeGen: C++ Algorithmic Differentiation with Source Code Generation:
* Copyright (C) 2013 Ciengis
* Copyright (C) 2020 Joao Leal
*
* CppADCodeGen is distributed under multiple licenses:
*
Expand Down Expand Up @@ -121,7 +122,6 @@ class ModelLibrary {
*/
virtual void setThreadPoolNumberOfTimeMeas(unsigned int n) = 0;


/**
* Provides the number of time measurements taken by each computational
* task during multithreaded model evaluations. This is used to schedule
Expand All @@ -134,8 +134,7 @@ class ModelLibrary {
*/
virtual unsigned int getThreadPoolNumberOfTimeMeas() const = 0;

inline virtual ~ModelLibrary() {
}
inline virtual ~ModelLibrary() = default;

};

Expand Down

0 comments on commit 56009f7

Please sign in to comment.