Skip to content

Commit 385ec87

Browse files
authored
Merge pull request #21 from proyan/devel
[code-handler] use Eigen::Ref for the independent and dependent variables in generateCode and makeVariables
2 parents 20cefc6 + 264dc67 commit 385ec87

8 files changed

+51
-56
lines changed

.github/workflows/ci-windows-clang.yml

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ name: PyCppAD CI for Windows - Clang
22
on:
33
pull_request:
44
push:
5-
5+
66
env:
77
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
88
BUILD_TYPE: Release
9-
9+
1010
jobs:
1111
build:
1212
runs-on: ${{ matrix.os }}
@@ -35,7 +35,7 @@ jobs:
3535
- name: Install cmake and update conda
3636
run: |
3737
conda install cmake -c main
38-
- name: Build CppADCodeGen, CppAD, PyCppAD
38+
- name: Build PyCppAD
3939
shell: cmd /C CALL {0}
4040
env:
4141
ACTIONS_ALLOW_UNSECURE_COMMANDS: 'true'
@@ -47,22 +47,23 @@ jobs:
4747
set PATH=%PATH:C:\hostedtoolcache\windows\Boost\1.72.0;=%
4848
4949
call "%programfiles(x86)%\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" amd64
50-
51-
:: Create build directory
52-
git clone --recursive https://github.com/proyan/CppAD
50+
51+
:: Install CppAD
52+
git clone --recursive https://github.com/jcarpent/CppAD
5353
cd CppAD
54+
git checkout topic/windows
5455
mkdir build
5556
pushd build
5657
cmake ^
5758
-G "Visual Studio 16 2019" -T "ClangCl" -DCMAKE_GENERATOR_PLATFORM=x64 ^
5859
-DCMAKE_INSTALL_PREFIX=%CONDA_PREFIX%\Library ^
5960
-DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} ^
6061
..
61-
cmake --build ${{github.workspace}}/CppAD/build --config ${{env.BUILD_TYPE}} --target check install
62-
cd ${{github.workspace}}/CppAD/build/
62+
cmake --build ${{github.workspace}}/CppAD/build --config ${{env.BUILD_TYPE}} --target install
6363
6464
cd ${{github.workspace}}
6565
66+
:: Install CppADCodeGen
6667
git clone --recursive https://github.com/joaoleal/CppADCodeGen
6768
cd CppADCodeGen
6869
mkdir build
@@ -74,13 +75,12 @@ jobs:
7475
-DGOOGLETEST_GIT=ON ^
7576
..
7677
cmake --build ${{github.workspace}}/CppADCodeGen/build --config ${{env.BUILD_TYPE}} --target install
77-
cd ${{github.workspace}}
7878
79+
cd ${{github.workspace}}
7980
8081
mkdir build
8182
pushd build
8283
83-
8484
:: Configure
8585
cmake ^
8686
-G "Visual Studio 16 2019" -T "ClangCl" -DCMAKE_GENERATOR_PLATFORM=x64 ^

.github/workflows/ci-windows-v142.yml

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ name: PyCppAD CI for Windows - (v142)
22
on:
33
pull_request:
44
push:
5-
5+
66
env:
77
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
88
BUILD_TYPE: Release
9-
9+
1010
jobs:
1111
build:
1212
runs-on: ${{ matrix.os }}
@@ -34,7 +34,8 @@ jobs:
3434
- name: Install cmake and update conda
3535
run: |
3636
conda install cmake -c main
37-
37+
conda install cppadcodegen -c conda-forge
38+
3839
- name: Build PyCppAD
3940
shell: cmd /C CALL {0}
4041
env:
@@ -47,7 +48,7 @@ jobs:
4748
set PATH=%PATH:C:\hostedtoolcache\windows\Boost\1.72.0;=%
4849
4950
call "%programfiles(x86)%\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" amd64
50-
51+
5152
:: Create build directory
5253
mkdir build
5354
pushd build

.gitmodules

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[submodule "cmake"]
22
path = cmake
3-
url = git://github.com/jrl-umi3218/jrl-cmakemodules.git
3+
url = https://github.com/jrl-umi3218/jrl-cmakemodules.git

.pre-commit-config.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.2.0
4+
hooks:
5+
- id: check-json
6+
- id: check-symlinks
7+
- id: check-toml
8+
- id: check-yaml

example/cppadcg_c_codegen.py

+4
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,8 @@
4242
langC = LanguageC("double", 3)
4343
nameGen = LangCDefaultVariableNameGenerator("y","x","v","array","sarray")
4444
code = handler.generateCode(langC, jac, nameGen, "source")
45+
output = code.splitlines()
46+
assert(output[0]==' y[1] = 0.5 * x[1] + 0.5 * x[1];')
47+
assert(output[1]==' // dependent variables without operations')
48+
assert(output[2]==' y[0] = 0.5;')
4549
print(code)

include/pycppad/ad_fun.hpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 INRIA
2+
* Copyright 2021-2022 INRIA
33
*/
44

55
#ifndef __pycppad_ad_fun_hpp__
@@ -68,9 +68,11 @@ namespace pycppad
6868
x = x_; y = y_;
6969
}
7070

71-
static ADFun* constructor(const ADVector & x, const ADVector & y)
71+
static ADFun* constructor(RefADVector x, RefADVector y)
7272
{
73-
ADFun * f = new ADFun(x,y);
73+
ADVector x_(x),y_(y);
74+
ADFun * f = new ADFun(x_,y_);
75+
x = x_; y = y_;
7476
return f;
7577
}
7678

include/pycppad/codegen/code-handler.hpp

