Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ SET(PROJECT_URL "http://github.com/roboptim/roboptim-core")

SET(HEADERS
${CMAKE_SOURCE_DIR}/include/roboptim/core.hh
${CMAKE_SOURCE_DIR}/include/roboptim/core/alloc.hh
${CMAKE_SOURCE_DIR}/include/roboptim/core/cache.hh
${CMAKE_SOURCE_DIR}/include/roboptim/core/cache.hxx
${CMAKE_SOURCE_DIR}/include/roboptim/core/callback/multiplexer.hh
Expand Down
35 changes: 35 additions & 0 deletions include/roboptim/core/alloc.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (C) 2015 by Félix Darricau, AIST, CNRS, EPITA
//
// This file is part of the roboptim.
//
// roboptim is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// roboptim is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with roboptim. If not, see <http://www.gnu.org/licenses/>.

#ifndef ROBOPTIM_CORE_ALLOC_HH
# define ROBOPTIM_CORE_ALLOC_HH

# include <roboptim/core/sys.hh>

# define EIGEN_RUNTIME_NO_MALLOC
# include <Eigen/Core>

namespace roboptim
{
ROBOPTIM_LOCAL
bool is_malloc_allowed_update(bool update, bool new_value);

ROBOPTIM_DLLAPI
bool set_is_malloc_allowed (bool allow);
}

#endif //! ROBOPTIM_CORE_ALLOC_HH
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ namespace roboptim
argument_ref xEps) const
{
#ifndef ROBOPTIM_DO_NOT_CHECK_ALLOCATION
Eigen::internal::set_is_malloc_allowed (true);
set_is_malloc_allowed (true);
#endif //! ROBOPTIM_DO_NOT_CHECK_ALLOCATION

typedef Eigen::Triplet<double> triplet_t;
Expand Down Expand Up @@ -431,6 +431,10 @@ namespace roboptim
}
}
jacobian.setFromTriplets (coefficients.begin (), coefficients.end ());

#ifndef ROBOPTIM_DO_NOT_CHECK_ALLOCATION
set_is_malloc_allowed (false);
#endif //! ROBOPTIM_DO_NOT_CHECK_ALLOCATION
}

