|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (C) 2024 Intel Corporation |
| 4 | + * |
| 5 | + * Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. |
| 6 | + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | + * |
| 8 | + */ |
| 9 | + |
| 10 | +#include <algorithm> |
| 11 | +#include <chrono> |
| 12 | +#include <cmath> |
| 13 | +#include <condition_variable> |
| 14 | +#include <functional> |
| 15 | +#include <mutex> |
| 16 | +#include <numeric> |
| 17 | +#include <thread> |
| 18 | +#include <vector> |
| 19 | + |
| 20 | +namespace umf_bench { |
| 21 | + |
| 22 | +template <typename Function> |
| 23 | +void parallel_exec(size_t threads_number, Function &&f) { |
| 24 | + std::vector<std::thread> threads; |
| 25 | + threads.reserve(threads_number); |
| 26 | + |
| 27 | + for (size_t i = 0; i < threads_number; ++i) { |
| 28 | + threads.emplace_back([&](size_t id) { f(id); }, i); |
| 29 | + } |
| 30 | + |
| 31 | + for (auto &t : threads) { |
| 32 | + t.join(); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +class latch { |
| 37 | + public: |
| 38 | + latch(size_t desired) : counter(desired) {} |
| 39 | + |
| 40 | + /* Returns true for the last thread arriving at the latch, false for all |
| 41 | + * other threads. */ |
| 42 | + bool wait(std::unique_lock<std::mutex> &lock) { |
| 43 | + counter--; |
| 44 | + if (counter > 0) { |
| 45 | + cv.wait(lock, [&] { return counter == 0; }); |
| 46 | + return false; |
| 47 | + } else { |
| 48 | + /* |
| 49 | + * notify_call could be called outside of a lock |
| 50 | + * (it would perform better) but drd complains |
| 51 | + * in that case |
| 52 | + */ |
| 53 | + cv.notify_all(); |
| 54 | + return true; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private: |
| 59 | + std::condition_variable cv; |
| 60 | + size_t counter = 0; |
| 61 | +}; |
| 62 | + |
| 63 | +/* Implements multi-use barrier (latch). Once all threads arrive at the |
| 64 | + * latch, a new latch is allocated and used by all subsequent calls to |
| 65 | + * syncthreads. */ |
| 66 | +struct syncthreads_barrier { |
| 67 | + syncthreads_barrier(size_t num_threads) : num_threads(num_threads) { |
| 68 | + mutex = std::shared_ptr<std::mutex>(new std::mutex); |
| 69 | + current_latch = std::shared_ptr<latch>(new latch(num_threads)); |
| 70 | + } |
| 71 | + |
| 72 | + syncthreads_barrier(const syncthreads_barrier &) = delete; |
| 73 | + syncthreads_barrier &operator=(const syncthreads_barrier &) = delete; |
| 74 | + syncthreads_barrier(syncthreads_barrier &&) = default; |
| 75 | + |
| 76 | + void operator()() { |
| 77 | + std::unique_lock<std::mutex> lock(*mutex); |
| 78 | + auto l = current_latch; |
| 79 | + if (l->wait(lock)) { |
| 80 | + current_latch = std::shared_ptr<latch>(new latch(num_threads)); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private: |
| 85 | + size_t num_threads; |
| 86 | + std::shared_ptr<std::mutex> mutex; |
| 87 | + std::shared_ptr<latch> current_latch; |
| 88 | +}; |
| 89 | + |
| 90 | +template <typename TimeUnit, typename F> |
| 91 | +typename TimeUnit::rep measure(F &&func) { |
| 92 | + auto start = std::chrono::steady_clock::now(); |
| 93 | + |
| 94 | + func(); |
| 95 | + |
| 96 | + auto duration = std::chrono::duration_cast<TimeUnit>( |
| 97 | + std::chrono::steady_clock::now() - start); |
| 98 | + return duration.count(); |
| 99 | +} |
| 100 | + |
| 101 | +/* Measure time of execution of run_workload(thread_id) function. */ |
| 102 | +template <typename TimeUnit, typename F> |
| 103 | +auto measure(size_t iterations, size_t concurrency, F &&run_workload) { |
| 104 | + using ResultsType = typename TimeUnit::rep; |
| 105 | + std::vector<ResultsType> results; |
| 106 | + |
| 107 | + for (size_t i = 0; i < iterations; i++) { |
| 108 | + std::vector<ResultsType> iteration_results(concurrency); |
| 109 | + syncthreads_barrier syncthreads(concurrency); |
| 110 | + parallel_exec(concurrency, [&](size_t id) { |
| 111 | + syncthreads(); |
| 112 | + |
| 113 | + iteration_results[id] = |
| 114 | + measure<TimeUnit>([&]() { run_workload(id); }); |
| 115 | + |
| 116 | + syncthreads(); |
| 117 | + }); |
| 118 | + results.insert(results.end(), iteration_results.begin(), |
| 119 | + iteration_results.end()); |
| 120 | + } |
| 121 | + |
| 122 | + return results; |
| 123 | +} |
| 124 | + |
| 125 | +template <typename T> T min(const std::vector<T> &values) { |
| 126 | + return *std::min_element(values.begin(), values.end()); |
| 127 | +} |
| 128 | + |
| 129 | +template <typename T> T max(const std::vector<T> &values) { |
| 130 | + return *std::max_element(values.begin(), values.end()); |
| 131 | +} |
| 132 | + |
| 133 | +template <typename T> double mean(const std::vector<T> &values) { |
| 134 | + return std::accumulate(values.begin(), values.end(), 0.0) / values.size(); |
| 135 | +} |
| 136 | + |
| 137 | +template <typename T> double std_dev(const std::vector<T> &values) { |
| 138 | + auto m = mean(values); |
| 139 | + std::vector<T> diff_squares; |
| 140 | + diff_squares.reserve(values.size()); |
| 141 | + |
| 142 | + for (auto &v : values) { |
| 143 | + diff_squares.push_back(std::pow((v - m), 2.0)); |
| 144 | + } |
| 145 | + |
| 146 | + auto variance = |
| 147 | + std::accumulate(diff_squares.begin(), diff_squares.end(), 0.0) / |
| 148 | + values.size(); |
| 149 | + |
| 150 | + return std::sqrt(variance); |
| 151 | +} |
| 152 | + |
| 153 | +} // namespace umf_bench |
0 commit comments