spla
cl_format_csr.hpp
Go to the documentation of this file.
1 /**********************************************************************************/
2 /* This file is part of spla project */
3 /* https://github.com/SparseLinearAlgebra/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_FORMAT_CSR_HPP
29 #define SPLA_CL_FORMAT_CSR_HPP
30 
31 #include <opencl/cl_formats.hpp>
32 
33 namespace spla {
34 
40  template<typename T>
41  void cl_csr_init(std::size_t n_rows,
42  std::size_t n_values,
43  const uint* Ap,
44  const uint* Aj,
45  const T* Ax,
46  CLCsr<T>& storage) {
47  auto& ctx = get_acc_cl()->get_context();
48  const auto flags = CL_MEM_READ_WRITE | CL_MEM_HOST_NO_ACCESS | CL_MEM_COPY_HOST_PTR;
49 
50  cl::Buffer cl_Ap(ctx, flags, (n_rows + 1) * sizeof(uint), (void*) Ap);
51  cl::Buffer cl_Aj(ctx, flags, n_values * sizeof(uint), (void*) Aj);
52  cl::Buffer cl_Ax(ctx, flags, n_values * sizeof(T), (void*) Ax);
53 
54  storage.Ap = std::move(cl_Ap);
55  storage.Aj = std::move(cl_Aj);
56  storage.Ax = std::move(cl_Ax);
57 
58  storage.values = n_values;
59  }
60 
61  template<typename T>
62  void cl_csr_resize(std::size_t n_rows,
63  std::size_t n_values,
64  CLCsr<T>& storage) {
65  auto& ctx = get_acc_cl()->get_context();
66  const auto flags = CL_MEM_READ_WRITE | CL_MEM_HOST_NO_ACCESS;
67 
68  cl::Buffer cl_Ap(ctx, flags, (n_rows + 1) * sizeof(uint));
69  cl::Buffer cl_Aj(ctx, flags, n_values * sizeof(uint));
70  cl::Buffer cl_Ax(ctx, flags, n_values * sizeof(T));
71 
72  storage.Ap = std::move(cl_Ap);
73  storage.Aj = std::move(cl_Aj);
74  storage.Ax = std::move(cl_Ax);
75 
76  storage.values = n_values;
77  }
78 
79  template<typename T>
80  void cl_csr_read(std::size_t n_rows,
81  std::size_t n_values,
82  uint* Ap,
83  uint* Aj,
84  T* Ax,
85  CLCsr<T>& storage,
86  cl::CommandQueue& queue,
87  cl_mem_flags staging_flags = CL_MEM_READ_ONLY | CL_MEM_HOST_READ_ONLY | CL_MEM_ALLOC_HOST_PTR,
88  bool blocking = true) {
89  const std::size_t buffer_size_Ap = (n_rows + 1) * sizeof(uint);
90  const std::size_t buffer_size_Aj = n_values * sizeof(uint);
91  const std::size_t buffer_size_Ax = n_values * sizeof(T);
92 
93  cl::Buffer staging_Ap(get_acc_cl()->get_context(), staging_flags, buffer_size_Ap);
94  cl::Buffer staging_Aj(get_acc_cl()->get_context(), staging_flags, buffer_size_Aj);
95  cl::Buffer staging_Ax(get_acc_cl()->get_context(), staging_flags, buffer_size_Ax);
96 
97  queue.enqueueCopyBuffer(storage.Ap, staging_Ap, 0, 0, buffer_size_Ap);
98  queue.enqueueCopyBuffer(storage.Aj, staging_Aj, 0, 0, buffer_size_Aj);
99  queue.enqueueCopyBuffer(storage.Ax, staging_Ax, 0, 0, buffer_size_Ax);
100 
101  queue.enqueueReadBuffer(staging_Ap, false, 0, buffer_size_Ap, Ap);
102  queue.enqueueReadBuffer(staging_Aj, false, 0, buffer_size_Aj, Aj);
103  queue.enqueueReadBuffer(staging_Ax, blocking, 0, buffer_size_Ax, Ax);
104  }
105 
110 }// namespace spla
111 
112 #endif//SPLA_CL_FORMAT_CSR_HPP
OpenCL compressed sparse row matrix representation.
Definition: cl_formats.hpp:94
cl::Buffer Ax
Definition: cl_formats.hpp:102
cl::Buffer Ap
Definition: cl_formats.hpp:100
cl::Buffer Aj
Definition: cl_formats.hpp:101
uint values
Definition: tdecoration.hpp:58
void cl_csr_init(std::size_t n_rows, std::size_t n_values, const uint *Ap, const uint *Aj, const T *Ax, CLCsr< T > &storage)
Definition: cl_format_csr.hpp:41
void cl_csr_resize(std::size_t n_rows, std::size_t n_values, CLCsr< T > &storage)
Definition: cl_format_csr.hpp:62
void cl_csr_read(std::size_t n_rows, std::size_t n_values, uint *Ap, uint *Aj, T *Ax, CLCsr< T > &storage, cl::CommandQueue &queue, cl_mem_flags staging_flags=CL_MEM_READ_ONLY|CL_MEM_HOST_READ_ONLY|CL_MEM_ALLOC_HOST_PTR, bool blocking=true)
Definition: cl_format_csr.hpp:80
std::uint32_t uint
Library index and size type.
Definition: config.hpp:56
Definition: algorithm.hpp:37