|
| 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 "testBase.hpp" |
| 29 | + |
| 30 | +template<typename _Case> |
| 31 | +struct TestTaskReset : public TestBase<4, 1, 3, TestTaskReset<_Case>> { |
| 32 | + public: |
| 33 | + using Case = _Case; |
| 34 | + using Base = TestBase<4, 1, 3, TestTaskReset<Case>>; |
| 35 | + |
| 36 | + template<typename T> |
| 37 | + static void test(std::array<T, Base::nIn> const& in, std::array<T, Base::nOut>& out) { |
| 38 | + |
| 39 | + int const N = 1000; |
| 40 | + T* a = new T[N]; |
| 41 | + T* b = new T[N]; |
| 42 | + T* c = new T[N]; |
| 43 | + |
| 44 | + assertAdjointAccessMode(opdi::LogicInterface::AdjointAccessMode::Atomic); |
| 45 | + |
| 46 | + OPDI_PARALLEL() |
| 47 | + { |
| 48 | + assertAdjointAccessMode(opdi::LogicInterface::AdjointAccessMode::Atomic); |
| 49 | + |
| 50 | + int outerNThreads = omp_get_num_threads(); |
| 51 | + int outerStart = ((N - 1) / outerNThreads + 1) * omp_get_thread_num(); |
| 52 | + int outerEnd = std::min(N, ((N - 1) / outerNThreads + 1) * (omp_get_thread_num() + 1)); |
| 53 | + |
| 54 | + // shared reading of in |
| 55 | + for (int i = outerStart; i < outerEnd; ++i) { |
| 56 | + Base::job1(i, in, a[i]); |
| 57 | + } |
| 58 | + |
| 59 | + #if _OPENMP |
| 60 | + opdi::logic->setAdjointAccessMode(opdi::LogicInterface::AdjointAccessMode::Classical); |
| 61 | + #endif |
| 62 | + |
| 63 | + assertAdjointAccessMode(opdi::LogicInterface::AdjointAccessMode::Classical); |
| 64 | + |
| 65 | + // no shared reading |
| 66 | + for (int i = outerStart; i < outerEnd; ++i) { |
| 67 | + b[i] = sin(exp(a[i])); |
| 68 | + } |
| 69 | + |
| 70 | + auto position = T::getTape().getPosition(); |
| 71 | + #if _OPENMP |
| 72 | + auto mode = opdi::logic->getAdjointAccessMode(); |
| 73 | + #endif |
| 74 | + |
| 75 | + OPDI_BARRIER() |
| 76 | + |
| 77 | + OPDI_PARALLEL() |
| 78 | + { |
| 79 | + assertAdjointAccessMode(opdi::LogicInterface::AdjointAccessMode::Classical); |
| 80 | + |
| 81 | + int innerNThreads = omp_get_num_threads(); |
| 82 | + int innerStart = outerStart + (((outerEnd - outerStart) - 1) / innerNThreads + 1) * omp_get_thread_num(); |
| 83 | + int innerEnd = std::min(outerEnd, outerStart + (((outerEnd - outerStart) - 1) / innerNThreads + 1) |
| 84 | + * (omp_get_thread_num() + 1)); |
| 85 | + |
| 86 | + #if _OPENMP |
| 87 | + opdi::logic->setAdjointAccessMode(opdi::LogicInterface::AdjointAccessMode::Atomic); |
| 88 | + #endif |
| 89 | + |
| 90 | + assertAdjointAccessMode(opdi::LogicInterface::AdjointAccessMode::Atomic); |
| 91 | + |
| 92 | + // shared reading on a |
| 93 | + for (int j = innerStart; j < innerEnd; ++j) { |
| 94 | + T arg = b[j]; |
| 95 | + for (int k = 0; k < 10; ++k) { |
| 96 | + arg += a[(j + k) % N]; |
| 97 | + } |
| 98 | + c[j] = cos(arg); |
| 99 | + } |
| 100 | + |
| 101 | + // atomic adjoint access mode to be transported out of the nested parallel region |
| 102 | + } |
| 103 | + OPDI_END_PARALLEL |
| 104 | + |
| 105 | + assertAdjointAccessMode(opdi::LogicInterface::AdjointAccessMode::Atomic); |
| 106 | + |
| 107 | + // remove the inner parallel region by positional reset |
| 108 | + T::getTape().resetTo(position); |
| 109 | + |
| 110 | + #if _OPENMP |
| 111 | + opdi::logic->resetTask(&position, mode); |
| 112 | + #endif |
| 113 | + |
| 114 | + assertAdjointAccessMode(opdi::LogicInterface::AdjointAccessMode::Classical); |
| 115 | + |
| 116 | + #if _OPENMP |
| 117 | + opdi::logic->setAdjointAccessMode(opdi::LogicInterface::AdjointAccessMode::Atomic); |
| 118 | + opdi::logic->addReverseBarrier(); |
| 119 | + #endif |
| 120 | + |
| 121 | + assertAdjointAccessMode(opdi::LogicInterface::AdjointAccessMode::Atomic); |
| 122 | + |
| 123 | + // shared reading on a |
| 124 | + for (int i = outerStart; i < outerEnd; ++i) { |
| 125 | + T arg = c[i] + b[i]; |
| 126 | + for (int j = 0; j < 10; ++j) { |
| 127 | + arg += a[(i + j) % N]; |
| 128 | + } |
| 129 | + c[i] = cos(arg); |
| 130 | + } |
| 131 | + |
| 132 | + } |
| 133 | + OPDI_END_PARALLEL |
| 134 | + |
| 135 | + assertAdjointAccessMode(opdi::LogicInterface::AdjointAccessMode::Atomic); |
| 136 | + |
| 137 | + for (int i = 0; i < N; ++i) { |
| 138 | + out[0] += c[i]; |
| 139 | + } |
| 140 | + |
| 141 | + delete [] a; |
| 142 | + delete [] b; |
| 143 | + delete [] c; |
| 144 | + } |
| 145 | +}; |
0 commit comments