|
| 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 TestCustomMutex : public TestBase<4, 1, 3, TestCustomMutex<_Case>> { |
| 32 | + public: |
| 33 | + using Case = _Case; |
| 34 | + using Base = TestBase<4, 1, 3, TestCustomMutex<Case>>; |
| 35 | + |
| 36 | + struct CustomMutex { |
| 37 | + private: |
| 38 | + int status; |
| 39 | + |
| 40 | + public: |
| 41 | + CustomMutex() : status(0) {} |
| 42 | + |
| 43 | + void lock() { |
| 44 | + int localStatus; |
| 45 | + |
| 46 | + while (true) { |
| 47 | + // wait until unlocked |
| 48 | + do { |
| 49 | + #ifdef _OPENMP |
| 50 | + #pragma omp atomic read |
| 51 | + #endif |
| 52 | + localStatus = status; |
| 53 | + } while (localStatus != 0); |
| 54 | + |
| 55 | + // try to lock |
| 56 | + #ifdef _OPENMP |
| 57 | + #pragma omp atomic capture |
| 58 | + #endif |
| 59 | + localStatus = ++status; |
| 60 | + |
| 61 | + // success |
| 62 | + if (localStatus == 1) { |
| 63 | + break; |
| 64 | + } |
| 65 | + |
| 66 | + // already acquired, try again |
| 67 | + #ifdef _OPENMP |
| 68 | + #pragma omp atomic update |
| 69 | + #endif |
| 70 | + --status; |
| 71 | + } |
| 72 | + #ifdef _OPENMP |
| 73 | + opdi::logic->onMutexAcquired(opdi::LogicInterface::MutexKind::Custom, reinterpret_cast<opdi::LogicInterface::WaitId>(&status)); |
| 74 | + #endif |
| 75 | + } |
| 76 | + |
| 77 | + void unlock() { |
| 78 | + #ifdef _OPENMP |
| 79 | + #pragma omp atomic update |
| 80 | + #endif |
| 81 | + --status; |
| 82 | + #ifdef _OPENMP |
| 83 | + opdi::logic->onMutexReleased(opdi::LogicInterface::MutexKind::Custom, reinterpret_cast<opdi::LogicInterface::WaitId>(&status)); |
| 84 | + #endif |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + template<typename T> |
| 89 | + static void test(std::array<T, Base::nIn> const& in, std::array<T, Base::nOut>& out) { |
| 90 | + |
| 91 | + int const N = 1000; |
| 92 | + T* jobResults = new T[N]; |
| 93 | + T out1 = 0.0; |
| 94 | + T out2 = 0.0; |
| 95 | + |
| 96 | + CustomMutex mutex1, mutex2; |
| 97 | + |
| 98 | + OPDI_PARALLEL() |
| 99 | + { |
| 100 | + int nThreads = omp_get_num_threads(); |
| 101 | + int start = ((N - 1) / nThreads + 1) * omp_get_thread_num(); |
| 102 | + int end = std::min(N, ((N - 1) / nThreads + 1) * (omp_get_thread_num() + 1)); |
| 103 | + |
| 104 | + for (int i = start; i < end; ++i) { |
| 105 | + Base::job1(i, in, jobResults[i]); |
| 106 | + |
| 107 | + mutex1.lock(); |
| 108 | + out1 += jobResults[i]; |
| 109 | + mutex1.unlock(); |
| 110 | + |
| 111 | + Base::job2(i, in, jobResults[i]); |
| 112 | + |
| 113 | + mutex2.lock(); |
| 114 | + out2 += jobResults[i]; |
| 115 | + mutex2.unlock(); |
| 116 | + } |
| 117 | + } |
| 118 | + OPDI_END_PARALLEL |
| 119 | + |
| 120 | + out[0] = out1 + out2; |
| 121 | + |
| 122 | + delete [] jobResults; |
| 123 | + } |
| 124 | +}; |
0 commit comments