|
| 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 TestLock2 : public TestBase<4, 1, 3, TestLock2<_Case>> { |
| 32 | + public: |
| 33 | + using Case = _Case; |
| 34 | + using Base = TestBase<4, 1, 3, TestLock2<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 = 100; |
| 40 | + T* jobResults = new T[N]; |
| 41 | + omp_lock_t lock1, lock2; |
| 42 | + INIT_LOCK(&lock1); |
| 43 | + INIT_LOCK(&lock2); |
| 44 | + |
| 45 | + SET_LOCK(&lock1); |
| 46 | + |
| 47 | + OPDI_PARALLEL() |
| 48 | + { |
| 49 | + int nThreads = omp_get_num_threads(); |
| 50 | + int start = ((N - 1) / nThreads + 1) * omp_get_thread_num(); |
| 51 | + int end = std::min(N, ((N - 1) / nThreads + 1) * (omp_get_thread_num() + 1)); |
| 52 | + |
| 53 | + if (omp_get_thread_num() != 0) { |
| 54 | + SET_LOCK(&lock1); |
| 55 | + } |
| 56 | + |
| 57 | + for (int i = start; i < end; ++i) { |
| 58 | + Base::job1(i, in, jobResults[i]); |
| 59 | + out[0] += jobResults[i]; |
| 60 | + } |
| 61 | + UNSET_LOCK(&lock1); |
| 62 | + |
| 63 | + OPDI_BARRIER() |
| 64 | + |
| 65 | + if (omp_get_thread_num() == 0) { |
| 66 | + SET_LOCK(&lock1); |
| 67 | + SET_LOCK(&lock2); |
| 68 | + } |
| 69 | + } |
| 70 | + OPDI_END_PARALLEL |
| 71 | + |
| 72 | + UNSET_LOCK(&lock2); |
| 73 | + |
| 74 | + OPDI_PARALLEL() |
| 75 | + { |
| 76 | + int nThreads = omp_get_num_threads(); |
| 77 | + int start = ((N - 1) / nThreads + 1) * omp_get_thread_num(); |
| 78 | + int end = std::min(N, ((N - 1) / nThreads + 1) * (omp_get_thread_num() + 1)); |
| 79 | + |
| 80 | + if (omp_get_thread_num() != 0) { |
| 81 | + SET_LOCK(&lock1); |
| 82 | + } |
| 83 | + |
| 84 | + for (int i = start; i < end; ++i) { |
| 85 | + Base::job2(i, in, jobResults[i]); |
| 86 | + out[0] += jobResults[i]; |
| 87 | + } |
| 88 | + UNSET_LOCK(&lock1); |
| 89 | + } |
| 90 | + OPDI_END_PARALLEL |
| 91 | + |
| 92 | + DESTROY_LOCK(&lock1); |
| 93 | + DESTROY_LOCK(&lock2); |
| 94 | + |
| 95 | + delete [] jobResults; |
| 96 | + } |
| 97 | +}; |
0 commit comments