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 // Ctx is the internal context interface class used by most of ruy's own code. 17 // It is subclassed by CtxImpl which provides the actual data members. 18 19 #ifndef RUY_RUY_CTX_H_ 20 #define RUY_RUY_CTX_H_ 21 22 #include <cstdint> 23 24 namespace ruy { 25 26 class CtxImpl; 27 class ThreadPool; 28 class Allocator; 29 class TuningResolver; 30 class PrepackedCache; 31 class CpuInfo; 32 enum class Path : std::uint8_t; 33 enum class Tuning; 34 enum class PerformanceAdvisory; 35 enum class NumThreadsStrategy : std::uint8_t; 36 37 // Ctx is the internal context class used throughout ruy code. Whereas Context 38 // is exposed to users, Ctx is internal to ruy. As many of ruy's internal 39 // headers, included by ruy public headers, need to use Ctx, it is important 40 // that it does not include definition of all the actual data members. This is 41 // solved by a variant of the 'pimpl' idiom, where instead of being implemented 42 // in the usual way with a pointer member, it is implemented in a subclass, 43 // CtxImpl. 44 class Ctx /* not final, subclassed by CtxImpl */ { 45 public: 46 Path last_used_path() const; 47 Tuning explicit_tuning() const; 48 void set_explicit_tuning(Tuning value); 49 const ThreadPool& thread_pool() const; 50 ThreadPool* mutable_thread_pool(); 51 int max_num_threads() const; 52 void set_max_num_threads(int value); 53 CpuInfo* mutable_cpuinfo(); 54 void clear_performance_advisories(); 55 void set_performance_advisory(PerformanceAdvisory advisory); 56 bool performance_advisory(PerformanceAdvisory advisory) const; 57 void set_num_threads_strategy(NumThreadsStrategy strategy); 58 NumThreadsStrategy num_threads_strategy() const; 59 60 // Returns the set of Path's that are available. By default, this is based on 61 // runtime detection of CPU features, as well as on which code paths were 62 // built. Detection results are stored on the context object so that 63 // subsequent calls are fast. This is overridden by SetRuntimeEnabledPaths. 64 Path GetRuntimeEnabledPaths(); 65 66 // Override auto-detection of supported code paths. 67 // 68 // Passing `paths == Path::kNone` means reverting to the default behavior. 69 // This will trigger auto-detection on the next use. 70 // 71 // Other values will override auto-detection with the explicitly provided set 72 // of paths. 73 // 74 // Paths in kNonArchPaths are always implicitly supported. 75 void SetRuntimeEnabledPaths(Path paths); 76 77 Path SelectPath(Path compiled_paths); 78 void EnsureThreadSpecificResources(int thread_count); 79 TuningResolver* GetThreadSpecificTuningResolver(int thread_index) const; 80 Allocator* GetThreadSpecificAllocator(int thread_index) const; 81 Allocator* GetMainAllocator(); 82 PrepackedCache* GetPrepackedCache(); 83 Tuning GetMainThreadTuning(); 84 void ClearPrepackedCache(); 85 86 private: 87 // Downcast helpers. 88 const CtxImpl& impl() const; 89 CtxImpl* mutable_impl(); 90 }; 91 92 } // namespace ruy 93 94 #endif // RUY_RUY_CTX_H_ 95