1/* Copyright 2019 Google LLC. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations 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
24namespace ruy {
25
26class CtxImpl;
27class ThreadPool;
28class Allocator;
29class TuningResolver;
30class PrepackedCache;
31class CpuInfo;
32enum class Path : std::uint8_t;
33enum class Tuning;
34enum class PerformanceAdvisory;
35
36// Ctx is the internal context class used throughout ruy code. Whereas Context
37// is exposed to users, Ctx is internal to ruy. As many of ruy's internal
38// headers, included by ruy public headers, need to use Ctx, it is important
39// that it does not include definition of all the actual data members. This is
40// solved by a variant of the 'pimpl' idiom, where instead of being implemented
41// in the usual way with a pointer member, it is implemented in a subclass,
42// CtxImpl.
43class Ctx /* not final, subclassed by CtxImpl */ {
44 public:
45 Path last_used_path() const;
46 Tuning explicit_tuning() const;
47 void set_explicit_tuning(Tuning value);
48 const ThreadPool& thread_pool() const;
49 ThreadPool* mutable_thread_pool();
50 int max_num_threads() const;
51 void set_max_num_threads(int value);
52 CpuInfo* mutable_cpuinfo();
53 void clear_performance_advisories();
54 void set_performance_advisory(PerformanceAdvisory advisory);
55 bool performance_advisory(PerformanceAdvisory advisory) const;
56
57 // Returns the set of Path's that are available. By default, this is based on
58 // runtime detection of CPU features, as well as on which code paths were
59 // built. Detection results are stored on the context object so that
60 // subsequent calls are fast. This is overridden by SetRuntimeEnabledPaths.
61 Path GetRuntimeEnabledPaths();
62
63 // Override auto-detection of supported code paths.
64 //
65 // Passing `paths == Path::kNone` means reverting to the default behavior.
66 // This will trigger auto-detection on the next use.
67 //
68 // Other values will override auto-detection with the explicitly provided set
69 // of paths.
70 //
71 // Paths in kNonArchPaths are always implicitly supported.
72 void SetRuntimeEnabledPaths(Path paths);
73
74 Path SelectPath(Path compiled_paths);
75 void EnsureThreadSpecificResources(int thread_count);
76 TuningResolver* GetThreadSpecificTuningResolver(int thread_index) const;
77 Allocator* GetThreadSpecificAllocator(int thread_index) const;
78 Allocator* GetMainAllocator();
79 PrepackedCache* GetPrepackedCache();
80 Tuning GetMainThreadTuning();
81 void ClearPrepackedCache();
82
83 private:
84 // Downcast helpers.
85 const CtxImpl& impl() const;
86 CtxImpl* mutable_impl();
87};
88
89} // namespace ruy
90
91#endif // RUY_RUY_CTX_H_
92