Repository navigation
Expand file tree
/
Copy pathVRAMCacheManager.cpp
More file actions
125 lines (103 loc) · 4.01 KB
/
Copy pathVRAMCacheManager.cpp
File metadata and controls
125 lines (103 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "VRAMCacheManager.h"
#include <dxgi.h>
#include <iostream>
#include <windows.h>
#include <cuda_runtime.h>
#include <d3d12.h>
#include "d3d11.h"
VRAMCacheManager::VRAMCacheManager() : maxCacheSize(1024), currentCacheSize(0) {}
VRAMCacheManager::~VRAMCacheManager() {
ReleaseVRAM();
}
bool VRAMCacheManager::Initialize(size_t maxCacheSizeMB) {
if (!DetectDirectXVersion()) {
std::cerr << "DirectX 11 or 12 not detected! VRAM Cache will not be used." << std::endl;
return false;
}
maxCacheSize = maxCacheSizeMB;
currentCacheSize = 0;
std::cout << "VRAM Cache initialized with max size: " << maxCacheSize << " MB" << std::endl;
return true;
}
bool VRAMCacheManager::DetectDirectXVersion() {
IDXGIFactory* factory = nullptr;
if (FAILED(CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)&factory))) {
std::cerr << "Failed to create DXGI Factory. DirectX may not be available." << std::endl;
return false;
}
IDXGIAdapter* adapter = nullptr;
if (FAILED(factory->EnumAdapters(0, &adapter))) {
std::cerr << "No DirectX-compatible GPU detected!" << std::endl;
factory->Release();
return false;
}
DXGI_ADAPTER_DESC desc;
adapter->GetDesc(&desc);
std::wcout << L"Detected GPU: " << desc.Description << std::endl;
bool dx12Supported = false, dx11Supported = false;
// Check if DirectX 12 is supported
ID3D12Device* d3d12Device = nullptr;
if (SUCCEEDED(D3D12CreateDevice(adapter, D3D_FEATURE_LEVEL_12_0, __uuidof(ID3D12Device), (void**)&d3d12Device))) {
std::cout << "DirectX 12 is supported!" << std::endl;
dx12Supported = true;
d3d12Device->Release();
}
// Check if DirectX 11 is supported
ID3D11Device* d3d11Device = nullptr;
ID3D11DeviceContext* context = nullptr;
if (SUCCEEDED(D3D11CreateDevice(adapter, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0, nullptr, 0,
D3D11_SDK_VERSION, &d3d11Device, nullptr, &context))) {
std::cout << "DirectX 11 is supported!" << std::endl;
dx11Supported = true;
d3d11Device->Release();
context->Release();
}
adapter->Release();
factory->Release();
return dx12Supported || dx11Supported;
}
bool VRAMCacheManager::StoreDataInVRAM(const std::string& key, const std::vector<float>& data) {
size_t dataSize = data.size() * sizeof(float);
if (currentCacheSize + dataSize > maxCacheSize * 1024 * 1024) {
std::cerr << "VRAM Cache limit exceeded! Cannot store " << key << std::endl;
return false;
}
void* deviceBuffer;
if (!AllocateVRAMBuffer(&deviceBuffer, dataSize)) return false;
cudaMemcpy(deviceBuffer, data.data(), dataSize, cudaMemcpyHostToDevice);
vramCache[key] = deviceBuffer;
vramCacheSize[key] = dataSize;
currentCacheSize += dataSize;
std::cout << "Stored " << key << " in VRAM (Size: " << dataSize / (1024.0 * 1024.0) << " MB)" << std::endl;
return true;
}
bool VRAMCacheManager::RetrieveDataFromVRAM(const std::string& key, std::vector<float>& data) {
if (vramCache.find(key) == vramCache.end()) {
std::cerr << "No VRAM cache found for key: " << key << std::endl;
return false;
}
size_t dataSize = vramCacheSize[key];
data.resize(dataSize / sizeof(float));
cudaMemcpy(data.data(), vramCache[key], dataSize, cudaMemcpyDeviceToHost);
std::cout << "Retrieved " << key << " from VRAM" << std::endl;
return true;
}
void VRAMCacheManager::ReleaseVRAM() {
for (auto& entry : vramCache) {
FreeVRAMBuffer(entry.second);
}
vramCache.clear();
vramCacheSize.clear();
currentCacheSize = 0;
std::cout << "Released all VRAM Cache." << std::endl;
}
bool VRAMCacheManager::AllocateVRAMBuffer(void** buffer, size_t size) {
if (cudaMalloc(buffer, size) != cudaSuccess) {
std::cerr << "Failed to allocate VRAM buffer of size: " << size << " bytes" << std::endl;
return false;
}
return true;
}
void VRAMCacheManager::FreeVRAMBuffer(void* buffer) {
cudaFree(buffer);
}