13 TEST(DiscreteSystemLinearizerADCG, JITCompilationTest)
16 const size_t state_dim = TestDiscreteNonlinearSystem::STATE_DIM;
17 const size_t control_dim = TestDiscreteNonlinearSystem::CONTROL_DIM;
20 typedef DiscreteSystemLinearizerADCG<state_dim, control_dim>::ADCGScalar ADCGScalar;
22 typedef tpl::TestDiscreteNonlinearSystem<ADCGScalar> TestDiscreteNonlinearSystemAD;
29 const double rate = 100.0;
31 shared_ptr<TestDiscreteNonlinearSystemAD> oscillatorAD(
32 new tpl::TestDiscreteNonlinearSystem<ADCGScalar>(AD_ValueType(rate)));
35 DiscreteSystemLinearizer<state_dim, control_dim> systemLinearizer(oscillator);
36 DiscreteSystemLinearizerADCG<state_dim, control_dim> adLinearizer(oscillatorAD);
39 std::cout <<
"compiling..." << std::endl;
40 adLinearizer.compileJIT(
"ADCGCodegenLib");
41 std::cout <<
"... done!" << std::endl;
43 std::shared_ptr<DiscreteSystemLinearizerADCG<state_dim, control_dim>> adLinearizerClone(adLinearizer.clone());
46 if (adLinearizerClone->getLinearizer().getDynamicLib() == adLinearizer.getLinearizer().getDynamicLib())
48 std::cout <<
"FATAL ERROR: dynamic library not cloned correctly in JIT." << std::endl;
53 StateVector<state_dim>
x;
54 ControlVector<control_dim>
u;
57 for (
size_t i = 0;
i < 1000;
i++)
66 systemLinearizer.getAandB(x, u, x, n, 1, A_system, B_system);
71 adLinearizer.getAandB(x, u, x, n, 1, A_ad, B_ad);
75 adLinearizerClone->getAandB(x, u, x, n, 1, A_adCloned, B_adCloned);
78 ASSERT_LT((A_system - A_ad).array().abs().maxCoeff(), 1e-5);
79 ASSERT_LT((B_system - B_ad).array().abs().maxCoeff(), 1e-5);
81 ASSERT_LT((A_system - A_adCloned).array().abs().maxCoeff(), 1e-5);
82 ASSERT_LT((B_system - B_adCloned).array().abs().maxCoeff(), 1e-5);
90 TEST(DiscreteSystemLinearizerADCG, CodegenTest)
93 const size_t state_dim = TestDiscreteNonlinearSystem::STATE_DIM;
94 const size_t control_dim = TestDiscreteNonlinearSystem::CONTROL_DIM;
97 typedef DiscreteSystemLinearizerADCG<state_dim, control_dim>::ADCGScalar ADCGScalar;
99 typedef tpl::TestDiscreteNonlinearSystem<ADCGScalar> TestDiscreteNonlinearSystemAD;
102 const double rate = 100.0;
103 shared_ptr<TestDiscreteNonlinearSystemAD> oscillatorAD(
104 new tpl::TestDiscreteNonlinearSystem<ADCGScalar>(AD_ValueType(rate)));
107 DiscreteSystemLinearizerADCG<state_dim, control_dim> adLinearizer(oscillatorAD);
111 std::cout <<
"generating code..." << std::endl;
113 adLinearizer.generateCode(
"TestDiscreteNonlinearSystemLinearized");
114 std::cout <<
"... done!" << std::endl;
115 }
catch (
const std::runtime_error& e)
117 std::cout <<
"code generation failed: " << e.what() << std::endl;
122 TEST(DiscreteSystemLinearizerADCGMP, JITCompilationTestMP)
125 const size_t state_dim = TestDiscreteNonlinearSystem::STATE_DIM;
126 const size_t control_dim = TestDiscreteNonlinearSystem::CONTROL_DIM;
129 typedef DiscreteSystemLinearizerADCG<state_dim, control_dim>::ADCGScalar ADCGScalar;
131 typedef tpl::TestDiscreteNonlinearSystem<ADCGScalar> TestDiscreteNonlinearSystemAD;
138 const double rate = 100.0;
140 shared_ptr<TestDiscreteNonlinearSystemAD> oscillatorAD(
141 new tpl::TestDiscreteNonlinearSystem<ADCGScalar>(AD_ValueType(rate)));
144 DiscreteSystemLinearizer<state_dim, control_dim> systemLinearizer(oscillator);
145 DiscreteSystemLinearizerADCG<state_dim, control_dim> adLinearizer(oscillatorAD);
146 adLinearizer.compileJIT(
"ADMPTestLib");
149 std::vector<std::shared_ptr<DiscreteSystemLinearizerADCG<state_dim, control_dim>>> adLinearizers;
150 std::vector<std::shared_ptr<DiscreteSystemLinearizer<state_dim, control_dim>>> systemLinearizers;
152 for (
size_t i = 0;
i < nThreads; ++
i)
154 adLinearizers.push_back(
155 std::shared_ptr<DiscreteSystemLinearizerADCG<state_dim, control_dim>>(adLinearizer.clone()));
156 adLinearizers.back()->compileJIT();
157 systemLinearizers.push_back(
158 std::shared_ptr<DiscreteSystemLinearizer<state_dim, control_dim>>(systemLinearizer.clone()));
162 size_t runs = 100000;
164 for (
size_t n = 0;
n < runs; ++
n)
166 std::vector<std::thread> threads;
168 for (
size_t i = 0;
i < nThreads; ++
i)
170 threads.push_back(std::thread([
i, &adLinearizers, &systemLinearizers]() {
171 StateVector<TestDiscreteNonlinearSystem::STATE_DIM>
x;
172 ControlVector<TestDiscreteNonlinearSystem::CONTROL_DIM>
u;
181 systemLinearizers[
i]->getAandB(x, u, x, n, 1, A_system, B_system);
186 adLinearizers[
i]->getAandB(x, u, x, n, 1, A_ad, B_ad);
189 ASSERT_LT((A_system - A_ad).array().abs().maxCoeff(), 1e-5);
190 ASSERT_LT((B_system - B_ad).array().abs().maxCoeff(), 1e-5);
195 for (
auto& thr : threads)
200 TEST(DiscreteSystemLinearizerADCG, FloatTest)
203 const size_t state_dim = TestDiscreteNonlinearSystem::STATE_DIM;
204 const size_t control_dim = TestDiscreteNonlinearSystem::CONTROL_DIM;
207 typedef DiscreteSystemLinearizerADCG<state_dim, control_dim, float>::ADCGScalar ADCGScalarFloat;
208 typedef DiscreteSystemLinearizerADCG<state_dim, control_dim, double>::ADCGScalar ADCGScalarDouble;
210 typedef typename ADCGScalarFloat::value_type AD_ValueTypeFloat;
211 typedef typename ADCGScalarDouble::value_type AD_ValueTypeDouble;
213 typedef tpl::TestDiscreteNonlinearSystem<ADCGScalarFloat> TestDiscreteNonlinearSystemADFloat;
214 typedef tpl::TestDiscreteNonlinearSystem<ADCGScalarDouble> TestDiscreteNonlinearSystemADDouble;
224 const float rateFloat = 100.0;
225 const double rateDouble =
static_cast<double>(rateFloat);
227 shared_ptr<TestDiscreteNonlinearSystemADFloat> oscillatorADFloat(
228 new tpl::TestDiscreteNonlinearSystem<ADCGScalarFloat>(AD_ValueTypeFloat(rateFloat)));
229 shared_ptr<TestDiscreteNonlinearSystemADDouble> oscillatorADDouble(
230 new tpl::TestDiscreteNonlinearSystem<ADCGScalarDouble>(AD_ValueTypeDouble(rateDouble)));
232 DiscreteSystemLinearizerADCG<state_dim, control_dim, float> adLinearizerFloat(oscillatorADFloat);
233 DiscreteSystemLinearizerADCG<state_dim, control_dim, double> adLinearizerDouble(oscillatorADDouble);
236 std::cout <<
"compiling..." << std::endl;
237 adLinearizerFloat.compileJIT(
"ADCGCodegenLibFloat");
238 adLinearizerDouble.compileJIT(
"ADCGCodegenLibDouble");
239 std::cout <<
"... done compiling!" << std::endl;
242 StateVector<state_dim, float> xFloat;
243 StateVector<state_dim, double> xDouble;
244 ControlVector<control_dim, float> uFloat;
245 ControlVector<control_dim, double> uDouble;
248 for (
size_t i = 0;
i < 1000;
i++)
252 xDouble = xFloat.cast<
double>();
254 uDouble = uFloat.cast<
double>();
258 adLinearizerFloat.getAandB(xFloat, uFloat, xFloat, n, 1, A_Float, B_Float);
260 A_typeDouble A_Double;
261 B_typeDouble B_Double;
262 adLinearizerDouble.getAandB(xDouble, uDouble, xDouble, n, 1, A_Double, B_Double);
265 ASSERT_LT((A_Double - A_Float.cast<
double>()).array().abs().maxCoeff(), 1e-5);
266 ASSERT_LT((B_Double - B_Float.cast<
double>()).array().abs().maxCoeff(), 1e-5);
271 std::cout <<
"generating code..." << std::endl;
273 adLinearizerFloat.generateCode(
"TestDiscreteNonlinearSystemLinearizedFloat");
274 adLinearizerDouble.generateCode(
"TestDiscreteNonlinearSystemLinearizedDouble");
275 std::cout <<
"... done!" << std::endl;
276 }
catch (
const std::runtime_error& e)
278 std::cout <<
"code generation failed: " << e.what() << std::endl;
ct::core::ControlVector< control_dim > u
Definition: StateMatrix.h:12
TEST(DiscreteSystemLinearizerADCG, JITCompilationTest)
Definition: DiscreteSystemLinearizerADCGTest.h:13
constexpr size_t n
Definition: MatrixInversionTest.cpp:14
const size_t state_dim
Definition: SymplecticIntegrationTest.cpp:14
tpl::TestDiscreteNonlinearSystem< double > TestDiscreteNonlinearSystem
Definition: TestDiscreteNonlinearSystem.h:56
const size_t control_dim
Definition: SymplecticIntegrationTest.cpp:17
ct::core::StateVector< state_dim > x
SCALAR::value_type AD_ValueType
Definition: StateControlMatrix.h:12