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_SYCL_HIPSYCL_CSVM_HPP_
13 #define PLSSVM_BACKENDS_SYCL_HIPSYCL_CSVM_HPP_
14 #pragma once
15 
16 #include "plssvm/backends/SYCL/hipSYCL/detail/device_ptr.hpp" // plssvm::hipsycl::detail::device_ptr
17 #include "plssvm/backends/SYCL/hipSYCL/detail/queue.hpp" // plssvm::hipsycl::detail::queue (PImpl)
18 
19 #include "plssvm/backends/SYCL/kernel_invocation_type.hpp" // plssvm::sycl::kernel_invocation_type
20 #include "plssvm/backends/gpu_csvm.hpp" // plssvm::detail::gpu_csvm
21 #include "plssvm/detail/type_traits.hpp" // PLSSVM_REQUIRES, plssvm::detail::remove_cvref_t
22 #include "plssvm/parameter.hpp" // plssvm::parameter, plssvm::detail::parameter
23 #include "plssvm/target_platforms.hpp" // plssvm::target_platform
24 
25 #include "igor/igor.hpp" // igor::parser
26 
27 #include <cstddef> // std::size_t
28 #include <type_traits> // std::is_same_v, std::true_type
29 #include <utility> // std::forward
30 
31 namespace plssvm {
32 
33 namespace detail {
34 
35 // forward declare execution_range class
36 class execution_range;
37 
38 } // namespace detail
39 
40 namespace hipsycl {
41 
45 class csvm : public ::plssvm::detail::gpu_csvm<detail::device_ptr, detail::queue> {
46  protected:
47  // protected for the test MOCK class
50 
51  using base_type::devices_;
52 
53  public:
55  using typename base_type::queue_type;
56 
64  explicit csvm(parameter params = {});
73  explicit csvm(target_platform target, parameter params = {});
74 
83  template <typename... Args, PLSSVM_REQUIRES(::plssvm::detail::has_only_sycl_parameter_named_args_v<Args...>)>
84  explicit csvm(Args &&...named_args) :
85  csvm{ plssvm::target_platform::automatic, std::forward<Args>(named_args)... } {}
95  template <typename... Args, PLSSVM_REQUIRES(::plssvm::detail::has_only_sycl_parameter_named_args_v<Args...>)>
96  explicit csvm(const target_platform target, Args &&...named_args) :
97  base_type{ named_args... } {
98  // check igor parameter
99  igor::parser parser{ std::forward<Args>(named_args)... };
100 
101  // check whether a specific SYCL kernel invocation type has been requested
102  if constexpr (parser.has(sycl_kernel_invocation_type)) {
103  // compile time check: the value must have the correct type
104  static_assert(std::is_same_v<::plssvm::detail::remove_cvref_t<decltype(parser(sycl_kernel_invocation_type))>, sycl::kernel_invocation_type>, "Provided sycl_kernel_invocation_type must be convertible to a plssvm::sycl::kernel_invocation_type!");
105  invocation_type_ = static_cast<sycl::kernel_invocation_type>(parser(sycl_kernel_invocation_type));
106  }
107  this->init(target);
108  }
109 
113  csvm(const csvm &) = delete;
117  csvm(csvm &&) noexcept = default;
121  csvm &operator=(const csvm &) = delete;
125  csvm &operator=(csvm &&) noexcept = default;
130  ~csvm() override;
131 
136  [[nodiscard]] sycl::kernel_invocation_type get_kernel_invocation_type() const noexcept { return invocation_type_; }
137 
138  protected:
142  void device_synchronize(const queue_type &queue) const final;
143 
147  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); }
151  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); }
155  template <typename real_type>
156  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;
160  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); }
164  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); }
168  template <typename real_type>
169  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;
173  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); }
177  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); }
181  template <typename real_type>
182  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;
186  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); }
190  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); }
194  template <typename real_type>
195  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;
196 
197  private:
204  void init(target_platform target);
205 
208 };
209 } // namespace hipsycl
210 
211 namespace detail {
212 
216 template <>
217 struct csvm_backend_exists<hipsycl::csvm> : std::true_type {};
218 
219 } // namespace detail
220 
221 } // namespace plssvm
222 
223 #endif // PLSSVM_BACKENDS_SYCL_HIPSYCL_CSVM_HPP_
Small wrapper around a SYCL device pointer for the hipSYCL SYCL implementation.
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::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 DPC++ as SYCL backend.
Definition: csvm.hpp:45
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.
sycl::kernel_invocation_type get_kernel_invocation_type() const noexcept
Return the kernel invocation type (nd_range or the SYCL specific hierarchical kernel) used in this SY...
Definition: csvm.hpp:134
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_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 init(target_platform target)
Initialize all important states related to the SYCL backend.
sycl::kernel_invocation_type invocation_type_
The SYCL kernel invocation type for the svm kernel. Either nd_range or hierarchical.
Definition: csvm.hpp:205
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...
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:164
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< 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:147
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:186
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.
void device_synchronize(const queue_type &queue) const final
Synchronize the device denoted by queue.
csvm(const csvm &)=delete
Delete copy-constructor since a CSVM is a move-only type.
void init(target_platform target)
Initialize all important states related to the SYCL backend.
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:190
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:173
csvm(target_platform target, parameter params={})
Construct a new C-SVM using the SYCL backend on the target platform with the parameters given through...
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(Args &&...named_args)
Construct a new C-SVM using the SYCL backend and the optionally provided named_args.
Definition: csvm.hpp:84
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:177
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:151
csvm(parameter params={})
Construct a new C-SVM using the SYCL backend with the parameters given through params.
csvm(csvm &&) noexcept=default
Default move-constructor since a virtual destructor has been declared. noexcept
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_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:160
csvm(const target_platform target, Args &&...named_args)
Construct a new C-SVM using the SYCL backend on the target platform and the optionally provided named...
Definition: csvm.hpp:96
Defines the base class for all C-SVM backends using a GPU. Used for code duplication reduction.
PImpl class used to hide the "sycl/sycl.hpp" header from the public interface.
Defines an enumeration holding all possible SYCL kernel invocation types.
std::remove_cv_t< std::remove_reference_t< T > > remove_cvref_t
Remove the topmost reference- and cv-qualifiers.
Definition: type_traits.hpp:46
kernel_invocation_type
Enum class for all possible SYCL kernel invocation types.
Definition: kernel_invocation_type.hpp:23
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 ...
Defines some generic type traits used in the PLSSVM library.
#define PLSSVM_REQUIRES(...)
A shorthand macro for the std::enable_if_t type trait.
Definition: type_traits.hpp:33