PLSSVM - Parallel Least Squares Support Vector Machine  2.0.0
A Least Squares Support Vector Machine implementation using different backends.
gpu_device_ptr.hpp
Go to the documentation of this file.
1 
12 #ifndef PLSSVM_BACKENDS_GPU_DEVICE_PTR_HPP_
13 #define PLSSVM_BACKENDS_GPU_DEVICE_PTR_HPP_
14 #pragma once
15 
16 #include <cstddef> // std::size_t
17 #include <type_traits> // std::is_same_v
18 #include <vector> // std::vector
19 
20 namespace plssvm::detail {
21 
28 template <typename T, typename queue_t, typename device_pointer_t = T *>
30  // any non-reference arithmetic type
31  static_assert(std::is_same_v<float, T> || std::is_same_v<double, T>, "Currently only 'float' or 'double' are allowed!");
32 
33  public:
35  using value_type = T;
41  using size_type = std::size_t;
43  using queue_type = queue_t;
45  using device_pointer_type = device_pointer_t;
46 
50  gpu_device_ptr() = default;
57 
61  gpu_device_ptr(const gpu_device_ptr &) = delete;
66  gpu_device_ptr(gpu_device_ptr &&other) noexcept;
67 
78 
82  virtual ~gpu_device_ptr() = default;
83 
88  void swap(gpu_device_ptr &other) noexcept;
94  friend void swap(gpu_device_ptr &lhs, gpu_device_ptr &rhs) noexcept { lhs.swap(rhs); }
95 
101  [[nodiscard]] explicit operator bool() const noexcept {
102  return data_ != device_pointer_type{};
103  }
108  [[nodiscard]] device_pointer_type get() noexcept {
109  return data_;
110  }
114  [[nodiscard]] device_pointer_type get() const noexcept {
115  return data_;
116  }
121  [[nodiscard]] size_type size() const noexcept {
122  return size_;
123  }
129  [[nodiscard]] bool empty() const noexcept {
130  return size_ == 0;
131  }
136  [[nodiscard]] queue_type queue() const noexcept {
137  return queue_;
138  }
139 
146  void memset(int pattern, size_type pos = 0);
155  virtual void memset(int pattern, size_type pos, size_type num_bytes) = 0;
156 
163  void fill(value_type value, size_type pos = 0);
172  virtual void fill(value_type value, size_type pos, size_type count) = 0;
173 
179  void copy_to_device(const std::vector<value_type> &data_to_copy);
188  void copy_to_device(const std::vector<value_type> &data_to_copy, size_type pos, size_type count);
201  virtual void copy_to_device(const_host_pointer_type data_to_copy, size_type pos, size_type count) = 0;
202 
208  void copy_to_host(std::vector<value_type> &buffer) const;
217  void copy_to_host(std::vector<value_type> &buffer, size_type pos, size_type count) const;
222  void copy_to_host(host_pointer_type buffer) const;
230  virtual void copy_to_host(host_pointer_type buffer, size_type pos, size_type count) const = 0;
231 
232  protected:
239 };
240 
241 } // namespace plssvm::detail
242 
243 #endif // PLSSVM_BACKENDS_GPU_DEVICE_PTR_HPP_
Small wrapper class around a GPU device pointer together with commonly used device functions for all ...
Definition: gpu_device_ptr.hpp:29
friend void swap(gpu_device_ptr &lhs, gpu_device_ptr &rhs) noexcept
Swap the contents of lhs and rhs.
Definition: gpu_device_ptr.hpp:94
virtual void memset(int pattern, size_type pos, size_type num_bytes)=0
Memset up-to num_bytes values to pattern starting at position pos.
virtual void copy_to_device(const_host_pointer_type data_to_copy, size_type pos, size_type count)=0
Copy up-to count many values from data_to_copy to the device starting at device pointer position pos.
const value_type * const_host_pointer_type
The const type of the host pointer corresponding to the wrapped device pointer.
Definition: gpu_device_ptr.hpp:39
queue_type queue_
The device queue used to manage the device memory associated with this device pointer.
Definition: gpu_device_ptr.hpp:234
queue_type queue() const noexcept
Return the queue managing the memory of the wrapped device pointer.
Definition: gpu_device_ptr.hpp:136
void copy_to_host(host_pointer_type buffer) const
Copy device_ptr::size() many values from the device to the host buffer buffer.
device_pointer_type get() const noexcept
Access the underlying device pointer.
Definition: gpu_device_ptr.hpp:114
device_pointer_type get() noexcept
Access the underlying device pointer.
Definition: gpu_device_ptr.hpp:108
T value_type
The type of the values used in the device_ptr.
Definition: gpu_device_ptr.hpp:35
device_pointer_t device_pointer_type
The type of the device pointer.
Definition: gpu_device_ptr.hpp:45
gpu_device_ptr(gpu_device_ptr &&other) noexcept
Move-constructor as device_ptr is a move-only type.
size_type size() const noexcept
Get the number of elements in the wrapped device_ptr.
Definition: gpu_device_ptr.hpp:121
queue_t queue_type
The type of the device queue used to manipulate the managed device memory.
Definition: gpu_device_ptr.hpp:43
size_type size_
The size of the managed memory.
Definition: gpu_device_ptr.hpp:238
gpu_device_ptr & operator=(gpu_device_ptr &&other) noexcept
Move-assignment-operator as device_ptr is a move-only type.
gpu_device_ptr(const gpu_device_ptr &)=delete
Delete copy-constructor to make device_ptr a move only type.
device_pointer_type data_
The device pointer pointing to the managed memory.
Definition: gpu_device_ptr.hpp:236
void swap(gpu_device_ptr &other) noexcept
Swap the contents of *this with the contents of other.
value_type * host_pointer_type
The type of the host pointer corresponding to the wrapped device pointer.
Definition: gpu_device_ptr.hpp:37
virtual void copy_to_host(host_pointer_type buffer, size_type pos, size_type count) const =0
Copy up-to count many values from the device starting at device pointer position pos to the host buff...
virtual ~gpu_device_ptr()=default
Free the memory managed by the device_ptr.
void copy_to_host(std::vector< value_type > &buffer, size_type pos, size_type count) const
Copy up-to count many values from the device starting at device pointer position pos to the host buff...
bool empty() const noexcept
Check whether the device_ptr currently maps zero elements.
Definition: gpu_device_ptr.hpp:129
void copy_to_device(const std::vector< value_type > &data_to_copy, size_type pos, size_type count)
Copy up-to count many values from data_to_copy to the device starting at device pointer position pos.
void fill(value_type value, size_type pos=0)
Fill all values with the value starting at position pos.
void copy_to_device(const_host_pointer_type data_to_copy)
Copy device_ptr::size() many values from data_to_copy to the device.
void copy_to_host(std::vector< value_type > &buffer) const
Copy device_ptr::size() many values from the device to the host buffer buffer.
gpu_device_ptr(size_type size, const queue_type queue)
Construct a device_ptr for the device managed by queue with the size size.
gpu_device_ptr & operator=(const gpu_device_ptr &)=delete
Delete copy-assignment-operator to make device_ptr a move only type.
void copy_to_device(const std::vector< value_type > &data_to_copy)
Copy device_ptr::size() many values from data_to_copy to the device.
gpu_device_ptr()=default
Default construct a gpu_device_ptr with a size of 0.
void memset(int pattern, size_type pos=0)
Memset all bytes using the pattern starting at position pos.
std::size_t size_type
The used size type.
Definition: gpu_device_ptr.hpp:41
virtual void fill(value_type value, size_type pos, size_type count)=0
Fill up-to count values to value starting at position pos.
Namespace containing implementation details. Should not directly be used by users.
Definition: csvm.hpp:27