| 40 | struct EigenEnvironment; |
| 41 | |
| 42 | class ThreadPool { |
| 43 | public: |
| 44 | // Constructs a pool that contains "num_threads" threads with specified |
| 45 | // "name". env->StartThread() is used to create individual threads with the |
| 46 | // given ThreadOptions. If "low_latency_hint" is true the thread pool |
| 47 | // implementation may use it as a hint that lower latency is preferred at the |
| 48 | // cost of higher CPU usage, e.g. by letting one or more idle threads spin |
| 49 | // wait. Conversely, if the threadpool is used to schedule high-latency |
| 50 | // operations like I/O the hint should be set to false. |
| 51 | // |
| 52 | // REQUIRES: num_threads > 0 |
| 53 | ThreadPool(Env* env, const ThreadOptions& thread_options, const string& name, |
| 54 | int num_threads, bool low_latency_hint, |
| 55 | Eigen::Allocator* allocator = nullptr); |
| 56 | |
| 57 | // Constructs a pool for low-latency ops that contains "num_threads" threads |
| 58 | // with specified "name". env->StartThread() is used to create individual |
| 59 | // threads. |
| 60 | // REQUIRES: num_threads > 0 |
| 61 | ThreadPool(Env* env, const string& name, int num_threads); |
| 62 | |
| 63 | // Constructs a pool for low-latency ops that contains "num_threads" threads |
| 64 | // with specified "name". env->StartThread() is used to create individual |
| 65 | // threads with the given ThreadOptions. |
| 66 | // REQUIRES: num_threads > 0 |
| 67 | ThreadPool(Env* env, const ThreadOptions& thread_options, const string& name, |
| 68 | int num_threads); |
| 69 | |
| 70 | // Constructs a pool that wraps around the thread::ThreadPoolInterface |
| 71 | // instance provided by the caller. Caller retains ownership of |
| 72 | // `user_threadpool` and must ensure its lifetime is longer than the |
| 73 | // ThreadPool instance. |
| 74 | explicit ThreadPool(thread::ThreadPoolInterface* user_threadpool); |
| 75 | |
| 76 | // Waits until all scheduled work has finished and then destroy the |
| 77 | // set of threads. |
| 78 | ~ThreadPool(); |
| 79 | |
| 80 | void SetThreadPoolAffinity(const cpu_set_t& cpuset); |
| 81 | |
| 82 | // Schedules fn() for execution in the pool of threads. |
| 83 | void Schedule(std::function<void()> fn); |
| 84 | void CostSchedule(std::function<void()> fn, int64 cost); |
| 85 | |
| 86 | void SetStealPartitions( |
| 87 | const std::vector<std::pair<unsigned, unsigned>>& partitions); |
| 88 | |
| 89 | void ScheduleWithHint(std::function<void()> fn, int start, int limit); |
| 90 | // Requires 0 < block_size <= total. |
| 91 | // Spawns k threads and calls fn(i*block_size, (i+1)*block_size) from the |
| 92 | // ith thread (i>=0). When (i+1)*block_size > total, fn(i*block_size, total) |
| 93 | // is called instead. k = NumShardsUsedByTransformRangeConcurrently(...). |
| 94 | // Note that when there aren't enough threads in the pool to achieve full |
| 95 | // parallelism, function calls will be automatically queued. |
| 96 | void TransformRangeConcurrently(const int64 block_size, const int64 total, |
| 97 | const std::function<void(int64, int64)>& fn); |
| 98 | |
| 99 | // Returns the number of threads spawned by calling TransformRangeConcurrently |