Implement backend factory

This commit is contained in:
2025-11-24 21:53:47 +01:00
parent a97ff8e1f6
commit a40ba96d4f
8 changed files with 104 additions and 41 deletions

View File

@@ -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