Skip to content

Commit 0303c00

Browse files
committed
Add variant of FirstOrderReverse with vector mode.
Add associated reference results.
1 parent 744b28c commit 0303c00

64 files changed

Lines changed: 1299 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
* OpDiLib, an Open Multiprocessing Differentiation Library
3+
*
4+
* Copyright (C) 2020-2022 Chair for Scientific Computing (SciComp), TU Kaiserslautern
5+
* Copyright (C) 2023-2025 Chair for Scientific Computing (SciComp), University of Kaiserslautern-Landau
6+
* Homepage: https://scicomp.rptu.de
7+
* Contact: Prof. Nicolas R. Gauger (opdi@scicomp.uni-kl.de)
8+
*
9+
* Lead developer: Johannes Blühdorn (SciComp, University of Kaiserslautern-Landau)
10+
*
11+
* This file is part of OpDiLib (https://scicomp.rptu.de/software/opdi).
12+
*
13+
* OpDiLib is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
14+
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
15+
* version.
16+
*
17+
* OpDiLib is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
18+
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
19+
* details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License along with OpDiLib. If not, see
22+
* <http://www.gnu.org/licenses/>.
23+
*
24+
*/
25+
26+
#pragma once
27+
28+
#include <array>
29+
#include <cstdlib>
30+
#include <iostream>
31+
32+
#include "codi.hpp"
33+
34+
#ifndef BUILD_REFERENCE
35+
#ifdef OPDI_USE_MACRO_BACKEND
36+
#include "opdi/backend/macro/macroBackend.hpp"
37+
#elif OPDI_USE_OMPT_BACKEND
38+
#include "opdi/backend/ompt/omptBackend.hpp"
39+
#endif
40+
#include "opdi.hpp"
41+
#ifdef OUTPUT_INSTRUMENT
42+
#include "opdi/logic/omp/instrument/ompLogicOutputInstrument.hpp"
43+
#endif
44+
#else
45+
#include "opdi/helpers/emptyMacros.hpp"
46+
#endif
47+
48+
#include "driverBase.hpp"
49+
50+
#ifdef BUILD_REFERENCE
51+
using TestReal = codi::RealReverseIndexVec<2>;
52+
#else
53+
using TestReal = codi::RealReverseIndexVecOpenMP<2>;
54+
55+
OPDI_DECLARE_REDUCTION(+, TestReal, +, 0.0);
56+
OPDI_DECLARE_REDUCTION(*, TestReal, *, 1.0);
57+
#endif
58+
59+
template<typename _Case>
60+
struct DriverFirstOrderReverseVector : public DriverBase<DriverFirstOrderReverseVector<_Case> > {
61+
public:
62+
63+
using Case = _Case;
64+
65+
static void run() {
66+
67+
#ifndef BUILD_REFERENCE
68+
#ifdef OPDI_USE_MACRO_BACKEND
69+
opdi::backend = new opdi::MacroBackend();
70+
opdi::backend->init();
71+
#else
72+
if (omp_get_num_threads() /* trigger OMPT initialization */ && opdi::backend == nullptr) {
73+
std::cout << "Could not initialize OMPT backend. Please check OMPT support." << std::endl;
74+
exit(1);
75+
}
76+
#endif
77+
#ifdef OUTPUT_INSTRUMENT
78+
opdi::ompLogicInstruments.push_back(new opdi::OmpLogicOutputInstrument);
79+
#endif
80+
opdi::logic = new opdi::OmpLogic;
81+
opdi::logic->init();
82+
opdi::tool = new CoDiOpDiLibTool<TestReal>;
83+
opdi::tool->init();
84+
#endif
85+
86+
std::array<std::array<TestReal, Case::nIn>, Case::nPoints> inputs = Case::template genPoints<TestReal>();
87+
88+
for (int p = 0; p < Case::nPoints; ++p) {
89+
double jacobian[Case::nOut][Case::nIn][2];
90+
double primal[Case::nOut];
91+
92+
for (int o = 0; o < Case::nOut; ++o) {
93+
TestReal::Tape& tape = TestReal::getTape();
94+
95+
std::array<TestReal, Case::nOut> outputs = {0.0};
96+
97+
tape.setActive();
98+
99+
for (int i = 0; i < Case::nIn; ++i)
100+
tape.registerInput(inputs[p][i]);
101+
102+
Case::template test<TestReal>(inputs[p], outputs);
103+
104+
tape.registerOutput(outputs[o]);
105+
106+
tape.setPassive();
107+
108+
outputs[o].gradient()[0] = 1.0;
109+
outputs[o].gradient()[1] = 1.25;
110+
111+
#ifndef BUILD_REFERENCE
112+
opdi::logic->prepareEvaluate();
113+
#endif
114+
tape.evaluate();
115+
#ifndef BUILD_REFERENCE
116+
opdi::logic->postEvaluate();
117+
#endif
118+
119+
for (int i = 0; i < Case::nIn; ++i)
120+
{
121+
jacobian[o][i][0] = inputs[p][i].getGradient()[0];
122+
jacobian[o][i][1] = inputs[p][i].getGradient()[1];
123+
inputs[p][i].gradient()[0] = 0.0;
124+
inputs[p][i].gradient()[1] = 0.0;
125+
}
126+
127+
primal[o] = outputs[o].getValue();
128+
129+
tape.reset();
130+
#ifndef BUILD_REFERENCE
131+
opdi::logic->reset();
132+
#endif
133+
}
134+
135+
std::cout << "Point " << p << " :" << std::endl;
136+
for (int o = 0; o < Case::nOut; ++o)
137+
std::cout << primal[o] << std::endl;
138+
139+
for (int o = 0; o < Case::nOut; ++o) {
140+
for (int i = 0; i < Case::nIn; ++i) {
141+
std::cout << jacobian[o][i][0] << " " << jacobian[o][i][1] << std::endl;
142+
}
143+
}
144+
}
145+
146+
#ifndef BUILD_REFERENCE
147+
opdi::tool->finalize();
148+
opdi::logic->finalize();
149+
#ifdef OUTPUT_INSTRUMENT
150+
delete opdi::ompLogicInstruments.front();
151+
opdi::ompLogicInstruments.clear();
152+
#endif
153+
opdi::backend->finalize();
154+
#ifdef OPDI_USE_MACRO_BACKEND
155+
delete opdi::backend;
156+
#endif
157+
delete opdi::tool;
158+
delete opdi::logic;
159+
#endif
160+
}
161+
};
162+
163+
#ifndef BUILD_REFERENCE
164+
#include "opdi.cpp"
165+
#endif
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Point 0 :
2+
-69.2206
3+
4072.98 5091.22
4+
472.851 591.064
5+
-170.998 -213.747
6+
585.639 732.049
7+
Point 1 :
8+
-22.7172
9+
109.401 136.752
10+
1062.08 1327.6
11+
2075.01 2593.77
12+
3004.49 3755.61
13+
Point 2 :
14+
34.8275
15+
1304.3 1630.38
16+
-1893.89 -2367.36
17+
-2333.22 -2916.53
18+
-3903.51 -4879.39
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Point 0 :
2+
-69.2206
3+
4072.98 5091.22
4+
472.851 591.064
5+
-170.998 -213.747
6+
585.639 732.049
7+
Point 1 :
8+
-22.7172
9+
109.401 136.752
10+
1062.08 1327.6
11+
2075.01 2593.77
12+
3004.49 3755.61
13+
Point 2 :
14+
34.8275
15+
1304.3 1630.38
16+
-1893.89 -2367.36
17+
-2333.22 -2916.53
18+
-3903.51 -4879.39
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Point 0 :
2+
-70.4151
3+
3091.49 3864.36
4+
382.947 478.684
5+
-307.231 -384.039
6+
413.529 516.911
7+
Point 1 :
8+
-20.7305
9+
-482.314 -602.893
10+
-117.294 -146.618
11+
1144.94 1431.18
12+
3279.1 4098.87
13+
Point 2 :
14+
36.6485
15+
2698.8 3373.51
16+
-3676.77 -4595.96
17+
-4553.23 -5691.54
18+
-6264.57 -7830.71
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Point 0 :
2+
43.7971
3+
-68441.7 -85552.1
4+
-108005 -135006
5+
451425 564282
6+
63930.5 79913.1
7+
Point 1 :
8+
26.3045
9+
3.17309e+06 3.96636e+06
10+
2.9848e+06 3.731e+06
11+
1.36585e+06 1.70731e+06
12+
1.10654e+06 1.38317e+06
13+
Point 2 :
14+
4.42991
15+
-2.35929e+06 -2.94911e+06
16+
2.68921e+06 3.36151e+06
17+
3.41815e+06 4.27269e+06
18+
1.85118e+06 2.31398e+06
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Point 0 :
2+
66.0028
3+
-223.25 -279.062
4+
-459.298 -574.122
5+
274.127 342.658
6+
-136.691 -170.863
7+
Point 1 :
8+
58.2352
9+
353.754 442.193
10+
917.8 1147.25
11+
823.982 1029.98
12+
-227.845 -284.806
13+
Point 2 :
14+
27.3712
15+
481.951 602.439
16+
-523.207 -654.009
17+
-737.256 -921.57
18+
-1570.04 -1962.55
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Point 0 :
2+
-38.9791
3+
373.795 467.244
4+
56.7667 70.9583
5+
553.336 691.67
6+
216.477 270.596
7+
Point 1 :
8+
-32.4856
9+
-1305.72 -1632.15
10+
-1569.5 -1961.88
11+
-889.039 -1111.3
12+
35.1266 43.9082
13+
Point 2 :
14+
-2.24915
15+
-461.574 -576.968
16+
405.365 506.706
17+
619.295 774.119
18+
832.111 1040.14
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Point 0 :
2+
-79.9235
3+
-225.656 -282.07
4+
423.421 529.276
5+
-257.64 -322.05
6+
88.8672 111.084
7+
Point 1 :
8+
-29.7063
9+
-877.013 -1096.27
10+
-1758.64 -2198.3
11+
-1586.99 -1983.74
12+
-263.202 -329.002
13+
Point 2 :
14+
28.3306
15+
-302.06 -377.575
16+
100.675 125.844
17+
299.426 374.283
18+
683.226 854.033
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Point 0 :
2+
-79.9235
3+
-225.656 -282.07
4+
423.421 529.276
5+
-257.64 -322.05
6+
88.8672 111.084
7+
Point 1 :
8+
-29.7063
9+
-877.013 -1096.27
10+
-1758.64 -2198.3
11+
-1586.99 -1983.74
12+
-263.202 -329.002
13+
Point 2 :
14+
28.3306
15+
-302.06 -377.575
16+
100.675 125.844
17+
299.426 374.283
18+
683.226 854.033
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Point 0 :
2+
-79.9235
3+
-225.656 -282.07
4+
423.421 529.276
5+
-257.64 -322.05
6+
88.8672 111.084
7+
Point 1 :
8+
-29.7063
9+
-877.013 -1096.27
10+
-1758.64 -2198.3
11+
-1586.99 -1983.74
12+
-263.202 -329.002
13+
Point 2 :
14+
28.3306
15+
-302.06 -377.575
16+
100.675 125.844
17+
299.426 374.283
18+
683.226 854.033

0 commit comments

Comments
 (0)