+17-34
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,37 @@ namespace pycppad
2727
typedef Eigen::Matrix<AD,Eigen::Dynamic,1> VectorAD;
2828
typedef Eigen::Matrix<ADCG,1,Eigen::Dynamic> RowVectorADCG;
2929
typedef Eigen::Matrix<CG,Eigen::Dynamic,1> VectorCG;
30-
typedef Eigen::Matrix<CG,1,Eigen::Dynamic> RowVectorCG;
30+
typedef Eigen::Matrix<CG,1,Eigen::Dynamic> RowVectorCG;
31+
typedef Eigen::Ref<VectorCG> RefVectorCG;
32+
typedef Eigen::Ref<RowVectorCG> RefRowVectorCG;
3133
typedef ::CppAD::cg::CodeHandler<Scalar> CodeHandler;
3234
typedef ::CppAD::cg::LanguageC<Scalar> LanguageC;
3335
typedef ::CppAD::cg::LangCDefaultVariableNameGenerator<Scalar> LangCDefaultVariableNameGenerator;
3436

3537

3638
protected:
37-
template<typename VectorType>
38-
static void makeVariables(CodeHandler& self, const VectorType& x)
39+
40+
template<typename Vector>
41+
static void makeVariables(CodeHandler& self, Eigen::Ref<Vector> x)
3942
{
40-
VectorType& x_= const_cast<VectorType&>(x);
41-
self.makeVariables(x_);
43+
Vector x_(x);
44+
::CppAD::cg::ArrayView<typename Vector::Scalar> independent_av(x_.data(), x_.size());
45+
self.makeVariables(independent_av);
46+
x = x_;
4247
return;
4348
}
4449

45-
template<typename VectorType, typename LangType, typename NameGenType>
50+
template<typename LangType, typename NameGenType>
4651
static std::string generateCode(CodeHandler& self,
4752
LangType& lang,
48-
const VectorType& dependent,
53+
RefVectorCG dependent,
4954
NameGenType& nameGen,
5055
const std::string& jobName)
5156
{
5257
std::ostringstream oss;
53-
VectorType& dependent_= const_cast<VectorType&>(dependent);
54-
::CppAD::cg::ArrayView<typename VectorType::Scalar> dependent_av(dependent_.data(), dependent_.size());
58+
VectorCG dependent_(dependent);
59+
::CppAD::cg::ArrayView<CG> dependent_av(dependent_.data(), dependent_.size());
60+
dependent = dependent_;
5561
self.generateCode(oss, lang, dependent_av, nameGen, jobName);
5662
return oss.str();
5763
}
@@ -86,19 +92,7 @@ namespace pycppad
8692
"Parameters:\n"
8793
"\tvariables: the vector of variables that will become independent variables")
8894
.def("makeVariables",
89-
&CodeHandler::template makeVariables<RowVectorADCG>,
90-
bp::args("self", "variables"),
91-
"Marks the provided variables as being independent variables.\n"
92-
"Parameters:\n"
93-
"\tvariables: the vector of variables that will become independent variables")
94-
.def("makeVariables",
95-
&CodeHandler::template makeVariables<VectorCG>,
96-
bp::args("self", "variables"),
97-
"Marks the provided variables as being independent variables.\n"
98-
"Parameters:\n"
99-
"\tvariables: the vector of variables that will become independent variables")
100-
.def("makeVariables",
101-
&CodeHandler::template makeVariables<RowVectorCG>,
95+
&makeVariables<VectorADCG>,
10296
bp::args("self", "variables"),
10397
"Marks the provided variables as being independent variables.\n"
10498
"Parameters:\n"
@@ -122,18 +116,7 @@ namespace pycppad
122116
"\tid: the atomic function ID.")
123117
//.def("getExternalFuncMaxForwardOrder", &CodeHandler::getExternalFuncMaxForwardOrder, bp::arg("self"))
124118
//.def("getExternalFuncMaxReverseOrder", &CodeHandler::getExternalFuncMaxReverseOrder, bp::arg("self"))
125-
.def("generateCode", &generateCode<VectorCG, LanguageC, LangCDefaultVariableNameGenerator>,
126-
(bp::arg("self"), bp::arg("lang"), bp::arg("dependent"), bp::arg("nameGen"), bp::arg("jobName")="source"),
127-
"Creates the source code from the operations registered so far.\n"
128-
"Parameters:\n"
129-
"\tlang: The targeted language.\n"
130-
"\tdependent: The dependent variables for which the source code\n"
131-
" should be generated. By defining this vector the \n"
132-
" number of operations in the source code can be\n"
133-
" reduced and thus providing a more optimized code.\n"
134-
"\tnameGen: Provides the rules for variable name creation. data related to the model\n"
135-
"\tjobName: Name of this job.")
136-
.def("generateCode", &generateCode<RowVectorCG, LanguageC, LangCDefaultVariableNameGenerator>,
119+
.def("generateCode", &generateCode<LanguageC, LangCDefaultVariableNameGenerator>,
137120
(bp::arg("self"), bp::arg("lang"), bp::arg("dependent"), bp::arg("nameGen"), bp::arg("jobName")="source"),
138121
"Creates the source code from the operations registered so far.\n"
139122
"Parameters:\n"

include/pycppad/independent.hpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,8 @@ namespace pycppad
2727
const bool record_compare_)
2828
{
2929
ADVector x_(x), dynamic(0);
30-
size_t abort_op_index = abort_op_index_;
31-
bool record_compare = record_compare_;
32-
::CppAD::Independent(x_, abort_op_index, record_compare, dynamic);
30+
::CppAD::Independent(x_, abort_op_index_, record_compare_, dynamic);
3331
x = x_;
34-
return;
3532
}
3633

3734
static void expose(const std::string & func_name = "Independent")

0 commit comments

Comments
 (0)