Go to the documentation of this file.
2 #ifndef TILEDARRAY_CUDA_CPU_CUDA_VECTOR_H__INCLUDED
3 #define TILEDARRAY_CUDA_CPU_CUDA_VECTOR_H__INCLUDED
5 #include <btas/array_adaptor.h>
17 template <
typename T,
typename HostAlloc = std::allocator<T>,
18 typename DeviceAlloc = thrust::device_allocator<T>>
24 typedef typename thrust::host_vector<T, HostAlloc>::size_type
size_type;
25 typedef typename thrust::host_vector<T, HostAlloc>::difference_type
27 typedef typename thrust::host_vector<T, HostAlloc>::iterator
iterator;
38 : host_vec_(static_cast<int>(st) & static_cast<int>(
state::host) ?
size
42 thrust::resize(device_vec_,
size);
49 static_cast<int>(st) & static_cast<int>(
state::host) ?
size : 0,
52 static_cast<int>(st) & static_cast<int>(
state::device) ?
size : 0,
57 template <
typename RandomAccessIterator>
62 if (
on_host())
return host_vec_.size();
63 if (
on_device())
return device_vec_.size();
67 if (
on_host()) host_vec_.resize(new_size);
77 device_vec_ = host_vec_;
83 host_vec_ = device_vec_;
89 return host_vec_.data();
94 return host_vec_.data();
98 return thrust::raw_pointer_cast(device_vec_.data());
103 return thrust::raw_pointer_cast(device_vec_.data());
111 return std::begin(host_vec_);
115 return std::cbegin(host_vec_);
119 return std::cbegin(host_vec_);
123 return std::end(host_vec_);
127 return std::cend(host_vec_);
131 return std::cend(host_vec_);
145 return static_cast<int>(state_) &
static_cast<int>(
state::host);
148 return static_cast<int>(state_) &
static_cast<int>(
state::device);
152 mutable thrust::host_vector<T, HostAlloc> host_vec_;
153 mutable thrust::device_vector<T, DeviceAlloc> device_vec_;
154 mutable state state_;
157 extern template class cpu_cuda_vector<double>;
158 extern template class cpu_cuda_vector<float>;
160 template <
MemorySpace Space,
typename T,
typename HostAlloc,
161 typename DeviceAlloc>
164 return (vec.on_host() &&
overlap(MemorySpace::CPU, Space)) ||
165 (vec.on_device() &&
overlap(MemorySpace::CUDA, Space));
169 typename DeviceAlloc>
171 cudaStream_t stream = 0) {
173 case ExecutionSpace::CPU: {
182 throw std::runtime_error(
"invalid execution space");
186 template <
typename T>
188 cudaStream_t stream = 0) {
192 template <
typename T>
197 template <
typename T>
208 template <
class Archive,
typename T>
210 template <
class Archive,
typename T>
213 template <
class Archive,
typename T>
215 static inline void load(
const Archive& ar,
220 for (
auto& xi : x) ar& xi;
224 template <
class Archive,
typename T>
226 static inline void store(
const Archive& ar,
229 for (
const auto& xi : x) ar& xi;
236 #endif // TILEDARRAY_CUDA_CPU_CUDA_VECTOR_H__INCLUDED
void to_device() const
moves the data from the host to the device (even if it's there)
ExecutionSpace
enumerates the execution spaces
const T & const_reference
const_iterator begin() const
cpu_cuda_vector()
creates an empty vector
const_iterator cbegin() const
constexpr bool overlap(MemorySpace space1, MemorySpace space2)
a vector that lives on either host or device side, or both
thrust::host_vector< T, HostAlloc >::iterator iterator
cpu_cuda_vector(size_type size, T value, state st=state::host)
thrust::host_vector< T, HostAlloc >::const_iterator const_iterator
const_reference operator[](std::size_t i) const
void make_device_storage(cpu_cuda_vector< T > &storage, std::size_t n, cudaStream_t stream=0)
const T * host_data() const
reference operator[](std::size_t i)
static void store(const Archive &ar, const TiledArray::cpu_cuda_vector< T > &x)
const_iterator cend() const
cpu_cuda_vector(size_type size, state st=state::host)
static void load(const Archive &ar, TiledArray::cpu_cuda_vector< T > &x)
T * device_data(cpu_cuda_vector< T > &storage)
const T * device_data() const
cpu_cuda_vector(RandomAccessIterator begin, RandomAccessIterator end)
void resize(size_type new_size)
void to_host() const
moves the data from the device to the host (even if it's there)
void to_execution_space(cpu_cuda_vector< T, HostAlloc, DeviceAlloc > &vec, cudaStream_t stream=0)
thrust::host_vector< T, HostAlloc >::size_type size_type
thrust::host_vector< T, HostAlloc >::difference_type difference_type
MemorySpace
enumerates the memory spaces
const_iterator end() const
bool in_memory_space(const cpu_cuda_vector< T, HostAlloc, DeviceAlloc > &vec) noexcept