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:
@@ -1,14 +1,55 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "shape.hpp"
|
||||
#ifdef USE_CUDA
|
||||
#include "backend/cuda/cuda.cuh"
|
||||
#endif
|
||||
|
||||
namespace CUDANet {
|
||||
|
||||
// Forward declaration
|
||||
class Tensor;
|
||||
|
||||
enum BackendType { CUDA_BACKEND, CPU_BACKEND };
|
||||
|
||||
struct BackendConfig {
|
||||
int device_id = 0;
|
||||
};
|
||||
|
||||
class BackendFactory {
|
||||
public:
|
||||
static std::unique_ptr<Backend> create(BackendType backend_type, const BackendConfig& config) {
|
||||
switch (backend_type)
|
||||
{
|
||||
case BackendType::CUDA_BACKEND:
|
||||
#ifdef USE_CUDA
|
||||
|
||||
if (!CUDANet::Backends::CUDA::is_cuda_available()) {
|
||||
throw std::runtime_error("No CUDA devices found")
|
||||
}
|
||||
|
||||
auto cuda = std::make_unique<CUDANet::Backends::CUDA>(config);
|
||||
cuda.initialize();
|
||||
|
||||
return cuda;
|
||||
|
||||
#else
|
||||
throw std::runtime_error("Library was compiled without CUDA support.");
|
||||
#endif
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
class Backend {
|
||||
public:
|
||||
// Memory management
|
||||
|
||||
@@ -27,7 +27,14 @@ do { \
|
||||
namespace CUDANet::Backends {
|
||||
|
||||
class CUDA : public Backend {
|
||||
private:
|
||||
int device_id;
|
||||
public:
|
||||
CUDA(const BackendConfig& config);
|
||||
|
||||
static bool is_cuda_available();
|
||||
void initialize();
|
||||
|
||||
// Memory management
|
||||
void* allocate(size_t bytes) override;
|
||||
void deallocate(void* ptr) override;
|
||||
|
||||
@@ -41,15 +41,15 @@
|
||||
#include "layers/concat.hpp"
|
||||
|
||||
// ============================================================================
|
||||
// Utilities
|
||||
// Dataset Labels
|
||||
// ============================================================================
|
||||
|
||||
#include "utils/imagenet.hpp"
|
||||
#include "datasets/imagenet.hpp"
|
||||
|
||||
// ============================================================================
|
||||
// Backend-Specific Includes (conditionally compiled)
|
||||
// ============================================================================
|
||||
|
||||
#ifdef USE_CUDA
|
||||
#include "backend/cuda/cuda_backend.cuh"
|
||||
#include "backend/cuda/all.cuh"
|
||||
#endif
|
||||
@@ -9,6 +9,7 @@
|
||||
#endif
|
||||
|
||||
#include <format>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
|
||||
namespace CUDANet {
|
||||
|
||||
Reference in New Issue
Block a user