From 017e5f51b716032cd91227dfcda7c1f1d64fe1df Mon Sep 17 00:00:00 2001 From: Rohan Budhiraja Date: Fri, 13 May 2022 17:01:09 +0200 Subject: [PATCH 01/12] [code-handler] use Eigen::Ref for the independent and dependent variables in generateCode and makeVariables --- include/pycppad/codegen/code-handler.hpp | 38 ++++++++++-------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/include/pycppad/codegen/code-handler.hpp b/include/pycppad/codegen/code-handler.hpp index f9fcfe5..f904000 100644 --- a/include/pycppad/codegen/code-handler.hpp +++ b/include/pycppad/codegen/code-handler.hpp @@ -27,31 +27,36 @@ namespace pycppad typedef Eigen::Matrix VectorAD; typedef Eigen::Matrix RowVectorADCG; typedef Eigen::Matrix VectorCG; - typedef Eigen::Matrix RowVectorCG; + typedef Eigen::Matrix RowVectorCG; + typedef Eigen::Ref RefVectorCG; + typedef Eigen::Ref RefRowVectorCG; typedef ::CppAD::cg::CodeHandler CodeHandler; typedef ::CppAD::cg::LanguageC LanguageC; typedef ::CppAD::cg::LangCDefaultVariableNameGenerator LangCDefaultVariableNameGenerator; protected: - template - static void makeVariables(CodeHandler& self, const VectorType& x) + + static void makeVariables(CodeHandler& self, RefVectorCG x) { - VectorType& x_= const_cast(x); - self.makeVariables(x_); + VectorCG x_(x); + ::CppAD::cg::ArrayView independent_av(x_.data(), x_.size()); + self.makeVariables(independent_av); + x = x_; return; } - template + template static std::string generateCode(CodeHandler& self, LangType& lang, - const VectorType& dependent, + RefVectorCG dependent, NameGenType& nameGen, const std::string& jobName) { std::ostringstream oss; - VectorType& dependent_= const_cast(dependent); - ::CppAD::cg::ArrayView dependent_av(dependent_.data(), dependent_.size()); + VectorCG dependent_(dependent); + ::CppAD::cg::ArrayView dependent_av(dependent_.data(), dependent_.size()); + dependent = dependent_; self.generateCode(oss, lang, dependent_av, nameGen, jobName); return oss.str(); } @@ -80,7 +85,7 @@ namespace pycppad "\tvariable: the variables that will become independent variable" ) .def("makeVariables", - &makeVariables, + &makeVariables, bp::args("self", "variables"), "Marks the provided variables as being independent variables.\n" "Parameters:\n" @@ -122,18 +127,7 @@ namespace pycppad "\tid: the atomic function ID.") //.def("getExternalFuncMaxForwardOrder", &CodeHandler::getExternalFuncMaxForwardOrder, bp::arg("self")) //.def("getExternalFuncMaxReverseOrder", &CodeHandler::getExternalFuncMaxReverseOrder, bp::arg("self")) - .def("generateCode", &generateCode, - (bp::arg("self"), bp::arg("lang"), bp::arg("dependent"), bp::arg("nameGen"), bp::arg("jobName")="source"), - "Creates the source code from the operations registered so far.\n" - "Parameters:\n" - "\tlang: The targeted language.\n" - "\tdependent: The dependent variables for which the source code\n" - " should be generated. By defining this vector the \n" - " number of operations in the source code can be\n" - " reduced and thus providing a more optimized code.\n" - "\tnameGen: Provides the rules for variable name creation. data related to the model\n" - "\tjobName: Name of this job.") - .def("generateCode", &generateCode, + .def("generateCode", &generateCode, (bp::arg("self"), bp::arg("lang"), bp::arg("dependent"), bp::arg("nameGen"), bp::arg("jobName")="source"), "Creates the source code from the operations registered so far.\n" "Parameters:\n" From c508998d3807ba48d3b8c6de6426f06ffb9c2b55 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 13 May 2022 17:04:31 +0200 Subject: [PATCH 02/12] pre-commit: add default file --- .pre-commit-config.yaml | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 .pre-commit-config.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..93b2372 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,8 @@ +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.2.0 + hooks: + - id: check-json + - id: check-symlinks + - id: check-toml + - id: check-yaml From 77ca0a7d5ace2954326212d91af00f5341a3554f Mon Sep 17 00:00:00 2001 From: Rohan Budhiraja Date: Fri, 13 May 2022 17:24:59 +0200 Subject: [PATCH 03/12] [example] assert on output tested against cpp code --- example/cppadcg_c_codegen.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/example/cppadcg_c_codegen.py b/example/cppadcg_c_codegen.py index 5c5ca8e..923c9b9 100644 --- a/example/cppadcg_c_codegen.py +++ b/example/cppadcg_c_codegen.py @@ -42,4 +42,8 @@ langC = LanguageC("double", 3) nameGen = LangCDefaultVariableNameGenerator("y","x","v","array","sarray") code = handler.generateCode(langC, jac, nameGen, "source") +output = code.splitlines() +assert(output[0]==' y[1] = 0.5 * x[1] + 0.5 * x[1];') +assert(output[1]==' // dependent variables without operations') +assert(output[2]==' y[0] = 0.5;') print(code) From cdbc0a36b3c0e1145d62b2ada43d58ac6bb8a894 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Wed, 18 May 2022 15:37:19 +0200 Subject: [PATCH 04/12] git: change address for submodules --- .gitmodules | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitmodules b/.gitmodules index 60c9164..2b7a4fe 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "cmake"] path = cmake - url = git://github.com/jrl-umi3218/jrl-cmakemodules.git + url = https://github.com/jrl-umi3218/jrl-cmakemodules.git From a370207832e985abbc8f82da135be09267ef820a Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Wed, 18 May 2022 16:48:20 +0200 Subject: [PATCH 05/12] ci/windows: install cppadcodegen from conda --- .github/workflows/ci-windows-clang.yml | 34 ++--------------------- .github/workflows/ci-windows-v142.yml | 8 +++--- .github/workflows/conda/conda-env-win.yml | 1 + 3 files changed, 8 insertions(+), 35 deletions(-) diff --git a/.github/workflows/ci-windows-clang.yml b/.github/workflows/ci-windows-clang.yml index 44bffc8..f288f08 100644 --- a/.github/workflows/ci-windows-clang.yml +++ b/.github/workflows/ci-windows-clang.yml @@ -2,11 +2,11 @@ name: PyCppAD CI for Windows - Clang on: pull_request: push: - + env: # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) BUILD_TYPE: Release - + jobs: build: runs-on: ${{ matrix.os }} @@ -35,7 +35,7 @@ jobs: - name: Install cmake and update conda run: | conda install cmake -c main - - name: Build CppADCodeGen, CppAD, PyCppAD + - name: Build PyCppAD shell: cmd /C CALL {0} env: ACTIONS_ALLOW_UNSECURE_COMMANDS: 'true' @@ -47,40 +47,12 @@ jobs: set PATH=%PATH:C:\hostedtoolcache\windows\Boost\1.72.0;=% call "%programfiles(x86)%\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" amd64 - - :: Create build directory - git clone --recursive https://github.com/proyan/CppAD - cd CppAD - mkdir build - pushd build - cmake ^ - -G "Visual Studio 16 2019" -T "ClangCl" -DCMAKE_GENERATOR_PLATFORM=x64 ^ - -DCMAKE_INSTALL_PREFIX=%CONDA_PREFIX%\Library ^ - -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} ^ - .. - cmake --build ${{github.workspace}}/CppAD/build --config ${{env.BUILD_TYPE}} --target check install - cd ${{github.workspace}}/CppAD/build/ - - cd ${{github.workspace}} - git clone --recursive https://github.com/joaoleal/CppADCodeGen - cd CppADCodeGen - mkdir build - pushd build - cmake ^ - -G "Visual Studio 16 2019" -T "ClangCl" -DCMAKE_GENERATOR_PLATFORM=x64 ^ - -DCMAKE_INSTALL_PREFIX=%CONDA_PREFIX%\Library ^ - -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} ^ - -DGOOGLETEST_GIT=ON ^ - .. - cmake --build ${{github.workspace}}/CppADCodeGen/build --config ${{env.BUILD_TYPE}} --target install cd ${{github.workspace}} - mkdir build pushd build - :: Configure cmake ^ -G "Visual Studio 16 2019" -T "ClangCl" -DCMAKE_GENERATOR_PLATFORM=x64 ^ diff --git a/.github/workflows/ci-windows-v142.yml b/.github/workflows/ci-windows-v142.yml index 6fd7fbb..3d4c8aa 100644 --- a/.github/workflows/ci-windows-v142.yml +++ b/.github/workflows/ci-windows-v142.yml @@ -2,11 +2,11 @@ name: PyCppAD CI for Windows - (v142) on: pull_request: push: - + env: # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) BUILD_TYPE: Release - + jobs: build: runs-on: ${{ matrix.os }} @@ -34,7 +34,7 @@ jobs: - name: Install cmake and update conda run: | conda install cmake -c main - + - name: Build PyCppAD shell: cmd /C CALL {0} env: @@ -47,7 +47,7 @@ jobs: set PATH=%PATH:C:\hostedtoolcache\windows\Boost\1.72.0;=% call "%programfiles(x86)%\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" amd64 - + :: Create build directory mkdir build pushd build diff --git a/.github/workflows/conda/conda-env-win.yml b/.github/workflows/conda/conda-env-win.yml index d8f75b1..89c99bd 100644 --- a/.github/workflows/conda/conda-env-win.yml +++ b/.github/workflows/conda/conda-env-win.yml @@ -6,3 +6,4 @@ dependencies: - boost - eigenpy - python + - cppadcodegen From 26f0c730b69f46a05ae138816588168c64a11470 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Wed, 18 May 2022 17:33:26 +0200 Subject: [PATCH 06/12] ci/windows: build CppAD from source --- .github/workflows/ci-windows-clang.yml | 29 ++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/.github/workflows/ci-windows-clang.yml b/.github/workflows/ci-windows-clang.yml index f288f08..2a74221 100644 --- a/.github/workflows/ci-windows-clang.yml +++ b/.github/workflows/ci-windows-clang.yml @@ -48,6 +48,35 @@ jobs: call "%programfiles(x86)%\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" amd64 + :: Install CppAD + git clone --recursive https://github.com/jcarpent/CppAD + cd CppAD + git checkout topic/windows + mkdir build + pushd build + cmake ^ + -G "Visual Studio 16 2019" -T "ClangCl" -DCMAKE_GENERATOR_PLATFORM=x64 ^ + -DCMAKE_INSTALL_PREFIX=%CONDA_PREFIX%\Library ^ + -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} ^ + .. + cmake --build ${{github.workspace}}/CppAD/build --config ${{env.BUILD_TYPE}} --target check install + cd ${{github.workspace}}/CppAD/build/ + + cd ${{github.workspace}} + + :: Install CppADCodeGen + git clone --recursive https://github.com/joaoleal/CppADCodeGen + cd CppADCodeGen + mkdir build + pushd build + cmake ^ + -G "Visual Studio 16 2019" -T "ClangCl" -DCMAKE_GENERATOR_PLATFORM=x64 ^ + -DCMAKE_INSTALL_PREFIX=%CONDA_PREFIX%\Library ^ + -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} ^ + -DGOOGLETEST_GIT=ON ^ + .. + cmake --build ${{github.workspace}}/CppADCodeGen/build --config ${{env.BUILD_TYPE}} --target install + cd ${{github.workspace}} mkdir build From dac23ddaeb2d27ab5c8324918a7e23c4084e7934 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Thu, 19 May 2022 10:21:04 +0200 Subject: [PATCH 07/12] cppadcg: fix ADFun constructor --- include/pycppad/ad_fun.hpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/include/pycppad/ad_fun.hpp b/include/pycppad/ad_fun.hpp index 570ea6d..dda718a 100644 --- a/include/pycppad/ad_fun.hpp +++ b/include/pycppad/ad_fun.hpp @@ -1,5 +1,5 @@ /* - * Copyright 2021 INRIA + * Copyright 2021-2022 INRIA */ #ifndef __pycppad_ad_fun_hpp__ @@ -68,9 +68,11 @@ namespace pycppad x = x_; y = y_; } - static ADFun* constructor(const ADVector & x, const ADVector & y) + static ADFun* constructor(RefADVector x, RefADVector y) { - ADFun * f = new ADFun(x,y); + ADVector x_(x),y_(y); + ADFun * f = new ADFun(x_,y_); + x = x_; y = y_; return f; } From 31c1b98411e82c7c86b0b463a7440609518e749f Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Thu, 19 May 2022 10:41:15 +0200 Subject: [PATCH 08/12] independant: fix logic --- include/pycppad/independent.hpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/include/pycppad/independent.hpp b/include/pycppad/independent.hpp index 5e92e33..55f26a7 100644 --- a/include/pycppad/independent.hpp +++ b/include/pycppad/independent.hpp @@ -27,11 +27,8 @@ namespace pycppad const bool record_compare_) { ADVector x_(x), dynamic(0); - size_t abort_op_index = abort_op_index_; - bool record_compare = record_compare_; - ::CppAD::Independent(x_, abort_op_index, record_compare, dynamic); + ::CppAD::Independent(x_, abort_op_index_, record_compare_, dynamic); x = x_; - return; } static void expose(const std::string & func_name = "Independent") From b5d1f2966f76eac6ecdb572bdf983896ea735dd0 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Thu, 19 May 2022 10:41:33 +0200 Subject: [PATCH 09/12] cppadcg: fix makeVariables --- include/pycppad/codegen/code-handler.hpp | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/include/pycppad/codegen/code-handler.hpp b/include/pycppad/codegen/code-handler.hpp index f904000..c1e995c 100644 --- a/include/pycppad/codegen/code-handler.hpp +++ b/include/pycppad/codegen/code-handler.hpp @@ -37,7 +37,8 @@ namespace pycppad protected: - static void makeVariables(CodeHandler& self, RefVectorCG x) + template + static void makeVariables(CodeHandler& self, Eigen::Ref x) { VectorCG x_(x); ::CppAD::cg::ArrayView independent_av(x_.data(), x_.size()); @@ -85,7 +86,7 @@ namespace pycppad "\tvariable: the variables that will become independent variable" ) .def("makeVariables", - &makeVariables, + &makeVariables, bp::args("self", "variables"), "Marks the provided variables as being independent variables.\n" "Parameters:\n" @@ -96,18 +97,6 @@ namespace pycppad "Marks the provided variables as being independent variables.\n" "Parameters:\n" "\tvariables: the vector of variables that will become independent variables") - .def("makeVariables", - &CodeHandler::template makeVariables, - bp::args("self", "variables"), - "Marks the provided variables as being independent variables.\n" - "Parameters:\n" - "\tvariables: the vector of variables that will become independent variables") - .def("makeVariables", - &CodeHandler::template makeVariables, - bp::args("self", "variables"), - "Marks the provided variables as being independent variables.\n" - "Parameters:\n" - "\tvariables: the vector of variables that will become independent variables") .def("getIndependentVariableSize", &CodeHandler::getIndependentVariableSize, bp::arg("self")) .def("getIndependentVariableIndex", &CodeHandler::getIndependentVariableIndex, bp::args("self", "var")) .def("getMaximumVariableID", &CodeHandler::getMaximumVariableID, bp::arg("self")) From 28dd510e4ee8593b7aba0c970fd2c7ae7ad5aff5 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Thu, 19 May 2022 14:31:26 +0200 Subject: [PATCH 10/12] core: fix call --- include/pycppad/codegen/code-handler.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pycppad/codegen/code-handler.hpp b/include/pycppad/codegen/code-handler.hpp index c1e995c..2dacbcb 100644 --- a/include/pycppad/codegen/code-handler.hpp +++ b/include/pycppad/codegen/code-handler.hpp @@ -92,7 +92,7 @@ namespace pycppad "Parameters:\n" "\tvariables: the vector of variables that will become independent variables") .def("makeVariables", - &CodeHandler::template makeVariables, + &makeVariables, bp::args("self", "variables"), "Marks the provided variables as being independent variables.\n" "Parameters:\n" From 28e1874e632a0d89aa791ead1a12993217c120d3 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Thu, 19 May 2022 14:31:46 +0200 Subject: [PATCH 11/12] ci: use local build for windows-clang --- .github/workflows/ci-windows-clang.yml | 3 +-- .github/workflows/ci-windows-v142.yml | 1 + .github/workflows/conda/conda-env-win.yml | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci-windows-clang.yml b/.github/workflows/ci-windows-clang.yml index 2a74221..deb5ca2 100644 --- a/.github/workflows/ci-windows-clang.yml +++ b/.github/workflows/ci-windows-clang.yml @@ -59,8 +59,7 @@ jobs: -DCMAKE_INSTALL_PREFIX=%CONDA_PREFIX%\Library ^ -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} ^ .. - cmake --build ${{github.workspace}}/CppAD/build --config ${{env.BUILD_TYPE}} --target check install - cd ${{github.workspace}}/CppAD/build/ + cmake --build ${{github.workspace}}/CppAD/build --config ${{env.BUILD_TYPE}} --target install cd ${{github.workspace}} diff --git a/.github/workflows/ci-windows-v142.yml b/.github/workflows/ci-windows-v142.yml index 3d4c8aa..d93e549 100644 --- a/.github/workflows/ci-windows-v142.yml +++ b/.github/workflows/ci-windows-v142.yml @@ -34,6 +34,7 @@ jobs: - name: Install cmake and update conda run: | conda install cmake -c main + conda install cppadcodegen -c conda-forge - name: Build PyCppAD shell: cmd /C CALL {0} diff --git a/.github/workflows/conda/conda-env-win.yml b/.github/workflows/conda/conda-env-win.yml index 89c99bd..d8f75b1 100644 --- a/.github/workflows/conda/conda-env-win.yml +++ b/.github/workflows/conda/conda-env-win.yml @@ -6,4 +6,3 @@ dependencies: - boost - eigenpy - python - - cppadcodegen From 264dc67cf0797d1529c0753a736e18a51cb7a16b Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Thu, 19 May 2022 14:52:00 +0200 Subject: [PATCH 12/12] cppadcg: fix Scalar type --- include/pycppad/codegen/code-handler.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/pycppad/codegen/code-handler.hpp b/include/pycppad/codegen/code-handler.hpp index 2dacbcb..f96b6ed 100644 --- a/include/pycppad/codegen/code-handler.hpp +++ b/include/pycppad/codegen/code-handler.hpp @@ -37,11 +37,11 @@ namespace pycppad protected: - template - static void makeVariables(CodeHandler& self, Eigen::Ref x) + template + static void makeVariables(CodeHandler& self, Eigen::Ref x) { - VectorCG x_(x); - ::CppAD::cg::ArrayView independent_av(x_.data(), x_.size()); + Vector x_(x); + ::CppAD::cg::ArrayView independent_av(x_.data(), x_.size()); self.makeVariables(independent_av); x = x_; return;