the beginning of the implementation of Vulkan
This commit is contained in:
+45
-11
@@ -5,41 +5,51 @@
|
||||
#include <vulkan/vulkan.h>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
#include <fstream>
|
||||
|
||||
|
||||
|
||||
NeuralNetwork::NeuralNetwork(LayerStructure_t layers[], int count, bool useVulkan) : numLayers(count) {
|
||||
|
||||
if (useVulkan) {
|
||||
|
||||
vk::ApplicationInfo appInfo{"Xenith", 1, nullptr, 0, VK_API_VERSION_1_1};
|
||||
instance = vk::createInstance({{}, &appInfo});
|
||||
|
||||
// 2. Выбор видеокарты
|
||||
auto physicalDevices = instance.enumeratePhysicalDevices();
|
||||
if (physicalDevices.empty()) throw std::runtime_error("GPU с поддержкой Vulkan не найдены!");
|
||||
physDev = physicalDevices[0];
|
||||
auto props = physDev.getProperties();
|
||||
std::cout << "Используем GPU: " << props.deviceName << std::endl;
|
||||
|
||||
std::cout << "Используем GPU: " << physDev.getProperties().deviceName << std::endl;
|
||||
|
||||
// 3. Поиск очереди для вычислений
|
||||
// 3. Поиск очереди для вычислений (Compute)
|
||||
auto queueProps = physDev.getQueueFamilyProperties();
|
||||
int computeFamily = -1;
|
||||
for (int i = 0; i < queueProps.size(); i++) {
|
||||
if (queueProps[i].queueFlags & vk::QueueFlagBits::eCompute) {
|
||||
computeFamily = i; break;
|
||||
computeFamily = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (computeFamily == -1) throw std::runtime_error("GPU не поддерживает Compute");
|
||||
|
||||
if (computeFamily == -1) throw std::runtime_error("GPU не поддерживает вычисления (Compute)");
|
||||
|
||||
// ВАЖНО: Сохраняем индекс в переменную класса, чтобы использовать её везде
|
||||
this->computeQueueFamilyIndex = (uint32_t)computeFamily;
|
||||
|
||||
// 4. Логическое устройство
|
||||
// 4. Создание логического устройства
|
||||
float priority = 1.0f;
|
||||
vk::DeviceQueueCreateInfo queueInfo({}, (uint32_t)computeFamily, 1, &priority);
|
||||
vk::DeviceQueueCreateInfo queueInfo({}, computeQueueFamilyIndex, 1, &priority);
|
||||
vk::DeviceCreateInfo deviceCreateInfo({}, 1, &queueInfo);
|
||||
device = physDev.createDevice(deviceCreateInfo);
|
||||
|
||||
queue = device.getQueue(computeFamily, 0);
|
||||
// 5. Получаем саму очередь
|
||||
queue = device.getQueue(computeQueueFamilyIndex, 0);
|
||||
|
||||
// 6. Создаем пул команд (теперь используем правильный индекс)
|
||||
vk::CommandPoolCreateInfo poolInfo({}, computeQueueFamilyIndex);
|
||||
cmdPool = device.createCommandPool(poolInfo);
|
||||
}
|
||||
|
||||
for (int i = 0; i < count; i++) sizes.push_back(layers[i].size);
|
||||
@@ -132,6 +142,25 @@ uint32_t NeuralNetwork::findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFl
|
||||
}
|
||||
|
||||
|
||||
// Внутри класса NeuralNetwork в секции private:
|
||||
std::vector<char> NeuralNetwork::readFile(const std::string& filename) {
|
||||
std::ifstream file(filename, std::ios::ate | std::ios::binary);
|
||||
|
||||
if (!file.is_open()) {
|
||||
throw std::runtime_error("Не удалось открыть файл шейдера: " + filename);
|
||||
}
|
||||
|
||||
size_t fileSize = (size_t)file.tellg();
|
||||
std::vector<char> buffer(fileSize);
|
||||
|
||||
file.seekg(0);
|
||||
file.read(buffer.data(), fileSize);
|
||||
file.close();
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
double NeuralNetwork::trainVulkan() {
|
||||
// 1. Создание буферов
|
||||
vk::Buffer inputBuffer = device.createBuffer({{}, sizeof(float) * 2, vk::BufferUsageFlagBits::eStorageBuffer});
|
||||
@@ -218,4 +247,9 @@ double NeuralNetwork::trainVulkan() {
|
||||
device.destroyBuffer(outputBuffer); device.freeMemory(outputMemory);
|
||||
|
||||
return (double)result;
|
||||
}
|
||||
}
|
||||
|
||||
NeuralNetwork::~NeuralNetwork() {
|
||||
// Здесь позже мы добавим удаление vkInstance, vkDevice и прочего,
|
||||
// чтобы не было утечек памяти на видеокарте.
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user