VisionCpp  0.0.1
sycl_device.hpp
Go to the documentation of this file.
1 // This file is part of VisionCpp, a lightweight C++ template library
2 // for computer vision and image processing.
3 //
4 // Copyright (C) 2016 Codeplay Software Limited. All Rights Reserved.
5 //
6 // Contact: visioncpp@codeplay.com
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 
20 /// \file sycl_device.hpp
21 /// \brief This fill contains features related to sycl devices.
22 
23 #ifndef VISIONCPP_INCLUDE_FRAMEWORK_DEVICE_SYCL_DEVICE_HPP_
24 #define VISIONCPP_INCLUDE_FRAMEWORK_DEVICE_SYCL_DEVICE_HPP_
25 
26 namespace visioncpp {
27 namespace internal {
28 
29 /// \brief specialisation Device_ for sycl
30 /// \tparam device type supported by sycl
31 template <device dv>
32 class Device_<backend::sycl, dv> {
33  using QueueType = cl::sycl::queue;
34  using DevType = typename DeviceSelector<dv>::Type;
35 
36  private:
37  mutable QueueType dev;
38 
39  public:
41  : dev(QueueType(DevType(), [=](cl::sycl::exception_list l) {
42  for (const auto &e : l) {
43  try {
44  std::rethrow_exception(e);
45  } catch (cl::sycl::exception e) {
46  std::cout << e.what() << std::endl;
47  }
48  }
49  })) {}
50  template <size_t LC, size_t LR, size_t CGT, size_t RGT, size_t CLT,
51  size_t RLT, typename Expr>
52  void execute(Expr &expr) const {
53  /// generating the short class name for the AMD gpu
54  constexpr size_t TotalLeaves = LeafCount<Expr::ND_Category, Expr>::Count;
55  /// replacing the the leaf node in the expression tree with a placeholder
56  /// number
57  using placeHolderExprType =
58  typename MakePlaceHolderExprHelper<Expr::ND_Category, Expr,
59  TotalLeaves - 1>::Type;
60 
61  /// submitting the lambda expression to the sycl queue.
62  dev.submit([&](cl::sycl::handler &cgh) {
63 
64  /// creating global accessors on all input output buffers
65  auto global_accessor_tuple = extract_accessors(cgh, expr);
66  /// create the tuple of local output accessor
67  auto device_only_accessor_tuple =
68  create_local_accessors<LC, LR, Expr>(cgh);
69  /// starting point of local tuples
70  constexpr size_t Output_offset =
71  tools::tuple::size(global_accessor_tuple);
72  /// merge it with all the other existing tuples
73  auto device_tuple = tools::tuple::append(global_accessor_tuple,
74  device_only_accessor_tuple);
75  /// submitting the kernel lambda to the parallel
76 
77  cgh.parallel_for<Expr>(
78  cl::sycl::nd_range<Expr::Type::Dim>(
79  visioncpp::internal::get_range<Expr::Type::Dim>(RGT, CGT),
80  visioncpp::internal::get_range<Expr::Type::Dim>(RLT, CLT)),
81  [=](cl::sycl::nd_item<Expr::Type::Dim> itemID) {
82  /// creating the index access for each thread
83  auto cOffset = visioncpp::internal::memLocation<LC, LR>(itemID);
84 
85  /// creating the eval expression for evaluating the expression
86  /// tree. The output now moved to the front so the Output_offset
87  /// should be reduced by one.
88  eval<Output_offset, LC, LR, placeHolderExprType>(cOffset,
89  device_tuple);
90  });
91  });
92  dev.throw_asynchronous();
93  }
94 };
95 } // namespace internal
96 } // namespace visioncpp
97 #endif // VISIONCPP_INCLUDE_FRAMEWORK_DEVICE_SYCL_DEVICE_HPP_
enum class that defines supported backends.
class used to implement the execution of the expression tree.
static constexpr size_t size(Tuple< Args... > &)
size
Definition: tuple.hpp:163
Tuple< Args..., T > append(Tuple< Args... > t, T a)
append
Definition: tuple.hpp:235
VisionCpp namespace.
Definition: sycl/device.hpp:24
auto extract_accessors(cl::sycl::handler &cgh, Expr e) -> decltype(internal::ExtractAccessor< Expr::ND_Category, Expr >::getTuple(cgh, e))
@ sycl
represents sycl backend.
this class is used to define different types of device for sycl
Definition: sycl/device.hpp:29
is used to count the total number of leafNode in the expression tree.
it is used to create the PlaceHolder expression.