mirror of
https://github.com/lordmathis/CUDANet.git
synced 2025-12-22 14:24:22 +00:00
Implement backend factory
This commit is contained in:
52
src/backends/cuda/cuda.cu
Normal file
52
src/backends/cuda/cuda.cu
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <format>
|
||||
|
||||
#include "backend/cuda/cuda.cuh"
|
||||
|
||||
using namespace CUDANet::Backends;
|
||||
|
||||
|
||||
CUDA::CUDA(const BackendConfig& config) {
|
||||
device_id = config.device_id < 0 ? 0 : config.device_id;
|
||||
initialize();
|
||||
}
|
||||
|
||||
bool CUDA::is_cuda_available() {
|
||||
int device_count;
|
||||
cudaError_t result = cudaGetDeviceCount(&device_count);
|
||||
|
||||
// Return false instead of crashing
|
||||
if (result != cudaSuccess || device_count == 0) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void CUDA::initialize() {
|
||||
|
||||
int device_count;
|
||||
CUDA_CHECK(cudaGetDeviceCount(&device_count));
|
||||
if (device_id >= device_count) {
|
||||
throw std::runtime_error(std::format("Invalid device id {}, only {} devices available", device_id, device_count));
|
||||
}
|
||||
|
||||
CUDA_CHECK(cudaSetDevice(device_id));
|
||||
|
||||
cudaDeviceProp deviceProp;
|
||||
CUDA_CHECK(cudaGetDeviceProperties(&deviceProp, device_id));
|
||||
|
||||
std::printf("Using CUDA device %d: %s\n", device_id, deviceProp.name);
|
||||
}
|
||||
|
||||
void* CUDA::allocate(size_t bytes) {
|
||||
void* d_ptr = nullptr;
|
||||
CUDA_CHECK(cudaMalloc(&d_ptr, bytes));
|
||||
return d_ptr;
|
||||
}
|
||||
|
||||
void CUDA::deallocate(void* ptr) {
|
||||
CUDA_CHECK(cudaFree(ptr));
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "backend/cuda/cuda.cuh"
|
||||
|
||||
cudaDeviceProp initializeCUDA() {
|
||||
int deviceCount;
|
||||
CUDA_CHECK(cudaGetDeviceCount(&deviceCount));
|
||||
|
||||
if (deviceCount == 0) {
|
||||
std::fprintf(stderr, "No CUDA devices found. Exiting.\n");
|
||||
std::exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
int device = 0;
|
||||
CUDA_CHECK(cudaSetDevice(device));
|
||||
|
||||
cudaDeviceProp deviceProp;
|
||||
CUDA_CHECK(cudaGetDeviceProperties(&deviceProp, device));
|
||||
|
||||
std::printf("Using CUDA device %d: %s\n", device, deviceProp.name);
|
||||
|
||||
return deviceProp;
|
||||
}
|
||||
|
||||
using namespace CUDANet::Backends;
|
||||
|
||||
void* CUDA::allocate(size_t bytes) {
|
||||
void* d_ptr = nullptr;
|
||||
CUDA_CHECK(cudaMalloc(&d_ptr, bytes));
|
||||
return d_ptr;
|
||||
}
|
||||
|
||||
void CUDA::deallocate(void* ptr) {
|
||||
CUDA_CHECK(cudaFree(ptr));
|
||||
}
|
||||
Reference in New Issue
Block a user