diff --git a/Xenith/core.cpp b/Xenith/core.cpp index 3660fa0..23e1572 100644 --- a/Xenith/core.cpp +++ b/Xenith/core.cpp @@ -5,41 +5,51 @@ #include #include #include -#include #include +#include 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 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 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; -} \ No newline at end of file +} + +NeuralNetwork::~NeuralNetwork() { + // Здесь позже мы добавим удаление vkInstance, vkDevice и прочего, + // чтобы не было утечек памяти на видеокарте. +} diff --git a/Xenith/core.hpp b/Xenith/core.hpp index 55c53aa..5bed9ef 100644 --- a/Xenith/core.hpp +++ b/Xenith/core.hpp @@ -4,7 +4,6 @@ #include "typedef.hpp" #include #include -#include "core.hpp" #include #include #include @@ -13,28 +12,44 @@ class NeuralNetwork { private: + // Параметры нейросети int numLayers; std::vector sizes; std::vector>> weights; std::vector> biases; std::vector> 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 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 feedForward(const std::vector& input); double train(const std::vector& input, const std::vector& target, double lr); + + // Наш тест Vulkan double trainVulkan(); }; diff --git a/main b/main index 392016e..0e8baa1 100755 Binary files a/main and b/main differ diff --git a/main.cpp b/main.cpp index c5abef7..aadb8b3 100644 --- a/main.cpp +++ b/main.cpp @@ -190,7 +190,7 @@ int main() { std::cout << "System Prompt updated!" << std::endl; } else if (cmdIn == "/trainVulkan") { - nn.trainVulkan(); + std::cout << nn.trainVulkan(); } else if (cmdIn == "/help") { std::cout << "\n--- MENU ---" << std::endl;