40#ifndef PFC_GPU_VECTOR_HPP
41#define PFC_GPU_VECTOR_HPP
48#if defined(OpenPFC_ENABLE_CUDA)
49#include <cuda_runtime.h>
50#define PFC_GPU_CUDA_AVAILABLE 1
52#define PFC_GPU_CUDA_AVAILABLE 0
75#if PFC_GPU_CUDA_AVAILABLE
76 T *m_device_ptr =
nullptr;
78 void *m_device_ptr =
nullptr;
95#if PFC_GPU_CUDA_AVAILABLE
99 throw std::runtime_error(
"Failed to allocate GPU memory: " +
107 throw std::runtime_error(
"GPUVector: CUDA not enabled at compile time");
116#if PFC_GPU_CUDA_AVAILABLE
132 other.m_device_ptr =
nullptr;
140 if (
this != &
other) {
141#if PFC_GPU_CUDA_AVAILABLE
142 if (m_device_ptr)
cudaFree(m_device_ptr);
144 m_device_ptr =
other.m_device_ptr;
146 other.m_device_ptr =
nullptr;
161#if PFC_GPU_CUDA_AVAILABLE
172#if PFC_GPU_CUDA_AVAILABLE
182 size_t size()
const {
return m_size; }
187 bool empty()
const {
return m_size == 0; }
197 throw std::runtime_error(
"Size mismatch in copy_from_host: expected " +
198 std::to_string(m_size) +
", got " +
201#if PFC_GPU_CUDA_AVAILABLE
206 throw std::runtime_error(
"Failed to copy from host to device: " +
211 throw std::runtime_error(
"copy_from_host: CUDA not enabled at compile time");
224#if PFC_GPU_CUDA_AVAILABLE
229 throw std::runtime_error(
"Failed to copy from device to host: " +
234 throw std::runtime_error(
"copy_to_host: CUDA not enabled at compile time");
244 std::vector<T>
result(m_size);
253#undef PFC_GPU_CUDA_AVAILABLE
Simple GPU memory container (RAII)
Definition gpu_vector.hpp:73
const T * data() const
Get const pointer to GPU memory.
Definition gpu_vector.hpp:171
size_t size() const
Get the number of elements.
Definition gpu_vector.hpp:182
void copy_from_host(const std::vector< T > &host_data)
Copy data from host (CPU) to device (GPU)
Definition gpu_vector.hpp:195
~GPUVector()
Destructor - automatically frees GPU memory.
Definition gpu_vector.hpp:115
std::vector< T > to_host() const
Copy data from device to host and return as vector.
Definition gpu_vector.hpp:243
void copy_to_host(std::vector< T > &host_data) const
Copy data from device (GPU) to host (CPU)
Definition gpu_vector.hpp:220
GPUVector(GPUVector &&other) noexcept
Move constructor.
Definition gpu_vector.hpp:130
GPUVector & operator=(GPUVector &&other) noexcept
Move assignment operator.
Definition gpu_vector.hpp:139
T * data()
Get pointer to GPU memory.
Definition gpu_vector.hpp:160
bool empty() const
Check if the vector is empty.
Definition gpu_vector.hpp:187
GPUVector(size_t size)
Construct a GPUVector with the specified size.
Definition gpu_vector.hpp:94
Represents the global simulation domain (the "world").
Definition world.hpp:91
const Int3 m_size
Grid dimensions: {nx, ny, nz}.
Definition world.hpp:94