the beginning of the implementation of Vulkan

This commit is contained in:
2026-04-30 01:35:33 +07:00
parent 8abdea6b77
commit 92a595b2f1
4 changed files with 63 additions and 14 deletions
+45 -11
View File
@@ -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 и прочего,
// чтобы не было утечек памяти на видеокарте.
}
+17 -2
View File
@@ -4,7 +4,6 @@
#include "typedef.hpp"
#include <vector>
#include <cmath>
#include "core.hpp"
#include <cstdlib>
#include <omp.h>
#include <vulkan/vulkan.hpp>
@@ -13,28 +12,44 @@
class NeuralNetwork {
private:
// Параметры нейросети
int numLayers;
std::vector<int> sizes;
std::vector<std::vector<std::vector<double>>> weights;
std::vector<std::vector<double>> biases;
std::vector<std::vector<double>> outputs;
// Объекты Vulkan
bool useVulkan; // Сохраняем выбор пользователя
vk::Instance instance;
vk::PhysicalDevice physDev;
vk::Device device;
vk::Queue queue;
vk::CommandPool cmdPool;
uint32_t computeQueueFamilyIndex; // Индекс очереди для команд
uint32_t NeuralNetwork::findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties);
// Вспомогательные методы Vulkan
uint32_t findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties);
std::vector<char> readFile(const std::string& filename);
// Математика CPU
double sigmoid(double x) { return 1.0 / (1.0 + exp(-x)); }
double sigmoidDeriv(double x) { return x * (1.0 - x); }
public:
int cpu_count = 1;
// Конструктор
NeuralNetwork(LayerStructure_t layers[], int count, bool useVulkan = false);
// Деструктор (ВАЖНО для очистки Vulkan)
~NeuralNetwork();
// Методы работы
std::vector<double> feedForward(const std::vector<double>& input);
double train(const std::vector<double>& input, const std::vector<double>& target, double lr);
// Наш тест Vulkan
double trainVulkan();
};