diff --git a/.github/workflows/ci-windows-clang.yml b/.github/workflows/ci-windows-clang.yml index 44bffc8..deb5ca2 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,10 +47,11 @@ 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 + + :: Install CppAD + git clone --recursive https://github.com/jcarpent/CppAD cd CppAD + git checkout topic/windows mkdir build pushd build cmake ^ @@ -58,11 +59,11 @@ 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}} + :: Install CppADCodeGen git clone --recursive https://github.com/joaoleal/CppADCodeGen cd CppADCodeGen mkdir build @@ -74,13 +75,12 @@ jobs: -DGOOGLETEST_GIT=ON ^ .. cmake --build ${{github.workspace}}/CppADCodeGen/build --config ${{env.BUILD_TYPE}} --target install - cd ${{github.workspace}} + 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..d93e549 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,8 @@ 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} env: @@ -47,7 +48,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/.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 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 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) 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; } diff --git a/include/pycppad/codegen/code-handler.hpp b/include/pycppad/codegen/code-handler.hpp index f9fcfe5..f96b6ed 100644 --- a/include/pycppad/codegen/code-handler.hpp +++ b/include/pycppad/codegen/code-handler.hpp @@ -27,31 +27,37 @@ 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) + + template + static void makeVariables(CodeHandler& self, Eigen::Ref x) { - VectorType& x_= const_cast(x); - self.makeVariables(x_); + Vector 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(); } @@ -86,19 +92,7 @@ namespace pycppad "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("makeVariables", - &CodeHandler::template makeVariables, + &makeVariables, bp::args("self", "variables"), "Marks the provided variables as being independent variables.\n" "Parameters:\n" @@ -122,18 +116,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" 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")