spla
cl_v_map.hpp
Go to the documentation of this file.
1 /**********************************************************************************/
2 /* This file is part of spla project */
3 /* https://github.com/JetBrains-Research/spla */
4 /**********************************************************************************/
5 /* MIT License */
6 /* */
7 /* Copyright (c) 2023 SparseLinearAlgebra */
8 /* */
9 /* Permission is hereby granted, free of charge, to any person obtaining a copy */
10 /* of this software and associated documentation files (the "Software"), to deal */
11 /* in the Software without restriction, including without limitation the rights */
12 /* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
13 /* copies of the Software, and to permit persons to whom the Software is */
14 /* furnished to do so, subject to the following conditions: */
15 /* */
16 /* The above copyright notice and this permission notice shall be included in all */
17 /* copies or substantial portions of the Software. */
18 /* */
19 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
20 /* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
21 /* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
22 /* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
23 /* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
24 /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
25 /* SOFTWARE. */
26 /**********************************************************************************/
27 
28 #ifndef SPLA_CL_V_MAP_HPP
29 #define SPLA_CL_V_MAP_HPP
30 
32 
33 #include <core/dispatcher.hpp>
34 #include <core/registry.hpp>
35 #include <core/top.hpp>
36 #include <core/tscalar.hpp>
37 #include <core/ttype.hpp>
38 #include <core/tvector.hpp>
39 
40 #include <opencl/cl_formats.hpp>
41 #include <opencl/cl_map.hpp>
42 
43 #include <sstream>
44 
45 namespace spla {
46 
47  template<typename T>
48  class Algo_v_map_cl final : public RegistryAlgo {
49  public:
50  ~Algo_v_map_cl() override = default;
51 
52  std::string get_name() override {
53  return "v_map";
54  }
55 
56  std::string get_description() override {
57  return "parallel vector map on opencl device";
58  }
59 
60  Status execute(const DispatchContext& ctx) override {
61  auto t = ctx.task.template cast_safe<ScheduleTask_v_map>();
62  ref_ptr<TVector<T>> v = t->v.template cast_safe<TVector<T>>();
63 
64  if (v->is_valid(FormatVector::AccCoo)) {
65  return execute_sp(ctx);
66  }
67  if (v->is_valid(FormatVector::AccDense)) {
68  return execute_dn(ctx);
69  }
70  if (v->is_valid(FormatVector::CpuCoo)) {
71  return execute_sp(ctx);
72  }
73  if (v->is_valid(FormatVector::CpuDense)) {
74  return execute_dn(ctx);
75  }
76 
77  return execute_sp(ctx);
78  }
79 
80  private:
81  Status execute_dn(const DispatchContext& ctx) {
82  TIME_PROFILE_SCOPE("opencl/vector_map_dense");
83 
84  auto t = ctx.task.template cast_safe<ScheduleTask_v_map>();
85 
86  auto r = t->r.template cast_safe<TVector<T>>();
87  auto v = t->v.template cast_safe<TVector<T>>();
88  auto op = t->op.template cast_safe<TOpUnary<T, T>>();
89 
90  r->validate_wd(FormatVector::AccDense);
91  v->validate_rw(FormatVector::AccDense);
92 
93  auto* p_cl_dense_r = r->template get<CLDenseVec<T>>();
94  const auto* p_cl_dense_v = v->template get<CLDenseVec<T>>();
95  auto* p_cl_acc = get_acc_cl();
96  auto& queue = p_cl_acc->get_queue_default();
97 
98  cl_map(queue, p_cl_dense_v->Ax, p_cl_dense_r->Ax, r->get_n_rows(), op);
99 
100  return Status::Ok;
101  }
102 
103  Status execute_sp(const DispatchContext& ctx) {
104  TIME_PROFILE_SCOPE("opencl/vector_map_sparse");
105 
106  auto t = ctx.task.template cast_safe<ScheduleTask_v_map>();
107 
108  auto r = t->r.template cast_safe<TVector<T>>();
109  auto v = t->v.template cast_safe<TVector<T>>();
110  auto op = t->op.template cast_safe<TOpUnary<T, T>>();
111 
112  r->validate_wd(FormatVector::AccCoo);
113  v->validate_rw(FormatVector::AccCoo);
114 
115  auto* p_cl_coo_r = r->template get<CLCooVec<T>>();
116  const auto* p_cl_coo_v = v->template get<CLCooVec<T>>();
117  auto* p_cl_acc = get_acc_cl();
118  auto& queue = p_cl_acc->get_queue_default();
119 
120  const uint N = p_cl_coo_v->values;
121 
122  cl_coo_vec_resize(N, *p_cl_coo_r);
123 
124  queue.enqueueCopyBuffer(p_cl_coo_v->Ai, p_cl_coo_r->Ai, 0, 0, sizeof(uint) * N);
125  cl_map(queue, p_cl_coo_v->Ax, p_cl_coo_r->Ax, N, op);
126 
127  return Status::Ok;
128  }
129  };
130 
131 }// namespace spla
132 
133 #endif//SPLA_CL_V_MAP_HPP
Status of library operation execution.
Definition: cl_v_map.hpp:48
~Algo_v_map_cl() override=default
Status execute(const DispatchContext &ctx) override
Definition: cl_v_map.hpp:60
std::string get_name() override
Definition: cl_v_map.hpp:52
std::string get_description() override
Definition: cl_v_map.hpp:56
Algorithm suitable to process schedule task based on task string key.
Definition: registry.hpp:66
Automates reference counting and behaves as shared smart pointer.
Definition: ref.hpp:117
void cl_coo_vec_resize(const std::size_t n_values, CLCooVec< T > &storage)
Definition: cl_format_coo_vec.hpp:72
std::uint32_t uint
Library index and size type.
Definition: config.hpp:56
Definition: algorithm.hpp:37
void cl_map(cl::CommandQueue &queue, const cl::Buffer &source, cl::Buffer &dest, uint n, const ref_ptr< TOpUnary< T, T >> &op)
Definition: cl_map.hpp:38
Execution context of a single task.
Definition: dispatcher.hpp:46
ref_ptr< ScheduleTask > task
Definition: dispatcher.hpp:48
#define TIME_PROFILE_SCOPE(name)
Definition: time_profiler.hpp:92