chrono_batch_render/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
22 / 25
88.0%
Branches:
31 / 44
70.5%
Functions:
1 / 1, 3 excl
100.0%

device/program.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2 #pragma once
3
4 #include <chrono>
5 #include <cstdint>
6 #include <map>
7 #include <memory>
8 #include <optional>
9 #include <string>
10 #include <string_view>
11 #include <vector>
12
13 #include "chrono_batch_render/device/adapter.h"
14 #include "chrono_batch_render/device/resource.h"
15 #include "chrono_batch_render/export.h"
16
17 namespace chrono::batch_render {
18 class DiskCache;
19 struct KernelLibrary;
20 struct DeviceGlobals;
21 }
22
23 namespace chrono::batch_render::device {
24
25 struct KernelCode;
26 class Program;
27
28 struct Size3 {
29 uint32_t x = 1;
30 uint32_t y = 1;
31 uint32_t z = 1;
32
33 bool operator==(const Size3&) const = default;
34 };
35
36 inline constexpr uint32_t kMaxGroupsPerAxis = 65535;
37
38 171 constexpr Size3 FoldGroups(uint32_t count) {
39 171 const uint64_t plane = uint64_t(kMaxGroupsPerAxis) * kMaxGroupsPerAxis;
40 171 const uint32_t z = uint32_t((count + plane - 1) / plane);
41 171 const uint64_t perLayer = (uint64_t(count) + z - 1) / z;
42 171 const uint32_t y = uint32_t((perLayer + kMaxGroupsPerAxis - 1) / kMaxGroupsPerAxis);
43 171 const uint32_t x = uint32_t((perLayer + y - 1) / y);
44 171 return {x, y, z};
45 }
46
47 67378 constexpr uint32_t GroupCount(uint64_t threadCount, uint32_t threadsPerGroup) {
48
25/25
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 36101 times.
✓ Branch 1 taken 15433 times.
✓ Branch 2 taken 947 times.
✓ Branch 3 taken 30 times.
✓ Branch 4 taken 383 times.
✓ Branch 4 taken 40 times.
✓ Branch 4 taken 2994 times.
✓ Branch 5 taken 947 times.
✓ Branch 6 taken 36 times.
✓ Branch 7 taken 1205 times.
✓ Branch 7 taken 43 times.
✓ Branch 7 taken 101 times.
✓ Branch 7 taken 757 times.
✓ Branch 7 taken 1217 times.
✓ Branch 10 taken 7 times.
✓ Branch 10 taken 784 times.
✓ Branch 10 taken 101 times.
✓ Branch 10 taken 811 times.
✓ Branch 13 taken 273 times.
✓ Branch 13 taken 76 times.
✓ Branch 15 taken 8 times.
✓ Branch 16 taken 76 times.
✓ Branch 18 taken 8 times.
✓ Branch 21 taken 8 times.
67378 return uint32_t((threadCount + threadsPerGroup - 1) / threadsPerGroup);
49 }
50
51 CHRONO_BATCH_RENDER_API void RegisterFile(std::string path, std::string source);
52
53 struct ProgramDesc {
54 std::vector<std::string> modules;
55 const KernelLibrary* library = nullptr;
56 std::map<std::string, std::string, std::less<>> sources;
57 std::vector<std::string> entries;
58 bool loadEntries = true;
59 std::string label = "program";
60 Traversal traversal = Traversal::Auto;
61 };
62
63 ✗ struct KernelStat {
64 std::string name;
65 std::string value;
66 };
67
68 1117 struct KernelStats {
69 double compileMs = 0;
70 bool cached = false;
71 std::optional<uint32_t> registers;
72 std::optional<uint32_t> spillBytes;
73 std::optional<uint32_t> sharedBytes;
74 std::vector<KernelStat> driver;
75 };
76
77 class CHRONO_BATCH_RENDER_API Kernel {
78 public:
79 virtual ~Kernel();
80 Kernel(const Kernel&) = delete;
81 Kernel& operator=(const Kernel&) = delete;
82
83 3354 const std::string& Name() const {
84
5/16
<unknown function 63>:
✓ Branch 0 taken 1102 times.
✓ Branch 1 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 1117 times.
✓ Branch 8 taken 1119 times.
<unknown function 83>:
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 1 not taken.
✗ Branch 4 not taken.
chrono::batch_render::device::FoldGroups(unsigned int):
✗ Branch 1 not taken.
✗ Branch 4 not taken.
✗ Branch 7 not taken.
✗ Branch 10 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 19 not taken.
4471 return m_name;
85 }
86
87 233700 Program& GetProgram() const {
88
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 1 not taken.
233700 return *m_program;
89 }
90
91 bool Ready() const;
92
93 void Wait() const;
94
95 Size3 GroupSize() const;
96
97 uint32_t ParamBytes() const;
98
99 bool Foldable() const;
100
101 const KernelStats& Stats() const;
102
103 protected:
104 Kernel();
105
106 virtual KernelStats Load(const KernelCode& code, const DiskCache& cache,
107 const DeviceGlobals& globals) = 0;
108
109 private:
110 friend class Program;
111 struct State;
112
113 std::string m_name;
114 Program* m_program = nullptr;
115 std::unique_ptr<State> m_state;
116 };
117
118 CHRONO_BATCH_RENDER_API void WaitForBuilds();
119
120 class CHRONO_BATCH_RENDER_API Program : public Resource {
121 public:
122 ~Program() override;
123
124 Kernel& Entry(std::string_view name);
125
126 private:
127 friend class Device;
128 struct State;
129
130 Program(Device& device, const ProgramDesc& desc,
131 std::shared_ptr<const std::map<std::string, std::string, std::less<>>> sources);
132
133 Kernel& Request(std::string_view name, bool wanted);
134
135 void QueueLoad(Kernel& kernel);
136
137 void Load(Kernel& kernel, const KernelCode& code, const DiskCache& cache,
138 std::chrono::steady_clock::time_point start);
139
140 void Finish(Kernel& kernel, const std::string& label);
141
142 std::unique_ptr<State> m_state;
143 };
144
145 }
146