|
| 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 | + #include "opdi/tool/emptyTool.hpp" |
| 42 | + #ifdef OUTPUT_INSTRUMENT |
| 43 | + #include "opdi/logic/omp/instrument/ompLogicOutputInstrument.hpp" |
| 44 | + #endif |
| 45 | +#else |
| 46 | + #include "opdi/helpers/emptyMacros.hpp" |
| 47 | +#endif |
| 48 | + |
| 49 | +#include "driverBase.hpp" |
| 50 | + |
| 51 | +using TestReal = codi::RealForward; |
| 52 | + |
| 53 | +#ifndef BUILD_REFERENCE |
| 54 | + OPDI_DECLARE_REDUCTION(+, TestReal, +, 0.0); |
| 55 | + OPDI_DECLARE_REDUCTION(*, TestReal, *, 1.0); |
| 56 | +#endif |
| 57 | + |
| 58 | +template<typename _Case> |
| 59 | +struct DriverFirstOrderForward : public DriverBase<DriverFirstOrderForward<_Case> > { |
| 60 | + public: |
| 61 | + |
| 62 | + using Case = _Case; |
| 63 | + |
| 64 | + static void run() { |
| 65 | + |
| 66 | +#ifndef BUILD_REFERENCE |
| 67 | + #ifdef OPDI_USE_MACRO_BACKEND |
| 68 | + opdi::backend = new opdi::MacroBackend(); |
| 69 | + opdi::backend->init(); |
| 70 | + #else |
| 71 | + if (omp_get_num_threads() /* trigger OMPT initialization */ && opdi::backend == nullptr) { |
| 72 | + std::cout << "Could not initialize OMPT backend. Please check OMPT support." << std::endl; |
| 73 | + exit(1); |
| 74 | + } |
| 75 | + #endif |
| 76 | + #ifdef OUTPUT_INSTRUMENT |
| 77 | + opdi::ompLogicInstruments.push_back(new opdi::OmpLogicOutputInstrument); |
| 78 | + #endif |
| 79 | + opdi::logic = new opdi::OmpLogic; |
| 80 | + opdi::logic->init(); |
| 81 | + opdi::tool = new opdi::EmptyTool; |
| 82 | + opdi::tool->init(); |
| 83 | +#endif |
| 84 | + |
| 85 | + std::array<std::array<TestReal, Case::nIn>, Case::nPoints> inputs = Case::template genPoints<TestReal>(); |
| 86 | + |
| 87 | + for (int p = 0; p < Case::nPoints; ++p) { |
| 88 | + double jacobian[Case::nOut][Case::nIn]; |
| 89 | + double primal[Case::nOut]; |
| 90 | + |
| 91 | + for (int i = 0; i < Case::nIn; ++i) { |
| 92 | + std::array<TestReal, Case::nOut> outputs = {0.0}; |
| 93 | + |
| 94 | + inputs[p][i].setGradient(1.0); |
| 95 | + |
| 96 | + Case::template test<TestReal>(inputs[p], outputs); |
| 97 | + |
| 98 | + for (int o = 0; o < Case::nOut; ++o) { |
| 99 | + jacobian[o][i] = outputs[o].getGradient(); |
| 100 | + primal[o] = outputs[o].getValue(); |
| 101 | + } |
| 102 | + |
| 103 | + inputs[p][i].setGradient(0.0); |
| 104 | + |
| 105 | +#ifndef BUILD_REFERENCE |
| 106 | + opdi::logic->reset(); |
| 107 | +#endif |
| 108 | + } |
| 109 | + |
| 110 | + std::cout << "Point " << p << " :" << std::endl; |
| 111 | + for (int o = 0; o < Case::nOut; ++o) |
| 112 | + std::cout << primal[o] << std::endl; |
| 113 | + |
| 114 | + for (int o = 0; o < Case::nOut; ++o) { |
| 115 | + for (int i = 0; i < Case::nIn; ++i) { |
| 116 | + std::cout << jacobian[o][i] << std::endl; |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | +#ifndef BUILD_REFERENCE |
| 122 | + opdi::tool->finalize(); |
| 123 | + opdi::logic->finalize(); |
| 124 | + #ifdef OUTPUT_INSTRUMENT |
| 125 | + delete opdi::ompLogicInstruments.front(); |
| 126 | + opdi::ompLogicInstruments.clear(); |
| 127 | + #endif |
| 128 | + opdi::backend->finalize(); |
| 129 | + #ifdef OPDI_USE_MACRO_BACKEND |
| 130 | + delete opdi::backend; |
| 131 | + #endif |
| 132 | + delete opdi::tool; |
| 133 | + delete opdi::logic; |
| 134 | +#endif |
| 135 | + } |
| 136 | +}; |
| 137 | + |
| 138 | +#ifndef BUILD_REFERENCE |
| 139 | + #include "opdi.cpp" |
| 140 | +#endif |
0 commit comments