-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmacroexample.cpp
More file actions
105 lines (82 loc) · 2.68 KB
/
macroexample.cpp
File metadata and controls
105 lines (82 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* OpDiLib, an Open Multiprocessing Differentiation Library
*
* Copyright (C) 2020-2022 Chair for Scientific Computing (SciComp), TU Kaiserslautern
* Copyright (C) 2023-2026 Chair for Scientific Computing (SciComp), RPTU University Kaiserslautern-Landau
* Homepage: https://scicomp.rptu.de
* Contact: Prof. Nicolas R. Gauger (opdi@scicomp.uni-kl.de)
*
* Lead developer: Johannes Blühdorn (SciComp, RPTU University Kaiserslautern-Landau)
*
* This file is part of OpDiLib (https://scicomp.rptu.de/software/opdi).
*
* OpDiLib is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* OpDiLib is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License along with OpDiLib. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include <codi.hpp>
#include <iostream>
#include <opdi/backend/macro/macroBackend.hpp>
#include <opdi.hpp>
using Real = codi::RealReverseIndexOpenMP; // use a suitable CoDiPack type
using Tape = typename Real::Tape;
int main(int nargs, char** args) {
// initialize OpDiLib
opdi::backend = new opdi::MacroBackend();
opdi::backend->init();
opdi::logic = new opdi::OmpLogic;
opdi::logic->init();
opdi::tool = new CoDiOpDiLibTool<Real>;
opdi::tool->init();
// usual AD workflow
Real x = 4.0;
Tape& tape = Real::getTape();
tape.setActive();
tape.registerInput(x);
// parallel computation
size_t constexpr N = 10000000;
Real y = 0.0;
OPDI_PARALLEL()
{
Real localSum = 0.0;
OPDI_FOR()
for (size_t i = 0; i < N; ++i)
{
localSum += sin(x * i);
}
OPDI_END_FOR
OPDI_CRITICAL()
{
y += localSum;
}
OPDI_END_CRITICAL
}
OPDI_END_PARALLEL
// usual AD workflow
tape.registerOutput(y);
tape.setPassive();
y.setGradient(1.0);
opdi::logic->prepareEvaluate(); // prepare OpDiLib for evaluation
tape.evaluate();
opdi::logic->postEvaluate(); // OpDiLib-specific postprocessing
std::cout << "f(" << x << ") = " << y << std::endl;
std::cout << "df/dx(" << x << ") = " << x.getGradient() << std::endl;
// finalize OpDiLib
opdi::tool->finalize();
opdi::logic->finalize();
opdi::backend->finalize();
delete opdi::tool;
delete opdi::logic;
delete opdi::backend;
return 0;
}
// don't forget to include the OpDiLib source file
#include "opdi.cpp"