Debian Codegraph @

packages/kokkos-kernels/perf_test/batched/dense/do-not-use/KokkosBatched_Test_LU_Cuda.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 /// \author Kyungjoo Kim ([email protected])
17
18 #include "Kokkos_Core.hpp"
19 #include "Kokkos_Timer.hpp"
20
21 #if defined(KOKKOS_ENABLE_CUDA)
22
23 #include <iomanip>
24
25 #if defined(__KOKKOSKERNELS_NVIDIA_CUBLAS__)
26 #include "cuda_runtime.h"
27 #include "cublas_v2.h"
28 #include "cublas_api.h"
29 #endif
30
31 #include "KokkosBatched_Vector.hpp"
32
33 #include "KokkosBatched_Copy_Decl.hpp"
34 #include "KokkosBatched_Copy_Impl.hpp"
35
36 #include "KokkosBatched_LU_Decl.hpp"
37 #include "KokkosBatched_LU_Serial_Impl.hpp"
38 #include "KokkosBatched_LU_Team_Impl.hpp"
39
40 namespace KokkosBatched {
41 namespace PerfTest {
42
43 #define FLOP_MUL 1.0
44 #define FLOP_ADD 1.0
45 typedef double value_type;
46
47 double FlopCount(int mm, int nn) {
48 double m = (double)mm;
49 double n = (double)nn;
50 if (m > n)
51 return (FLOP_MUL * (0.5 * m * n * n - (1.0 / 6.0) * n * n * n + 0.5 * m * n - 0.5 * n * n + (2.0 / 3.0) * n) +
52 FLOP_ADD * (0.5 * m * n * n - (1.0 / 6.0) * n * n * n - 0.5 * m * n + (1.0 / 6.0) * n));
53 else
54 return (FLOP_MUL * (0.5 * n * m * m - (1.0 / 6.0) * m * m * m + 0.5 * n * m - 0.5 * m * m + (2.0 / 3.0) * m) +
55 FLOP_ADD * (0.5 * n * m * m - (1.0 / 6.0) * m * m * m - 0.5 * n * m + (1.0 / 6.0) * m));
56 }
57
58 struct RangeTag {};
59 struct TeamTagV1 {};
60 struct TeamTagV2 {};
61 struct TeamTagV3 {};
62 struct TeamTagHandmade {};
63
64 template <typename ViewType, typename AlgoTagType, int VectorLength = 0>
65 struct Functor {
66 UnmanagedViewType<ViewType> _a;
67
68 KOKKOS_INLINE_FUNCTION
69 Functor() = default;
70
71 KOKKOS_INLINE_FUNCTION
72 Functor(const ViewType &a) : _a(a) {}
73
74 KOKKOS_INLINE_FUNCTION
75 void operator()(const RangeTag &, const int k) const {
76 auto aa = Kokkos::subview(_a, k, Kokkos::ALL(), Kokkos::ALL());
77 SerialLU<AlgoTagType>::invoke(aa);
78 }
79
80 template <typename MemberType>
81 KOKKOS_INLINE_FUNCTION void operator()(const TeamTagV1 &, const MemberType &member) const {
82 const int kbeg = (member.league_rank() * (member.team_size() * VectorLength) + member.team_rank() * VectorLength);
83 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, VectorLength), [&](const int &k) {
84 const int kk = kbeg + k;
85 if (kk < _a.extent_int(0)) {
86 auto aa = Kokkos::subview(_a, kk, Kokkos::ALL(), Kokkos::ALL());
87 SerialLU<AlgoTagType>::invoke(aa);
88 }
89 });
90 }
91
92 template <typename MemberType>
93 KOKKOS_INLINE_FUNCTION void operator()(const TeamTagV2 &, const MemberType &member) const {
94 const int kbeg = member.league_rank() * VectorLength;
95 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, VectorLength), [&](const int &k) {
96 const int kk = kbeg + k;
97 if (kk < _a.extent_int(0)) {
98 auto aa = Kokkos::subview(_a, kk, Kokkos::ALL(), Kokkos::ALL());
99 TeamLU<MemberType, AlgoTagType>::invoke(member, aa);
100 }
101 });
102 }
103
104 template <typename MemberType>
105 KOKKOS_INLINE_FUNCTION void operator()(const TeamTagV3 &, const MemberType &member) const {
106 const int lvl = 0;
107 ScratchViewType<ViewType> sa(member.team_scratch(lvl), VectorLength, _a.extent(1), _a.extent(2));
108
109 const int kbeg = member.league_rank() * VectorLength;
110 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, VectorLength), [&](const int &k) {
111 const int kk = kbeg + k;
112 if (kk < _a.extent_int(0)) {
113 auto aa = Kokkos::subview(_a, kk, Kokkos::ALL(), Kokkos::ALL());
114 auto saa = Kokkos::subview(sa, k, Kokkos::ALL(), Kokkos::ALL());
115
116 TeamCopy<MemberType, Trans::NoTranspose>::invoke(member, aa, saa);
117 member.team_barrier();
118 TeamLU<MemberType, AlgoTagType>::invoke(member, saa);
119 member.team_barrier();
120 TeamCopy<MemberType, Trans::NoTranspose>::invoke(member, saa, aa);
121 }
122 });
123 }
124 };
125
126 template <typename DeviceSpaceType, typename AlgoTagType>
127 void LU(const int NN, const int BlkSize) {
128 typedef Kokkos::Schedule<Kokkos::Static> ScheduleType;
129
130 constexpr int VectorLength = DefaultVectorLength<value_type, typename DeviceSpaceType::memory_space>::value;
131 const int N = NN / VectorLength;
132
133 {
134 std::string value_type_name;
135 if (std::is_same<value_type, double>::value) value_type_name = "double";
136 if (std::is_same<value_type, Kokkos::complex<double> >::value) value_type_name = "Kokkos::complex<double>";
137
138 std::cout << "SIMD is defined: datatype " << value_type_name << " a vector length " << VectorLength << "\n";
139 }
140
141 const double flop = (N * VectorLength) * FlopCount(BlkSize, BlkSize);
142 const double tmax = 1.0e15;
143
144 typedef Kokkos::DefaultHostExecutionSpace HostSpaceType;
145 typedef typename DeviceSpaceType::memory_space DeviceMemorySpaceType;
146
147 const int iter_begin = -3, iter_end = 50;
148 Kokkos::Timer timer;
149
150 Kokkos::View<value_type ***, Kokkos::LayoutLeft, HostSpaceType> amat("amat", N * VectorLength, BlkSize, BlkSize),
151 aref("aref", N * VectorLength, BlkSize, BlkSize);
152
153 {
154 Random<value_type> random;
155 for (int k = 0; k < N * VectorLength; ++k) {
156 // use tridiagonal matrices; for now we just check elementwise l/u factors
157 // do not allow pivots
158 for (int i = 0; i < BlkSize; ++i) {
159 amat(k, i, i) = random.value() + 10.0;
160 if ((i + 1) < BlkSize) {
161 amat(k, i, i + 1) = random.value() + 1.0;
162 amat(k, i + 1, i) = random.value() + 1.0;
163 }
164 }
165
166 // value_type d[BlkSize], v[BlkSize][BlkSize];
167 // for (int i=0;i<BlkSize;++i) {
168 // d[i] = random.value() + 1.0; // positive value
169 // for (int j=0;j<BlkSize;++j)
170 // v[i][j] = random.value();
171 // }
172 // for (int i=0;i<BlkSize;++i)
173 // for (int j=0;j<BlkSize;++j)
174 // for (int l=0;l<BlkSize;++l)
175 // amat(k, i, j) = v[i][l]*d[l]*v[l][j];
176 }
177 }
178
179 constexpr size_t LLC_CAPACITY = 56 * 4 * 1024 * 1024;
180 Flush<LLC_CAPACITY> flush;
181
182 #if defined(__KOKKOSKERNELS_NVIDIA_CUBLAS__)
183 if (1) {
184 ///
185 /// CUBLAS Batch version
186 ///
187 const Kokkos::LayoutStride stride(N * VectorLength, BlkSize * BlkSize, BlkSize, 1, BlkSize, BlkSize);
188
189 Kokkos::View<value_type ***, Kokkos::LayoutStride, DeviceSpaceType> a("a", stride);
190 Kokkos::View<int *, DeviceSpaceType> info("info", N * VectorLength);
191
192 cublasStatus_t stat;
193 cublasHandle_t handle;
194
195 stat = cublasCreate(&handle);
196 if (stat != CUBLAS_STATUS_SUCCESS) Kokkos::abort("CUBLAS initialization failed\n");
197
198 auto amat_device = Kokkos::create_mirror_view(typename DeviceSpaceType::memory_space(), amat);
199 Kokkos::deep_copy(amat_device, amat);
200
201 Kokkos::fence();
202 {
203 double tavg = 0, tmin = tmax;
204 value_type *aa[N * VectorLength];
205
206 for (int k = 0; k < N * VectorLength; ++k) {
207 aa[k] = a.data() + k * a.stride_0();
208 }
209 value_type **aa_device;
210 if (cudaMalloc(&aa_device, N * VectorLength * sizeof(value_type *)) != cudaSuccess) {
211 Kokkos::abort("CUDA memory allocation failed\n");
212 }
213 if (cudaMemcpy(aa_device, aa, sizeof(value_type *) * N * VectorLength, cudaMemcpyHostToDevice) != cudaSuccess) {
214 Kokkos::abort("CUDA memcpy failed\n");
215 }
216 Kokkos::fence();
217 for (int iter = iter_begin; iter < iter_end; ++iter) {
218 // flush
219 flush.run();
220
221 // initialize matrix
222 Kokkos::deep_copy(a, amat_device);
223
224 Kokkos::fence();
225 timer.reset();
226
227 stat = cublasDgetrfBatched(handle, BlkSize, (value_type **)aa_device, BlkSize, NULL, (int *)info.data(),
228 N * VectorLength);
229 if (stat != CUBLAS_STATUS_SUCCESS) {
230 Kokkos::abort("CUBLAS LU Batched failed\n");
231 }
232
233 Kokkos::fence();
234 const double t = timer.seconds();
235 tmin = std::min(tmin, t);
236 tavg += (iter >= 0) * t;
237 }
238 tavg /= iter_end;
239
240 auto asol = Kokkos::create_mirror_view(typename HostSpaceType::memory_space(), a);
241 Kokkos::deep_copy(asol, a);
242 Kokkos::deep_copy(aref, asol);
243
244 if (cudaFree(aa_device) != cudaSuccess) {
245 Kokkos::abort("CUDA memory free failed\n");
246 }
247
248 std::cout << std::setw(8) << "CUBLAS" << std::setw(8) << "Batch"
249 << " BlkSize = " << std::setw(3) << BlkSize << " TeamSize = N/A"
250 << " ScratchSize (KB) = N/A"
251 << " time = " << std::scientific << tmin << " avg flop/s = " << (flop / tavg)
252 << " max flop/s = " << (flop / tmin) << std::endl;
253 }
254 }
255 #endif
256
257 if (1) {
258 ///
259 /// Range policy version
260 ///
261 typedef Kokkos::View<value_type ***, DeviceSpaceType> view_type;
262 view_type a("a", N * VectorLength, BlkSize, BlkSize);
263
264 double tavg = 0, tmin = tmax;
265 {
266 typedef Functor<view_type, AlgoTagType> functor_type;
267 const Kokkos::RangePolicy<DeviceSpaceType, ScheduleType, RangeTag> policy(0, N * VectorLength);
268
269 for (int iter = iter_begin; iter < iter_end; ++iter) {
270 // flush
271 flush.run();
272
273 // initialize matrix
274 Kokkos::deep_copy(a, amat);
275
276 Kokkos::fence();
277 timer.reset();
278
279 Kokkos::parallel_for("KokkosBatched::PerfTest::LUCuda::RangeTag", policy, functor_type(a));
280
281 Kokkos::fence();
282 const double t = timer.seconds();
283 tmin = std::min(tmin, t);
284 tavg += (iter >= 0) * t;
285 }
286 tavg /= iter_end;
287
288 auto asol = Kokkos::create_mirror_view(typename HostSpaceType::memory_space(), a);
289 Kokkos::deep_copy(asol, a);
290
291 double diff = 0;
292 for (int i = 0, iend = aref.extent(0); i < iend; ++i)
293 for (int j = 0, jend = aref.extent(1); j < jend; ++j)
294 for (int k = 0, kend = aref.extent(2); k < kend; ++k)
295 diff += Kokkos::ArithTraits<value_type>::abs(aref(i, j, k) - asol(i, j, k));
296
297 std::cout << std::setw(8) << "Kokkos" << std::setw(8) << "Range"
298 << " BlkSize = " << std::setw(3) << BlkSize << " TeamSize = N/A"
299 << " ScratchSize (KB) = 0"
300 << " time = " << std::scientific << tmin << " avg flop/s = " << (flop / tavg)
301 << " max flop/s = " << (flop / tmin);
302 #if defined(__KOKKOSKERNELS_NVIDIA_CUBLAS__)
303 std::cout << " diff to ref = " << diff;
304 #endif
305 std::cout << std::endl;
306 }
307 }
308 if (1) {
309 ///
310 /// Team V1
311 ///
312 typedef Kokkos::View<value_type ***, DeviceSpaceType> view_type;
313 view_type a("a", N * VectorLength, BlkSize, BlkSize);
314
315 double tavg = 0, tmin = tmax;
316 {
317 typedef Kokkos::TeamPolicy<DeviceSpaceType, ScheduleType, TeamTagV1> policy_type;
318 typedef Functor<view_type, AlgoTagType, VectorLength> functor_type;
319
320 const int team_size = policy_type(N / 32, Kokkos::AUTO, VectorLength)
321 .team_size_recommended(functor_type(), Kokkos::ParallelForTag());
322
323 const policy_type policy(N / team_size, team_size, VectorLength);
324 for (int iter = iter_begin; iter < iter_end; ++iter) {
325 // flush
326 flush.run();
327
328 // initialize matrix
329 Kokkos::deep_copy(a, amat);
330
331 Kokkos::fence();
332 timer.reset();
333
334 Kokkos::parallel_for("KokkosBatched::PerfTest::LUCuda::TeamTagV1", policy, functor_type(a));
335
336 Kokkos::fence();
337 const double t = timer.seconds();
338 tmin = std::min(tmin, t);
339 tavg += (iter >= 0) * t;
340 }
341 tavg /= iter_end;
342
343 auto asol = Kokkos::create_mirror_view(typename HostSpaceType::memory_space(), a);
344 Kokkos::deep_copy(asol, a);
345
346 double diff = 0;
347 for (int i = 0, iend = aref.extent(0); i < iend; ++i)
348 for (int j = 0, jend = aref.extent(1); j < jend; ++j)
349 for (int k = 0, kend = aref.extent(2); k < kend; ++k)
350 diff += Kokkos::ArithTraits<value_type>::abs(aref(i, j, k) - asol(i, j, k));
351
352 std::cout << std::setw(8) << "Kokkos" << std::setw(8) << "Team V1"
353 << " BlkSize = " << std::setw(3) << BlkSize << " TeamSize = " << std::setw(3) << team_size
354 << " ScratchSize (KB) = 0"
355 << " time = " << std::scientific << tmin << " avg flop/s = " << (flop / tavg)
356 << " max flop/s = " << (flop / tmin);
357 #if defined(__KOKKOSKERNELS_NVIDIA_CUBLAS__)
358 std::cout << " diff to ref = " << diff;
359 #endif
360 std::cout << std::endl;
361 }
362 }
363 if (1) {
364 ///
365 /// Team V2
366 ///
367 typedef Kokkos::View<value_type ***, DeviceSpaceType> view_type;
368 view_type a("a", N * VectorLength, BlkSize, BlkSize);
369
370 double tavg = 0, tmin = tmax;
371 {
372 typedef Kokkos::TeamPolicy<DeviceSpaceType, ScheduleType, TeamTagV2> policy_type;
373 typedef Functor<view_type, AlgoTagType, VectorLength> functor_type;
374
375 const int is_blocked_algo = (std::is_same<AlgoTagType, Algo::LU::Blocked>::value),
376 mb = Algo::LU::Blocked::mb<DeviceMemorySpaceType>();
377 // mp = BlkSize%mb > 0;
378
379 const int
380 // mblk = is_blocked_algo ? (BlkSize/mb + mp) : BlkSize;
381 mblk = is_blocked_algo ? (BlkSize - mb) : (BlkSize - 1);
382
383 const int max_team_size =
384 policy_type(N, Kokkos::AUTO, VectorLength).team_size_max(functor_type(), Kokkos::ParallelForTag());
385 const int team_size = std::min(std::max(mblk * 2, 1), max_team_size);
386
387 const policy_type policy(N, team_size, VectorLength);
388 for (int iter = iter_begin; iter < iter_end; ++iter) {
389 // flush
390 flush.run();
391
392 // initialize matrix
393 Kokkos::deep_copy(a, amat);
394
395 Kokkos::fence();
396 timer.reset();
397
398 Kokkos::parallel_for("KokkosBatched::PerfTest::LUCuda::TeamTagV2", policy, functor_type(a));
399
400 Kokkos::fence();
401 const double t = timer.seconds();
402 tmin = std::min(tmin, t);
403 tavg += (iter >= 0) * t;
404 }
405 tavg /= iter_end;
406
407 auto asol = Kokkos::create_mirror_view(typename HostSpaceType::memory_space(), a);
408 Kokkos::deep_copy(asol, a);
409
410 double diff = 0;
411 for (int i = 0, iend = aref.extent(0); i < iend; ++i)
412 for (int j = 0, jend = aref.extent(1); j < jend; ++j)
413 for (int k = 0, kend = aref.extent(2); k < kend; ++k)
414 diff += Kokkos::ArithTraits<value_type>::abs(aref(i, j, k) - asol(i, j, k));
415
416 std::cout << std::setw(8) << "Kokkos" << std::setw(8) << "Team V2"
417 << " BlkSize = " << std::setw(3) << BlkSize << " TeamSize = " << std::setw(3) << team_size
418 << " ScratchSize (KB) = 0"
419 << " time = " << std::scientific << tmin << " avg flop/s = " << (flop / tavg)
420 << " max flop/s = " << (flop / tmin);
421 #if defined(__KOKKOSKERNELS_NVIDIA_CUBLAS__)
422 std::cout << " diff to ref = " << diff;
423 #endif
424 std::cout << std::endl;
425 }
426 }
427 if (1) {
428 ///
429 /// Team V3
430 ///
431 typedef Kokkos::View<value_type ***, DeviceSpaceType> view_type;
432 view_type a("a", N * VectorLength, BlkSize, BlkSize);
433
434 double tavg = 0, tmin = tmax;
435 {
436 typedef Kokkos::TeamPolicy<DeviceSpaceType, ScheduleType, TeamTagV3> policy_type;
437 typedef Functor<view_type, AlgoTagType, VectorLength> functor_type;
438
439 const int lvl = 0, per_team_scratch = ScratchViewType<view_type>::shmem_size(VectorLength, BlkSize, BlkSize);
440 if (per_team_scratch / 1024 < 48) {
441 const int is_blocked_algo = (std::is_same<AlgoTagType, Algo::LU::Blocked>::value),
442 mb = Algo::LU::Blocked::mb<DeviceMemorySpaceType>();
443 // mp = BlkSize%mb > 0;
444
445 const int
446 // mblk = is_blocked_algo ? (BlkSize/mb + mp) : BlkSize;
447 mblk = is_blocked_algo ? (BlkSize - mb) : (BlkSize - 1);
448
449 const int max_team_size = policy_type(N, Kokkos::AUTO, VectorLength)
450 .set_scratch_size(lvl, Kokkos::PerTeam(per_team_scratch))
451 .team_size_max(functor_type(), Kokkos::ParallelForTag());
452 const int team_size = std::min(std::max(mblk * 2, 1), max_team_size);
453
454 policy_type policy(N, team_size, VectorLength);
455 for (int iter = iter_begin; iter < iter_end; ++iter) {
456 // flush
457 flush.run();
458
459 // initialize matrix
460 Kokkos::deep_copy(a, amat);
461
462 Kokkos::fence();
463 timer.reset();
464
465 Kokkos::parallel_for("KokkosBatched::PerfTest::LUCuda::TeamTagV3",
466 policy.set_scratch_size(lvl, Kokkos::PerTeam(per_team_scratch)), functor_type(a));
467
468 Kokkos::fence();
469 const double t = timer.seconds();
470 tmin = std::min(tmin, t);
471 tavg += (iter >= 0) * t;
472 }
473 tavg /= iter_end;
474
475 auto asol = Kokkos::create_mirror_view(typename HostSpaceType::memory_space(), a);
476 Kokkos::deep_copy(asol, a);
477
478 double diff = 0;
479 for (int i = 0, iend = aref.extent(0); i < iend; ++i)
480 for (int j = 0, jend = aref.extent(1); j < jend; ++j)
481 for (int k = 0, kend = aref.extent(2); k < kend; ++k)
482 diff += Kokkos::ArithTraits<value_type>::abs(aref(i, j, k) - asol(i, j, k));
483
484 std::cout << std::setw(8) << "Kokkos" << std::setw(8) << "Team V3"
485 << " BlkSize = " << std::setw(3) << BlkSize << " TeamSize = " << std::setw(3) << team_size
486 << " ScratchSize (KB) = " << std::setw(3) << (per_team_scratch / 1024)
487 << " time = " << std::scientific << tmin << " avg flop/s = " << (flop / tavg)
488 << " max flop/s = " << (flop / tmin);
489 #if defined(__KOKKOSKERNELS_NVIDIA_CUBLAS__)
490 std::cout << " diff to ref = " << diff;
491 #endif
492 std::cout << std::endl;
493 } else {
494 std::cout << std::setw(8) << "Kokkos" << std::setw(8) << "Team V3"
495 << " Scratch per team is too big (KB): " << (per_team_scratch / 1024) << std::endl;
496 }
497 }
498 }
499 std::cout << "\n\n";
500 }
501 } // namespace PerfTest
502 } // namespace KokkosBatched
503
504 using namespace KokkosBatched;
505
506 template <typename AlgoTagType>
507 void run(const int N, const int B) {
508 typedef Kokkos::DefaultExecutionSpace ExecSpace;
509
510 Kokkos::print_configuration(std::cout, false);
511
512 if (B != 0) {
513 PerfTest::LU<ExecSpace, AlgoTagType>(N, B);
514 } else {
515 PerfTest::LU<ExecSpace, AlgoTagType>(N, 3);
516 PerfTest::LU<ExecSpace, AlgoTagType>(N, 5);
517 PerfTest::LU<ExecSpace, AlgoTagType>(N, 10);
518 PerfTest::LU<ExecSpace, AlgoTagType>(N, 15);
519 }
520 }
521
522 int main(int argc, char *argv[]) {
523 Kokkos::initialize(argc, argv);
524
525 int N = 128 * 128, B = 0;
526
527 for (int i = 1; i < argc; ++i) {
528 const std::string &token = argv[i];
529 if (token == std::string("-N")) N = std::atoi(argv[++i]);
530 if (token == std::string("-B")) B = std::atoi(argv[++i]);
531 }
532
533 {
534 std::cout << " N = " << N << std::endl;
535
536 std::cout << "\n Testing Algo::LU::Unblocked\n";
537 run<Algo::LU::Unblocked>(N, B);
538
539 std::cout << "\n Testing LayoutLeft Algo::LU::Blocked\n";
540 run<Algo::LU::Blocked>(N, B);
541 }
542
543 Kokkos::finalize();
544
545 return 0;
546 }
547
548 #else
549
550 int main(int argc, char *argv[]) {
551 std::cout << "Kokkos::Cuda is not enabled\n";
552 return -1;
553 }
554 #endif