13 TEST(ADCodegenLinearizerTest, JITCompilationTest)
16 const size_t state_dim = TestNonlinearSystem::STATE_DIM;
17 const size_t control_dim = TestNonlinearSystem::CONTROL_DIM;
20 typedef ADCodegenLinearizer<state_dim, control_dim>::ADCGScalar
Scalar;
22 typedef tpl::TestNonlinearSystem<Scalar> TestNonlinearSystemAD;
29 const double w_n = 100.0;
31 shared_ptr<TestNonlinearSystemAD> oscillatorAD(
new tpl::TestNonlinearSystem<Scalar>(AD_ValueType(w_n)));
34 SystemLinearizer<state_dim, control_dim> systemLinearizer(oscillator);
35 ADCodegenLinearizer<state_dim, control_dim> adLinearizer(oscillatorAD);
38 std::cout <<
"compiling..." << std::endl;
39 adLinearizer.compileJIT(
"ADCGCodegenLib");
40 std::cout <<
"... done!" << std::endl;
42 std::shared_ptr<ADCodegenLinearizer<state_dim, control_dim>> adLinearizerClone(adLinearizer.clone());
43 std::cout <<
"compiling the clone..." << std::endl;
44 adLinearizerClone->compileJIT(
"ADCGCodegenLibCone");
45 std::cout <<
"... done!" << std::endl;
48 StateVector<TestNonlinearSystem::STATE_DIM>
x;
49 ControlVector<TestNonlinearSystem::CONTROL_DIM>
u;
52 for (
size_t i = 0;
i < 1000;
i++)
59 A_type A_system = systemLinearizer.getDerivativeState(x, u, t);
60 B_type B_system = systemLinearizer.getDerivativeControl(x, u, t);
63 A_type A_ad = adLinearizer.getDerivativeState(x, u, t);
64 B_type B_ad = adLinearizer.getDerivativeControl(x, u, t);
66 A_type A_adCloned = adLinearizerClone->getDerivativeState(x, u, t);
67 B_type B_adCloned = adLinearizerClone->getDerivativeControl(x, u, t);
70 ASSERT_LT((A_system - A_ad).array().abs().maxCoeff(), 1e-5);
71 ASSERT_LT((B_system - B_ad).array().abs().maxCoeff(), 1e-5);
73 ASSERT_LT((A_system - A_adCloned).array().abs().maxCoeff(), 1e-5);
74 ASSERT_LT((B_system - B_adCloned).array().abs().maxCoeff(), 1e-5);
82 TEST(ADCodegenLinearizerTest, JITCloneTest)
85 const size_t state_dim = TestNonlinearSystem::STATE_DIM;
86 const size_t control_dim = TestNonlinearSystem::CONTROL_DIM;
89 typedef ADCodegenLinearizer<state_dim, control_dim>::ADCGScalar
Scalar;
91 typedef tpl::TestNonlinearSystem<Scalar> TestNonlinearSystemAD;
98 const double w_n = 100.0;
100 shared_ptr<TestNonlinearSystemAD> oscillatorAD(
new tpl::TestNonlinearSystem<Scalar>(AD_ValueType(w_n)));
103 SystemLinearizer<state_dim, control_dim> systemLinearizer(oscillator);
104 ADCodegenLinearizer<state_dim, control_dim> adLinearizer(oscillatorAD);
107 std::cout <<
"compiling..." << std::endl;
108 adLinearizer.compileJIT(
"ADCGCodegenLib");
109 std::cout <<
"... done!" << std::endl;
112 std::cout <<
"cloning without compilation..." << std::endl;
113 std::shared_ptr<ADCodegenLinearizer<state_dim, control_dim>> adLinearizerClone(adLinearizer.clone());
116 if (adLinearizerClone->getLinearizer().getDynamicLib() == adLinearizer.getLinearizer().getDynamicLib())
118 std::cout <<
"FATAL ERROR: dynamic library not cloned correctly in JIT." << std::endl;
123 StateVector<TestNonlinearSystem::STATE_DIM>
x;
124 ControlVector<TestNonlinearSystem::CONTROL_DIM>
u;
127 for (
size_t i = 0;
i < 1000;
i++)
134 A_type A_system = systemLinearizer.getDerivativeState(x, u, t);
135 B_type B_system = systemLinearizer.getDerivativeControl(x, u, t);
138 A_type A_ad = adLinearizer.getDerivativeState(x, u, t);
139 B_type B_ad = adLinearizer.getDerivativeControl(x, u, t);
141 A_type A_adCloned = adLinearizerClone->getDerivativeState(x, u, t);
142 B_type B_adCloned = adLinearizerClone->getDerivativeControl(x, u, t);
145 ASSERT_LT((A_system - A_ad).array().abs().maxCoeff(), 1e-5);
146 ASSERT_LT((B_system - B_ad).array().abs().maxCoeff(), 1e-5);
148 ASSERT_LT((A_system - A_adCloned).array().abs().maxCoeff(), 1e-5);
149 ASSERT_LT((B_system - B_adCloned).array().abs().maxCoeff(), 1e-5);
157 TEST(ADCodegenLinearizerTest, CodegenTest)
160 const size_t state_dim = TestNonlinearSystem::STATE_DIM;
161 const size_t control_dim = TestNonlinearSystem::CONTROL_DIM;
164 typedef ADCodegenLinearizer<state_dim, control_dim>::ADCGScalar
Scalar;
166 typedef tpl::TestNonlinearSystem<Scalar> TestNonlinearSystemAD;
169 const double w_n = 100.0;
170 shared_ptr<TestNonlinearSystemAD> oscillatorAD(
new tpl::TestNonlinearSystem<Scalar>(AD_ValueType(w_n)));
173 ADCodegenLinearizer<state_dim, control_dim> adLinearizer(oscillatorAD);
177 std::cout <<
"generating code..." << std::endl;
179 adLinearizer.generateCode(
"TestNonlinearSystemLinearized");
180 std::cout <<
"... done!" << std::endl;
181 }
catch (
const std::runtime_error& e)
183 std::cout <<
"code generation failed: " << e.what() << std::endl;
188 TEST(ADCodegenLinearizerTestMP, JITCompilationTestMP)
190 const size_t state_dim = TestNonlinearSystem::STATE_DIM;
191 const size_t control_dim = TestNonlinearSystem::CONTROL_DIM;
194 typedef ADCodegenLinearizer<state_dim, control_dim>::ADCGScalar
Scalar;
196 typedef tpl::TestNonlinearSystem<Scalar> TestNonlinearSystemAD;
199 typedef Eigen::Matrix<double, state_dim, state_dim> A_type;
200 typedef Eigen::Matrix<double, state_dim, control_dim> B_type;
203 const double w_n = 100.0;
205 shared_ptr<TestNonlinearSystemAD> oscillatorAD(
new tpl::TestNonlinearSystem<Scalar>(AD_ValueType(w_n)));
208 SystemLinearizer<state_dim, control_dim> systemLinearizer(oscillator);
210 ADCodegenLinearizer<state_dim, control_dim> adLinearizer(oscillatorAD);
211 adLinearizer.compileJIT(
"ADMPTestLib");
214 std::vector<std::shared_ptr<ADCodegenLinearizer<state_dim, control_dim>>> adLinearizers;
215 std::vector<std::shared_ptr<SystemLinearizer<state_dim, control_dim>>> systemLinearizers;
217 for (
size_t i = 0;
i < nThreads; ++
i)
219 adLinearizers.push_back(std::shared_ptr<ADCodegenLinearizer<state_dim, control_dim>>(adLinearizer.clone()));
220 adLinearizers.back()->compileJIT();
221 systemLinearizers.push_back(
222 std::shared_ptr<SystemLinearizer<state_dim, control_dim>>(systemLinearizer.clone()));
226 size_t runs = 100000;
228 for (
size_t n = 0;
n < runs; ++
n)
230 std::vector<std::thread> threads;
232 for (
size_t i = 0;
i < nThreads; ++
i)
234 threads.push_back(std::thread([
i, &adLinearizers, &systemLinearizers]() {
235 StateVector<TestNonlinearSystem::STATE_DIM>
x;
236 ControlVector<TestNonlinearSystem::CONTROL_DIM>
u;
243 A_type A_system = systemLinearizers[
i]->getDerivativeState(x, u, t);
244 B_type B_system = systemLinearizers[
i]->getDerivativeControl(x, u, t);
247 A_type A_ad = adLinearizers[
i]->getDerivativeState(x, u, t);
248 B_type B_ad = adLinearizers[
i]->getDerivativeControl(x, u, t);
251 ASSERT_LT((A_system - A_ad).array().abs().maxCoeff(), 1e-5);
252 ASSERT_LT((B_system - B_ad).array().abs().maxCoeff(), 1e-5);
257 for (
auto& thr : threads)
tpl::TestNonlinearSystem< double > TestNonlinearSystem
Definition: TestNonlinearSystem.h:53
ct::core::ControlVector< control_dim > u
Definition: StateMatrix.h:12
ct::core::ADCodegenLinearizer< state_dim, control_dim >::ADCGScalar Scalar
clear all close all load ct GNMSLog0 mat reformat t
constexpr size_t n
Definition: MatrixInversionTest.cpp:14
const size_t state_dim
Definition: SymplecticIntegrationTest.cpp:14
const size_t control_dim
Definition: SymplecticIntegrationTest.cpp:17
TEST(ADCodegenLinearizerTest, JITCompilationTest)
Definition: ADCodegenLinearizerTest.h:13
ct::core::StateVector< state_dim > x
SCALAR::value_type AD_ValueType
Definition: StateControlMatrix.h:12