|
| 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 TestParallelCopyin : public TestBase<4, 1, 3, TestParallelCopyin<_Case>> { |
| 32 | + public: |
| 33 | + using Case = _Case; |
| 34 | + using Base = TestBase<4, 1, 3, TestParallelCopyin<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 | + /* gcc refuses to compile this, so copyin is actually not tested with gcc right now */ |
| 40 | + static T helper; |
| 41 | + #if !defined(__GNUC__) || defined(__clang__) |
| 42 | + #if _OPENMP |
| 43 | + #pragma omp threadprivate(helper) |
| 44 | + #endif |
| 45 | + #endif |
| 46 | + |
| 47 | + int const N = 1000; |
| 48 | + T* jobResults = new T[N]; |
| 49 | + |
| 50 | + #ifndef BUILD_REFERENCE |
| 51 | + /* Set activity of default tapes. They might record copy operations due to firstprivate. OpDiLib's AD approach |
| 52 | + * for parallel regions moves these recordings to the correct tapes. */ |
| 53 | + if (T::getTape().isActive()) { |
| 54 | + opdi::logic->beginSkippedParallelRegion(); |
| 55 | + OPDI_PARALLEL() |
| 56 | + { |
| 57 | + /* due to skipping, OpDiLib has not exchanged the tapes */ |
| 58 | + if (omp_get_thread_num() != 0) { |
| 59 | + T::getTape().setActive(); |
| 60 | + } |
| 61 | + } |
| 62 | + OPDI_END_PARALLEL |
| 63 | + opdi::logic->endSkippedParallelRegion(); |
| 64 | + } |
| 65 | + #endif |
| 66 | + |
| 67 | + helper = in[0] * in[1] * in[2] * in[3]; |
| 68 | + |
| 69 | + #if !defined(__GNUC__) || defined(__clang__) |
| 70 | + OPDI_PARALLEL(copyin(helper)) |
| 71 | + #else |
| 72 | + OPDI_PARALLEL(firstprivate(helper)) |
| 73 | + #endif |
| 74 | + { |
| 75 | + int nThreads = omp_get_num_threads(); |
| 76 | + int start = ((N - 1) / nThreads + 1) * omp_get_thread_num(); |
| 77 | + int end = std::min(N, ((N - 1) / nThreads + 1) * (omp_get_thread_num() + 1)); |
| 78 | + |
| 79 | + for (int i = start; i < end; ++i) { |
| 80 | + Base::job1(i, in, jobResults[i]); |
| 81 | + jobResults[i] = sin(jobResults[i] * helper); |
| 82 | + } |
| 83 | + } |
| 84 | + OPDI_END_PARALLEL |
| 85 | + |
| 86 | + for (int i = 0; i < N; ++i) { |
| 87 | + out[0] += jobResults[i]; |
| 88 | + } |
| 89 | + |
| 90 | + delete [] jobResults; |
| 91 | + } |
| 92 | +}; |
0 commit comments