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