Debian Codegraph @

packages/kokkos-kernels/perf_test/batched/sparse/GMRES/KokkosBatched_Test_GMRES.cpp

raw
1 //@HEADER
2 // ************************************************************************
3 //
4 // Kokkos v. 4.0
5 // Copyright (2022) National Technology & Engineering
6 // Solutions of Sandia, LLC (NTESS).
7 //
8 // Under the terms of Contract DE-NA0003525 with NTESS,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12 // See https://kokkos.org/LICENSE for license information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //@HEADER
16
17 #include <fstream>
18
19 #define KOKKOSKERNELS_DEBUG_LEVEL 0
20
21 /// Kokkos headers
22 #include "Kokkos_Core.hpp"
23
24 #include "Kokkos_ArithTraits.hpp"
25 #include "KokkosBatched_Util.hpp"
26 #include "KokkosBatched_Vector.hpp"
27
28 #include "KokkosBatched_Test_Sparse_Helper.hpp"
29
30 #include "KokkosBatched_Spmv.hpp"
31 #include "KokkosBatched_CrsMatrix.hpp"
32 #include "KokkosBatched_Krylov_Handle.hpp"
33 #include "KokkosBatched_GMRES.hpp"
34 #include "KokkosBatched_JacobiPrec.hpp"
35
36 typedef Kokkos::DefaultExecutionSpace exec_space;
37 typedef typename exec_space::memory_space memory_space;
38 typedef Kokkos::DefaultHostExecutionSpace host_space;
39 typedef typename Kokkos::Device<exec_space, memory_space> device;
40
41 #include "Functor_TestBatchedTeamVectorGMRES_1.hpp"
42 #include "Functor_TestBatchedTeamVectorGMRES_2.hpp"
43 #include "Functor_TestBatchedTeamVectorGMRES_3.hpp"
44
45 int main(int argc, char *argv[]) {
46 Kokkos::initialize(argc, argv);
47 {
48 #if defined(KOKKOS_ENABLE_CUDA) && defined(KOKKOSBATCHED_PROFILE)
49 cudaProfilerStop();
50 #endif
51 Kokkos::print_configuration(std::cout);
52
53 // typedef Kokkos::ArithTraits<value_type> ats;
54
55 ///
56 /// input arguments parsing
57 ///
58 int n_rep_1 = 10; // # of repetitions
59 int n_rep_2 = 1000; // # of repetitions
60 int team_size = 8;
61 int n_impl = 1;
62 int n_iterations = 10;
63 double tol = 1e-8;
64 bool layout_left = true;
65 bool layout_right = false;
66 bool use_preconditioner = false;
67 bool monitor_convergence = false;
68 int vector_length = 8;
69 int N_team_potential = 1;
70 int ortho_strategy = 0;
71 int other_level = 0;
72 int arnoldi_level = 0;
73
74 std::string name_A = "A.mm";
75 std::string name_B = "B.mm";
76
77 std::string name_timer = "timers";
78 std::string name_X = "X";
79 std::string name_conv = "res";
80
81 std::vector<int> impls;
82 for (int i = 1; i < argc; ++i) {
83 const std::string &token = argv[i];
84 if (token == std::string("--help") || token == std::string("-h")) {
85 std::cout << "Kokkos Batched GMRES performance test options:" << std::endl
86 << "-A : Filename of the input batched matrix." << std::endl
87 << "-B : Filename of the input batched right-hand "
88 "side."
89 << std::endl
90 << "-X : Filename of the output batched solution." << std::endl
91 << "-res : Filename of the output residual history." << std::endl
92 << "-timers : Filename of the output timers." << std::endl
93 << "-ortho_strategy : Select the orthogonalization strategy." << std::endl
94 << "-arnoldi_level : Select the scratch pad level (if used) "
95 "where Arnoldi vectors are stored."
96 << std::endl
97 << "-other_level : Select the scratch pad level (if used) "
98 "where everything except the Arnoldi vectors are stored."
99 << std::endl
100 << "-n1 : Number of repetitions of the experience." << std::endl
101 << "-n2 : Number of the kernel calls inside one "
102 "experience."
103 << std::endl
104 << "-team_size : Used team size." << std::endl
105 << "-n_implementations: Number of implementations to use: test "
106 "all "
107 "implementations [0, specified number -1]."
108 << std::endl
109 << "-implementation : Specify only one implementation at a time." << std::endl
110 << " Note: implementation 0 : does not use "
111 "scratch pad."
112 << std::endl
113 << " Note: implementation 1 : use scratch pad "
114 "for the graph and for the diagonal entries of the matrices."
115 << std::endl
116 << " Note: implementation 2 : use scratch pad "
117 "for the graph and for the diagonal entries of the matrices and "
118 "for the temporary variable but not for the Arnoldi vectors."
119 << std::endl
120 << "-l : Specify left layout." << std::endl
121 << "-r : Specify right layout." << std::endl
122 << "-P : Specify if a Jacobi preconditioner is "
123 "used."
124 << std::endl
125 << "-C : Specify if the convergence is monitored." << std::endl
126 << "-N_team : Specify the number of systems per team." << std::endl
127 << "-vector_length : Specify the vector length." << std::endl
128 << std::endl;
129 return 0;
130 }
131 if (token == std::string("-A")) name_A = argv[++i];
132 if (token == std::string("-B")) name_B = argv[++i];
133 if (token == std::string("-X")) name_X = argv[++i];
134 if (token == std::string("-res")) name_conv = argv[++i];
135 if (token == std::string("-timers")) name_timer = argv[++i];
136 if (token == std::string("-ortho_strategy")) ortho_strategy = std::atoi(argv[++i]);
137 if (token == std::string("-arnoldi_level")) arnoldi_level = std::atoi(argv[++i]);
138 if (token == std::string("-other_level")) other_level = std::atoi(argv[++i]);
139 if (token == std::string("-n1")) n_rep_1 = std::atoi(argv[++i]);
140 if (token == std::string("-n2")) n_rep_2 = std::atoi(argv[++i]);
141 if (token == std::string("-n_iterations")) n_iterations = std::atoi(argv[++i]);
142 if (token == std::string("-tol")) tol = std::stod(argv[++i]);
143 if (token == std::string("-team_size")) team_size = std::atoi(argv[++i]);
144 if (token == std::string("-N_team")) N_team_potential = std::atoi(argv[++i]);
145 if (token == std::string("-vector_length")) vector_length = std::atoi(argv[++i]);
146 if (token == std::string("-n_implementations")) n_impl = std::atoi(argv[++i]);
147 if (token == std::string("-implementation")) impls.push_back(std::atoi(argv[++i]));
148 if (token == std::string("-l")) {
149 layout_left = true;
150 layout_right = false;
151 }
152 if (token == std::string("-r")) {
153 layout_left = false;
154 layout_right = true;
155 }
156 if (token == std::string("-P")) use_preconditioner = true;
157 if (token == std::string("-C")) monitor_convergence = true;
158 }
159
160 int N, Blk, nnz, ncols;
161
162 readSizesFromMM(name_A, Blk, ncols, nnz, N);
163
164 std::cout << "N_team_potential = " << N_team_potential << ", n = " << Blk << ", N = " << N
165 << ", team_size = " << team_size << ", vector_length = " << vector_length << std::endl;
166
167 if (impls.size() == 0)
168 for (int i = 0; i < n_impl; ++i) impls.push_back(i);
169
170 // V100 L2 cache 6MB per core
171 constexpr size_t LLC_CAPACITY = 80 * 6 * 1024 * 1024;
172 KokkosBatched::Flush<LLC_CAPACITY, exec_space> flush;
173
174 printf(" :::: GMRES Testing (N = %d, Blk = %d, nnz = %d, vl = %d, n = %d)\n", N, Blk, nnz, vector_length, n_rep_1);
175
176 typedef Kokkos::LayoutRight LR;
177 typedef Kokkos::LayoutLeft LL;
178
179 using IntView = Kokkos::View<int *, LR>;
180 using AMatrixValueViewLR = Kokkos::View<double **, LR>;
181 using AMatrixValueViewLL = Kokkos::View<double **, LL>;
182 using XYTypeLR = Kokkos::View<double **, LR>;
183 using XYTypeLL = Kokkos::View<double **, LL>;
184
185 using alphaViewType = Kokkos::View<double *>;
186 alphaViewType alphaV("alpha", N);
187 alphaViewType betaV("alpha", N);
188
189 IntView rowOffsets("values", Blk + 1);
190 IntView colIndices("values", nnz);
191 AMatrixValueViewLR valuesLR("values", N, nnz);
192 AMatrixValueViewLL valuesLL("values", N, nnz);
193
194 AMatrixValueViewLR diagLR("values", N, Blk);
195 AMatrixValueViewLL diagLL("values", N, Blk);
196
197 XYTypeLR xLR("values", N, Blk);
198 XYTypeLR yLR("values", N, Blk);
199
200 XYTypeLL xLL("values", N, Blk);
201 XYTypeLL yLL("values", N, Blk);
202
203 if (layout_left) printf(" :::: Testing left layout (team_size = %d)\n", team_size);
204 if (layout_right) printf(" :::: Testing right layout (team_size = %d)\n", team_size);
205
206 if (layout_left) {
207 readCRSFromMM(name_A, valuesLL, rowOffsets, colIndices);
208 readArrayFromMM(name_B, yLL);
209 if (use_preconditioner) getInvDiagFromCRS(valuesLL, rowOffsets, colIndices, diagLL);
210 }
211 if (layout_right) {
212 readCRSFromMM(name_A, valuesLR, rowOffsets, colIndices);
213 readArrayFromMM(name_B, yLR);
214 if (use_preconditioner) getInvDiagFromCRS(valuesLR, rowOffsets, colIndices, diagLR);
215 }
216
217 for (auto i_impl : impls) {
218 std::vector<double> timers;
219
220 int n_skip = 2;
221
222 int N_team = N_team_potential;
223
224 using ScalarType = typename AMatrixValueViewLL::non_const_value_type;
225 using Layout = typename AMatrixValueViewLL::array_layout;
226 using EXSP = typename AMatrixValueViewLL::execution_space;
227
228 using MagnitudeType = typename Kokkos::ArithTraits<ScalarType>::mag_type;
229
230 using Norm2DViewType = Kokkos::View<MagnitudeType **, Layout, EXSP>;
231 using Scalar3DViewType = Kokkos::View<ScalarType ***, Layout, EXSP>;
232 using IntViewType = Kokkos::View<int *, Layout, EXSP>;
233
234 using KrylovHandleType = KokkosBatched::KrylovHandle<Norm2DViewType, IntViewType, Scalar3DViewType>;
235 KrylovHandleType handle(N, N_team, n_iterations, true);
236 handle.Arnoldi_view = Scalar3DViewType("", N, n_iterations, Blk + n_iterations + 3);
237 // handle.tmp_view = typename KrylovHandleType::TemporaryViewType(
238 // "", N, Blk + n_iterations + 3);
239
240 handle.set_max_iteration(n_iterations);
241 handle.set_tolerance(tol);
242 handle.set_ortho_strategy(ortho_strategy);
243 handle.set_scratch_pad_level(0);
244 handle.set_compute_last_residual(false);
245
246 for (int i_rep = 0; i_rep < n_rep_1 + n_skip; ++i_rep) {
247 double t_spmv = 0;
248 for (int j_rep = 0; j_rep < n_rep_2; ++j_rep) {
249 #if defined(KOKKOS_ENABLE_CUDA) && defined(KOKKOSBATCHED_PROFILE)
250 cudaProfilerStart();
251 #endif
252 exec_space().fence();
253 Kokkos::deep_copy(xLL, 0.0);
254 Kokkos::deep_copy(xLR, 0.0);
255 flush.run();
256 exec_space().fence();
257
258 if (i_impl == 0 && layout_left) {
259 if (use_preconditioner)
260 t_spmv += Functor_TestBatchedTeamVectorGMRES_1<exec_space, AMatrixValueViewLL, IntView, XYTypeLL,
261 KrylovHandleType, true>(
262 valuesLL, diagLL, rowOffsets, colIndices, xLL, yLL, N_team, team_size, vector_length,
263 n_iterations, tol, ortho_strategy, arnoldi_level, other_level, handle)
264 .run();
265 else
266 t_spmv += Functor_TestBatchedTeamVectorGMRES_1<exec_space, AMatrixValueViewLL, IntView, XYTypeLL,
267 KrylovHandleType, false>(
268 valuesLL, rowOffsets, colIndices, xLL, yLL, N_team, team_size, vector_length, n_iterations,
269 tol, ortho_strategy, arnoldi_level, other_level, handle)
270 .run();
271 }
272 if (i_impl == 1 && layout_left) {
273 if (use_preconditioner)
274 t_spmv += Functor_TestBatchedTeamVectorGMRES_2<exec_space, AMatrixValueViewLL, IntView, XYTypeLL,
275 KrylovHandleType, true>(
276 valuesLL, diagLL, rowOffsets, colIndices, xLL, yLL, N_team, team_size, vector_length,
277 n_iterations, tol, ortho_strategy, arnoldi_level, other_level, handle)
278 .run();
279 else
280 t_spmv += Functor_TestBatchedTeamVectorGMRES_2<exec_space, AMatrixValueViewLL, IntView, XYTypeLL,
281 KrylovHandleType, false>(
282 valuesLL, rowOffsets, colIndices, xLL, yLL, N_team, team_size, vector_length, n_iterations,
283 tol, ortho_strategy, arnoldi_level, other_level, handle)
284 .run();
285 }
286 if (i_impl == 2 && layout_left) {
287 if (use_preconditioner)
288 t_spmv += Functor_TestBatchedTeamVectorGMRES_3<exec_space, AMatrixValueViewLL, IntView, XYTypeLL,
289 KrylovHandleType, true>(
290 valuesLL, diagLL, rowOffsets, colIndices, xLL, yLL, N_team, team_size, vector_length,
291 n_iterations, tol, ortho_strategy, arnoldi_level, other_level, handle)
292 .run();
293 else
294 t_spmv += Functor_TestBatchedTeamVectorGMRES_3<exec_space, AMatrixValueViewLL, IntView, XYTypeLL,
295 KrylovHandleType, false>(
296 valuesLL, rowOffsets, colIndices, xLL, yLL, N_team, team_size, vector_length, n_iterations,
297 tol, ortho_strategy, arnoldi_level, other_level, handle)
298 .run();
299 }
300 if (i_impl == 0 && layout_right) {
301 if (use_preconditioner)
302 t_spmv += Functor_TestBatchedTeamVectorGMRES_1<exec_space, AMatrixValueViewLR, IntView, XYTypeLR,
303 KrylovHandleType, true>(
304 valuesLR, diagLR, rowOffsets, colIndices, xLR, yLR, N_team, team_size, vector_length,
305 n_iterations, tol, ortho_strategy, arnoldi_level, other_level, handle)
306 .run();
307 else
308 t_spmv += Functor_TestBatchedTeamVectorGMRES_1<exec_space, AMatrixValueViewLR, IntView, XYTypeLR,
309 KrylovHandleType, false>(
310 valuesLR, rowOffsets, colIndices, xLR, yLR, N_team, team_size, vector_length, n_iterations,
311 tol, ortho_strategy, arnoldi_level, other_level, handle)
312 .run();
313 }
314 if (i_impl == 1 && layout_right) {
315 if (use_preconditioner)
316 t_spmv += Functor_TestBatchedTeamVectorGMRES_2<exec_space, AMatrixValueViewLR, IntView, XYTypeLR,
317 KrylovHandleType, true>(
318 valuesLR, diagLR, rowOffsets, colIndices, xLR, yLR, N_team, team_size, vector_length,
319 n_iterations, tol, ortho_strategy, arnoldi_level, other_level, handle)
320 .run();
321 else
322 t_spmv += Functor_TestBatchedTeamVectorGMRES_2<exec_space, AMatrixValueViewLR, IntView, XYTypeLR,
323 KrylovHandleType, false>(
324 valuesLR, rowOffsets, colIndices, xLR, yLR, N_team, team_size, vector_length, n_iterations,
325 tol, ortho_strategy, arnoldi_level, other_level, handle)
326 .run();
327 }
328 if (i_impl == 2 && layout_right) {
329 if (use_preconditioner)
330 t_spmv += Functor_TestBatchedTeamVectorGMRES_3<exec_space, AMatrixValueViewLR, IntView, XYTypeLR,
331 KrylovHandleType, true>(
332 valuesLR, diagLR, rowOffsets, colIndices, xLR, yLR, N_team, team_size, vector_length,
333 n_iterations, tol, ortho_strategy, arnoldi_level, other_level, handle)
334 .run();
335 else
336 t_spmv += Functor_TestBatchedTeamVectorGMRES_3<exec_space, AMatrixValueViewLR, IntView, XYTypeLR,
337 KrylovHandleType, false>(
338 valuesLR, rowOffsets, colIndices, xLR, yLR, N_team, team_size, vector_length, n_iterations,
339 tol, ortho_strategy, arnoldi_level, other_level, handle)
340 .run();
341 }
342 exec_space().fence();
343
344 #if defined(KOKKOS_ENABLE_CUDA) && defined(KOKKOSBATCHED_PROFILE)
345 cudaProfilerStop();
346 #endif
347 }
348 if (i_rep > n_skip) timers.push_back(t_spmv / n_rep_2);
349 }
350
351 {
352 std::ofstream myfile;
353 std::string name;
354 if (layout_left) name = name_timer + "_" + std::to_string(i_impl) + "_left.txt";
355 if (layout_right) name = name_timer + "_" + std::to_string(i_impl) + "_right.txt";
356
357 myfile.open(name);
358
359 for (size_t i = 0; i < timers.size(); ++i) myfile << timers[i] << " ";
360
361 myfile << std::endl;
362
363 myfile.close();
364 }
365
366 double average_time = 0.;
367
368 for (size_t i = 0; i < timers.size(); ++i) average_time += timers[i] / timers.size();
369
370 if (layout_left) printf("Left layout: Implementation %d: solve time = %f\n", i_impl, average_time);
371 if (layout_right) printf("Right layout: Implementation %d: solve time = %f\n", i_impl, average_time);
372
373 if (layout_left) {
374 writeArrayToMM(name_X + std::to_string(i_impl) + "_l.mm", xLL);
375 }
376 if (layout_right) {
377 writeArrayToMM(name_X + std::to_string(i_impl) + "_r.mm", xLR);
378 }
379 if (monitor_convergence) {
380 writeArrayToMM(name_conv + std::to_string(i_impl) + ".mm", handle.residual_norms);
381 }
382 }
383 }
384 Kokkos::finalize();
385
386 return 0;
387 }