1 /* Copyright 2019 Google LLC. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 // Context is the user-facing context class. 17 18 #ifndef RUY_RUY_CONTEXT_H_ 19 #define RUY_RUY_CONTEXT_H_ 20 21 #include <cstdint> 22 23 namespace ruy { 24 25 class Ctx; 26 class CtxImpl; 27 class ThreadPool; 28 enum class Path : std::uint8_t; 29 enum class Tuning; 30 enum class PerformanceAdvisory; 31 enum class NumThreadsStrategy : std::uint8_t; 32 33 // A Context holds runtime information used by Ruy. It holds runtime resources 34 // such as the workers thread pool and the allocator (which holds buffers for 35 // temporary data), as well as runtime options controlling which Paths are 36 // enabled (typically based on which instruction sets are detected) and how 37 // many threads to use. 38 class Context final { 39 public: 40 Context(); 41 ~Context(); 42 43 // Returns the Path enum value that corresponds to the code path used by 44 // the last ruy::Mul with this Context. 45 Path last_used_path() const; 46 47 // Control of whether to use kernels tuned for in-order or out-of-order CPU 48 // cores. The default is auto-detection, so these methods should only be used 49 // to override that auto-detection if it's not working as intended or for 50 // testing. 51 Tuning explicit_tuning() const; 52 void set_explicit_tuning(Tuning value); 53 54 // The thread pool held by this context to dispatch a ruy::Mul to worker 55 // threads. 56 // 57 // By default, threads may spin-wait for a few milliseconds before reverting 58 // to passive wait. This can be controlled by 59 // `mutable_thread_pool()->set_spin_milliseconds(value)`. 60 const ThreadPool& thread_pool() const; 61 ThreadPool* mutable_thread_pool(); 62 63 // Controls the maximum number of threads to be used by ruy::Mul with this 64 // Context. The number of threads in the pool will be that value minus one, 65 // as the remaining portion of the work is done directly on the calling 66 // thread. 67 // 68 // This defaults to 1. Multi-threading in ruy is always opt-in. There is 69 // no auto-detection of hardware concurrency. That is on purpose, ruy focuses 70 // on mobile applications where such concepts are difficult to define 71 // (e.g. ARM big.LITTLE). 72 int max_num_threads() const; 73 void set_max_num_threads(int value); 74 75 // Controls the logic to determine how many threads to use. 76 NumThreadsStrategy num_threads_strategy() const; 77 void set_num_threads_strategy(NumThreadsStrategy strategy); 78 79 // Returns true of the last ruy::Mul using this Context flagged the specified 80 // `advisory`. This is reset by each ruy::Mul call. 81 bool performance_advisory(PerformanceAdvisory advisory) const; 82 83 // When using Matrix::set_cache_policy(), this Context will keep a cache of 84 // pre-packed matrix data. This function clears that cache. 85 void ClearPrepackedCache(); 86 87 // Override auto-detection of supported code paths. 88 // 89 // Passing `paths == Path::kNone` means reverting to the default behavior. 90 // This will trigger auto-detection on the next use. 91 // 92 // Other values will override auto-detection with the explicitly provided set 93 // of paths. 94 // 95 // Paths in kNonArchPaths are always implicitly supported. 96 void set_runtime_enabled_paths(Path paths); 97 98 // Returns the set of Path's that are available. 99 Path get_runtime_enabled_paths(); 100 101 private: 102 CtxImpl* const impl_; 103 104 const Ctx& ctx() const; 105 Ctx* mutable_ctx(); 106 107 friend const Ctx* get_ctx(const Context*); 108 friend Ctx* get_ctx(Context*); 109 110 // Disallow copy 111 Context(const Context&) = delete; 112 }; 113 114 } // end namespace ruy 115 116 #endif // RUY_RUY_CONTEXT_H_ 117