Debian Codegraph @

packages/kokkos-kernels/perf_test/batched/dense/KokkosBatched_Test_BlockTridiagJacobi.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 /// Kokkos headers
17 #include "Kokkos_Core.hpp"
18 #include "Kokkos_Timer.hpp"
19 #include "Kokkos_Random.hpp"
20
21 #if !(defined(KOKKOS_ENABLE_CUDA) && !defined(KOKKOS_ENABLE_CUDA_LAMBDA))
22 #define KOKKOSBATCHED_TEST_BLOCKTRIDIAGJACOBI
23 #endif
24
25 #if defined(KOKKOSBATCHED_TEST_BLOCKTRIDIAGJACOBI)
26
27 /// KokkosKernels headers
28 #include "KokkosBatched_Util.hpp"
29 #include "KokkosBatched_Vector.hpp"
30
31 #include <Kokkos_ArithTraits.hpp>
32 #include <KokkosBatched_Util.hpp>
33 #include <KokkosBatched_Vector.hpp>
34 #include <KokkosBatched_Copy_Decl.hpp>
35 #include <KokkosBatched_Copy_Impl.hpp>
36 #include <KokkosBatched_SetIdentity_Decl.hpp>
37 #include <KokkosBatched_SetIdentity_Impl.hpp>
38 #include <KokkosBatched_AddRadial_Decl.hpp>
39 #include <KokkosBatched_AddRadial_Impl.hpp>
40 #include <KokkosBatched_Gemm_Decl.hpp>
41 #include <KokkosBatched_Gemm_Serial_Impl.hpp>
42 #include <KokkosBatched_Gemm_Team_Impl.hpp>
43 #include <KokkosBatched_Gemv_Decl.hpp>
44 #include <KokkosBatched_Gemv_Serial_Impl.hpp>
45 #include <KokkosBatched_Gemv_Team_Impl.hpp>
46 #include <KokkosBatched_Trsm_Decl.hpp>
47 #include <KokkosBatched_Trsm_Serial_Impl.hpp>
48 #include <KokkosBatched_Trsm_Team_Impl.hpp>
49 #include <KokkosBatched_Trsv_Decl.hpp>
50 #include <KokkosBatched_Trsv_Serial_Impl.hpp>
51 #include <KokkosBatched_Trsv_Team_Impl.hpp>
52 #include <KokkosBatched_LU_Decl.hpp>
53 #include <KokkosBatched_LU_Serial_Impl.hpp>
54 #include <KokkosBatched_LU_Team_Impl.hpp>
55
56 #define KOKKOSBATCHED_PROFILE 1
57 #if defined(KOKKOS_ENABLE_CUDA) && defined(KOKKOSBATCHED_PROFILE)
58 #include "cuda_profiler_api.h"
59 #endif
60
61 #define KOKKOSBATCHED_USE_128BIT_MEMORY_INST
62
63 typedef Kokkos::DefaultExecutionSpace exec_space;
64 typedef typename exec_space::memory_space memory_space;
65 typedef Kokkos::DefaultHostExecutionSpace host_space;
66
67 typedef double value_type;
68
69 /// 128*128*128/16*5 * (2*8) / 16
70 ///
71 /// simd typedefs
72 ///
73 using namespace KokkosBatched;
74
75 static constexpr int vector_length = DefaultVectorLength<value_type, memory_space>::value;
76 #if defined(KOKKOSBATCHED_USE_128BIT_MEMORY_INST)
77 static constexpr int internal_vector_length = DefaultInternalVectorLength<value_type, memory_space>::value;
78 #else
79 static constexpr int internal_vector_length = 1;
80 #endif
81
82 typedef Vector<SIMD<value_type>, vector_length> vector_type;
83 #if defined(KOKKOSBATCHED_USE_128BIT_MEMORY_INST)
84 typedef Vector<SIMD<value_type>, internal_vector_length> internal_vector_type;
85 #else
86 typedef value_type internal_vector_type;
87 #endif
88
89 template <typename ExecutionSpace>
90 struct InverseDiagonalsModeAndAlgo;
91
92 struct InverseDiagonalsModeAndAlgoHostImpl {
93 typedef Mode::Serial mode_type;
94 typedef Algo::Level3::Blocked algo_type;
95 };
96
97 #if defined(KOKKOS_ENABLE_SERIAL)
98 template <>
99 struct InverseDiagonalsModeAndAlgo<Kokkos::Serial> : InverseDiagonalsModeAndAlgoHostImpl {};
100 #endif
101
102 #if defined(KOKKOS_ENABLE_THREADS)
103 template <>
104 struct InverseDiagonalsModeAndAlgo<Kokkos::Threads> : InverseDiagonalsModeAndAlgoHostImpl {};
105 #endif
106
107 #if defined(KOKKOS_ENABLE_ONPENMP)
108 template <>
109 struct InverseDiagonalsModeAndAlgo<Kokkos::Threads> : InverseDiagonalsModeAndAlgoHostImpl {};
110 #endif
111
112 struct InverseDiagonalsModeAndAlgoDeviceImpl {
113 typedef Mode::Team mode_type;
114 typedef Algo::Level3::Unblocked algo_type;
115 };
116
117 #if defined(KOKKOS_ENABLE_CUDA)
118 template <>
119 struct InverseDiagonalsModeAndAlgo<Kokkos::Cuda> : InverseDiagonalsModeAndAlgoDeviceImpl {};
120 #endif
121
122 #if defined(KOKKOS_ENABLE_HIP)
123 template <>
124 struct InverseDiagonalsModeAndAlgo<Kokkos::HIP> : InverseDiagonalsModeAndAlgoDeviceImpl {};
125 #endif
126
127 template <typename ExecutionSpace>
128 struct SolveModeAndAlgo;
129
130 struct SolveModeAndAlgoHostImpl {
131 typedef Mode::Serial mode_type;
132 typedef Algo::Level2::Blocked algo_type;
133 };
134
135 #if defined(KOKKOS_ENABLE_SERIAL)
136 template <>
137 struct SolveModeAndAlgo<Kokkos::Serial> : SolveModeAndAlgoHostImpl {};
138 #endif
139
140 #if defined(KOKKOS_ENABLE_THREADS)
141 template <>
142 struct SolveModeAndAlgo<Kokkos::Threads> : SolveModeAndAlgoHostImpl {};
143 #endif
144
145 #if defined(KOKKOS_ENABLE_OPENMP)
146 template <>
147 struct SolveModeAndAlgo<Kokkos::OpenMP> : SolveModeAndAlgoHostImpl {};
148 #endif
149
150 struct SolveModeAndAlgoDeviceImpl {
151 typedef Mode::Team mode_type;
152 typedef Algo::Level2::Unblocked algo_type;
153 };
154
155 #if defined(KOKKOS_ENABLE_CUDA)
156 template <>
157 struct SolveModeAndAlgo<Kokkos::Cuda> : SolveModeAndAlgoDeviceImpl {};
158 #endif
159
160 #if defined(KOKKOS_ENABLE_HIP)
161 template <>
162 struct SolveModeAndAlgo<Kokkos::HIP> : SolveModeAndAlgoDeviceImpl {};
163 #endif
164
165 int main(int argc, char *argv[]) {
166 Kokkos::initialize(argc, argv);
167 {
168 #if defined(KOKKOS_ENABLE_CUDA) && defined(KOKKOSBATCHED_PROFILE)
169 cudaProfilerStop();
170 #endif
171 Kokkos::print_configuration(std::cout);
172
173 // typedef Kokkos::ArithTraits<value_type> ats;
174 Kokkos::Timer timer;
175
176 ///
177 /// input arguments parsing
178 ///
179 int N = 128 * 128; /// # of problems (batch size)
180 int L = 128; /// length of block tridiags
181 int Blk = 5; /// block dimension
182 int Nvec = 1;
183 int S = 0; /// scratch size
184 int niter = 1;
185 int nsweep = 10;
186 for (int i = 1; i < argc; ++i) {
187 const std::string &token = argv[i];
188 if (token == std::string("-N")) N = std::atoi(argv[++i]);
189 if (token == std::string("-L")) L = std::atoi(argv[++i]);
190 if (token == std::string("-B")) Blk = std::atoi(argv[++i]);
191 if (token == std::string("-Nvec")) Nvec = std::atoi(argv[++i]);
192 if (token == std::string("-S")) S = std::atoi(argv[++i]);
193 if (token == std::string("-Niter")) niter = std::atoi(argv[++i]);
194 if (token == std::string("-Nsweep")) nsweep = std::atoi(argv[++i]);
195 }
196
197 printf(
198 " :::: Testing (N = %d, L = %d, Blk = %d, vl = %d, vi = %d, niter = "
199 "%d, nsweep = %d)\n",
200 N, L, Blk, vector_length, internal_vector_length, niter, nsweep);
201
202 ///
203 /// problem container
204 ///
205
206 /// double 16
207 Kokkos::View<vector_type *****, Kokkos::LayoutRight, exec_space> Av("A", N / vector_length, L, 4, Blk, Blk);
208
209 /// double
210 Kokkos::View<value_type ******, Kokkos::LayoutRight, exec_space> As(
211 (value_type *)Av.data(), Av.extent(0), Av.extent(1), Av.extent(2), Av.extent(3), Av.extent(4), vector_length);
212
213 /// double 2
214 Kokkos::View<internal_vector_type ******, Kokkos::LayoutRight, exec_space> Ai(
215 (internal_vector_type *)Av.data(), Av.extent(0), Av.extent(1), Av.extent(2), Av.extent(3), Av.extent(4),
216 vector_length / internal_vector_length);
217 /// double 16
218 Kokkos::View<vector_type *****, Kokkos::LayoutRight, exec_space> xv("x", N / vector_length, Nvec, 2, L, Blk);
219
220 /// double
221 Kokkos::View<value_type ******, Kokkos::LayoutRight, exec_space> xs(
222 (value_type *)xv.data(), xv.extent(0), xv.extent(1), xv.extent(2), xv.extent(3), xv.extent(4), vector_length);
223
224 /// double 2
225 Kokkos::View<internal_vector_type ******, Kokkos::LayoutRight, exec_space> xi(
226 (internal_vector_type *)xv.data(), xv.extent(0), xv.extent(1), xv.extent(2), xv.extent(3), xv.extent(4),
227 vector_length / internal_vector_length);
228
229 /// double 16
230 Kokkos::View<vector_type ****, Kokkos::LayoutRight, exec_space> bv("b", N / vector_length, Nvec, L, Blk);
231
232 /// double
233 Kokkos::View<value_type *****, Kokkos::LayoutRight, exec_space> bs(
234 (value_type *)bv.data(), bv.extent(0), bv.extent(1), bv.extent(2), bv.extent(3), vector_length);
235
236 /// double 2
237 Kokkos::View<internal_vector_type *****, Kokkos::LayoutRight, exec_space> bi(
238 (internal_vector_type *)bv.data(), bv.extent(0), bv.extent(1), bv.extent(2), bv.extent(3),
239 vector_length / internal_vector_length);
240
241 /// double copy of A
242 Kokkos::View<value_type ******, Kokkos::LayoutRight, exec_space> Acopy(
243 "Acopy", As.extent(0), As.extent(1), As.extent(2), As.extent(3), As.extent(4), As.extent(5));
244
245 Kokkos::View<value_type *****, Kokkos::LayoutRight, exec_space> rs("rs", bs.extent(0), bs.extent(1), bs.extent(2),
246 bs.extent(3), bs.extent(4));
247
248 #if defined(KOKKOSBATCHED_USE_128BIT_MEMORY_INST)
249 auto AA = Ai;
250 auto bb = bi;
251 auto xx = xi;
252 #else
253 auto AA = As;
254 auto bb = bs;
255 auto xx = xs;
256 #endif
257
258 /// randomize input
259 Kokkos::Random_XorShift64_Pool<exec_space> random(13245);
260 Kokkos::fill_random(As, random, value_type(1.0));
261 Kokkos::fill_random(bs, random, value_type(1.0));
262
263 ///
264 /// diagonal dominant
265 ///
266 if (1) {
267 #if defined(KOKKOS_ENABLE_CUDA) && defined(KOKKOSBATCHED_PROFILE)
268 cudaProfilerStart();
269 #endif
270 using policy_type = Kokkos::TeamPolicy<exec_space>;
271 using member_type = typename policy_type::member_type;
272 policy_type policy(AA.extent(0) * L, Kokkos::AUTO(), AA.extent(5));
273 Kokkos::parallel_for(
274 "diagonal dominant", policy, KOKKOS_LAMBDA(const member_type &member) {
275 const int i = member.league_rank() / L;
276 const int k = member.league_rank() % L;
277 Kokkos::parallel_for(Kokkos::TeamThreadRange(member, Blk), [&](const int &j) {
278 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, AA.extent(5)),
279 [&](const int &v) { AA(i, k, 1, j, j, v) += internal_vector_type(9 * Blk); });
280 });
281 });
282 Kokkos::fence();
283 #if defined(KOKKOS_ENABLE_CUDA) && defined(KOKKOSBATCHED_PROFILE)
284 cudaProfilerStop();
285 #endif
286 }
287
288 Kokkos::deep_copy(Acopy, As);
289
290 ///
291 /// compute the inverse of diagonals
292 ///
293 if (1) {
294 #if defined(KOKKOS_ENABLE_CUDA) && defined(KOKKOSBATCHED_PROFILE)
295 cudaProfilerStart();
296 #endif
297 timer.reset();
298 typedef internal_vector_type scratch_value_type;
299 typedef Kokkos::View<scratch_value_type ***, Kokkos::LayoutRight, typename exec_space::scratch_memory_space,
300 Kokkos::MemoryUnmanaged>
301 scratch_view_type;
302
303 using policy_type = Kokkos::TeamPolicy<exec_space>;
304 using member_type = typename policy_type::member_type;
305 const int per_team_scratch = scratch_view_type::shmem_size(Blk, Blk, AA.extent(5));
306 int team_size = 0;
307 if (Blk < 8) {
308 team_size = 32 / AA.extent(5);
309 } else if (Blk < 12) {
310 team_size = 32 / AA.extent(5);
311 } else {
312 team_size = 64 / AA.extent(5);
313 }
314
315 policy_type policy(AA.extent(0) * L, team_size, AA.extent(5));
316 Kokkos::parallel_for(
317 "inverse diagonals", policy.set_scratch_size(0, Kokkos::PerTeam(S < per_team_scratch ? per_team_scratch : S)),
318 KOKKOS_LAMBDA(const member_type &member) {
319 typedef InverseDiagonalsModeAndAlgo<Kokkos::DefaultExecutionSpace> default_mode_and_algo_type;
320 typedef default_mode_and_algo_type::mode_type mode_type;
321 typedef default_mode_and_algo_type::algo_type algo_type;
322
323 const int i = member.league_rank() / L;
324 const int k = member.league_rank() % L;
325
326 scratch_view_type WW(member.team_scratch(0), Blk, Blk, AA.extent(5));
327 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, AA.extent(5)), [&](const int &v) {
328 auto A = Kokkos::subview(AA, i, k, 1, Kokkos::ALL(), Kokkos::ALL(), v);
329 auto D = Kokkos::subview(AA, i, k, 3, Kokkos::ALL(), Kokkos::ALL(), v);
330 auto W = Kokkos::subview(WW, Kokkos::ALL(), Kokkos::ALL(), v);
331
332 Copy<member_type, Trans::NoTranspose, mode_type>::invoke(member, A, W);
333 SetIdentity<member_type, mode_type>::invoke(member, D);
334 member.team_barrier();
335 LU<member_type, mode_type, algo_type>::invoke(member, W);
336 Trsm<member_type, Side::Left, Uplo::Lower, Trans::NoTranspose, Diag::Unit, mode_type, algo_type>::invoke(
337 member, 1.0, W, D);
338 Trsm<member_type, Side::Left, Uplo::Upper, Trans::NoTranspose, Diag::NonUnit, mode_type,
339 algo_type>::invoke(member, 1.0, W, D);
340 });
341 });
342 Kokkos::fence();
343 const double t = timer.seconds();
344 #if defined(KOKKOS_ENABLE_CUDA) && defined(KOKKOSBATCHED_PROFILE)
345 cudaProfilerStop();
346 #endif
347 printf("inverse time = %f , # of inverse per min = %f \n", t, 1.0 / t * 60);
348 }
349
350 ///
351 /// solve the matrix 20 times
352 ///
353 if (1) {
354 #if defined(KOKKOS_ENABLE_CUDA) && defined(KOKKOSBATCHED_PROFILE)
355 cudaProfilerStart();
356 #endif
357 timer.reset();
358 typedef internal_vector_type scratch_value_type;
359 typedef Kokkos::View<scratch_value_type **, Kokkos::LayoutRight, typename exec_space::scratch_memory_space,
360 Kokkos::MemoryUnmanaged>
361 scratch_view_type;
362 const int per_team_scratch = scratch_view_type::shmem_size(Blk, AA.extent(5));
363
364 using policy_type = Kokkos::TeamPolicy<exec_space>;
365 using member_type = typename policy_type::member_type;
366 int team_size = 0;
367 if (Blk < 8) {
368 team_size = 32 / AA.extent(5);
369 } else if (Blk < 12) {
370 team_size = 32 / AA.extent(5);
371 } else {
372 team_size = 32 / AA.extent(5);
373 }
374 policy_type policy(AA.extent(0) * L, team_size, AA.extent(5));
375
376 for (int iter = 0; iter < niter; ++iter) {
377 auto xxx = Kokkos::subview(xx, Kokkos::ALL(), Kokkos::ALL(), 0, Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL());
378 auto yyy = Kokkos::subview(xx, Kokkos::ALL(), Kokkos::ALL(), 1, Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL());
379
380 for (int nis = 0; nis < nsweep; ++nis) {
381 Kokkos::parallel_for(
382 "solve", policy.set_scratch_size(0, Kokkos::PerTeam(S < per_team_scratch ? per_team_scratch : S)),
383 KOKKOS_LAMBDA(const member_type &member) {
384 typedef SolveModeAndAlgo<Kokkos::DefaultExecutionSpace> default_mode_and_algo_type;
385 typedef default_mode_and_algo_type::mode_type mode_type;
386 typedef default_mode_and_algo_type::algo_type algo_type;
387
388 scratch_view_type WW(member.team_scratch(0), Blk, AA.extent(5));
389 const int i = member.league_rank() / L; //%AA.extent(0);
390 const int k = member.league_rank() % L;
391 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, AA.extent(5)), [&](const int &v) {
392 auto A = Kokkos::subview(AA, i, k, 1, Kokkos::ALL(), Kokkos::ALL(), v);
393 auto D = Kokkos::subview(AA, i, k, 3, Kokkos::ALL(), Kokkos::ALL(), v);
394 auto B = Kokkos::subview(AA, i, k, 2, Kokkos::ALL(), Kokkos::ALL(), v);
395 auto C = Kokkos::subview(AA, i, k ? k - 1 : 0, 0, Kokkos::ALL(), Kokkos::ALL(), v);
396 auto u = Kokkos::subview(WW, Kokkos::ALL(), v);
397 for (int jvec = 0; jvec < Nvec; ++jvec) {
398 auto x0 = Kokkos::subview(xxx, i, jvec, k == 0 ? 0 : k - 1, Kokkos::ALL(), v);
399 auto x1 = Kokkos::subview(xxx, i, jvec, k, Kokkos::ALL(), v);
400 auto x2 = Kokkos::subview(xxx, i, jvec, k == L - 1 ? 0 : k + 1, Kokkos::ALL(), v);
401 auto y1 = Kokkos::subview(yyy, i, jvec, k, Kokkos::ALL(), v);
402 auto b = Kokkos::subview(bb, i, jvec, k, Kokkos::ALL(), v);
403
404 if (L == 1) {
405 Gemv<member_type, Trans::NoTranspose, mode_type, algo_type>::invoke(member, 1.0, D, b, 0.0, x1);
406 } else {
407 Copy<member_type, Trans::NoTranspose, mode_type>::invoke(member, b, u);
408 if (k == 0) {
409 Gemv<member_type, Trans::NoTranspose, mode_type, algo_type>::invoke(member, -1.0, B, x2, 1.0,
410 u);
411 } else if (k == L - 1) {
412 Gemv<member_type, Trans::NoTranspose, mode_type, algo_type>::invoke(member, -1.0, C, x0, 1.0,
413 u);
414 } else {
415 Gemv<member_type, Trans::NoTranspose, mode_type, algo_type>::invoke(member, -1.0, B, x2, 1.0,
416 u);
417 Gemv<member_type, Trans::NoTranspose, mode_type, algo_type>::invoke(member, -1.0, C, x0, 1.0,
418 u);
419 }
420 Gemv<member_type, Trans::NoTranspose, mode_type, algo_type>::invoke(member, 1.0, D, u, 0.0, y1);
421 }
422 }
423 });
424 });
425 auto tmp = xxx;
426 xxx = yyy;
427 yyy = tmp;
428 }
429 Kokkos::fence();
430 }
431 const double t = timer.seconds();
432 #if defined(KOKKOS_ENABLE_CUDA) && defined(KOKKOSBATCHED_PROFILE)
433 cudaProfilerStop();
434 #endif
435 printf("solve time = %f , # of solves per min = %f\n", t, 1.0 / t * 60 * niter);
436 }
437
438 ///
439 /// compute residual
440 ///
441 if (1) {
442 typedef KokkosBatched::Algo::Level2::Unblocked algo_type;
443 using policy_type = Kokkos::TeamPolicy<exec_space>;
444 policy_type policy(Acopy.extent(0), Kokkos::AUTO(), Acopy.extent(5));
445 Kokkos::parallel_for(
446 "compute residual", policy, KOKKOS_LAMBDA(const typename policy_type::member_type &member) {
447 const int i = member.league_rank();
448 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, Acopy.extent(5)), [&](const int &v) {
449 auto A = Kokkos::subview(Acopy, i, Kokkos::ALL(), 1, Kokkos::ALL(), Kokkos::ALL(), v);
450 auto B = Kokkos::subview(Acopy, i, Kokkos::ALL(), 2, Kokkos::ALL(), Kokkos::ALL(), v);
451 auto C = Kokkos::subview(Acopy, i, Kokkos::ALL(), 0, Kokkos::ALL(), Kokkos::ALL(), v);
452
453 for (int jvec = 0, jvecend = rs.extent(1); jvec < jvecend; ++jvec) {
454 auto x = Kokkos::subview(xs, i, jvec, nsweep % 2, Kokkos::ALL(), Kokkos::ALL(), v);
455 auto b = Kokkos::subview(bs, i, jvec, Kokkos::ALL(), Kokkos::ALL(), v);
456 auto r = Kokkos::subview(rs, i, jvec, Kokkos::ALL(), Kokkos::ALL(), v);
457
458 if (L == 1) {
459 auto A0 = Kokkos::subview(A, 0, Kokkos::ALL(), Kokkos::ALL());
460 auto x0 = Kokkos::subview(x, 0, Kokkos::ALL());
461 auto b0 = Kokkos::subview(b, 0, Kokkos::ALL());
462 auto r0 = Kokkos::subview(r, 0, Kokkos::ALL());
463
464 TeamCopy<typename policy_type::member_type, Trans::NoTranspose>::invoke(member, b0, r0);
465 TeamGemv<typename policy_type::member_type, Trans::NoTranspose, algo_type>::invoke(member, -1.0, A0,
466 x0, 1.0, r0);
467 } else {
468 int k = 0;
469 {
470 /// first row
471 auto A1 = Kokkos::subview(A, k, Kokkos::ALL(), Kokkos::ALL());
472 auto B2 = Kokkos::subview(B, k, Kokkos::ALL(), Kokkos::ALL());
473
474 auto x1 = Kokkos::subview(x, k, Kokkos::ALL());
475 auto x2 = Kokkos::subview(x, k + 1, Kokkos::ALL());
476
477 auto bk = Kokkos::subview(b, k, Kokkos::ALL());
478 auto rk = Kokkos::subview(r, k, Kokkos::ALL());
479 TeamCopy<typename policy_type::member_type, Trans::NoTranspose>::invoke(member, bk, rk);
480 member.team_barrier();
481 TeamGemv<typename policy_type::member_type, Trans::NoTranspose, algo_type>::invoke(member, -1.0, A1,
482 x1, 1.0, rk);
483 TeamGemv<typename policy_type::member_type, Trans::NoTranspose, algo_type>::invoke(member, -1.0, B2,
484 x2, 1.0, rk);
485 ++k;
486 }
487 for (; k < (L - 1); ++k) {
488 auto C0 = Kokkos::subview(C, k - 1, Kokkos::ALL(), Kokkos::ALL());
489 auto A1 = Kokkos::subview(A, k, Kokkos::ALL(), Kokkos::ALL());
490 auto B2 = Kokkos::subview(B, k, Kokkos::ALL(), Kokkos::ALL());
491
492 auto x0 = Kokkos::subview(x, k - 1, Kokkos::ALL());
493 auto x1 = Kokkos::subview(x, k, Kokkos::ALL());
494 auto x2 = Kokkos::subview(x, k + 1, Kokkos::ALL());
495
496 auto bk = Kokkos::subview(b, k, Kokkos::ALL());
497 auto rk = Kokkos::subview(r, k, Kokkos::ALL());
498 TeamCopy<typename policy_type::member_type, Trans::NoTranspose>::invoke(member, bk, rk);
499 member.team_barrier();
500 TeamGemv<typename policy_type::member_type, Trans::NoTranspose, algo_type>::invoke(member, -1.0, C0,
501 x0, 1.0, rk);
502 TeamGemv<typename policy_type::member_type, Trans::NoTranspose, algo_type>::invoke(member, -1.0, A1,
503 x1, 1.0, rk);
504 TeamGemv<typename policy_type::member_type, Trans::NoTranspose, algo_type>::invoke(member, -1.0, B2,
505 x2, 1.0, rk);
506 }
507 {
508 // last row
509 auto C0 = Kokkos::subview(C, k - 1, Kokkos::ALL(), Kokkos::ALL());
510 auto A1 = Kokkos::subview(A, k, Kokkos::ALL(), Kokkos::ALL());
511
512 auto x0 = Kokkos::subview(x, k - 1, Kokkos::ALL());
513 auto x1 = Kokkos::subview(x, k, Kokkos::ALL());
514
515 auto bk = Kokkos::subview(b, k, Kokkos::ALL());
516 auto rk = Kokkos::subview(r, k, Kokkos::ALL());
517 TeamCopy<typename policy_type::member_type, Trans::NoTranspose>::invoke(member, bk, rk);
518 member.team_barrier();
519 TeamGemv<typename policy_type::member_type, Trans::NoTranspose, algo_type>::invoke(member, -1.0, C0,
520 x0, 1.0, rk);
521 TeamGemv<typename policy_type::member_type, Trans::NoTranspose, algo_type>::invoke(member, -1.0, A1,
522 x1, 1.0, rk);
523 }
524 }
525 }
526 });
527 });
528 Kokkos::fence();
529 auto rs_host = Kokkos::create_mirror_view(rs);
530 auto bs_host = Kokkos::create_mirror_view(bs);
531 Kokkos::deep_copy(rs_host, rs);
532 Kokkos::deep_copy(bs_host, bs);
533 Kokkos::fence();
534 {
535 double norm2 = 0, diff2 = 0;
536 for (int i0 = 0, i0end = rs.extent(0); i0 < i0end; ++i0) // N/vector_length
537 for (int i1 = 0, i1end = rs.extent(1); i1 < i1end; ++i1) // Nvec
538 for (int i2 = 0, i2end = rs.extent(2); i2 < i2end; ++i2) // L
539 for (int i3 = 0, i3end = rs.extent(3); i3 < i3end; ++i3) // Blk
540 for (int i4 = 0, i4end = rs.extent(4); i4 < i4end; ++i4) { // vector_length
541 const auto val = bs_host(i0, i1, i2, i3, i4);
542 const auto res = rs_host(i0, i1, i2, i3, i4);
543 norm2 += val * val;
544 diff2 += res * res;
545 }
546 printf("rel error = %e\n", diff2 / norm2);
547
548 // const int i0 = 0;
549 // const int i1 = 0;
550 // const int i4 = 0;
551 // for (int i2=0;i2<rs.extent(2);++i2) // L
552 // for (int i3=0;i3<rs.extent(3);++i3) // Blk
553 // printf("row %d, block row %d residual %e\n",
554 // i2, i3, rs_host(i0,i1,i2,i3,i4));
555 }
556 }
557 }
558 Kokkos::finalize();
559
560 return 0;
561 }
562 #else
563 int main() { return 0; }
564 #endif