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 // Internal implementation details for Ctx. Drags in the entire world. Avoid
17 // #including this, use "ctx.h" instead.
18 
19 #ifndef RUY_RUY_CTX_IMPL_H_
20 #define RUY_RUY_CTX_IMPL_H_
21 
22 #include <cstddef>
23 #include <memory>
24 #include <vector>
25 
26 #include "ruy/allocator.h"
27 #include "ruy/cpuinfo.h"
28 #include "ruy/ctx.h"
29 #include "ruy/path.h"
30 #include "ruy/performance_advisory.h"
31 #include "ruy/prepacked_cache.h"
32 #include "ruy/thread_pool.h"
33 #include "ruy/tune.h"
34 
35 namespace ruy {
36 
37 // The resources private to each Ruy thread.
38 struct ThreadSpecificResource final {
39   // Each thread may be running on a different microarchitecture. For example,
40   // some threads may be on big cores, while others are on little cores. Thus,
41   // it's best for the tuning to be per-thread.
42   TuningResolver tuning_resolver;
43   // Each thread has its own local allocator.
44   Allocator allocator;
45 };
46 
47 // CtxImpl is what actually holds all the data members in a context.
48 // It is a subclass of Ctx, which provides the interface that is what most
49 // of ruy's code needs.
50 //
51 // A key requirement is that since many ruy files, including public headers,
52 // need a definition of Ctx, the "ctx.h" header defining it must minimize how
53 // many other ruy internal headers it includes. That is achieved by putting data
54 // members in the CtxImpl subclass, and ensuring that only a few .cc files, not
55 // header files, need a definition of CtxImpl.
56 class CtxImpl final : public Ctx {
57  private:
58   friend class Ctx;
59 
60   // Single Path bit indicating which Path was used last.
61   Path last_used_path_ = Path::kNone;
62   PerformanceAdvisory performance_advisory_ = PerformanceAdvisory::kNone;
63   Tuning explicit_tuning_ = Tuning::kAuto;
64   ThreadPool thread_pool_;
65   int max_num_threads_ = 1;
66   // Allocator for main thread work before invoking the threadpool.
67   // Our simple Allocator does not allow reserving/allocating more blocks
68   // while it's already in committed state, so the main thread needs both
69   // this allocator, and its per-thread allocator.
70   std::unique_ptr<Allocator> main_allocator_;
71   std::unique_ptr<PrepackedCache> prepacked_cache_;
72   // Set of Paths enabled at runtime. By default, that is based on runtime
73   // detection, but may be overridden. The initial value kNone
74   // means that detection has not yet been performed.
75   Path runtime_enabled_paths_ = Path::kNone;
76   CpuInfo cpuinfo_;
77   // State for each thread in the thread pool. Entry 0 is the main thread.
78   std::vector<std::unique_ptr<ThreadSpecificResource>>
79       thread_specific_resources_;
80 };
81 
82 }  // namespace ruy
83 
84 #endif  // RUY_RUY_CTX_IMPL_H_
85