mirror of
https://github.com/lordmathis/CUDANet.git
synced 2025-11-05 17:34:21 +00:00
Restructure cuda backend
This commit is contained in:
@@ -20,6 +20,7 @@ if(USE_CUDA)
|
||||
endif()
|
||||
|
||||
file(GLOB_RECURSE CPU_SOURCES
|
||||
src/layers/*.cpp
|
||||
src/model/*.cpp
|
||||
)
|
||||
|
||||
@@ -27,10 +28,11 @@ set(LIBRARY_SOURCES ${CPU_SOURCES})
|
||||
|
||||
if(USE_CUDA)
|
||||
file(GLOB_RECURSE CUDA_SOURCES
|
||||
src/*.cu
|
||||
src/cuda/utils/*.cu
|
||||
src/cuda/kernels/*.cu
|
||||
src/cuda/layers/*.cu
|
||||
src/backends/cuda/*.cu
|
||||
src/backends/cuda/utils/*.cu
|
||||
src/backends/cuda/kernels/*.cu
|
||||
src/backends/cuda/layers/*.cu
|
||||
src/layers/*.cu # To be removed
|
||||
)
|
||||
set(LIBRARY_SOURCES ${LIBRARY_SOURCES} ${CUDA_SOURCES})
|
||||
endif()
|
||||
|
||||
@@ -25,13 +25,23 @@ class Add {
|
||||
* @param d_inputB Device pointer to the second input
|
||||
*
|
||||
*/
|
||||
void forward(const float* d_inputA, const float* d_inputB);
|
||||
float* forward(const float* inputA, const float* inputB);
|
||||
|
||||
private:
|
||||
int inputSize;
|
||||
|
||||
float* output;
|
||||
|
||||
float* forwardCPU(const float* inputA, const float* inputB);
|
||||
|
||||
#ifdef USE_CUDA
|
||||
float* d_output;
|
||||
int gridSize;
|
||||
|
||||
float* d_output;
|
||||
float* forwardCUDA(const float* d_inputA, const float* d_inputB);
|
||||
void initCUDA();
|
||||
void delCUDA();
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace CUDANet::Layers
|
||||
@@ -1,26 +1,21 @@
|
||||
#include "add.cuh"
|
||||
#include "add.hpp"
|
||||
#include "matmul.cuh"
|
||||
#include "cuda_helper.cuh"
|
||||
|
||||
using namespace CUDANet::Layers;
|
||||
|
||||
|
||||
Add::Add(int inputSize)
|
||||
: inputSize(inputSize) {
|
||||
|
||||
void Add::initCUDA() {
|
||||
d_output = nullptr;
|
||||
CUDA_CHECK(cudaMalloc((void**)&d_output, sizeof(float) * inputSize));
|
||||
|
||||
gridSize = (inputSize + BLOCK_SIZE - 1) / BLOCK_SIZE;
|
||||
}
|
||||
|
||||
|
||||
Add::~Add() {
|
||||
void Add::delCUDA() {
|
||||
cudaFree(d_output);
|
||||
}
|
||||
|
||||
|
||||
void Add::forward(const float* d_inputA, const float* d_inputB) {
|
||||
float* Add::forwardCUDA(const float* d_inputA, const float* d_inputB) {
|
||||
|
||||
Kernels::vec_vec_add<<<gridSize, BLOCK_SIZE>>>(
|
||||
d_inputA, d_inputB, d_output, inputSize
|
||||
@@ -28,4 +23,6 @@ void Add::forward(const float* d_inputA, const float* d_inputB) {
|
||||
CUDA_CHECK(cudaGetLastError());
|
||||
CUDA_CHECK(cudaDeviceSynchronize());
|
||||
|
||||
}
|
||||
return d_output;
|
||||
|
||||
}
|
||||
44
src/layers/add.cpp
Normal file
44
src/layers/add.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "add.hpp"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
using namespace CUDANet::Layers;
|
||||
|
||||
|
||||
Add::Add(int inputSize)
|
||||
: inputSize(inputSize) {
|
||||
|
||||
output = new float[inputSize];
|
||||
|
||||
#ifdef USE_CUDA
|
||||
initCUDA();
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
Add::~Add() {
|
||||
#ifdef USE_CUDA
|
||||
delCUDA();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
float* Add::forward(const float* inputA, const float* inputB) {
|
||||
|
||||
#ifdef USE_CUDA
|
||||
return forwardCUDA(inputA, inputB);
|
||||
#else
|
||||
return forwardCPU(inputA, inputB);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
float* Add::forwardCPU(const float* inputA, const float* inputB) {
|
||||
for (size_t i = 0; i < inputSize; i++)
|
||||
{
|
||||
output[i] = inputA[i] + inputB[i];
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
Reference in New Issue
Block a user