mirror of
https://github.com/StepanovPlaton/NeuralNetwork.git
synced 2026-04-03 20:30:39 +04:00
First NN forward
This commit is contained in:
@@ -1,15 +1,35 @@
|
||||
#include <random>
|
||||
|
||||
#include "matrix.hpp"
|
||||
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
|
||||
Matrices::GPU::GPU(int rows, int cols)
|
||||
: IMatrix(rows, cols), queue(openCL.getContext(), openCL.getDevice()) {
|
||||
validateDimensions(rows, cols);
|
||||
std::vector<float> matrix;
|
||||
matrix.reserve(rows * cols);
|
||||
for (size_t i = 0; i < (size_t)rows * (size_t)cols; ++i)
|
||||
matrix.push_back(std::generate_canonical<float, 32>(gen));
|
||||
buffer = new cl::Buffer(openCL.getContext(), CL_MEM_READ_WRITE,
|
||||
rows * cols * sizeof(float));
|
||||
queue.enqueueWriteBuffer(*buffer, CL_TRUE, 0, rows * cols * sizeof(float),
|
||||
matrix.data());
|
||||
queue.finish();
|
||||
}
|
||||
|
||||
Matrices::GPU::GPU(int rows, int cols, const std::vector<float> &matrix)
|
||||
: IMatrix(rows, cols), queue(openCL.getContext(), openCL.getDevice()) {
|
||||
validateDimensions(rows, cols);
|
||||
if (matrix.size() != static_cast<size_t>(rows * cols)) {
|
||||
throw std::invalid_argument("Matrix data size doesn't match dimensions");
|
||||
}
|
||||
|
||||
buffer = new cl::Buffer(
|
||||
openCL.getContext(), CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
|
||||
rows * cols * sizeof(float), const_cast<float *>(matrix.data()));
|
||||
buffer = new cl::Buffer(openCL.getContext(), CL_MEM_READ_WRITE,
|
||||
rows * cols * sizeof(float));
|
||||
queue.enqueueWriteBuffer(*buffer, CL_TRUE, 0, rows * cols * sizeof(float),
|
||||
matrix.data());
|
||||
queue.finish();
|
||||
}
|
||||
|
||||
const std::vector<float> Matrices::GPU::toVector() const {
|
||||
|
||||
Reference in New Issue
Block a user