|
| 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::RealReverseIndexOpenMPGen<double, codi::Direction<double, 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 |
0 commit comments