PLSSVM - Parallel Least Squares Support Vector Machine  2.0.0
A Least Squares Support Vector Machine implementation using different backends.
csvm.hpp
Go to the documentation of this file.
1 
12 #ifndef PLSSVM_BACKENDS_OPENCL_CSVM_HPP_
13 #define PLSSVM_BACKENDS_OPENCL_CSVM_HPP_
14 #pragma once
15 
16 #include "plssvm/backends/OpenCL/detail/command_queue.hpp" // plssvm::opencl::detail::command_queue
17 #include "plssvm/backends/OpenCL/detail/context.hpp" // plssvm::opencl::detail::context
18 #include "plssvm/backends/OpenCL/detail/device_ptr.hpp" // plssvm::opencl::detail::device_ptr
19 #include "plssvm/backends/gpu_csvm.hpp" // plssvm::detail::gpu_csvm
20 #include "plssvm/parameter.hpp" // plssvm::parameter, plssvm::detail::parameter
21 #include "plssvm/target_platforms.hpp" // plssvm::target_platform
22 
23 #include <cstddef> // std::size_t
24 #include <type_traits> // std::true_type
25 #include <utility> // std::forward
26 #include <vector> // std::vector
27 
28 namespace plssvm {
29 
30 namespace detail {
31 
32 // forward declare execution_range class
33 class execution_range;
34 
35 } // namespace detail
36 
37 namespace opencl {
38 
42 class csvm : public ::plssvm::detail::gpu_csvm<detail::device_ptr, detail::command_queue> {
43  protected:
44  // protected for test MOCK class
47 
48  using base_type::devices_;
49 
50  public:
52  using typename base_type::queue_type;
53 
62  explicit csvm(parameter params = {});
72  explicit csvm(target_platform target, parameter params = {});
73 
82  template <typename... Args, PLSSVM_REQUIRES(::plssvm::detail::has_only_parameter_named_args_v<Args...>)>
83  explicit csvm(Args &&...named_args) :
84  csvm{ plssvm::target_platform::automatic, std::forward<Args>(named_args)... } {}
94  template <typename... Args, PLSSVM_REQUIRES(::plssvm::detail::has_only_parameter_named_args_v<Args...>)>
95  explicit csvm(const target_platform target, Args &&...named_args) :
96  base_type{ std::forward<Args>(named_args)... } {
97  this->init(target);
98  }
99 
103  csvm(const csvm &) = delete;
107  csvm(csvm &&) noexcept = default;
111  csvm &operator=(const csvm &) = delete;
115  csvm &operator=(csvm &&) noexcept = default;
120  ~csvm() override;
121 
122  protected:
126  void device_synchronize(const queue_type &queue) const final;
127 
131  void run_q_kernel(std::size_t device, const ::plssvm::detail::execution_range &range, const ::plssvm::detail::parameter<float> &params, device_ptr_type<float> &q_d, const device_ptr_type<float> &data_d, const device_ptr_type<float> &data_last_d, std::size_t num_data_points_padded, std::size_t num_features) const final { this->run_q_kernel_impl(device, range, params, q_d, data_d, data_last_d, num_data_points_padded, num_features); }
135  void run_q_kernel(std::size_t device, const ::plssvm::detail::execution_range &range, const ::plssvm::detail::parameter<double> &params, device_ptr_type<double> &q_d, const device_ptr_type<double> &data_d, const device_ptr_type<double> &data_last_d, std::size_t num_data_points_padded, std::size_t num_features) const final { this->run_q_kernel_impl(device, range, params, q_d, data_d, data_last_d, num_data_points_padded, num_features); }
139  template <typename real_type>
140  void run_q_kernel_impl(std::size_t device, const ::plssvm::detail::execution_range &range, const ::plssvm::detail::parameter<real_type> &params, device_ptr_type<real_type> &q_d, const device_ptr_type<real_type> &data_d, const device_ptr_type<real_type> &data_last_d, std::size_t num_data_points_padded, std::size_t num_features) const;
144  void run_svm_kernel(std::size_t device, const ::plssvm::detail::execution_range &range, const ::plssvm::detail::parameter<float> &params, const device_ptr_type<float> &q_d, device_ptr_type<float> &r_d, const device_ptr_type<float> &x_d, const device_ptr_type<float> &data_d, float QA_cost, float add, std::size_t num_data_points_padded, std::size_t num_features) const final { this->run_svm_kernel_impl(device, range, params, q_d, r_d, x_d, data_d, QA_cost, add, num_data_points_padded, num_features); }
148  void run_svm_kernel(std::size_t device, const ::plssvm::detail::execution_range &range, const ::plssvm::detail::parameter<double> &params, const device_ptr_type<double> &q_d, device_ptr_type<double> &r_d, const device_ptr_type<double> &x_d, const device_ptr_type<double> &data_d, double QA_cost, double add, std::size_t num_data_points_padded, std::size_t num_features) const final { this->run_svm_kernel_impl(device, range, params, q_d, r_d, x_d, data_d, QA_cost, add, num_data_points_padded, num_features); }
152  template <typename real_type>
153  void run_svm_kernel_impl(std::size_t device, const ::plssvm::detail::execution_range &range, const ::plssvm::detail::parameter<real_type> &params, const device_ptr_type<real_type> &q_d, device_ptr_type<real_type> &r_d, const device_ptr_type<real_type> &x_d, const device_ptr_type<real_type> &data_d, real_type QA_cost, real_type add, std::size_t num_data_points_padded, std::size_t num_features) const;
157  void run_w_kernel(std::size_t device, const ::plssvm::detail::execution_range &range, device_ptr_type<float> &w_d, const device_ptr_type<float> &alpha_d, const device_ptr_type<float> &data_d, const device_ptr_type<float> &data_last_d, std::size_t num_data_points, std::size_t num_features) const final { this->run_w_kernel_impl(device, range, w_d, alpha_d, data_d, data_last_d, num_data_points, num_features); }
161  void run_w_kernel(std::size_t device, const ::plssvm::detail::execution_range &range, device_ptr_type<double> &w_d, const device_ptr_type<double> &alpha_d, const device_ptr_type<double> &data_d, const device_ptr_type<double> &data_last_d, std::size_t num_data_points, std::size_t num_features) const final { this->run_w_kernel_impl(device, range, w_d, alpha_d, data_d, data_last_d, num_data_points, num_features); }
165  template <typename real_type>
166  void run_w_kernel_impl(std::size_t device, const ::plssvm::detail::execution_range &range, device_ptr_type<real_type> &w_d, const device_ptr_type<real_type> &alpha_d, const device_ptr_type<real_type> &data_d, const device_ptr_type<real_type> &data_last_d, std::size_t num_data_points, std::size_t num_features) const;
170  void run_predict_kernel(const ::plssvm::detail::execution_range &range, const ::plssvm::detail::parameter<float> &params, device_ptr_type<float> &out_d, const device_ptr_type<float> &alpha_d, const device_ptr_type<float> &point_d, const device_ptr_type<float> &data_d, const device_ptr_type<float> &data_last_d, std::size_t num_support_vectors, std::size_t num_predict_points, std::size_t num_features) const final { this->run_predict_kernel_impl(range, params, out_d, alpha_d, point_d, data_d, data_last_d, num_support_vectors, num_predict_points, num_features); }
174  void run_predict_kernel(const ::plssvm::detail::execution_range &range, const ::plssvm::detail::parameter<double> &params, device_ptr_type<double> &out_d, const device_ptr_type<double> &alpha_d, const device_ptr_type<double> &point_d, const device_ptr_type<double> &data_d, const device_ptr_type<double> &data_last_d, std::size_t num_support_vectors, std::size_t num_predict_points, std::size_t num_features) const final { this->run_predict_kernel_impl(range, params, out_d, alpha_d, point_d, data_d, data_last_d, num_support_vectors, num_predict_points, num_features); }
178  template <typename real_type>
179  void run_predict_kernel_impl(const ::plssvm::detail::execution_range &range, const ::plssvm::detail::parameter<real_type> &params, device_ptr_type<real_type> &out_d, const device_ptr_type<real_type> &alpha_d, const device_ptr_type<real_type> &point_d, const device_ptr_type<real_type> &data_d, const device_ptr_type<real_type> &data_last_d, std::size_t num_support_vectors, std::size_t num_predict_points, std::size_t num_features) const;
180 
182  std::vector<detail::context> contexts_{};
183 
184  private:
192  void init(target_platform target);
193 };
194 
195 } // namespace opencl
196 
197 namespace detail {
198 
202 template <>
203 struct csvm_backend_exists<opencl::csvm> : std::true_type {};
204 
205 } // namespace detail
206 
207 } // namespace plssvm
208 
209 #endif // PLSSVM_BACKENDS_OPENCL_CSVM_HPP_
Small wrapper around a OpenCL device pointer.
Base class for all C-SVM backends.
Definition: csvm.hpp:50
A C-SVM implementation for all GPU backends to reduce code duplication.
Definition: gpu_csvm.hpp:46
std::vector< queue_type > devices_
The available/used backend devices.
Definition: gpu_csvm.hpp:280
detail::command_queue queue_type
The type of the device queue (dependent on the used backend).
Definition: gpu_csvm.hpp:52
detail::device_ptr< real_type > device_ptr_type
The type of the device pointer (dependent on the used backend).
Definition: gpu_csvm.hpp:50
A C-SVM implementation using OpenCL as backend.
Definition: csvm.hpp:42
std::vector< detail::context > contexts_
The available OpenCL contexts for the current target platform with the associated devices.
Definition: csvm.hpp:182
csvm(const target_platform target, Args &&...named_args)
Construct a new C-SVM using the OpenCL backend on the target platform and the optionally provided nam...
Definition: csvm.hpp:95
csvm(target_platform target, parameter params={})
Construct a new C-SVM using the OpenCL backend on the target platform with the parameters given throu...
void run_predict_kernel_impl(const ::plssvm::detail::execution_range &range, const ::plssvm::detail::parameter< real_type > &params, device_ptr_type< real_type > &out_d, const device_ptr_type< real_type > &alpha_d, const device_ptr_type< real_type > &point_d, const device_ptr_type< real_type > &data_d, const device_ptr_type< real_type > &data_last_d, std::size_t num_support_vectors, std::size_t num_predict_points, std::size_t num_features) const
Run the device kernel (only on the first device) to predict the new data points point_d.
void run_q_kernel(std::size_t device, const ::plssvm::detail::execution_range &range, const ::plssvm::detail::parameter< double > &params, device_ptr_type< double > &q_d, const device_ptr_type< double > &data_d, const device_ptr_type< double > &data_last_d, std::size_t num_data_points_padded, std::size_t num_features) const final
Run the device kernel filling the q vector.
Definition: csvm.hpp:135
void run_q_kernel(std::size_t device, const ::plssvm::detail::execution_range &range, const ::plssvm::detail::parameter< float > &params, device_ptr_type< float > &q_d, const device_ptr_type< float > &data_d, const device_ptr_type< float > &data_last_d, std::size_t num_data_points_padded, std::size_t num_features) const final
Run the device kernel filling the q vector.
Definition: csvm.hpp:131
void run_svm_kernel_impl(std::size_t device, const ::plssvm::detail::execution_range &range, const ::plssvm::detail::parameter< real_type > &params, const device_ptr_type< real_type > &q_d, device_ptr_type< real_type > &r_d, const device_ptr_type< real_type > &x_d, const device_ptr_type< real_type > &data_d, real_type QA_cost, real_type add, std::size_t num_data_points_padded, std::size_t num_features) const
Run the main device kernel used in the CG algorithm.
void run_w_kernel_impl(std::size_t device, const ::plssvm::detail::execution_range &range, device_ptr_type< real_type > &w_d, const device_ptr_type< real_type > &alpha_d, const device_ptr_type< real_type > &data_d, const device_ptr_type< real_type > &data_last_d, std::size_t num_data_points, std::size_t num_features) const
Run the device kernel the calculate the w vector used to speed up the prediction when using the linea...
csvm(csvm &&) noexcept=default
Default move-constructor since a virtual destructor has been declared. noexcept
void run_w_kernel(std::size_t device, const ::plssvm::detail::execution_range &range, device_ptr_type< double > &w_d, const device_ptr_type< double > &alpha_d, const device_ptr_type< double > &data_d, const device_ptr_type< double > &data_last_d, std::size_t num_data_points, std::size_t num_features) const final
Run the device kernel the calculate the w vector used to speed up the prediction when using the linea...
Definition: csvm.hpp:161
csvm(Args &&...named_args)
Construct a new C-SVM using the OpenCL backend and the optionally provided named_args.
Definition: csvm.hpp:83
void device_synchronize(const queue_type &queue) const final
Synchronize the device denoted by queue.
void run_q_kernel_impl(std::size_t device, const ::plssvm::detail::execution_range &range, const ::plssvm::detail::parameter< real_type > &params, device_ptr_type< real_type > &q_d, const device_ptr_type< real_type > &data_d, const device_ptr_type< real_type > &data_last_d, std::size_t num_data_points_padded, std::size_t num_features) const
Run the device kernel filling the q vector.
csvm(const csvm &)=delete
Delete copy-constructor since a CSVM is a move-only type.
void run_predict_kernel(const ::plssvm::detail::execution_range &range, const ::plssvm::detail::parameter< float > &params, device_ptr_type< float > &out_d, const device_ptr_type< float > &alpha_d, const device_ptr_type< float > &point_d, const device_ptr_type< float > &data_d, const device_ptr_type< float > &data_last_d, std::size_t num_support_vectors, std::size_t num_predict_points, std::size_t num_features) const final
Run the device kernel (only on the first device) to predict the new data points point_d.
Definition: csvm.hpp:170
void run_predict_kernel(const ::plssvm::detail::execution_range &range, const ::plssvm::detail::parameter< double > &params, device_ptr_type< double > &out_d, const device_ptr_type< double > &alpha_d, const device_ptr_type< double > &point_d, const device_ptr_type< double > &data_d, const device_ptr_type< double > &data_last_d, std::size_t num_support_vectors, std::size_t num_predict_points, std::size_t num_features) const final
Run the device kernel (only on the first device) to predict the new data points point_d.
Definition: csvm.hpp:174
void init(target_platform target)
Initialize all important states related to the OpenCL backend.
void run_svm_kernel(std::size_t device, const ::plssvm::detail::execution_range &range, const ::plssvm::detail::parameter< float > &params, const device_ptr_type< float > &q_d, device_ptr_type< float > &r_d, const device_ptr_type< float > &x_d, const device_ptr_type< float > &data_d, float QA_cost, float add, std::size_t num_data_points_padded, std::size_t num_features) const final
Run the main device kernel used in the CG algorithm.
Definition: csvm.hpp:144
void run_svm_kernel(std::size_t device, const ::plssvm::detail::execution_range &range, const ::plssvm::detail::parameter< double > &params, const device_ptr_type< double > &q_d, device_ptr_type< double > &r_d, const device_ptr_type< double > &x_d, const device_ptr_type< double > &data_d, double QA_cost, double add, std::size_t num_data_points_padded, std::size_t num_features) const final
Run the main device kernel used in the CG algorithm.
Definition: csvm.hpp:148
void run_w_kernel(std::size_t device, const ::plssvm::detail::execution_range &range, device_ptr_type< float > &w_d, const device_ptr_type< float > &alpha_d, const device_ptr_type< float > &data_d, const device_ptr_type< float > &data_last_d, std::size_t num_data_points, std::size_t num_features) const final
Run the device kernel the calculate the w vector used to speed up the prediction when using the linea...
Definition: csvm.hpp:157
csvm(parameter params={})
Construct a new C-SVM using the OpenCL backend with the parameters given through params.
Defines a very small RAII wrapper around a cl_command_queue including information about its associate...
Defines a very small RAII wrapper around a cl_context including all associated devices and one comman...
Defines the base class for all C-SVM backends using a GPU. Used for code duplication reduction.
The main namespace containing all public API functions.
Definition: backend_types.hpp:24
target_platform
Enum class for all possible targets.
Definition: target_platforms.hpp:25
Implements the parameter class encapsulating all important C-SVM parameters.
Sets the value of the value member to true if T is a C-SVM using an available backend....
Definition: csvm.hpp:410
Defines an enumeration holding all possible target platforms. Can also include targets not available ...
#define PLSSVM_REQUIRES(...)
A shorthand macro for the std::enable_if_t type trait.
Definition: type_traits.hpp:33