Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /acts/Core/include/Acts/Utilities/detail/WorkerPool.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include <cstddef>
0012 #include <functional>
0013 #include <memory>
0014 
0015 namespace Acts::detail {
0016 
0017 /// A minimal fork-join worker pool for data-parallel loops inside Core.
0018 ///
0019 /// The pool owns `numThreads - 1` persistent worker threads; the thread
0020 /// calling @ref parallelFor participates as the remaining worker. A pool
0021 /// with `numThreads <= 1` owns no threads and runs every loop serially on
0022 /// the calling thread.
0023 ///
0024 /// Calls to @ref parallelFor from different threads are serialized. A call
0025 /// issued from inside a loop body of any pool runs serially instead of
0026 /// dead-locking.
0027 class WorkerPool {
0028  public:
0029   /// Create a pool with the given total concurrency
0030   /// @param numThreads Total number of threads including the caller
0031   explicit WorkerPool(std::size_t numThreads);
0032 
0033   WorkerPool(const WorkerPool&) = delete;
0034   WorkerPool& operator=(const WorkerPool&) = delete;
0035   WorkerPool(WorkerPool&&) = delete;
0036   WorkerPool& operator=(WorkerPool&&) = delete;
0037 
0038   /// Stops and joins the worker threads
0039   ~WorkerPool();
0040 
0041   /// Total concurrency including the calling thread
0042   /// @return the number of threads used by @ref parallelFor
0043   std::size_t size() const;
0044 
0045   /// Run @p body for every index in `[0, n)` and block until all are done.
0046   ///
0047   /// Indices are handed out dynamically in chunks of @p grainSize, so the
0048   /// execution order across indices is unspecified. The first exception
0049   /// thrown by any invocation is rethrown on the calling thread after all
0050   /// threads have stopped working on the loop.
0051   ///
0052   /// @param n Number of loop iterations
0053   /// @param body Callable invoked with each index
0054   /// @param grainSize Number of consecutive indices claimed at once,
0055   ///        0 picks a default based on @p n and the pool size
0056   void parallelFor(std::size_t n, const std::function<void(std::size_t)>& body,
0057                    std::size_t grainSize = 0);
0058 
0059  private:
0060   struct Impl;
0061   std::unique_ptr<Impl> m_impl;
0062 };
0063 
0064 }  // namespace Acts::detail