template <typename T>
Expand Down Expand Up @@ -668,7 +672,7 @@ namespace roboptim
argument_ref xEps) const
{
#ifndef ROBOPTIM_DO_NOT_CHECK_ALLOCATION
Eigen::internal::set_is_malloc_allowed (true);
set_is_malloc_allowed (true);
#endif //! ROBOPTIM_DO_NOT_CHECK_ALLOCATION

typedef Eigen::Triplet<double> triplet_t;
Expand Down
8 changes: 4 additions & 4 deletions include/roboptim/core/differentiable-function.hh
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ namespace roboptim
assert (argument.size () == this->inputSize ());
assert (isValidJacobian (jacobian));
#ifndef ROBOPTIM_DO_NOT_CHECK_ALLOCATION
Eigen::internal::set_is_malloc_allowed (false);
set_is_malloc_allowed (false);
#endif //! ROBOPTIM_DO_NOT_CHECK_ALLOCATION
this->impl_jacobian (jacobian, argument);
#ifndef ROBOPTIM_DO_NOT_CHECK_ALLOCATION
Eigen::internal::set_is_malloc_allowed (true);
set_is_malloc_allowed (true);
#endif //! ROBOPTIM_DO_NOT_CHECK_ALLOCATION
assert (isValidJacobian (jacobian));
}
Expand Down Expand Up @@ -194,11 +194,11 @@ namespace roboptim
assert (argument.size () == this->inputSize ());
assert (isValidGradient (gradient));
#ifndef ROBOPTIM_DO_NOT_CHECK_ALLOCATION
Eigen::internal::set_is_malloc_allowed (false);
set_is_malloc_allowed (false);
#endif //! ROBOPTIM_DO_NOT_CHECK_ALLOCATION
this->impl_gradient (gradient, argument, functionId);
#ifndef ROBOPTIM_DO_NOT_CHECK_ALLOCATION
Eigen::internal::set_is_malloc_allowed (true);
set_is_malloc_allowed (true);
#endif //! ROBOPTIM_DO_NOT_CHECK_ALLOCATION
assert (isValidGradient (gradient));
}
Expand Down
5 changes: 1 addition & 4 deletions include/roboptim/core/differentiable-function.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,9 @@ namespace roboptim
(jacobian_ref jacobian, const_argument_ref argument)
const
{
#ifndef ROBOPTIM_DO_NOT_CHECK_ALLOCATION
Eigen::internal::set_is_malloc_allowed (true);
#endif //! ROBOPTIM_DO_NOT_CHECK_ALLOCATION

typedef Eigen::Triplet<value_type> triplet_t;
std::vector<triplet_t> coefficients;

for (jacobian_t::Index i = 0; i < this->outputSize (); ++i)
{
gradient_t grad = gradient (argument, i);
Expand Down
7 changes: 4 additions & 3 deletions include/roboptim/core/function.hh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
# include <boost/preprocessor/punctuation/comma.hpp>

# define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
# define EIGEN_RUNTIME_NO_MALLOC

# include <roboptim/core/alloc.hh>
# include <Eigen/Core>
# include <Eigen/Dense>
# include <Eigen/Sparse>
Expand Down Expand Up @@ -476,11 +477,11 @@ namespace roboptim
assert (argument.size () == inputSize ());
assert (isValidResult (result));
#ifndef ROBOPTIM_DO_NOT_CHECK_ALLOCATION
Eigen::internal::set_is_malloc_allowed (false);
set_is_malloc_allowed (false);
#endif //! ROBOPTIM_DO_NOT_CHECK_ALLOCATION
this->impl_compute (result, argument);
#ifndef ROBOPTIM_DO_NOT_CHECK_ALLOCATION
Eigen::internal::set_is_malloc_allowed (true);
set_is_malloc_allowed (true);
#endif //! ROBOPTIM_DO_NOT_CHECK_ALLOCATION
assert (isValidResult (result));
}
Expand Down
14 changes: 12 additions & 2 deletions include/roboptim/core/n-times-derivable-function.hh
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,15 @@ namespace roboptim
size_type functionId = 0) const
{
#ifndef ROBOPTIM_DO_NOT_CHECK_ALLOCATION
Eigen::internal::set_is_malloc_allowed (true);
set_is_malloc_allowed (true);
#endif //! ROBOPTIM_DO_NOT_CHECK_ALLOCATION

derivative_t derivative (derivativeSize ());

#ifndef ROBOPTIM_DO_NOT_CHECK_ALLOCATION
set_is_malloc_allowed (false);
#endif //! ROBOPTIM_DO_NOT_CHECK_ALLOCATION

derivative.setZero ();

this->derivative (derivative, argument[0], 1);
Expand Down Expand Up @@ -250,12 +255,17 @@ namespace roboptim
size_type functionId = 0) const
{
# ifndef ROBOPTIM_DO_NOT_CHECK_ALLOCATION
Eigen::internal::set_is_malloc_allowed (true);
set_is_malloc_allowed (true);
# endif //! ROBOPTIM_DO_NOT_CHECK_ALLOCATION

assert (functionId == 0);

derivative_t derivative (derivativeSize ());

#ifndef ROBOPTIM_DO_NOT_CHECK_ALLOCATION
set_is_malloc_allowed (false);
#endif //! ROBOPTIM_DO_NOT_CHECK_ALLOCATION

derivative.setZero ();

this->derivative (derivative, argument[0], 2);
Expand Down
7 changes: 6 additions & 1 deletion include/roboptim/core/numeric-quadratic-function.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,15 @@ namespace roboptim
(jacobian_ref jacobian, const_argument_ref x) const
{
#ifndef ROBOPTIM_DO_NOT_CHECK_ALLOCATION
Eigen::internal::set_is_malloc_allowed (true);
set_is_malloc_allowed (true);
#endif //! ROBOPTIM_DO_NOT_CHECK_ALLOCATION

Eigen::MatrixXd j = 2 * x.transpose () * a_;

#ifndef ROBOPTIM_DO_NOT_CHECK_ALLOCATION
set_is_malloc_allowed (false);
#endif //! ROBOPTIM_DO_NOT_CHECK_ALLOCATION

for (size_type i = 0; i < this->inputSize (); ++i)
j.coeffRef (0, i) += b_[i];

Expand Down
10 changes: 1 addition & 9 deletions include/roboptim/core/sum-of-c1-squares.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,9 @@ namespace roboptim {
void GenericSumOfC1Squares<T>::
impl_compute(result_ref result, const_argument_ref x) const
{
#ifndef ROBOPTIM_DO_NOT_CHECK_ALLOCATION
Eigen::internal::set_is_malloc_allowed (true);
#endif //! ROBOPTIM_DO_NOT_CHECK_ALLOCATION

computeFunction (x);
value_type sumSquares = 0;
for (size_t i = 0; i < value_.size(); i++) {
for (typename result_t::Index i = 0; i < value_.size(); i++) {
value_type y = value_[i];
sumSquares += y*y;
}
Expand All @@ -80,10 +76,6 @@ namespace roboptim {
impl_gradient(gradient_ref gradient, const_argument_ref x,
size_type ROBOPTIM_DEBUG_ONLY (row)) const
{
#ifndef ROBOPTIM_DO_NOT_CHECK_ALLOCATION
Eigen::internal::set_is_malloc_allowed (true);
#endif //! ROBOPTIM_DO_NOT_CHECK_ALLOCATION

assert (row == 0);
computeFunction (x);
gradient.setZero ();
Expand Down
4 changes: 2 additions & 2 deletions include/roboptim/core/twice-differentiable-function.hh
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ namespace roboptim
"Evaluating hessian at point: " << argument);
assert (isValidHessian (hessian));
#ifndef ROBOPTIM_DO_NOT_CHECK_ALLOCATION
Eigen::internal::set_is_malloc_allowed (false);
set_is_malloc_allowed (false);
#endif //! ROBOPTIM_DO_NOT_CHECK_ALLOCATION
this->impl_hessian (hessian, argument, functionId);
#ifndef ROBOPTIM_DO_NOT_CHECK_ALLOCATION
Eigen::internal::set_is_malloc_allowed (true);
set_is_malloc_allowed (true);
#endif //! ROBOPTIM_DO_NOT_CHECK_ALLOCATION

assert (isValidHessian (hessian));
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ ADD_LIBRARY(roboptim-core SHARED
${HEADERS}
debug.hh
doc.hh
alloc.cc
finite-difference-gradient.cc
generic-solver.cc
indent.cc
Expand Down
37 changes: 37 additions & 0 deletions src/alloc.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (C) 2015 by Félix Darricau, AIST, CNRS, EPITA
//
// This file is part of the roboptim.
//
// roboptim is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// roboptim is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with roboptim. If not, see <http://www.gnu.org/licenses/>.

#include "roboptim/core/alloc.hh"

namespace roboptim
{
bool is_malloc_allowed_update(bool update = false, bool new_value = false)
{
static bool value = true;
if (update)
value = new_value;
return value;
}

/// \brief Manage the calls to Eigen::set_is_malloc_allowed.
bool set_is_malloc_allowed (bool allow)
{
is_malloc_allowed_update(true, allow);

return Eigen::internal::set_is_malloc_allowed(allow);
}
} // end of namespace roboptim.
3 changes: 3 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ ROBOPTIM_CORE_TEST(function-identity)
ROBOPTIM_CORE_TEST(function-sin)
ROBOPTIM_CORE_TEST(function-polynomial)

# Sum Of C1 Squares.
ROBOPTIM_CORE_TEST(sum-of-c1-squares)

# Decorators.
ROBOPTIM_CORE_TEST(decorator-cached-function)
ROBOPTIM_CORE_TEST(decorator-finite-difference-gradient)
Expand Down
4 changes: 2 additions & 2 deletions tests/detail-utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ namespace detail
matrix_t m (2, 2);
m << 1., 2., 3., 4.;

internal::set_is_malloc_allowed (false);
set_is_malloc_allowed (false);
foo (m.row (1));
internal::set_is_malloc_allowed (true);
set_is_malloc_allowed (true);

BOOST_CHECK (!m.row (0).isZero ());
BOOST_CHECK (m.row (1).isZero ());
Expand Down
4 changes: 2 additions & 2 deletions tests/function-pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE (function_pool, T, functionTypes_t)
"Joint position pool");

#ifndef ROBOPTIM_DO_NOT_CHECK_ALLOCATION
Eigen::internal::set_is_malloc_allowed (true);
set_is_malloc_allowed (true);
#endif //! ROBOPTIM_DO_NOT_CHECK_ALLOCATION

// Pre-allocate memory
Expand All @@ -325,7 +325,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE (function_pool, T, functionTypes_t)
fd_denseJac (pool->outputSize (), pool->inputSize ());

#ifndef ROBOPTIM_DO_NOT_CHECK_ALLOCATION
Eigen::internal::set_is_malloc_allowed (false);
set_is_malloc_allowed (false);
#endif //! ROBOPTIM_DO_NOT_CHECK_ALLOCATION

// Call the pool
Expand Down
4 changes: 2 additions & 2 deletions tests/ref.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ BOOST_AUTO_TEST_CASE (ref)
F::jacobian_ref jac_ref (jac);
F::const_argument_ref x_ref (map_x);

Eigen::internal::set_is_malloc_allowed (false);
set_is_malloc_allowed (false);
f.jacobian (jac_ref, x_ref);
Eigen::internal::set_is_malloc_allowed (true);
set_is_malloc_allowed (true);

(*output) << map_x << std::endl;
(*output) << f << std::endl;
Expand Down
Loading