From 7e04b11b6ec52f7446ce940c27eb7774d5ba7993 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 20 Aug 2021 09:36:09 +0200 Subject: [PATCH 01/25] cg: cosmetic change --- include/pycppad/codegen/cg.hpp | 100 ++++++++++++++++----------------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/include/pycppad/codegen/cg.hpp b/include/pycppad/codegen/cg.hpp index c219b6c..fff6384 100644 --- a/include/pycppad/codegen/cg.hpp +++ b/include/pycppad/codegen/cg.hpp @@ -19,7 +19,7 @@ namespace pycppad template class CGVisitor - : public bp::def_visitor< CGVisitor > + : public bp::def_visitor< CGVisitor > { public: typedef ::CppAD::cg::CG CG; @@ -27,85 +27,85 @@ namespace pycppad template void visit(PyClass& cl) const { - cl - .def(bp::init<>(bp::arg("self"),"Default constructor")) - .def(bp::init(bp::args("self","value"), - std::string("Constructor from a ").append(typeid(Scalar).name()).c_str())) - .def(bp::init(bp::args("self","other"),"Copy constructor")) - .def("isIdenticalZero", &CG::isIdenticalZero, bp::arg("self")) - .def("isIdenticalOne", &CG::isIdenticalOne, bp::arg("self")) - .def("isValueDefined", &CG::isValueDefined, bp::arg("self")) - .def("isParameter", &CG::isParameter, bp::arg("self")) - .def("isVariable", &CG::isVariable, bp::arg("self")) - - .def(bp::self + bp::self) - .def(bp::self - bp::self) - .def(bp::self * bp::self) - .def(bp::self / bp::self) - .def(bp::self += bp::self) + cl + .def(bp::init<>(bp::arg("self"),"Default constructor")) + .def(bp::init(bp::args("self","value"), + std::string("Constructor from a ").append(typeid(Scalar).name()).c_str())) + .def(bp::init(bp::args("self","other"),"Copy constructor")) + .def("isIdenticalZero", &CG::isIdenticalZero, bp::arg("self")) + .def("isIdenticalOne", &CG::isIdenticalOne, bp::arg("self")) + .def("isValueDefined", &CG::isValueDefined, bp::arg("self")) + .def("isParameter", &CG::isParameter, bp::arg("self")) + .def("isVariable", &CG::isVariable, bp::arg("self")) + + .def(bp::self + bp::self) + .def(bp::self - bp::self) + .def(bp::self * bp::self) + .def(bp::self / bp::self) + .def(bp::self += bp::self) #ifdef __clang__ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wself-assign-overloaded" #endif - .def(bp::self /= bp::self) - .def(bp::self -= bp::self) // See https://bugs.llvm.org/show_bug.cgi?id=43124 for the bug + .def(bp::self /= bp::self) + .def(bp::self -= bp::self) // See https://bugs.llvm.org/show_bug.cgi?id=43124 for the bug #ifdef __clang__ #pragma GCC diagnostic pop #endif - .def(bp::self *= bp::self) - .add_property("value", - bp::make_function(&CG::getValue, - bp::return_value_policy()), - &CG::setValue) - .def("__str__",&print) - .def("__repr__",&print) - .add_property("__float__", - bp::make_function(&CG::getValue, - bp::return_value_policy()), - &CG::setValue) - .def("__int__",&__int__) - ; + .def(bp::self *= bp::self) + .add_property("value", + bp::make_function(&CG::getValue, + bp::return_value_policy()), + &CG::setValue) + .def("__str__",&print) + .def("__repr__",&print) + .add_property("__float__", + bp::make_function(&CG::getValue, + bp::return_value_policy()), + &CG::setValue) + .def("__int__",&__int__) + ; } private: static std::string print(const CG & self) { - std::stringstream ss; - ss << get_class_name() << "(" << self <<")"; - return ss.str(); + std::stringstream ss; + ss << get_class_name() << "(" << self <<")"; + return ss.str(); } - + static int64_t __int__(const CG & self) { - return static_cast(self.getValue()); + return static_cast(self.getValue()); } - + protected: static std::string & get_class_name() { - static std::string class_name; - return class_name; + static std::string class_name; + return class_name; } static void set_class_name(const std::string & class_name) { - get_class_name() = class_name; + get_class_name() = class_name; } public: - + static void expose(const std::string & class_name = "CG") { - set_class_name(class_name); - bp::class_(class_name.c_str(), - std::string("CG type corresponding to the scalar type ").append(typeid(Scalar).name()).c_str(), - bp::no_init) - .def(CGVisitor()); - - eigenpy::registerNewType(); - eigenpy::registerCommonUfunc(); + set_class_name(class_name); + bp::class_(class_name.c_str(), + std::string("CG type corresponding to the scalar type ").append(typeid(Scalar).name()).c_str(), + bp::no_init) + .def(CGVisitor()); + + eigenpy::registerNewType(); + eigenpy::registerCommonUfunc(); } }; From 0aca3f25db81805561608d6c63ad69530aefe655 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 20 Aug 2021 09:36:26 +0200 Subject: [PATCH 02/25] all: rename PYCPPAD_BUILD_CPPAD_CODEGEN_BINDINGS --- CMakeLists.txt | 2 +- src/cppad.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cd677a0..e8dc0e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,7 +48,7 @@ OPTION(BUILD_CODEGEN_BINDINGS "Build the python bindings for code generation (vi IF(BUILD_CODEGEN_BINDINGS) ADD_PROJECT_DEPENDENCY(cppadcg 2.4.1 REQUIRED PKG_CONFIG_REQUIRES "cppadcg >= 2.4.1") # CppADCodeGen 2.4.1 is the first version to check the minimal version of CppAD - ADD_DEFINITIONS(-DPYCPPAD_BUILD_CPPAD_CODEGEN_BINDINGS) + ADD_DEFINITIONS(-DPYCPPAD_WITH_CPPAD_CODEGEN_BINDINGS) ENDIF(BUILD_CODEGEN_BINDINGS) SET(${PROJECT_NAME}_HEADERS diff --git a/src/cppad.cpp b/src/cppad.cpp index cbdf7ce..13e85bd 100644 --- a/src/cppad.cpp +++ b/src/cppad.cpp @@ -2,8 +2,8 @@ * Copyright 2021 INRIA */ -#ifdef PYCPPAD_BUILD_CPPAD_CODEGEN_BINDINGS -#include "pycppad/codegen/cppadcg-scalar.hpp" +#ifdef PYCPPAD_WITH_CPPAD_CODEGEN_BINDINGS + #include "pycppad/codegen/cppadcg-scalar.hpp" #endif #include "pycppad/cppad.hpp" @@ -17,7 +17,7 @@ namespace pycppad { exposeCppADScalar(); -#ifdef PYCPPAD_BUILD_CPPAD_CODEGEN_BINDINGS +#ifdef PYCPPAD_WITH_CPPAD_CODEGEN_BINDINGS codegen::exposeCppADCGScalar(); #endif From 1e83a7527cf8af5c1d06bb9bafa1dca8f7861427 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 20 Aug 2021 09:50:24 +0200 Subject: [PATCH 03/25] cg: fix guard --- include/pycppad/codegen/cppadcg-scalar.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/pycppad/codegen/cppadcg-scalar.hpp b/include/pycppad/codegen/cppadcg-scalar.hpp index 5ccef8e..5e544c3 100644 --- a/include/pycppad/codegen/cppadcg-scalar.hpp +++ b/include/pycppad/codegen/cppadcg-scalar.hpp @@ -2,8 +2,8 @@ * Copyright 2021 INRIA */ -#ifndef __pycppad_cppad_codegen_scalar_hpp__ -#define __pycppad_cppad_codegen_scalar_hpp__ +#ifndef __pycppad_cppad_codegen_cppadcg_scalar_hpp__ +#define __pycppad_cppad_codegen_cppadcg_scalar_hpp__ #include #include @@ -44,4 +44,4 @@ namespace pycppad } } -#endif // ifndef __pycppad_cppad_scalar_hpp__ +#endif // ifndef __pycppad_cppad_codegen_cppadcg_scalar_hpp__ From f43aa7c182bc72ddb1038f5b5d52b88575ad58da Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 20 Aug 2021 10:05:21 +0200 Subject: [PATCH 04/25] cmake: fix whole packaging --- CMakeLists.txt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e8dc0e8..7234c18 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,6 +58,7 @@ SET(${PROJECT_NAME}_HEADERS include/${PROJECT_NAME}/ad_fun.hpp include/${PROJECT_NAME}/cppad.hpp include/${PROJECT_NAME}/cppad-scalar.hpp + include/${PROJECT_NAME}/utils/scope.hpp ) SET(${PROJECT_NAME}_SOURCES @@ -79,10 +80,16 @@ IF(SUFFIX_SO_VERSION) ENDIF(SUFFIX_SO_VERSION) TARGET_LINK_BOOST_PYTHON(${PROJECT_NAME} PUBLIC) +TARGET_LINK_LIBRARIES(${PROJECT_NAME} PUBLIC cppad eigenpy::eigenpy) +IF(BUILD_CODEGEN_BINDINGS) + TARGET_INCLUDE_DIRECTORIES(${PROJECT_NAME} + SYSTEM PUBLIC + ${cppadcg_INCLUDE_DIR} + ) +ENDIF(BUILD_CODEGEN_BINDINGS) TARGET_INCLUDE_DIRECTORIES(${PROJECT_NAME} SYSTEM PUBLIC - ${cppad_INCLUDE_DIR} ${Boost_INCLUDE_DIRS} ${EIGEN3_INCLUDE_DIR} ${PYTHON_INCLUDE_DIRS} From d72c3b15a52d5d9cea62dea688dee7dd3c9b3f98 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 20 Aug 2021 10:09:08 +0200 Subject: [PATCH 05/25] cmake: check cppadcg before cppad --- CMakeLists.txt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7234c18..502e9ff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,16 +40,17 @@ ADD_PROJECT_DEPENDENCY(Boost REQUIRED) SEARCH_FOR_BOOST_PYTHON(REQUIRED) EXPORT_BOOST_DEFAULT_OPTIONS() +IF(BUILD_CODEGEN_BINDINGS) + ADD_PROJECT_DEPENDENCY(cppadcg 2.4.1 REQUIRED PKG_CONFIG_REQUIRES "cppadcg >= 2.4.1") # CppADCodeGen 2.4.1 is the first version to check the minimal version of CppAD + ADD_DEFINITIONS(-DPYCPPAD_WITH_CPPAD_CODEGEN_BINDINGS) +ENDIF(BUILD_CODEGEN_BINDINGS) + ADD_PROJECT_DEPENDENCY(cppad 20180000.0 REQUIRED PKG_CONFIG_REQUIRES "cppad >= 20180000.0") ADD_PROJECT_DEPENDENCY(Eigen3 REQUIRED PKG_CONFIG_REQUIRES "eigen3 >= 3.0.5") ADD_PROJECT_DEPENDENCY(eigenpy 2.6.6 REQUIRED) OPTION(BUILD_CODEGEN_BINDINGS "Build the python bindings for code generation (via CppADCodeGen)" OFF) -IF(BUILD_CODEGEN_BINDINGS) - ADD_PROJECT_DEPENDENCY(cppadcg 2.4.1 REQUIRED PKG_CONFIG_REQUIRES "cppadcg >= 2.4.1") # CppADCodeGen 2.4.1 is the first version to check the minimal version of CppAD - ADD_DEFINITIONS(-DPYCPPAD_WITH_CPPAD_CODEGEN_BINDINGS) -ENDIF(BUILD_CODEGEN_BINDINGS) SET(${PROJECT_NAME}_HEADERS include/${PROJECT_NAME}/fwd.hpp From 3aafd79a7feda7abf954110f891fa44426e697b7 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 20 Aug 2021 10:10:21 +0200 Subject: [PATCH 06/25] cmake: fixe linkage to cppad --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 502e9ff..7eb2b85 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,7 +81,7 @@ IF(SUFFIX_SO_VERSION) ENDIF(SUFFIX_SO_VERSION) TARGET_LINK_BOOST_PYTHON(${PROJECT_NAME} PUBLIC) -TARGET_LINK_LIBRARIES(${PROJECT_NAME} PUBLIC cppad eigenpy::eigenpy) +TARGET_LINK_LIBRARIES(${PROJECT_NAME} PUBLIC ${cppad_LIBRARY} eigenpy::eigenpy) IF(BUILD_CODEGEN_BINDINGS) TARGET_INCLUDE_DIRECTORIES(${PROJECT_NAME} SYSTEM PUBLIC From 5e69ffd13c1bc06da8829a9df6232f23edc30851 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 20 Aug 2021 10:10:34 +0200 Subject: [PATCH 07/25] cmake: fix linkage to cppadcg --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7eb2b85..d146708 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,7 +51,6 @@ ADD_PROJECT_DEPENDENCY(eigenpy 2.6.6 REQUIRED) OPTION(BUILD_CODEGEN_BINDINGS "Build the python bindings for code generation (via CppADCodeGen)" OFF) - SET(${PROJECT_NAME}_HEADERS include/${PROJECT_NAME}/fwd.hpp include/${PROJECT_NAME}/ad.hpp @@ -87,6 +86,7 @@ IF(BUILD_CODEGEN_BINDINGS) SYSTEM PUBLIC ${cppadcg_INCLUDE_DIR} ) + TARGET_LINK_LIBRARIES(${PROJECT_NAME} PUBLIC ${CMAKE_DL_LIBS}) ENDIF(BUILD_CODEGEN_BINDINGS) TARGET_INCLUDE_DIRECTORIES(${PROJECT_NAME} From 01df2708743da559ece25711b02e3770aeee2703 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 20 Aug 2021 10:24:25 +0200 Subject: [PATCH 08/25] core: add scope helper --- include/pycppad/utils/scope.hpp | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 include/pycppad/utils/scope.hpp diff --git a/include/pycppad/utils/scope.hpp b/include/pycppad/utils/scope.hpp new file mode 100644 index 0000000..7bdda7e --- /dev/null +++ b/include/pycppad/utils/scope.hpp @@ -0,0 +1,36 @@ +// +// Copyright (c) 2019-2021 INRIA +// + +#ifndef __pycppad_utils_scope_hpp__ +#define __pycppad_utils_scope_hpp__ + +#include + +namespace pycppad +{ + + /// + /// \brief Helper to create or simply return an existing namespace in Python + /// + /// \param[in] submodule_name name of the submodule + /// + /// \returns The submodule related to the namespace name. + /// + inline boost::python::scope scope(const std::string & submodule_name) + { + namespace bp = boost::python; + + bp::scope current_scope; + std::string current_scope_name(bp::extract(current_scope.attr("__name__"))); + std::string complete_submodule_name = current_scope_name + "." + submodule_name; + + bp::object submodule(bp::borrowed(PyImport_AddModule(complete_submodule_name.c_str()))); + current_scope.attr(submodule_name.c_str()) = submodule; + + return bp::scope(submodule); + } + +} // namespace pycppad + +#endif // ifndef __pycppad_utils_scope_hpp__ From 25323c11c31e64b7da0e7c01431924c0a2ab5675 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 20 Aug 2021 10:59:43 +0200 Subject: [PATCH 09/25] cg: add codegen scope --- include/pycppad/codegen/cppadcg-scalar.hpp | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/include/pycppad/codegen/cppadcg-scalar.hpp b/include/pycppad/codegen/cppadcg-scalar.hpp index 5e544c3..a1cf8a6 100644 --- a/include/pycppad/codegen/cppadcg-scalar.hpp +++ b/include/pycppad/codegen/cppadcg-scalar.hpp @@ -13,15 +13,16 @@ #include "pycppad/ad.hpp" #include "pycppad/independent.hpp" #include "pycppad/ad_fun.hpp" - +#include "pycppad/utils/scope.hpp" namespace pycppad { - namespace codegen { - + namespace codegen + { template void exposeCppADCGScalar() { + namespace bp = boost::python; typedef ::CppAD::cg::CG CGScalar; typedef ::CppAD::AD ADCGScalar; typedef Eigen::Matrix VectorADCG; @@ -35,11 +36,13 @@ namespace pycppad CGVisitor::expose(); - - pycppad::ADVisitor::expose("ADCG"); - pycppad::ADFunVisitor::expose("ADCGFun"); - pycppad::IndependentVisitor::expose("CGIndependent"); - pycppad::IndependentVisitor::expose("CGIndependent"); + { + bp::scope current_scope = pycppad::scope("codegen"); + pycppad::ADVisitor::expose("CG"); + pycppad::ADFunVisitor::expose("CGFun"); + pycppad::IndependentVisitor::expose("Independent"); + pycppad::IndependentVisitor::expose("Independent"); + } } } } From d024cb6481c94ce378d062af699c647431cdc3af Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 20 Aug 2021 11:00:12 +0200 Subject: [PATCH 10/25] all: enhance cast More flexible and generic --- CMakeLists.txt | 1 + include/pycppad/ad.hpp | 13 ++++------ include/pycppad/cast.hpp | 45 ++++++++++++++++++++++++++++++++++ include/pycppad/codegen/cg.hpp | 33 ++++++++++++++++++++++--- 4 files changed, 80 insertions(+), 12 deletions(-) create mode 100644 include/pycppad/cast.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d146708..f06aaaa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,6 +56,7 @@ SET(${PROJECT_NAME}_HEADERS include/${PROJECT_NAME}/ad.hpp include/${PROJECT_NAME}/independent.hpp include/${PROJECT_NAME}/ad_fun.hpp + include/${PROJECT_NAME}/cast.hpp include/${PROJECT_NAME}/cppad.hpp include/${PROJECT_NAME}/cppad-scalar.hpp include/${PROJECT_NAME}/utils/scope.hpp diff --git a/include/pycppad/ad.hpp b/include/pycppad/ad.hpp index b89510f..3b0cdcc 100644 --- a/include/pycppad/ad.hpp +++ b/include/pycppad/ad.hpp @@ -6,8 +6,10 @@ #define __pycppad_ad_hpp__ #include "pycppad/fwd.hpp" -#include "eigenpy/user-type.hpp" -#include "eigenpy/ufunc.hpp" +#include "pycppad/cast.hpp" + +#include +#include namespace pycppad { @@ -71,7 +73,7 @@ namespace pycppad .def("__repr__",&print) .def("__float__",&::CppAD::Value) - .def("__int__",&__int__) + .def("__int__",&internal::Cast::run) ; } @@ -83,11 +85,6 @@ namespace pycppad ss << get_class_name() << "(" << self <<")"; return ss.str(); } - - static int64_t __int__(const AD & self) - { - return static_cast(::CppAD::Value(self)); - } protected: diff --git a/include/pycppad/cast.hpp b/include/pycppad/cast.hpp new file mode 100644 index 0000000..7cd2b19 --- /dev/null +++ b/include/pycppad/cast.hpp @@ -0,0 +1,45 @@ +/* + * Copyright 2021 INRIA + */ + +#ifndef __pycppad_cast_hpp__ +#define __pycppad_cast_hpp__ + +#include "pycppad/fwd.hpp" + +namespace pycppad +{ + namespace internal + { + + template + struct Cast + { + static To run(const From & from) + { + return static_cast(from); + } + }; + + template + struct CppADValue + { + static Scalar get(const ::CppAD::AD & v) + { + return ::CppAD::Value(v); + } + }; + + template + struct Cast<::CppAD::AD,To> + { + typedef ::CppAD::AD From; + static To run(const From & from) + { + return static_cast(CppADValue::get(from)); + } + }; + } // namespace internal +} // namespace pycppad + +#endif //#ifndef __pycppad_cast_hpp__ diff --git a/include/pycppad/codegen/cg.hpp b/include/pycppad/codegen/cg.hpp index fff6384..cdd7556 100644 --- a/include/pycppad/codegen/cg.hpp +++ b/include/pycppad/codegen/cg.hpp @@ -7,13 +7,38 @@ #include -#include "eigenpy/user-type.hpp" -#include "eigenpy/ufunc.hpp" +#include +#include +#include "pycppad/cast.hpp" namespace pycppad { - namespace codegen { + namespace internal + { + + template + struct CppADValue<::CppAD::cg::CG> + { + static const Scalar & get(const ::CppAD::AD<::CppAD::cg::CG> & v) + { + return ::CppAD::Value<::CppAD::cg::CG>(v).getValue(); + } + }; + + template + struct Cast<::CppAD::cg::CG,To> + { + typedef ::CppAD::cg::CG From; + static To run(const From & from) + { + return static_cast(::CppAD::Value(from).getValue()); + } + }; + } + + namespace codegen + { namespace bp = boost::python; @@ -63,7 +88,7 @@ namespace pycppad bp::make_function(&CG::getValue, bp::return_value_policy()), &CG::setValue) - .def("__int__",&__int__) + .def("__int__",&internal::Cast::run) ; } From bdcfab602bc533742917c1caed1b26628bdeb420 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 20 Aug 2021 11:18:37 +0200 Subject: [PATCH 11/25] core: remove useless code --- include/pycppad/independent.hpp | 26 +++----------------------- 1 file changed, 3 insertions(+), 23 deletions(-) diff --git a/include/pycppad/independent.hpp b/include/pycppad/independent.hpp index 749f04d..5e92e33 100644 --- a/include/pycppad/independent.hpp +++ b/include/pycppad/independent.hpp @@ -19,26 +19,7 @@ namespace pycppad : public bp::def_visitor< IndependentVisitor > { typedef Eigen::Ref RefADVector; - - public: - template - void visit(PyClass&) const - { } - - protected: - - static std::string & get_class_name() - { - static std::string class_name; - return class_name; - } - - static void set_class_name(const std::string & class_name) - { - get_class_name() = class_name; - } - public: static void Independent(RefADVector x, @@ -53,12 +34,11 @@ namespace pycppad return; } - static void expose(const std::string & class_name = "Independent") + static void expose(const std::string & func_name = "Independent") { - set_class_name(class_name); - bp::def(class_name.c_str(),&Independent, + bp::def(func_name.c_str(),&Independent, (bp::arg("x"), bp::arg("abort_op_index") = 0, bp::arg("record_compare") = true), - "define a variable as Independent." + "Define a variable as Independent." "Parameters:\n" "\tx: variable\n" "\tabort_op_index: operator index at which execution will be aborted (during the recording of operations). The value zero corresponds to not aborting (will not match).\n" From 57c667a7be47b1e5b84d924a4efcf8b80eb9669a Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 20 Aug 2021 11:19:00 +0200 Subject: [PATCH 12/25] core: fix name extraction for types --- include/pycppad/ad.hpp | 4 ++-- include/pycppad/codegen/cg.hpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/pycppad/ad.hpp b/include/pycppad/ad.hpp index 3b0cdcc..e2a5680 100644 --- a/include/pycppad/ad.hpp +++ b/include/pycppad/ad.hpp @@ -29,7 +29,7 @@ namespace pycppad cl .def(bp::init<>(bp::arg("self"),"Default constructor")) .def(bp::init(bp::args("self","value"), - std::string("Constructor from a ").append(typeid(Scalar).name()).c_str())) + std::string("Constructor from a ").append(bp::type_id().name()).c_str())) .def(bp::init(bp::args("self","other"),"Copy constructor")) .def(bp::self + bp::self) .def(bp::self - bp::self) @@ -106,7 +106,7 @@ namespace pycppad { set_class_name(class_name); bp::class_(class_name.c_str(), - std::string("AD type corresponding to the scalar type ").append(typeid(Scalar).name()).c_str(), + std::string("AD type corresponding to the scalar type ").append(bp::type_id().name()).c_str(), bp::no_init) .def(ADVisitor()); diff --git a/include/pycppad/codegen/cg.hpp b/include/pycppad/codegen/cg.hpp index cdd7556..5e09343 100644 --- a/include/pycppad/codegen/cg.hpp +++ b/include/pycppad/codegen/cg.hpp @@ -55,7 +55,7 @@ namespace pycppad cl .def(bp::init<>(bp::arg("self"),"Default constructor")) .def(bp::init(bp::args("self","value"), - std::string("Constructor from a ").append(typeid(Scalar).name()).c_str())) + std::string("Constructor from a ").append(bp::type_id().name()).c_str())) .def(bp::init(bp::args("self","other"),"Copy constructor")) .def("isIdenticalZero", &CG::isIdenticalZero, bp::arg("self")) .def("isIdenticalOne", &CG::isIdenticalOne, bp::arg("self")) @@ -125,7 +125,7 @@ namespace pycppad { set_class_name(class_name); bp::class_(class_name.c_str(), - std::string("CG type corresponding to the scalar type ").append(typeid(Scalar).name()).c_str(), + std::string("CG type corresponding to the scalar type ").append(bp::type_id().name()).c_str(), bp::no_init) .def(CGVisitor()); From 6d62e8374ed9a9bda2123c3dd7433a8b60d7a120 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 20 Aug 2021 11:20:22 +0200 Subject: [PATCH 13/25] cg: don't use scope --- include/pycppad/codegen/cppadcg-scalar.hpp | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/include/pycppad/codegen/cppadcg-scalar.hpp b/include/pycppad/codegen/cppadcg-scalar.hpp index a1cf8a6..5ad81d9 100644 --- a/include/pycppad/codegen/cppadcg-scalar.hpp +++ b/include/pycppad/codegen/cppadcg-scalar.hpp @@ -9,11 +9,9 @@ #include #include "pycppad/codegen/cg.hpp" -#include "pycppad/codegen/ad.hpp" #include "pycppad/ad.hpp" #include "pycppad/independent.hpp" #include "pycppad/ad_fun.hpp" -#include "pycppad/utils/scope.hpp" namespace pycppad { @@ -36,13 +34,10 @@ namespace pycppad CGVisitor::expose(); - { - bp::scope current_scope = pycppad::scope("codegen"); - pycppad::ADVisitor::expose("CG"); - pycppad::ADFunVisitor::expose("CGFun"); - pycppad::IndependentVisitor::expose("Independent"); - pycppad::IndependentVisitor::expose("Independent"); - } + pycppad::ADVisitor::expose("ADCG"); + pycppad::ADFunVisitor::expose("ADCGFun"); + pycppad::IndependentVisitor::expose("Independent"); + pycppad::IndependentVisitor::expose("Independent"); } } } From 9f442fb5cac78e69b0d1568f275c8d382a69f77d Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 20 Aug 2021 11:20:30 +0200 Subject: [PATCH 14/25] utils: minor fix --- include/pycppad/utils/scope.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pycppad/utils/scope.hpp b/include/pycppad/utils/scope.hpp index 7bdda7e..1eed6db 100644 --- a/include/pycppad/utils/scope.hpp +++ b/include/pycppad/utils/scope.hpp @@ -28,7 +28,7 @@ namespace pycppad bp::object submodule(bp::borrowed(PyImport_AddModule(complete_submodule_name.c_str()))); current_scope.attr(submodule_name.c_str()) = submodule; - return bp::scope(submodule); + return submodule; } } // namespace pycppad From 7d7c2a56787be8b62dd7cf50d817c8fd62d9106d Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 20 Aug 2021 11:22:07 +0200 Subject: [PATCH 15/25] cg: remove useless file supersede by the cast --- CMakeLists.txt | 1 - include/pycppad/codegen/ad.hpp | 20 -------------------- 2 files changed, 21 deletions(-) delete mode 100644 include/pycppad/codegen/ad.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f06aaaa..dec5dcf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -69,7 +69,6 @@ SET(${PROJECT_NAME}_SOURCES IF(BUILD_CODEGEN_BINDINGS) LIST(APPEND ${PROJECT_NAME}_HEADERS include/${PROJECT_NAME}/codegen/cg.hpp - include/${PROJECT_NAME}/codegen/ad.hpp include/${PROJECT_NAME}/codegen/cppadcg-scalar.hpp ) ENDIF(BUILD_CODEGEN_BINDINGS) diff --git a/include/pycppad/codegen/ad.hpp b/include/pycppad/codegen/ad.hpp deleted file mode 100644 index 008d90c..0000000 --- a/include/pycppad/codegen/ad.hpp +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright 2021 INRIA - */ - -#ifndef __pycppad_codegen_ad_hpp__ -#define __pycppad_codegen_ad_hpp__ - -#include "pycppad/ad.hpp" - -namespace pycppad -{ - - template<> - int64_t ADVisitor<::CppAD::cg::CG >::__int__(const AD & self) - { - return static_cast(::CppAD::Value(self).getValue()); - } -} - -#endif //#ifndef __pycppad_ad_hpp__ From 34d8f0bc416f45aa740b0105a6333b838038e021 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 20 Aug 2021 11:26:01 +0200 Subject: [PATCH 16/25] ci: rename file --- .github/workflows/{cmake.yml => ci-ubuntu.yml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/workflows/{cmake.yml => ci-ubuntu.yml} (100%) diff --git a/.github/workflows/cmake.yml b/.github/workflows/ci-ubuntu.yml similarity index 100% rename from .github/workflows/cmake.yml rename to .github/workflows/ci-ubuntu.yml From 8f7cef49d8a9fbaaa734eaff748c294147d8c13b Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 20 Aug 2021 11:35:22 +0200 Subject: [PATCH 17/25] cmake: add generated files --- CMakeLists.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index dec5dcf..592d204 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.1) SET(PROJECT_NAME pycppad) SET(PROJECT_DESCRIPTION "Python bindings for CppAD and CppADCodeGen using Boost.Python") SET(PROJECT_URL "https://github.com/simple-robotics/pycppad") +SET(PROJECT_CUSTOM_HEADER_EXTENSION "hpp") # Project options OPTION(SUFFIX_SO_VERSION "Suffix library name with its version" ON) @@ -60,6 +61,11 @@ SET(${PROJECT_NAME}_HEADERS include/${PROJECT_NAME}/cppad.hpp include/${PROJECT_NAME}/cppad-scalar.hpp include/${PROJECT_NAME}/utils/scope.hpp + + # Generated headers + ${${PROJECT_NAME}_BINARY_DIR}/include/${PROJECT_NAME}/config.hpp + ${${PROJECT_NAME}_BINARY_DIR}/include/${PROJECT_NAME}/deprecated.hpp + ${${PROJECT_NAME}_BINARY_DIR}/include/${PROJECT_NAME}/warning.hpp ) SET(${PROJECT_NAME}_SOURCES From e2b0fceea777ecda1e16cd092957906c0e055b25 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 20 Aug 2021 11:35:39 +0200 Subject: [PATCH 18/25] cmake: export project --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 592d204..c7831e0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ SET(PROJECT_NAME pycppad) SET(PROJECT_DESCRIPTION "Python bindings for CppAD and CppADCodeGen using Boost.Python") SET(PROJECT_URL "https://github.com/simple-robotics/pycppad") SET(PROJECT_CUSTOM_HEADER_EXTENSION "hpp") +SET(PROJECT_USE_CMAKE_EXPORT TRUE) # Project options OPTION(SUFFIX_SO_VERSION "Suffix library name with its version" ON) From 41f5ec8f8d509fbfab9224ac5ffb5163ccea4a78 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 20 Aug 2021 11:36:01 +0200 Subject: [PATCH 19/25] core: include generated headers --- include/pycppad/fwd.hpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/pycppad/fwd.hpp b/include/pycppad/fwd.hpp index 9ebd9c7..0c7d80c 100644 --- a/include/pycppad/fwd.hpp +++ b/include/pycppad/fwd.hpp @@ -5,6 +5,10 @@ #ifndef __pycppad_fwd_hpp__ #define __pycppad_fwd_hpp__ +#include "pycppad/config.hpp" +#include "pycppad/deprecated.hpp" +#include "pycppad/warning.hpp" + #define BOOST_BIND_GLOBAL_PLACEHOLDERS #include #include From 52bbf60b190ab80ec178cc863f3f6dc72e3ae1a1 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 20 Aug 2021 11:38:01 +0200 Subject: [PATCH 20/25] python: expose version of the project --- python/main.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/python/main.cpp b/python/main.cpp index e0b561b..6cd3801 100644 --- a/python/main.cpp +++ b/python/main.cpp @@ -9,9 +9,26 @@ #include "pycppad/ad.hpp" #include "pycppad/independent.hpp" +#include + +inline std::string printVersion(const std::string & delimiter = ".") +{ + std::ostringstream oss; + oss + << PYCPPAD_MAJOR_VERSION << delimiter + << PYCPPAD_MINOR_VERSION << delimiter + << PYCPPAD_PATCH_VERSION; + return oss.str(); +} + +namespace bp = boost::python; BOOST_PYTHON_MODULE(pycppad) { + bp::docstring_options module_docstring_options(true,true,false); + + bp::scope().attr("__version__") = printVersion(); + bp::scope().attr("__raw_version__") = bp::str(PYCPPAD_VERSION); eigenpy::enableEigenPy(); pycppad::enablePyCppAD(); From a5b60fa0d3a5f6aea3a74251c5990575d9050702 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 20 Aug 2021 12:19:47 +0200 Subject: [PATCH 21/25] cg: remove useless lines --- include/pycppad/codegen/cg.hpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/include/pycppad/codegen/cg.hpp b/include/pycppad/codegen/cg.hpp index 5e09343..37700ff 100644 --- a/include/pycppad/codegen/cg.hpp +++ b/include/pycppad/codegen/cg.hpp @@ -101,11 +101,6 @@ namespace pycppad return ss.str(); } - static int64_t __int__(const CG & self) - { - return static_cast(self.getValue()); - } - protected: static std::string & get_class_name() From c7984f126708fca4de156973d12bf488bd22ebcd Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 20 Aug 2021 14:51:46 +0200 Subject: [PATCH 22/25] cmake: remove useless linkage Already present in the main lib --- python/CMakeLists.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 3ff4061..7d37acf 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -13,8 +13,7 @@ SET_TARGET_PROPERTIES(python PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD True) ADD_LIBRARY(${PYWRAP} SHARED main.cpp) ADD_DEPENDENCIES(python ${PYWRAP}) -TARGET_INCLUDE_DIRECTORIES(${PYWRAP} SYSTEM PUBLIC ${cppad_INCLUDE_DIR}) -TARGET_LINK_LIBRARIES(${PYWRAP} PUBLIC ${PROJECT_NAME} ${cppad_LIBRARY} eigenpy::eigenpy) +TARGET_LINK_LIBRARIES(${PYWRAP} PUBLIC ${PROJECT_NAME}) TARGET_LINK_BOOST_PYTHON(${PYWRAP} PUBLIC) # BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS spews conversion warnings from int to long unsigned int. # Unfortunately, using literals does not work in a macro. As such, this turns them off for the entire wrapper: From 4a807924a73ca58abd34215a0c6835f6a0700dd7 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 20 Aug 2021 14:55:11 +0200 Subject: [PATCH 23/25] cmake: add missing PYTHON_BUILD --- python/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 7d37acf..6adb789 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -42,6 +42,7 @@ SET(PYTHON_FILES ) FOREACH(python ${PYTHON_FILES}) + PYTHON_BUILD(${PROJECT_NAME} ${python}) INSTALL(FILES "${CMAKE_CURRENT_SOURCE_DIR}/pycppad/${python}" DESTINATION ${${PYWRAP}_INSTALL_DIR}) From 6b7ffb2ee196f6a06bb4e772174c748b5df89409 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 20 Aug 2021 19:15:48 +0200 Subject: [PATCH 24/25] cmake: fix options --- CMakeLists.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c7831e0..3d9d191 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,8 +10,6 @@ SET(PROJECT_URL "https://github.com/simple-robotics/pycppad") SET(PROJECT_CUSTOM_HEADER_EXTENSION "hpp") SET(PROJECT_USE_CMAKE_EXPORT TRUE) -# Project options -OPTION(SUFFIX_SO_VERSION "Suffix library name with its version" ON) # Project configuration SET(PROJECT_USE_CMAKE_EXPORT TRUE) @@ -35,6 +33,10 @@ COMPUTE_PROJECT_ARGS(PROJECT_ARGS LANGUAGES CXX) PROJECT(${PROJECT_NAME} ${PROJECT_ARGS}) CHECK_MINIMAL_CXX_STANDARD(11 ENFORCE) +# Project options +OPTION(SUFFIX_SO_VERSION "Suffix library name with its version" ON) +OPTION(BUILD_CODEGEN_BINDINGS "Build the python bindings for code generation (via CppADCodeGen)" OFF) + FINDPYTHON() SET_BOOST_DEFAULT_OPTIONS() @@ -51,8 +53,6 @@ ADD_PROJECT_DEPENDENCY(cppad 20180000.0 REQUIRED PKG_CONFIG_REQUIRES "cppad >= 2 ADD_PROJECT_DEPENDENCY(Eigen3 REQUIRED PKG_CONFIG_REQUIRES "eigen3 >= 3.0.5") ADD_PROJECT_DEPENDENCY(eigenpy 2.6.6 REQUIRED) -OPTION(BUILD_CODEGEN_BINDINGS "Build the python bindings for code generation (via CppADCodeGen)" OFF) - SET(${PROJECT_NAME}_HEADERS include/${PROJECT_NAME}/fwd.hpp include/${PROJECT_NAME}/ad.hpp From f7f071cea9b54dcf0137b63858b1adeb1eb0c3e4 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 20 Aug 2021 19:16:41 +0200 Subject: [PATCH 25/25] cmake: fix variable name --- .github/workflows/ci-ubuntu.yml | 2 +- CMakeLists.txt | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci-ubuntu.yml b/.github/workflows/ci-ubuntu.yml index 53685ab..9149985 100644 --- a/.github/workflows/ci-ubuntu.yml +++ b/.github/workflows/ci-ubuntu.yml @@ -38,7 +38,7 @@ jobs: export PYTHONPATH=$PYTHONPATH:/opt/openrobots/lib/python3/site-packages:/opt/openrobots/lib/python3/dist-packages export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/openrobots/lib64:/opt/openrobots/lib/x86_64-linux-gnu:/opt/openrobots/lib/plugin:/opt/openrobots/lib export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/opt/openrobots/lib/pkgconfig:/opt/openrobots/share/pkgconfig:/opt/openrobots/lib/x86_64-linux-gnu/pkgconfig:/opt/openrobots/lib64/pkgconfig - cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DPYTHON_EXECUTABLE=$(which python3) -DCMAKE_PREFIX_PATH=/opt/openrobots -DBUILD_CODEGEN_BINDINGS=ON + cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DPYTHON_EXECUTABLE=$(which python3) -DCMAKE_PREFIX_PATH=/opt/openrobots -DBUILD_WITH_CPPAD_CODEGEN_BINDINGS=ON - name: Build # Build your program with the given configuration diff --git a/CMakeLists.txt b/CMakeLists.txt index 3d9d191..73e506b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,7 +35,7 @@ CHECK_MINIMAL_CXX_STANDARD(11 ENFORCE) # Project options OPTION(SUFFIX_SO_VERSION "Suffix library name with its version" ON) -OPTION(BUILD_CODEGEN_BINDINGS "Build the python bindings for code generation (via CppADCodeGen)" OFF) +OPTION(BUILD_WITH_CPPAD_CODEGEN_BINDINGS "Build the python bindings for code generation (via CppADCodeGen)" OFF) FINDPYTHON() @@ -44,10 +44,10 @@ ADD_PROJECT_DEPENDENCY(Boost REQUIRED) SEARCH_FOR_BOOST_PYTHON(REQUIRED) EXPORT_BOOST_DEFAULT_OPTIONS() -IF(BUILD_CODEGEN_BINDINGS) +IF(BUILD_WITH_CPPAD_CODEGEN_BINDINGS) ADD_PROJECT_DEPENDENCY(cppadcg 2.4.1 REQUIRED PKG_CONFIG_REQUIRES "cppadcg >= 2.4.1") # CppADCodeGen 2.4.1 is the first version to check the minimal version of CppAD ADD_DEFINITIONS(-DPYCPPAD_WITH_CPPAD_CODEGEN_BINDINGS) -ENDIF(BUILD_CODEGEN_BINDINGS) +ENDIF(BUILD_WITH_CPPAD_CODEGEN_BINDINGS) ADD_PROJECT_DEPENDENCY(cppad 20180000.0 REQUIRED PKG_CONFIG_REQUIRES "cppad >= 20180000.0") ADD_PROJECT_DEPENDENCY(Eigen3 REQUIRED PKG_CONFIG_REQUIRES "eigen3 >= 3.0.5") @@ -73,12 +73,12 @@ SET(${PROJECT_NAME}_SOURCES src/cppad.cpp ) -IF(BUILD_CODEGEN_BINDINGS) +IF(BUILD_WITH_CPPAD_CODEGEN_BINDINGS) LIST(APPEND ${PROJECT_NAME}_HEADERS include/${PROJECT_NAME}/codegen/cg.hpp include/${PROJECT_NAME}/codegen/cppadcg-scalar.hpp ) -ENDIF(BUILD_CODEGEN_BINDINGS) +ENDIF(BUILD_WITH_CPPAD_CODEGEN_BINDINGS) ADD_LIBRARY(${PROJECT_NAME} SHARED ${${PROJECT_NAME}_SOURCES} ${${PROJECT_NAME}_HEADERS}) @@ -88,13 +88,13 @@ ENDIF(SUFFIX_SO_VERSION) TARGET_LINK_BOOST_PYTHON(${PROJECT_NAME} PUBLIC) TARGET_LINK_LIBRARIES(${PROJECT_NAME} PUBLIC ${cppad_LIBRARY} eigenpy::eigenpy) -IF(BUILD_CODEGEN_BINDINGS) +IF(BUILD_WITH_CPPAD_CODEGEN_BINDINGS) TARGET_INCLUDE_DIRECTORIES(${PROJECT_NAME} SYSTEM PUBLIC ${cppadcg_INCLUDE_DIR} ) TARGET_LINK_LIBRARIES(${PROJECT_NAME} PUBLIC ${CMAKE_DL_LIBS}) -ENDIF(BUILD_CODEGEN_BINDINGS) +ENDIF(BUILD_WITH_CPPAD_CODEGEN_BINDINGS) TARGET_INCLUDE_DIRECTORIES(${PROJECT_NAME} SYSTEM PUBLIC