/* Copyright 2019 Google LLC. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ // This file is a fork of gemmlowp's multi_thread_gemm.h, under Apache 2.0 // license. #ifndef RUY_RUY_THREAD_POOL_H_ #define RUY_RUY_THREAD_POOL_H_ #include <vector> #include "ruy/blocking_counter.h" #include "ruy/time.h" namespace ruy { // A workload for a thread. struct Task { … }; class Thread; // A simple pool of threads, that only allows the very // specific parallelization pattern that we use here: // One thread, which we call the 'main thread', calls Execute, distributing // a Task each to N threads, being N-1 'worker threads' and the main thread // itself. After the main thread has completed its own Task, it waits for // the worker threads to have all completed. That is the only synchronization // performed by this ThreadPool. // // In particular, there is a naive 1:1 mapping of Tasks to threads. // This ThreadPool considers it outside of its own scope to try to work // with fewer threads than there are Tasks. The idea is that such N:M mappings // of tasks to threads can be implemented as a higher-level feature on top of // the present low-level 1:1 threadpool. For example, a user might have a // Task subclass referencing a shared atomic counter indexing into a vector of // finer-granularity subtasks. Different threads would then concurrently // increment this atomic counter, getting each their own subtasks to work on. // That approach is the one used in ruy's multi-thread matrix multiplication // implementation --- see ruy's TrMulTask. class ThreadPool { … }; } // namespace ruy #endif // RUY_RUY_THREAD_POOL_H_