Skip to content

Commit

Permalink
Merge pull request #21 from proyan/devel
Browse files Browse the repository at this point in the history
[code-handler] use Eigen::Ref for the independent and dependent variables in generateCode and makeVariables
  • Loading branch information
jcarpent authored May 19, 2022
2 parents 20cefc6 + 264dc67 commit 385ec87
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 56 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/ci-windows-clang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -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'
Expand All @@ -47,22 +47,23 @@ 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 ^
-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/
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
Expand All @@ -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 ^
Expand Down
9 changes: 5 additions & 4 deletions .github/workflows/ci-windows-v142.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions example/cppadcg_c_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
8 changes: 5 additions & 3 deletions include/pycppad/ad_fun.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 INRIA
* Copyright 2021-2022 INRIA
*/

#ifndef __pycppad_ad_fun_hpp__
Expand Down Expand Up @@ -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;
}

Expand Down
51 changes: 17 additions & 34 deletions include/pycppad/codegen/code-handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,37 @@ namespace pycppad
typedef Eigen::Matrix<AD,Eigen::Dynamic,1> VectorAD;
typedef Eigen::Matrix<ADCG,1,Eigen::Dynamic> RowVectorADCG;
typedef Eigen::Matrix<CG,Eigen::Dynamic,1> VectorCG;
typedef Eigen::Matrix<CG,1,Eigen::Dynamic> RowVectorCG;
typedef Eigen::Matrix<CG,1,Eigen::Dynamic> RowVectorCG;
typedef Eigen::Ref<VectorCG> RefVectorCG;
typedef Eigen::Ref<RowVectorCG> RefRowVectorCG;
typedef ::CppAD::cg::CodeHandler<Scalar> CodeHandler;
typedef ::CppAD::cg::LanguageC<Scalar> LanguageC;
typedef ::CppAD::cg::LangCDefaultVariableNameGenerator<Scalar> LangCDefaultVariableNameGenerator;


protected:
template<typename VectorType>
static void makeVariables(CodeHandler& self, const VectorType& x)

template<typename Vector>
static void makeVariables(CodeHandler& self, Eigen::Ref<Vector> x)
{
VectorType& x_= const_cast<VectorType&>(x);
self.makeVariables(x_);
Vector x_(x);
::CppAD::cg::ArrayView<typename Vector::Scalar> independent_av(x_.data(), x_.size());
self.makeVariables(independent_av);
x = x_;
return;
}

template<typename VectorType, typename LangType, typename NameGenType>
template<typename LangType, typename NameGenType>
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<VectorType&>(dependent);
::CppAD::cg::ArrayView<typename VectorType::Scalar> dependent_av(dependent_.data(), dependent_.size());
VectorCG dependent_(dependent);
::CppAD::cg::ArrayView<CG> dependent_av(dependent_.data(), dependent_.size());
dependent = dependent_;
self.generateCode(oss, lang, dependent_av, nameGen, jobName);
return oss.str();
}
Expand Down Expand Up @@ -86,19 +92,7 @@ namespace pycppad
"Parameters:\n"
"\tvariables: the vector of variables that will become independent variables")
.def("makeVariables",
&CodeHandler::template makeVariables<RowVectorADCG>,
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<VectorCG>,
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<RowVectorCG>,
&makeVariables<VectorADCG>,
bp::args("self", "variables"),
"Marks the provided variables as being independent variables.\n"
"Parameters:\n"
Expand All @@ -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<VectorCG, LanguageC, LangCDefaultVariableNameGenerator>,
(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<RowVectorCG, LanguageC, LangCDefaultVariableNameGenerator>,
.def("generateCode", &generateCode<LanguageC, LangCDefaultVariableNameGenerator>,
(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"
Expand Down
5 changes: 1 addition & 4 deletions include/pycppad/independent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 385ec87

Please sign in to comment.