chrono_batch_render/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
0 / 8
0.0%
Branches:
0 / 31
0.0%
Functions:
-
-%

device/cuda/device.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2 #pragma once
3
4 #include <memory>
5 #include <string>
6 #include <string_view>
7 #include <utility>
8 #include <vector>
9
10 #include "chrono_batch_render/device/cuda/api.h"
11 #include "chrono_batch_render/device/device.h"
12 #include "chrono_batch_render/device/slang/compiler.h"
13
14 namespace chrono::batch_render::device::cuda {
15
16 constexpr Version kMinimumComputeCapability{7, 0};
17
18 std::vector<AdapterInfo> ListAdapters();
19
20 class ContextScope {
21 public:
22 ContextScope(const Api& cuda, CUcontext context);
23 ~ContextScope();
24 ContextScope(const ContextScope&) = delete;
25 ContextScope& operator=(const ContextScope&) = delete;
26
27 private:
28 const Api& m_cuda;
29 };
30
31 class CudaDevice : public Device {
32 public:
33 CudaDevice(const AdapterInfo& adapter, const DeviceDesc& desc, Traversal traversal);
34 ~CudaDevice() override;
35
36 ✗ const Api& Functions() const {
37
0/30
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✗ Branch 6 not taken.
✗ Branch 9 not taken.
✗ Branch 14 not taken.
✗ Branch 24 not taken.
✗ Branch 27 not taken.
✗ Branch 30 not taken.
✗ Branch 33 not taken.
✗ Branch 36 not taken.
✗ Branch 40 not taken.
✗ Branch 43 not taken.
✗ Branch 46 not taken.
✗ Branch 53 not taken.
✗ Branch 56 not taken.
✗ Branch 59 not taken.
✗ Branch 62 not taken.
✗ Branch 65 not taken.
✗ Branch 68 not taken.
✗ Branch 78 not taken.
✗ Branch 81 not taken.
✗ Branch 83 not taken.
✗ Branch 84 not taken.
✗ Branch 86 not taken.
✗ Branch 89 not taken.
✗ Branch 92 not taken.
✗ Branch 94 not taken.
✗ Branch 95 not taken.
✗ Branch 98 not taken.
✗ return m_cuda;
38 }
39
40 ✗ CUdevice Handle() const {
41 ✗ return m_device;
42 }
43
44 ✗ CUcontext Context() const {
45 ✗ return m_context;
46 }
47
48 ✗ compute::ModuleCache& Modules() {
49
0/1
✗ Branch 1 not taken.
✗ return m_modules;
50 }
51
52 protected:
53 Ref<Buffer> NewBuffer(const BufferDesc& desc) override;
54 Ref<Texture> NewTexture(const TextureDesc& desc) override;
55 void ExternalCopy(BufferRange dst, uint64_t source, Stream producer) override;
56 std::unique_ptr<Kernel> NewKernel() override;
57 std::vector<MemoryHeap> Heaps() const override;
58
59 private:
60 const Api& m_cuda;
61 CUdevice m_device = 0;
62 CUcontext m_context = nullptr;
63 compute::ModuleCache m_modules;
64 };
65
66 struct Traits {
67 using Device = CudaDevice;
68 using Api = cuda::Api;
69 using Stream = CUstream;
70 using Event = CUevent;
71 using Graph = CUgraphExec;
72 using Pool = CUmemoryPool;
73 using Module = CUmodule;
74 using Function = CUfunction;
75 using ArrayDesc = CUDA_ARRAY3D_DESCRIPTOR;
76 using ResourceDesc = CUDA_RESOURCE_DESC;
77 using SamplerDesc = CUDA_TEXTURE_DESC;
78
79 static ContextScope Enter(const CudaDevice& device);
80
81 static uint64_t Allocate(const Api& cuda, uint64_t bytes);
82 static void Free(const Api& cuda, uint64_t address);
83 static CUmemoryPool CreatePool(const CudaDevice& device, uint64_t keptBytes);
84 static void DestroyPool(const Api& cuda, CUmemoryPool pool);
85 static uint64_t AllocateFrom(const Api& cuda, CUmemoryPool pool, uint64_t bytes,
86 CUstream stream);
87 static void FreeTo(const Api& cuda, uint64_t address, CUstream stream);
88 static void SynchronizeStream(const Api& cuda, CUstream stream);
89 static void* AllocateHost(const Api& cuda, uint64_t bytes, bool writeCombined);
90 static uint64_t DeviceAddress(const Api& cuda, void* host);
91 static void FreeHost(const Api& cuda, void* host);
92
93 static CUstream CreateStream(const Api& cuda);
94 static void DestroyStream(const Api& cuda, CUstream stream);
95 static CUevent CreateEvent(const Api& cuda, bool timed);
96 static void DestroyEvent(const Api& cuda, CUevent event);
97 static void Record(const Api& cuda, CUevent event, CUstream stream, bool node);
98 static void WaitEvent(const Api& cuda, CUstream stream, CUevent event);
99 static CUstream ApplicationStream(void* handle);
100 static uint64_t ElapsedNs(const Api& cuda, CUevent from, CUevent to);
101 static bool Query(const Api& cuda, CUevent event);
102 static void Synchronize(const Api& cuda, CUevent event);
103 static void Copy(const Api& cuda, uint64_t dst, uint64_t src, uint64_t bytes, CUstream stream);
104 static void Fill(const Api& cuda, uint64_t dst, uint32_t value, uint64_t bytes,
105 CUstream stream);
106 static void Upload(const Api& cuda, uint64_t dst, const void* host, uint64_t bytes,
107 CUstream stream);
108 static void* CreateTextureArray(const Api& cuda, const ArrayDesc& desc, uint32_t levels);
109 static void DestroyTextureArray(const Api& cuda, void* array);
110 static void* TextureLevel(const Api& cuda, void* array, uint32_t level);
111 static uint64_t CreateTextureObject(const Api& cuda, const ResourceDesc& resource,
112 const SamplerDesc& sampler);
113 static void DestroyTextureObject(const Api& cuda, uint64_t object);
114 static void CopyToArray(const Api& cuda, void* level, uint64_t source, uint64_t rowBytes,
115 uint32_t rows, CUstream stream);
116 static void BeginCapture(const Api& cuda, CUstream stream);
117 static CUgraphExec EndCapture(const Api& cuda, CUstream stream);
118 static void LaunchGraph(const Api& cuda, CUgraphExec graph, CUstream stream);
119 static void DestroyGraph(const Api& cuda, CUgraphExec graph);
120
121 static const compute::RuntimeCompiler& RuntimeCompiler();
122 static std::string_view Prelude();
123 static std::vector<std::string> CompileOptions(const std::string& architecture, Size3 groupSize,
124 uint32_t maxRegisters, bool lineInfo);
125 static CUmodule LoadModule(const Api& cuda, const std::string& binary);
126 static void UnloadModule(const Api& cuda, CUmodule module);
127 static std::pair<uint64_t, size_t> Symbol(const Api& cuda, CUmodule module, const char* name);
128 static void CopyToDevice(const Api& cuda, uint64_t dst, const void* host, uint64_t bytes);
129 static CUfunction GetFunction(const Api& cuda, CUmodule module, const char* name);
130 static KernelStats Stats(const Api& cuda, CUfunction function);
131 static void Launch(const Api& cuda, CUfunction function, Size3 groups, Size3 threads,
132 void** extra, CUstream stream);
133 static constexpr uintptr_t kParamBuffer = CU_LAUNCH_PARAM_BUFFER_POINTER;
134 static constexpr uintptr_t kParamBufferSize = CU_LAUNCH_PARAM_BUFFER_SIZE;
135 static constexpr uintptr_t kParamEnd = CU_LAUNCH_PARAM_END;
136 };
137
138 }
139