1- #pragma once
1+ /*
2+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3+ * All rights reserved.
4+ *
5+ * This source code is licensed under the BSD-style license found in the
6+ * LICENSE file in the root directory of this source tree.
7+ */
28
3- #include < pthreadpool.h >
9+ #pragma once
410
5- // @nolint PATTERNLINT Ok to use stdlib for this optional library
611#include < functional>
7- // @nolint PATTERNLINT Ok to use stdlib for this optional library
812#include < memory>
9- // @nolint PATTERNLINT Ok to use stdlib for this optional library
1013#include < mutex>
1114
12- namespace torch {
13- namespace executorch {
14- namespace threadpool {
15+ # include < pthreadpool.h >
16+
17+ namespace executorch ::extension:: threadpool {
1518
1619class ThreadPool final {
1720 public:
@@ -26,55 +29,64 @@ class ThreadPool final {
2629 ThreadPool& operator =(const ThreadPool&) = delete ;
2730
2831 // Make threadpool non-movable.
29- // For now this is non-movable, but if we want to have clients
30- // such as say torch::executorch::Executor, to be able to own
31- // threadpool, then we will have to make this movable.
3232 ThreadPool (ThreadPool&&) = delete ;
3333 ThreadPool& operator =(ThreadPool&&) = delete ;
3434
3535 size_t get_thread_count () const ;
3636
37- /*
38- * Resets the threadpool by creating a new threadpool with requested # of
39- * threads. This is not a thread safe call. When calling this method, threads
40- * of the threadpool might be doing some work. Some other code may also be
41- * holding on to the threadpool pointer, that is no longer valid. This is a
42- * private API, which will later be replaced by something that allows creating
43- * of threadpool with requested size and use such a threadpool with backend
44- * delegates, custom ops or optimized lib.
37+ /* *
38+ * INTERNAL: Resets the threadpool by creating a new threadpool with requested
39+ * # of threads. This is not a thread safe call. When calling this method,
40+ * threads of the threadpool might be doing some work. Some other code may
41+ * also be holding on to the threadpool pointer, that is no longer valid. This
42+ * is a private API, which will later be replaced by something that allows
43+ * creating of threadpool with requested size and use such a threadpool with
44+ * backend delegates, custom ops or optimized lib.
4545 */
46+ [[deprecated(" This API is experimental and may change without notice." )]]
4647 bool _unsafe_reset_threadpool (uint32_t num_threads);
4748
48- // Run, in parallel, function fn(task_id) over task_id in range [0, range).
49- // This function is blocking. All input is processed by the time it returns.
50- // NoThreadPoolGuard (see threadpool_guard.h) can used to disable
51- // use of multiple threads with the scope of the guard
52- // When NoThreadPoolGuard is not used all calls to run method are serialized.
49+ /* *
50+ * Run, in parallel, function fn(task_id) over task_id in range [0, range).
51+ * This function is blocking. All input is processed by the time it returns.
52+ * NoThreadPoolGuard (see threadpool_guard.h) can used to disable use of
53+ * multiple threads with the scope of the guard When NoThreadPoolGuard is not
54+ * used all calls to run method are serialized.
55+ */
5356 void run (const std::function<void (size_t )>& fn, size_t range);
5457
5558 private:
5659 friend pthreadpool_t get_pthreadpool ();
5760
5861 private:
59- // This mutex is used inside get_thread_count API but it is not
60- // really needed. Since data members of ThreadPool objects are not
61- // really mutable.
62- // Figure out if we will allow set_num_threads API, in which mutex
63- // will be useful. Otherwise remove it.
64- // TODO(kimishpatel)
62+ // This mutex is used inside get_thread_count API but it is not really needed
63+ // since data members of ThreadPool objects are not really mutable.
64+ // TODO(kimishpatel): Figure out if we will allow set_num_threads API, in
65+ // which case this mutex will be useful. Otherwise remove it.
6566 mutable std::mutex mutex_;
6667 std::unique_ptr<pthreadpool, decltype (&pthreadpool_destroy)> threadpool_;
6768};
6869
69- // Return a singleton instance of ThreadPool for ATen/TH multithreading.
70+ /* *
71+ * Returns the singleton instance of ThreadPool for ATen/TH multithreading.
72+ */
7073ThreadPool* get_threadpool ();
7174
72- // Exposes the underlying implementation of ThreadPool.
73- // Only for use in external libraries so as to unify threading across
74- // internal (i.e. ATen, etc.) and external (e.g. NNPACK, QNNPACK, XNNPACK)
75- // use cases.
75+ /* *
76+ * Returns the underlying pthreadpool instance used by the implementation of
77+ * ThreadPool returned by `get_threadpool()`. Only for use in external libraries
78+ * so as to unify threading across internal (i.e. ATen, etc.) and external (e.g.
79+ * NNPACK, QNNPACK, XNNPACK) use cases.
80+ */
7681pthreadpool_t get_pthreadpool ();
7782
78- } // namespace threadpool
79- } // namespace executorch
80- } // namespace torch
83+ } // namespace executorch::extension::threadpool
84+
85+ namespace torch ::executorch::threadpool { // DEPRECATED
86+ // TODO(T197294990): Remove these deprecated aliases once all users have moved
87+ // to the new `::executorch` namespaces. Note that threadpool incorrectly used
88+ // the namespace `torch::executorch` instead of `torch::executor`.
89+ using ::executorch::extension::threadpool::get_pthreadpool; // DEPRECATED
90+ using ::executorch::extension::threadpool::get_threadpool; // DEPRECATED
91+ using ::executorch::extension::threadpool::ThreadPool; // DEPRECATED
92+ } // namespace torch::executorch::threadpool
0 commit comments