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 
26 #include <opm/simulators/linalg/bda/opencl/opencl.hpp>
27 
28 namespace Opm
29 {
30 namespace Accelerator
31 {
32 
33 using spmv_blocked_kernel_type = cl::KernelFunctor<cl::Buffer&, cl::Buffer&, cl::Buffer&, const unsigned int,
34  const cl::Buffer&, cl::Buffer&, const unsigned int, cl::LocalSpaceArg>;
35 using spmv_kernel_type = cl::KernelFunctor<cl::Buffer&, cl::Buffer&, cl::Buffer&, const unsigned int,
36  const cl::Buffer&, cl::Buffer&, cl::LocalSpaceArg>;
37 using residual_blocked_kernel_type = cl::KernelFunctor<cl::Buffer&, cl::Buffer&, cl::Buffer&, const unsigned int,
38  cl::Buffer&, const cl::Buffer&, cl::Buffer&, const unsigned int, cl::LocalSpaceArg>;
39 using residual_kernel_type = cl::KernelFunctor<cl::Buffer&, cl::Buffer&, cl::Buffer&, const unsigned int,
40  cl::Buffer&, const cl::Buffer&, cl::Buffer&, cl::LocalSpaceArg>;
41 using ilu_apply1_kernel_type = cl::KernelFunctor<cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&, const cl::Buffer&,
42  cl::Buffer&, cl::Buffer&, const unsigned int, const unsigned int, cl::LocalSpaceArg>;
43 using ilu_apply2_kernel_type = cl::KernelFunctor<cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&,
44  cl::Buffer&, cl::Buffer&, const unsigned int, const unsigned int, cl::LocalSpaceArg>;
45 using stdwell_apply_kernel_type = cl::KernelFunctor<cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&,
46  cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&,
47  const unsigned int, const unsigned int, cl::Buffer&,
48  cl::LocalSpaceArg, cl::LocalSpaceArg, cl::LocalSpaceArg>;
49 using stdwell_apply_no_reorder_kernel_type = cl::KernelFunctor<cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&,
50  cl::Buffer&, cl::Buffer&, cl::Buffer&,
51  const unsigned int, const unsigned int, cl::Buffer&,
52  cl::LocalSpaceArg, cl::LocalSpaceArg, cl::LocalSpaceArg>;
53 using ilu_decomp_kernel_type = cl::KernelFunctor<const unsigned int, const unsigned int, cl::Buffer&, cl::Buffer&,
54  cl::Buffer&, cl::Buffer&, cl::Buffer&, const int, cl::LocalSpaceArg>;
55 using isaiL_kernel_type = cl::KernelFunctor<cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&,
56  cl::Buffer&, cl::Buffer&, cl::Buffer&, const unsigned int>;
57 using isaiU_kernel_type = cl::KernelFunctor<cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&,
58  cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&, cl::Buffer&, const unsigned int>;
59 
61 {
62 private:
63  static int verbosity;
64  static cl::CommandQueue *queue;
65  static std::vector<double> tmp; // used as tmp CPU buffer for dot() and norm()
66  static bool initialized;
67 
68  static std::unique_ptr<cl::KernelFunctor<cl::Buffer&, cl::Buffer&, cl::Buffer&, const unsigned int, cl::LocalSpaceArg> > dot_k;
69  static std::unique_ptr<cl::KernelFunctor<cl::Buffer&, cl::Buffer&, const unsigned int, cl::LocalSpaceArg> > norm_k;
70  static std::unique_ptr<cl::KernelFunctor<cl::Buffer&, const double, cl::Buffer&, const unsigned int> > axpy_k;
71  static std::unique_ptr<cl::KernelFunctor<cl::Buffer&, const double, const unsigned int> > scale_k;
72  static std::unique_ptr<cl::KernelFunctor<const double, cl::Buffer&, cl::Buffer&, cl::Buffer&, const unsigned int> > vmul_k;
73  static std::unique_ptr<cl::KernelFunctor<cl::Buffer&, cl::Buffer&, cl::Buffer&, const double, const double, const unsigned int> > custom_k;
74  static std::unique_ptr<cl::KernelFunctor<const cl::Buffer&, cl::Buffer&, cl::Buffer&, const unsigned int> > full_to_pressure_restriction_k;
75  static std::unique_ptr<cl::KernelFunctor<cl::Buffer&, cl::Buffer&, const unsigned int, const unsigned int> > add_coarse_pressure_correction_k;
76  static std::unique_ptr<cl::KernelFunctor<const cl::Buffer&, cl::Buffer&, const cl::Buffer&, const unsigned int> > prolongate_vector_k;
77  static std::unique_ptr<spmv_blocked_kernel_type> spmv_blocked_k;
78  static std::unique_ptr<spmv_blocked_kernel_type> spmv_blocked_add_k;
79  static std::unique_ptr<spmv_kernel_type> spmv_k;
80  static std::unique_ptr<spmv_kernel_type> spmv_noreset_k;
81  static std::unique_ptr<residual_blocked_kernel_type> residual_blocked_k;
82  static std::unique_ptr<residual_kernel_type> residual_k;
83  static std::unique_ptr<ilu_apply1_kernel_type> ILU_apply1_k;
84  static std::unique_ptr<ilu_apply2_kernel_type> ILU_apply2_k;
85  static std::unique_ptr<stdwell_apply_kernel_type> stdwell_apply_k;
86  static std::unique_ptr<stdwell_apply_no_reorder_kernel_type> stdwell_apply_no_reorder_k;
87  static std::unique_ptr<ilu_decomp_kernel_type> ilu_decomp_k;
88  static std::unique_ptr<isaiL_kernel_type> isaiL_k;
89  static std::unique_ptr<isaiU_kernel_type> isaiU_k;
90 
91  OpenclKernels(){}; // disable instantiation
92 
93 public:
94  static const std::string axpy_str;
95  static const std::string scale_str;
96  static const std::string vmul_str;
97  static const std::string dot_1_str;
98  static const std::string norm_str;
99  static const std::string custom_str;
100  static const std::string full_to_pressure_restriction_str;
101  static const std::string add_coarse_pressure_correction_str;
102  static const std::string prolongate_vector_str;
103  static const std::string spmv_blocked_str;
104  static const std::string spmv_blocked_add_str;
105  static const std::string spmv_str;
106  static const std::string spmv_noreset_str;
107  static const std::string residual_blocked_str;
108  static const std::string residual_str;
109 #if CHOW_PATEL
110  static const std::string ILU_apply1_str;
111  static const std::string ILU_apply2_str;
112 #else
113  static const std::string ILU_apply1_fm_str;
114  static const std::string ILU_apply2_fm_str;
115 #endif
116  static const std::string stdwell_apply_str;
117  static const std::string stdwell_apply_no_reorder_str;
118  static const std::string ILU_decomp_str;
119  static const std::string isaiL_str;
120  static const std::string isaiU_str;
121 
122  static void init(cl::Context *context, cl::CommandQueue *queue, std::vector<cl::Device>& devices, int verbosity);
123 
124  static double dot(cl::Buffer& in1, cl::Buffer& in2, cl::Buffer& out, int N);
125  static double norm(cl::Buffer& in, cl::Buffer& out, int N);
126  static void axpy(cl::Buffer& in, const double a, cl::Buffer& out, int N);
127  static void scale(cl::Buffer& in, const double a, int N);
128  static void vmul(const double alpha, cl::Buffer& in1, cl::Buffer& in2, cl::Buffer& out, int N);
129  static void custom(cl::Buffer& p, cl::Buffer& v, cl::Buffer& r, const double omega, const double beta, int N);
130  static void full_to_pressure_restriction(const cl::Buffer& fine_y, cl::Buffer& weights, cl::Buffer& coarse_y, int Nb);
131  static void add_coarse_pressure_correction(cl::Buffer& coarse_x, cl::Buffer& fine_x, int pressure_idx, int Nb);
132  static void prolongate_vector(const cl::Buffer& in, cl::Buffer& out, const cl::Buffer& cols, int N);
133  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);
134  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);
135 
136  static void ILU_apply1(cl::Buffer& vals, cl::Buffer& cols, cl::Buffer& rows, cl::Buffer& diagIndex,
137  const cl::Buffer& y, cl::Buffer& x, cl::Buffer& rowsPerColor, int color, int Nb, unsigned int block_size);
138 
139  static void ILU_apply2(cl::Buffer& vals, cl::Buffer& cols, cl::Buffer& rows, cl::Buffer& diagIndex,
140  cl::Buffer& invDiagVals, cl::Buffer& x, cl::Buffer& rowsPerColor, int color, int Nb, unsigned int block_size);
141 
142  static void ILU_decomp(int firstRow, int lastRow, cl::Buffer& vals, cl::Buffer& cols, cl::Buffer& rows,
143  cl::Buffer& diagIndex, cl::Buffer& invDiagVals, int Nb, unsigned int block_size);
144 
145  static void apply_stdwells_reorder(cl::Buffer& d_Cnnzs_ocl, cl::Buffer &d_Dnnzs_ocl, cl::Buffer &d_Bnnzs_ocl,
146  cl::Buffer &d_Ccols_ocl, cl::Buffer &d_Bcols_ocl, cl::Buffer &d_x, cl::Buffer &d_y,
147  cl::Buffer &d_toOrder, int dim, int dim_wells, cl::Buffer &d_val_pointers_ocl, int num_std_wells);
148 
149  static void apply_stdwells_no_reorder(cl::Buffer& d_Cnnzs_ocl, cl::Buffer &d_Dnnzs_ocl, cl::Buffer &d_Bnnzs_ocl,
150  cl::Buffer &d_Ccols_ocl, cl::Buffer &d_Bcols_ocl, cl::Buffer &d_x, cl::Buffer &d_y,
151  int dim, int dim_wells, cl::Buffer &d_val_pointers_ocl, int num_std_wells);
152 
153  static void isaiL(cl::Buffer& diagIndex, cl::Buffer& colPointers, cl::Buffer& mapping, cl::Buffer& nvc,
154  cl::Buffer& luIdxs, cl::Buffer& xxIdxs, cl::Buffer& dxIdxs, cl::Buffer& LUvals, cl::Buffer& invLvals, unsigned int Nb);
155 
156  static void isaiU(cl::Buffer& diagIndex, cl::Buffer& colPointers, cl::Buffer& rowIndices, cl::Buffer& mapping,
157  cl::Buffer& nvc, cl::Buffer& luIdxs, cl::Buffer& xxIdxs, cl::Buffer& dxIdxs, cl::Buffer& LUvals,
158  cl::Buffer& invDiagVals, cl::Buffer& invUvals, unsigned int Nb);
159 };
160 
161 } // namespace Accelerator
162 } // namespace Opm
163 
164 #endif
Definition: openclKernels.hpp:61
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: BlackoilPhases.hpp:27