My Project
openclKernels.hpp
1 /*
2  Copyright 2020 Equinor ASA
3 
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef OPENCL_HPP
21 #define OPENCL_HPP
22 
23 #include <string>
24 #include <memory>
25 #include <cstddef>
26 
27 #include <opm/simulators/linalg/bda/opencl/opencl.hpp>
28 
29 namespace Opm
30 {
31 namespace Accelerator
32 {
33 
34 using spmv_blocked_kernel_type = cl::KernelFunctor<cl::Buffer&, cl::Buffer&, cl::Buffer&, const unsigned int,
35  const cl::Buffer&, cl::Buffer&, const unsigned int, cl::LocalSpaceArg>;
36 using spmv_kernel_type = cl::KernelFunctor<cl::Buffer&, cl::Buffer&, cl::Buffer&, const unsigned int,
37  const cl::Buffer&, cl::Buffer&, cl::LocalSpaceArg>;
38 using residual_blocked_kernel_type = cl::KernelFunctor<cl::Buffer&, cl::Buffer&, cl::Buffer&, const unsigned int,
39  cl::Buffer&, const cl::Buffer&, cl::Buffer&, const unsigned int, cl::LocalSpaceArg>;
40 using residual_kernel_type = cl::KernelFunctor<cl::Buffer&, cl::Buffer&, cl::Buffer&, const unsigned int,
41  cl::Buffer&, const cl::Buffer&, cl::Buffer&, cl::LocalSpaceArg>;
42 using ilu_apply1_kernel_type = cl::KernelFunctor<cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&, const cl::Buffer&,
43  cl::Buffer&, cl::Buffer&, const unsigned int, const unsigned int, cl::LocalSpaceArg>;
44 using ilu_apply2_kernel_type = cl::KernelFunctor<cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&,
45  cl::Buffer&, cl::Buffer&, const unsigned int, const unsigned int, cl::LocalSpaceArg>;
46 using stdwell_apply_kernel_type = cl::KernelFunctor<cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&,
47  cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&,
48  const unsigned int, const unsigned int, cl::Buffer&,
49  cl::LocalSpaceArg, cl::LocalSpaceArg, cl::LocalSpaceArg>;
50 using stdwell_apply_no_reorder_kernel_type = cl::KernelFunctor<cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&,
51  cl::Buffer&, cl::Buffer&, cl::Buffer&,
52  const unsigned int, const unsigned int, cl::Buffer&,
53  cl::LocalSpaceArg, cl::LocalSpaceArg, cl::LocalSpaceArg>;
54 using ilu_decomp_kernel_type = cl::KernelFunctor<const unsigned int, const unsigned int, cl::Buffer&, cl::Buffer&,
55  cl::Buffer&, cl::Buffer&, cl::Buffer&, const int, cl::LocalSpaceArg>;
56 using isaiL_kernel_type = cl::KernelFunctor<cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&,
57  cl::Buffer&, cl::Buffer&, cl::Buffer&, const unsigned int>;
58 using isaiU_kernel_type = cl::KernelFunctor<cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&,
59  cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&, const unsigned int>;
60 
62 {
63 private:
64  static int verbosity;
65  static cl::CommandQueue *queue;
66  static std::vector<double> tmp; // used as tmp CPU buffer for dot() and norm()
67  static bool initialized;
68  static std::size_t preferred_workgroup_size_multiple; // stores CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE
69 
70  static std::unique_ptr<cl::KernelFunctor<cl::Buffer&, cl::Buffer&, cl::Buffer&, const unsigned int, cl::LocalSpaceArg> > dot_k;
71  static std::unique_ptr<cl::KernelFunctor<cl::Buffer&, cl::Buffer&, const unsigned int, cl::LocalSpaceArg> > norm_k;
72  static std::unique_ptr<cl::KernelFunctor<cl::Buffer&, const double, cl::Buffer&, const unsigned int> > axpy_k;
73  static std::unique_ptr<cl::KernelFunctor<cl::Buffer&, const double, const unsigned int> > scale_k;
74  static std::unique_ptr<cl::KernelFunctor<const double, cl::Buffer&, cl::Buffer&, cl::Buffer&, const unsigned int> > vmul_k;
75  static std::unique_ptr<cl::KernelFunctor<cl::Buffer&, cl::Buffer&, cl::Buffer&, const double, const double, const unsigned int> > custom_k;
76  static std::unique_ptr<cl::KernelFunctor<const cl::Buffer&, cl::Buffer&, cl::Buffer&, const unsigned int> > full_to_pressure_restriction_k;
77  static std::unique_ptr<cl::KernelFunctor<cl::Buffer&, cl::Buffer&, const unsigned int, const unsigned int> > add_coarse_pressure_correction_k;
78  static std::unique_ptr<cl::KernelFunctor<const cl::Buffer&, cl::Buffer&, const cl::Buffer&, const unsigned int> > prolongate_vector_k;
79  static std::unique_ptr<spmv_blocked_kernel_type> spmv_blocked_k;
80  static std::unique_ptr<spmv_blocked_kernel_type> spmv_blocked_add_k;
81  static std::unique_ptr<spmv_kernel_type> spmv_k;
82  static std::unique_ptr<spmv_kernel_type> spmv_noreset_k;
83  static std::unique_ptr<residual_blocked_kernel_type> residual_blocked_k;
84  static std::unique_ptr<residual_kernel_type> residual_k;
85  static std::unique_ptr<ilu_apply1_kernel_type> ILU_apply1_k;
86  static std::unique_ptr<ilu_apply2_kernel_type> ILU_apply2_k;
87  static std::unique_ptr<stdwell_apply_kernel_type> stdwell_apply_k;
88  static std::unique_ptr<stdwell_apply_no_reorder_kernel_type> stdwell_apply_no_reorder_k;
89  static std::unique_ptr<ilu_decomp_kernel_type> ilu_decomp_k;
90  static std::unique_ptr<isaiL_kernel_type> isaiL_k;
91  static std::unique_ptr<isaiU_kernel_type> isaiU_k;
92 
93  OpenclKernels(){}; // disable instantiation
94 
95 public:
96  static const std::string axpy_str;
97  static const std::string scale_str;
98  static const std::string vmul_str;
99  static const std::string dot_1_str;
100  static const std::string norm_str;
101  static const std::string custom_str;
102  static const std::string full_to_pressure_restriction_str;
103  static const std::string add_coarse_pressure_correction_str;
104  static const std::string prolongate_vector_str;
105  static const std::string spmv_blocked_str;
106  static const std::string spmv_blocked_add_str;
107  static const std::string spmv_str;
108  static const std::string spmv_noreset_str;
109  static const std::string residual_blocked_str;
110  static const std::string residual_str;
111 #if CHOW_PATEL
112  static const std::string ILU_apply1_str;
113  static const std::string ILU_apply2_str;
114 #else
115  static const std::string ILU_apply1_fm_str;
116  static const std::string ILU_apply2_fm_str;
117 #endif
118  static const std::string stdwell_apply_str;
119  static const std::string stdwell_apply_no_reorder_str;
120  static const std::string ILU_decomp_str;
121  static const std::string isaiL_str;
122  static const std::string isaiU_str;
123 
124  static void init(cl::Context *context, cl::CommandQueue *queue, std::vector<cl::Device>& devices, int verbosity);
125 
126  static double dot(cl::Buffer& in1, cl::Buffer& in2, cl::Buffer& out, int N);
127  static double norm(cl::Buffer& in, cl::Buffer& out, int N);
128  static void axpy(cl::Buffer& in, const double a, cl::Buffer& out, int N);
129  static void scale(cl::Buffer& in, const double a, int N);
130  static void vmul(const double alpha, cl::Buffer& in1, cl::Buffer& in2, cl::Buffer& out, int N);
131  static void custom(cl::Buffer& p, cl::Buffer& v, cl::Buffer& r, const double omega, const double beta, int N);
132  static void full_to_pressure_restriction(const cl::Buffer& fine_y, cl::Buffer& weights, cl::Buffer& coarse_y, int Nb);
133  static void add_coarse_pressure_correction(cl::Buffer& coarse_x, cl::Buffer& fine_x, int pressure_idx, int Nb);
134  static void prolongate_vector(const cl::Buffer& in, cl::Buffer& out, const cl::Buffer& cols, int N);
135  static void spmv(cl::Buffer& vals, cl::Buffer& cols, cl::Buffer& rows, const cl::Buffer& x, cl::Buffer& b, int Nb, unsigned int block_size, bool reset = true, bool add = false);
136  static void residual(cl::Buffer& vals, cl::Buffer& cols, cl::Buffer& rows, cl::Buffer& x, const cl::Buffer& rhs, cl::Buffer& out, int Nb, unsigned int block_size);
137 
138  static void ILU_apply1(cl::Buffer& vals, cl::Buffer& cols, cl::Buffer& rows, cl::Buffer& diagIndex,
139  const cl::Buffer& y, cl::Buffer& x, cl::Buffer& rowsPerColor, int color, int Nb, unsigned int block_size);
140 
141  static void ILU_apply2(cl::Buffer& vals, cl::Buffer& cols, cl::Buffer& rows, cl::Buffer& diagIndex,
142  cl::Buffer& invDiagVals, cl::Buffer& x, cl::Buffer& rowsPerColor, int color, int Nb, unsigned int block_size);
143 
144  static void ILU_decomp(int firstRow, int lastRow, cl::Buffer& vals, cl::Buffer& cols, cl::Buffer& rows,
145  cl::Buffer& diagIndex, cl::Buffer& invDiagVals, int Nb, unsigned int block_size);
146 
147  static void apply_stdwells_reorder(cl::Buffer& d_Cnnzs_ocl, cl::Buffer &d_Dnnzs_ocl, cl::Buffer &d_Bnnzs_ocl,
148  cl::Buffer &d_Ccols_ocl, cl::Buffer &d_Bcols_ocl, cl::Buffer &d_x, cl::Buffer &d_y,
149  cl::Buffer &d_toOrder, int dim, int dim_wells, cl::Buffer &d_val_pointers_ocl, int num_std_wells);
150 
151  static void apply_stdwells_no_reorder(cl::Buffer& d_Cnnzs_ocl, cl::Buffer &d_Dnnzs_ocl, cl::Buffer &d_Bnnzs_ocl,
152  cl::Buffer &d_Ccols_ocl, cl::Buffer &d_Bcols_ocl, cl::Buffer &d_x, cl::Buffer &d_y,
153  int dim, int dim_wells, cl::Buffer &d_val_pointers_ocl, int num_std_wells);
154 
155  static void isaiL(cl::Buffer& diagIndex, cl::Buffer& colPointers, cl::Buffer& mapping, cl::Buffer& nvc,
156  cl::Buffer& luIdxs, cl::Buffer& xxIdxs, cl::Buffer& dxIdxs, cl::Buffer& LUvals, cl::Buffer& invLvals, unsigned int Nb);
157 
158  static void isaiU(cl::Buffer& diagIndex, cl::Buffer& colPointers, cl::Buffer& rowIndices, cl::Buffer& mapping,
159  cl::Buffer& nvc, cl::Buffer& luIdxs, cl::Buffer& xxIdxs, cl::Buffer& dxIdxs, cl::Buffer& LUvals,
160  cl::Buffer& invDiagVals, cl::Buffer& invUvals, unsigned int Nb);
161 };
162 
163 } // namespace Accelerator
164 } // namespace Opm
165 
166 #endif
Definition: openclKernels.hpp:62
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: BlackoilPhases.hpp:27