| 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 |
|
| 24 |
#include "Kokkos_ArithTraits.hpp" |
| 25 |
#include "KokkosBatched_Util.hpp" |
| 26 |
#include "KokkosBatched_Copy_Decl.hpp" |
| 27 |
#include "KokkosBatched_Copy_Impl.hpp" |
| 28 |
#include "KokkosBatched_SetIdentity_Decl.hpp" |
| 29 |
#include "KokkosBatched_SetIdentity_Impl.hpp" |
| 30 |
#include "KokkosBatched_Gemv_Decl.hpp" |
| 31 |
#include "KokkosBatched_Gemv_Team_Impl.hpp" |
| 32 |
#include "KokkosBatched_Trsm_Decl.hpp" |
| 33 |
#include "KokkosBatched_Trsm_Team_Impl.hpp" |
| 34 |
#include "KokkosBatched_LU_Decl.hpp" |
| 35 |
#include "KokkosBatched_LU_Team_Impl.hpp" |
| 36 |
|
| 37 |
/// cuda profile |
| 38 |
#if defined(KOKKOS_ENABLE_CUDA) |
| 39 |
#include "cuda_profiler_api.h" |
| 40 |
#endif |
| 41 |
|
| 42 |
using exec_space_type = Kokkos::DefaultExecutionSpace; |
| 43 |
using memory_space_type = typename exec_space_type::memory_space; |
| 44 |
using host_space = Kokkos::DefaultHostExecutionSpace; |
| 45 |
|
| 46 |
using val_type = double; |
| 47 |
using policy_type = Kokkos::TeamPolicy<exec_space_type>; |
| 48 |
using member_type = typename policy_type::member_type; |
| 49 |
|
| 50 |
using namespace KokkosBatched; |
| 51 |
|
| 52 |
template <typename ManyMatrixType, typename ManyVectorType> |
| 53 |
val_type computeResidual(const ManyMatrixType &A, const ManyVectorType &x, const ManyVectorType &b, |
| 54 |
const ManyVectorType &r) { |
| 55 |
/// compute residual |
| 56 |
val_type residual(0); |
| 57 |
{ |
| 58 |
policy_type policy(A.extent(0), Kokkos::AUTO()); |
| 59 |
Kokkos::deep_copy(r, b); |
| 60 |
Kokkos::parallel_reduce( |
| 61 |
"compute-residual", policy, |
| 62 |
KOKKOS_LAMBDA(const member_type &member, val_type &update) { |
| 63 |
const int i = member.league_rank(); |
| 64 |
const val_type one(1); |
| 65 |
auto AA = Kokkos::subview(A, i, Kokkos::ALL(), Kokkos::ALL()); |
| 66 |
auto xx = Kokkos::subview(x, i, Kokkos::ALL()); |
| 67 |
auto rr = Kokkos::subview(r, i, Kokkos::ALL()); |
| 68 |
|
| 69 |
TeamGemv<member_type, Trans::NoTranspose, Algo::Level2::Unblocked>::invoke(member, -one, AA, xx, one, rr); |
| 70 |
|
| 71 |
val_type sum(0); |
| 72 |
Kokkos::parallel_reduce( |
| 73 |
Kokkos::TeamThreadRange(member, rr.extent(0)), |
| 74 |
[&](const int &k, val_type &lsum) { lsum += Kokkos::ArithTraits<val_type>::abs(rr(k)); }, sum); |
| 75 |
Kokkos::single(Kokkos::PerTeam(member), [&]() { update += sum; }); |
| 76 |
}, |
| 77 |
residual); |
| 78 |
} |
| 79 |
return residual; |
| 80 |
} |
| 81 |
|
| 82 |
namespace ConstructBlockJacobi { |
| 83 |
template <class VT> |
| 84 |
struct Task1Factorize { |
| 85 |
private: |
| 86 |
VT __A; |
| 87 |
|
| 88 |
public: |
| 89 |
Task1Factorize(VT A) : __A(A) {} |
| 90 |
|
| 91 |
KOKKOS_INLINE_FUNCTION |
| 92 |
void operator()(const member_type &member) const { |
| 93 |
const int i = member.league_rank(); |
| 94 |
auto AA = Kokkos::subview(__A, i, Kokkos::ALL(), Kokkos::ALL()); |
| 95 |
TeamLU<member_type, Algo::Level3::Unblocked>::invoke(member, AA); |
| 96 |
} |
| 97 |
}; |
| 98 |
|
| 99 |
template <class VT> |
| 100 |
struct Task1SetIdentity { |
| 101 |
private: |
| 102 |
VT __A; |
| 103 |
|
| 104 |
public: |
| 105 |
Task1SetIdentity(VT A) : __A(A) {} |
| 106 |
|
| 107 |
KOKKOS_INLINE_FUNCTION |
| 108 |
void operator()(const member_type &member) const { |
| 109 |
const int i = member.league_rank(); |
| 110 |
auto AA = Kokkos::subview(__A, i, Kokkos::ALL(), Kokkos::ALL()); |
| 111 |
TeamSetIdentity<member_type>::invoke(member, AA); |
| 112 |
} |
| 113 |
}; |
| 114 |
|
| 115 |
template <class VTA, class VTT> |
| 116 |
struct Task1SolveLowerTriangular { |
| 117 |
private: |
| 118 |
VTA __A; |
| 119 |
VTT __T; |
| 120 |
|
| 121 |
public: |
| 122 |
Task1SolveLowerTriangular(VTA A, VTT T) : __A(A), __T(T) {} |
| 123 |
|
| 124 |
KOKKOS_INLINE_FUNCTION |
| 125 |
void operator()(const member_type &member) const { |
| 126 |
const int i = member.league_rank(); |
| 127 |
const val_type one(1); |
| 128 |
auto AA = Kokkos::subview(__A, i, Kokkos::ALL(), Kokkos::ALL()); |
| 129 |
auto TT = Kokkos::subview(__T, i, Kokkos::ALL(), Kokkos::ALL()); |
| 130 |
TeamTrsm<member_type, Side::Left, Uplo::Lower, Trans::NoTranspose, Diag::Unit, Algo::Level3::Unblocked>::invoke( |
| 131 |
member, one, TT, AA); |
| 132 |
} |
| 133 |
}; |
| 134 |
|
| 135 |
template <class VTA, class VTT> |
| 136 |
struct Task1SolveUpperTriangular { |
| 137 |
private: |
| 138 |
VTA __A; |
| 139 |
VTT __T; |
| 140 |
|
| 141 |
public: |
| 142 |
Task1SolveUpperTriangular(VTA A, VTT T) : __A(A), __T(T) {} |
| 143 |
|
| 144 |
KOKKOS_INLINE_FUNCTION |
| 145 |
void operator()(const member_type &member) const { |
| 146 |
const int i = member.league_rank(); |
| 147 |
const val_type one(1); |
| 148 |
auto AA = Kokkos::subview(__A, i, Kokkos::ALL(), Kokkos::ALL()); |
| 149 |
auto TT = Kokkos::subview(__T, i, Kokkos::ALL(), Kokkos::ALL()); |
| 150 |
TeamTrsm<member_type, Side::Left, Uplo::Upper, Trans::NoTranspose, Diag::NonUnit, Algo::Level3::Unblocked>::invoke( |
| 151 |
member, one, TT, AA); |
| 152 |
} |
| 153 |
}; |
| 154 |
} // namespace ConstructBlockJacobi |
| 155 |
|
| 156 |
template <class VTA, class VTX, class VTB> |
| 157 |
struct Task1ApplyBlockJacobi { |
| 158 |
private: |
| 159 |
VTA __A; |
| 160 |
VTX __x; |
| 161 |
VTB __b; |
| 162 |
|
| 163 |
public: |
| 164 |
Task1ApplyBlockJacobi(VTA A, VTX x, VTB b) : __A(A), __x(x), __b(b) {} |
| 165 |
|
| 166 |
KOKKOS_INLINE_FUNCTION |
| 167 |
void operator()(const member_type &member) const { |
| 168 |
const int i = member.league_rank(); |
| 169 |
const val_type one(1), zero(0); |
| 170 |
auto AA = Kokkos::subview(__A, i, Kokkos::ALL(), Kokkos::ALL()); |
| 171 |
auto xx = Kokkos::subview(__x, i, Kokkos::ALL()); |
| 172 |
auto bb = Kokkos::subview(__b, i, Kokkos::ALL()); |
| 173 |
TeamGemv<member_type, Trans::NoTranspose, Algo::Level2::Unblocked>::invoke(member, one, AA, bb, zero, xx); |
| 174 |
} |
| 175 |
}; |
| 176 |
|
| 177 |
template <class VTA, class VTT> |
| 178 |
struct Task2FactorizeInvert { |
| 179 |
private: |
| 180 |
VTA __A; |
| 181 |
VTT __T; |
| 182 |
|
| 183 |
public: |
| 184 |
Task2FactorizeInvert(VTA A, VTT T) : __A(A), __T(T) {} |
| 185 |
|
| 186 |
KOKKOS_INLINE_FUNCTION |
| 187 |
void operator()(const member_type &member) const { |
| 188 |
const val_type one(1); |
| 189 |
const int i = member.league_rank(); |
| 190 |
auto AA = Kokkos::subview(__A, i, Kokkos::ALL(), Kokkos::ALL()); |
| 191 |
auto TT = Kokkos::subview(__T, i, Kokkos::ALL(), Kokkos::ALL()); |
| 192 |
|
| 193 |
TeamLU<member_type, Algo::Level3::Unblocked>::invoke(member, AA); |
| 194 |
TeamCopy<member_type, Trans::NoTranspose>::invoke(member, AA, TT); |
| 195 |
TeamSetIdentity<member_type>::invoke(member, AA); |
| 196 |
TeamTrsm<member_type, Side::Left, Uplo::Lower, Trans::NoTranspose, Diag::Unit, Algo::Level3::Unblocked>::invoke( |
| 197 |
member, one, TT, AA); |
| 198 |
TeamTrsm<member_type, Side::Left, Uplo::Upper, Trans::NoTranspose, Diag::NonUnit, Algo::Level3::Unblocked>::invoke( |
| 199 |
member, one, TT, AA); |
| 200 |
} |
| 201 |
}; |
| 202 |
|
| 203 |
template <class VTA, class VTX, class VTB> |
| 204 |
struct Task2ApplyBlockJacobi { |
| 205 |
private: |
| 206 |
VTA __A; |
| 207 |
VTX __x; |
| 208 |
VTB __b; |
| 209 |
|
| 210 |
public: |
| 211 |
Task2ApplyBlockJacobi(VTA A, VTX x, VTB b) : __A(A), __x(x), __b(b) {} |
| 212 |
|
| 213 |
KOKKOS_INLINE_FUNCTION |
| 214 |
void operator()(const member_type &member) const { |
| 215 |
const int i = member.league_rank(); |
| 216 |
const val_type one(1), zero(0); |
| 217 |
auto AA = Kokkos::subview(__A, i, Kokkos::ALL(), Kokkos::ALL()); |
| 218 |
auto xx = Kokkos::subview(__x, i, Kokkos::ALL()); |
| 219 |
auto bb = Kokkos::subview(__b, i, Kokkos::ALL()); |
| 220 |
TeamGemv<member_type, Trans::NoTranspose, Algo::Level2::Unblocked>::invoke(member, one, AA, bb, zero, xx); |
| 221 |
} |
| 222 |
}; |
| 223 |
|
| 224 |
int main(int argc, char *argv[]) { |
| 225 |
Kokkos::initialize(argc, argv); |
| 226 |
{ |
| 227 |
#if defined(KOKKOS_ENABLE_CUDA) |
| 228 |
cudaProfilerStop(); |
| 229 |
#endif |
| 230 |
Kokkos::print_configuration(std::cout); |
| 231 |
|
| 232 |
Kokkos::Timer timer; |
| 233 |
|
| 234 |
/// |
| 235 |
/// input arguments parsing |
| 236 |
/// |
| 237 |
int N = 128 * 128; /// # of problems (batch size) |
| 238 |
int Blk = 5; /// block dimension |
| 239 |
for (int i = 1; i < argc; ++i) { |
| 240 |
const std::string &token = argv[i]; |
| 241 |
if (token == std::string("-N")) N = std::atoi(argv[++i]); |
| 242 |
if (token == std::string("-B")) Blk = std::atoi(argv[++i]); |
| 243 |
} |
| 244 |
printf(" :::: Testing (N = %d, Blk = %d)\n", N, Blk); |
| 245 |
|
| 246 |
/// |
| 247 |
/// Problem container: rank-3 array |
| 248 |
/// |
| 249 |
/// A - multiple block matrices representing block diagonals |
| 250 |
/// T - temporal block matrices to store its LU factors |
| 251 |
/// x - solution vector |
| 252 |
/// b - right hand side vector |
| 253 |
/// |
| 254 |
Kokkos::View<val_type ***, Kokkos::LayoutRight, exec_space_type> A("block diagonals", N, Blk, Blk); |
| 255 |
Kokkos::View<val_type ***, Kokkos::LayoutRight, exec_space_type> T("temporal block diagonals", N, Blk, Blk); |
| 256 |
Kokkos::View<val_type **, Kokkos::LayoutRight, exec_space_type> x("x", N, Blk); |
| 257 |
Kokkos::View<val_type **, Kokkos::LayoutRight, exec_space_type> b("b", N, Blk); |
| 258 |
|
| 259 |
/// copy of A to check residual |
| 260 |
Kokkos::View<val_type ***, Kokkos::LayoutRight, exec_space_type> Acopy("Acopy", A.extent(0), A.extent(1), |
| 261 |
A.extent(2)); |
| 262 |
|
| 263 |
/// residual vector |
| 264 |
Kokkos::View<val_type **, Kokkos::LayoutRight, exec_space_type> r("r", b.extent(0), b.extent(1)); |
| 265 |
|
| 266 |
/// The block diagonal matrices are assumed to be extracted from a block |
| 267 |
/// sparse matrix. Here we set the blocks with random values |
| 268 |
Kokkos::Random_XorShift64_Pool<exec_space_type> random(13245); |
| 269 |
Kokkos::fill_random(A, random, val_type(1.0)); |
| 270 |
Kokkos::fill_random(b, random, val_type(1.0)); |
| 271 |
|
| 272 |
Kokkos::deep_copy(Acopy, A); |
| 273 |
|
| 274 |
/// |
| 275 |
/// Objective : |
| 276 |
/// - Construct the inverse of A(i,:,:) for all i. |
| 277 |
/// - Solve the equation using matrix vector multiplication. |
| 278 |
|
| 279 |
/// Task 1. Use the so-called standard batch interface |
| 280 |
/// parallel_for(factorize) |
| 281 |
/// parallel_For(set identity matrix) |
| 282 |
/// parallel_for(solve lower triangular) |
| 283 |
/// parallel_for(solve upper triangular) |
| 284 |
/// parallel_for(matrix vector multiplication) |
| 285 |
|
| 286 |
{ |
| 287 |
#if defined(KOKKOS_ENABLE_CUDA) |
| 288 |
cudaProfilerStart(); |
| 289 |
#endif |
| 290 |
Kokkos::deep_copy(A, Acopy); |
| 291 |
|
| 292 |
/// construction of block jacobi using batched blas interface |
| 293 |
/// each parallel for is a batch function |
| 294 |
{ |
| 295 |
policy_type policy(A.extent(0), Kokkos::AUTO()); |
| 296 |
timer.reset(); |
| 297 |
Kokkos::parallel_for("task1.factorize", policy, ConstructBlockJacobi::Task1Factorize<decltype(A)>(A)); |
| 298 |
Kokkos::deep_copy(T, A); |
| 299 |
Kokkos::parallel_for("task1.set-identity", policy, ConstructBlockJacobi::Task1SetIdentity<decltype(A)>(A)); |
| 300 |
Kokkos::fence(); |
| 301 |
Kokkos::parallel_for("task1.solve-lower-triangular", policy, |
| 302 |
ConstructBlockJacobi::Task1SolveLowerTriangular<decltype(A), decltype(T)>(A, T)); |
| 303 |
Kokkos::fence(); |
| 304 |
Kokkos::parallel_for("task1.solve-upper-triangular", policy, |
| 305 |
ConstructBlockJacobi::Task1SolveUpperTriangular<decltype(A), decltype(T)>(A, T)); |
| 306 |
Kokkos::fence(); |
| 307 |
const double t = timer.seconds(); |
| 308 |
printf( |
| 309 |
"task 1: construction of jacobi time = %f , # of constructions per " |
| 310 |
"min = %.0f \n", |
| 311 |
t, 1.0 / t * 60); |
| 312 |
} |
| 313 |
|
| 314 |
/// apply block jacobi |
| 315 |
{ |
| 316 |
timer.reset(); |
| 317 |
policy_type policy(A.extent(0), Kokkos::AUTO()); |
| 318 |
Kokkos::parallel_for("task1.apply-block-jacobi", policy, |
| 319 |
Task1ApplyBlockJacobi<decltype(A), decltype(x), decltype(b)>(A, x, b)); |
| 320 |
const double t = timer.seconds(); |
| 321 |
printf( |
| 322 |
"task 1: application of jacobi time = %f , # of applications per " |
| 323 |
"min = %.0f \n", |
| 324 |
t, 1.0 / t * 60); |
| 325 |
} |
| 326 |
|
| 327 |
/// check residual |
| 328 |
{ |
| 329 |
const double residual = computeResidual(Acopy, x, b, r); |
| 330 |
printf("task 1: residual = %f\n", residual); |
| 331 |
} |
| 332 |
#if defined(KOKKOS_ENABLE_CUDA) |
| 333 |
cudaProfilerStop(); |
| 334 |
#endif |
| 335 |
} |
| 336 |
|
| 337 |
/// Task 2. Compose a new batch function using kokkos batched team-level |
| 338 |
/// interface |
| 339 |
/// parallel_for(LU, set identity, solve lower/upper triangular) |
| 340 |
/// parallel_for(matrix vector multiplication) |
| 341 |
|
| 342 |
{ |
| 343 |
#if defined(KOKKOS_ENABLE_CUDA) |
| 344 |
cudaProfilerStart(); |
| 345 |
#endif |
| 346 |
Kokkos::deep_copy(A, Acopy); |
| 347 |
|
| 348 |
/// construction of block jacobi using batched blas interface |
| 349 |
/// each parallel for is a batch function |
| 350 |
{ |
| 351 |
policy_type policy(A.extent(0), Kokkos::AUTO()); |
| 352 |
timer.reset(); |
| 353 |
Kokkos::parallel_for("task2.factorize-invert", policy, Task2FactorizeInvert<decltype(A), decltype(T)>(A, T)); |
| 354 |
Kokkos::fence(); |
| 355 |
const double t = timer.seconds(); |
| 356 |
printf( |
| 357 |
"task 2: construction of jacobi time = %f , # of constructions per " |
| 358 |
"min = %.0f \n", |
| 359 |
t, 1.0 / t * 60); |
| 360 |
} |
| 361 |
|
| 362 |
/// apply block jacobi |
| 363 |
{ |
| 364 |
timer.reset(); |
| 365 |
policy_type policy(A.extent(0), Kokkos::AUTO()); |
| 366 |
Kokkos::parallel_for("task2.apply-block-jacobi", policy, |
| 367 |
Task2ApplyBlockJacobi<decltype(A), decltype(x), decltype(b)>(A, x, b)); |
| 368 |
const double t = timer.seconds(); |
| 369 |
printf( |
| 370 |
"task 2: application of jacobi time = %f , # of applications per " |
| 371 |
"min = %.0f \n", |
| 372 |
t, 1.0 / t * 60); |
| 373 |
} |
| 374 |
|
| 375 |
/// check residual |
| 376 |
{ |
| 377 |
const double residual = computeResidual(Acopy, x, b, r); |
| 378 |
printf("task 2: residual = %f\n", residual); |
| 379 |
} |
| 380 |
#if defined(KOKKOS_ENABLE_CUDA) |
| 381 |
cudaProfilerStop(); |
| 382 |
#endif |
| 383 |
} |
| 384 |
} |
| 385 |
Kokkos::finalize(); |
| 386 |
|
| 387 |
return 0; |
| 388 |
} |