Debian Codegraph @

packages/kokkos-kernels/perf_test/batched/dense/do-not-use/KokkosBatched_Test_Trsm_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 #include "KokkosBatched_Util.hpp"
26 #if defined(__KOKKOSKERNELS_NVIDIA_CUBLAS__)
27 #include "cuda_runtime.h"
28 #include "cublas_v2.h"
29 #include "cublas_api.h"
30 #endif
31
32 #include "KokkosBatched_Vector.hpp"
33
34 #include "KokkosBatched_Copy_Decl.hpp"
35 #include "KokkosBatched_Copy_Impl.hpp"
36
37 #include "KokkosBatched_Trsm_Decl.hpp"
38 #include "KokkosBatched_Trsm_Serial_Impl.hpp"
39 #include "KokkosBatched_Trsm_Team_Impl.hpp"
40
41 namespace KokkosBatched {
42 namespace PerfTest {
43
44 #undef FLOP_MUL
45 #undef FLOP_ADD
46 #define FLOP_MUL 1.0
47 #define FLOP_ADD 1.0
48 typedef double value_type;
49
50 double FlopCountLower(int mm, int nn) {
51 double m = (double)mm;
52 double n = (double)nn;
53 return (FLOP_MUL * (0.5 * m * n * (n + 1.0)) + FLOP_ADD * (0.5 * m * n * (n - 1.0)));
54 }
55
56 double FlopCountUpper(int mm, int nn) {
57 double m = (double)mm;
58 double n = (double)nn;
59 return (FLOP_MUL * (0.5 * m * n * (n + 1.0)) + FLOP_ADD * (0.5 * m * n * (n - 1.0)));
60 }
61
62 struct RangeTag {};
63 struct TeamTagV1 {};
64 struct TeamTagV2 {};
65 struct TeamTagV3 {};
66 struct TeamTagHandmade {};
67
68 template <int test, typename ViewType, typename AlgoTagType, int VectorLength = 0>
69 struct Functor {
70 ConstUnmanagedViewType<ViewType> _a;
71 UnmanagedViewType<ViewType> _b;
72
73 KOKKOS_INLINE_FUNCTION
74 Functor() = default;
75
76 KOKKOS_INLINE_FUNCTION
77 Functor(const ViewType &a, const ViewType &b) : _a(a), _b(b) {}
78
79 KOKKOS_INLINE_FUNCTION
80 void operator()(const RangeTag &, const int k) const {
81 auto aa = Kokkos::subview(_a, k, Kokkos::ALL(), Kokkos::ALL());
82 auto bb = Kokkos::subview(_b, k, Kokkos::ALL(), Kokkos::ALL());
83
84 switch (test) {
85 case 0:
86 SerialTrsm<Side::Left, Uplo::Lower, Trans::NoTranspose, Diag::Unit, AlgoTagType>::invoke(1.0, aa, bb);
87 break;
88 case 1:
89 SerialTrsm<Side::Left, Uplo::Lower, Trans::NoTranspose, Diag::NonUnit, AlgoTagType>::invoke(1.0, aa, bb);
90 break;
91 case 2:
92 SerialTrsm<Side::Right, Uplo::Upper, Trans::NoTranspose, Diag::Unit, AlgoTagType>::invoke(1.0, aa, bb);
93 break;
94 case 3:
95 SerialTrsm<Side::Right, Uplo::Upper, Trans::NoTranspose, Diag::NonUnit, AlgoTagType>::invoke(1.0, aa, bb);
96 break;
97 case 4:
98 SerialTrsm<Side::Left, Uplo::Upper, Trans::NoTranspose, Diag::NonUnit, AlgoTagType>::invoke(1.0, aa, bb);
99 break;
100 }
101 }
102
103 template <typename MemberType>
104 KOKKOS_INLINE_FUNCTION void operator()(const TeamTagV1 &, const MemberType &member) const {
105 const int kbeg = (member.league_rank() * (member.team_size() * VectorLength) + member.team_rank() * VectorLength);
106 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, VectorLength), [&](const int &k) {
107 const int kk = kbeg + k;
108 if (kk < int(_b.extent(0))) {
109 auto aa = Kokkos::subview(_a, kk, Kokkos::ALL(), Kokkos::ALL());
110 auto bb = Kokkos::subview(_b, kk, Kokkos::ALL(), Kokkos::ALL());
111
112 switch (test) {
113 case 0:
114 SerialTrsm<Side::Left, Uplo::Lower, Trans::NoTranspose, Diag::Unit, AlgoTagType>::invoke(1.0, aa, bb);
115 break;
116 case 1:
117 SerialTrsm<Side::Left, Uplo::Lower, Trans::NoTranspose, Diag::NonUnit, AlgoTagType>::invoke(1.0, aa, bb);
118 break;
119 case 2:
120 SerialTrsm<Side::Right, Uplo::Upper, Trans::NoTranspose, Diag::Unit, AlgoTagType>::invoke(1.0, aa, bb);
121 break;
122 case 3:
123 SerialTrsm<Side::Right, Uplo::Upper, Trans::NoTranspose, Diag::NonUnit, AlgoTagType>::invoke(1.0, aa, bb);
124 break;
125 case 4:
126 SerialTrsm<Side::Left, Uplo::Upper, Trans::NoTranspose, Diag::NonUnit, AlgoTagType>::invoke(1.0, aa, bb);
127 break;
128 }
129 }
130 });
131 }
132
133 template <typename MemberType>
134 KOKKOS_INLINE_FUNCTION void operator()(const TeamTagV2 &, const MemberType &member) const {
135 const int kbeg = member.league_rank() * VectorLength;
136 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, VectorLength), [&](const int &k) {
137 const int kk = kbeg + k;
138 if (kk < int(_b.extent(0))) {
139 auto aa = Kokkos::subview(_a, kk, Kokkos::ALL(), Kokkos::ALL());
140 auto bb = Kokkos::subview(_b, kk, Kokkos::ALL(), Kokkos::ALL());
141
142 switch (test) {
143 case 0:
144 TeamTrsm<MemberType, Side::Left, Uplo::Lower, Trans::NoTranspose, Diag::Unit, AlgoTagType>::invoke(
145 member, 1.0, aa, bb);
146 break;
147 case 1:
148 TeamTrsm<MemberType, Side::Left, Uplo::Lower, Trans::NoTranspose, Diag::NonUnit, AlgoTagType>::invoke(
149 member, 1.0, aa, bb);
150 break;
151 case 2:
152 TeamTrsm<MemberType, Side::Right, Uplo::Upper, Trans::NoTranspose, Diag::Unit, AlgoTagType>::invoke(
153 member, 1.0, aa, bb);
154 break;
155 case 3:
156 TeamTrsm<MemberType, Side::Right, Uplo::Upper, Trans::NoTranspose, Diag::NonUnit, AlgoTagType>::invoke(
157 member, 1.0, aa, bb);
158 break;
159 case 4:
160 TeamTrsm<MemberType, Side::Left, Uplo::Upper, Trans::NoTranspose, Diag::NonUnit, AlgoTagType>::invoke(
161 member, 1.0, aa, bb);
162 break;
163 }
164 }
165 });
166 }
167
168 template <typename MemberType>
169 KOKKOS_INLINE_FUNCTION void operator()(const TeamTagV3 &, const MemberType &member) const {
170 const int lvl = 0;
171 ScratchViewType<ViewType> sa(member.team_scratch(lvl), VectorLength, _a.extent(1), _a.extent(2));
172 // ScratchViewType<ViewType> sb(member.team_scratch(lvl), VectorLength,
173 // _b.extent(1), _b.extent(2));
174
175 const int kbeg = member.league_rank() * VectorLength;
176 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, VectorLength), [&](const int &k) {
177 const int kk = kbeg + k;
178 if (kk < int(_b.extent(0))) {
179 auto aa = Kokkos::subview(_a, kk, Kokkos::ALL(), Kokkos::ALL());
180 auto bb = Kokkos::subview(_b, kk, Kokkos::ALL(), Kokkos::ALL());
181
182 auto saa = Kokkos::subview(sa, k, Kokkos::ALL(), Kokkos::ALL());
183
184 TeamCopy<MemberType, Trans::NoTranspose>::invoke(member, aa, saa);
185 member.team_barrier();
186
187 switch (test) {
188 case 0:
189 TeamTrsm<MemberType, Side::Left, Uplo::Lower, Trans::NoTranspose, Diag::Unit, AlgoTagType>::invoke(
190 member, 1.0, saa, bb);
191 break;
192 case 1:
193 TeamTrsm<MemberType, Side::Left, Uplo::Lower, Trans::NoTranspose, Diag::NonUnit, AlgoTagType>::invoke(
194 member, 1.0, saa, bb);
195 break;
196 case 2:
197 TeamTrsm<MemberType, Side::Right, Uplo::Upper, Trans::NoTranspose, Diag::Unit, AlgoTagType>::invoke(
198 member, 1.0, saa, bb);
199 break;
200 case 3:
201 TeamTrsm<MemberType, Side::Right, Uplo::Upper, Trans::NoTranspose, Diag::NonUnit, AlgoTagType>::invoke(
202 member, 1.0, saa, bb);
203 break;
204 case 4:
205 TeamTrsm<MemberType, Side::Left, Uplo::Upper, Trans::NoTranspose, Diag::NonUnit, AlgoTagType>::invoke(
206 member, 1.0, saa, bb);
207 break;
208 }
209 }
210 });
211 }
212 };
213
214 template <int test, typename DeviceSpaceType, typename AlgoTagType>
215 void Trsm(const int NN, const int BlkSize, const int NumCols) {
216 typedef Kokkos::Schedule<Kokkos::Static> ScheduleType;
217
218 constexpr int VectorLength = DefaultVectorLength<value_type, typename DeviceSpaceType::memory_space>::value;
219 const int N = NN / VectorLength;
220
221 {
222 std::string value_type_name;
223 if (std::is_same<value_type, double>::value) value_type_name = "double";
224 if (std::is_same<value_type, Kokkos::complex<double> >::value) value_type_name = "Kokkos::complex<double>";
225
226 std::cout << "SIMD is defined: datatype " << value_type_name << " a vector length " << VectorLength << "\n";
227 }
228
229 switch (test) {
230 case 0: std::cout << "TestID = Left, Lower, NoTrans, UnitDiag\n"; break;
231 case 1: std::cout << "TestID = Left, Lower, NoTrans, NonUnitDiag\n"; break;
232 case 2: std::cout << "TestID = Right, Upper, NoTrans, UnitDiag\n"; break;
233 case 3: std::cout << "TestID = Right, Upper, NoTrans, NonUnitDiag\n"; break;
234 case 4: std::cout << "TestID = Left, Upper, NoTrans, NonUnitDiag\n"; break;
235 }
236
237 // when m == n, lower upper does not matter (unit and nonunit)
238 double flop = 0;
239 switch (test) {
240 case 0:
241 case 1: flop = FlopCountLower(BlkSize, NumCols); break;
242 case 2:
243 case 3:
244 case 4: flop = FlopCountUpper(BlkSize, NumCols); break;
245 }
246 flop *= (N * VectorLength);
247 const double tmax = 1.0e15;
248
249 typedef Kokkos::DefaultHostExecutionSpace HostSpaceType;
250 typedef typename DeviceSpaceType::memory_space DeviceMemorySpaceType;
251
252 const int iter_begin = -3, iter_end = 30;
253 Kokkos::Timer timer;
254
255 Kokkos::View<value_type ***, Kokkos::LayoutLeft, HostSpaceType> amat("amat", N * VectorLength, BlkSize, BlkSize),
256 bmat("bmat", N * VectorLength, BlkSize, NumCols), bref("bmat", N * VectorLength, BlkSize, NumCols);
257
258 {
259 Random<value_type> random;
260 for (int k = 0; k < N * VectorLength; ++k) {
261 for (int i = 0; i < BlkSize; ++i)
262 for (int j = 0; j < BlkSize; ++j) amat(k, i, j) = random.value() + 4.0 * (i == j);
263 for (int i = 0; i < BlkSize; ++i)
264 for (int j = 0; j < NumCols; ++j) bmat(k, i, j) = random.value();
265 }
266 }
267
268 // P100 L2 cache 4MB per core
269 constexpr size_t LLC_CAPACITY = 56 * 4 * 1024 * 1024;
270 Flush<LLC_CAPACITY, DeviceSpaceType> flush;
271
272 #if defined(__KOKKOSKERNELS_NVIDIA_CUBLAS__)
273 if (1) {
274 ///
275 /// CUBLAS Batch version
276 ///
277 const Kokkos::LayoutStride stride(N * VectorLength, BlkSize * BlkSize, BlkSize, 1, BlkSize, BlkSize);
278
279 Kokkos::View<value_type ***, Kokkos::LayoutStride, DeviceSpaceType> a("a", stride), b("b", stride);
280
281 cublasStatus_t stat;
282 cublasHandle_t handle;
283
284 stat = cublasCreate(&handle);
285 if (stat != CUBLAS_STATUS_SUCCESS) Kokkos::abort("CUBLAS initialization failed\n");
286
287 auto amat_device = Kokkos::create_mirror_view(typename DeviceSpaceType::memory_space(), amat);
288 auto bmat_device = Kokkos::create_mirror_view(typename DeviceSpaceType::memory_space(), bmat);
289
290 Kokkos::deep_copy(amat_device, amat);
291 Kokkos::deep_copy(bmat_device, bmat);
292
293 Kokkos::fence();
294
295 const double one(1.0); //, zero(0.0);
296 {
297 double tavg = 0, tmin = tmax;
298 value_type *aa[N * VectorLength], *bb[N * VectorLength];
299 for (int k = 0; k < N * VectorLength; ++k) {
300 aa[k] = a.data() + k * a.stride_0();
301 bb[k] = b.data() + k * b.stride_0();
302 }
303 value_type **aa_device, **bb_device;
304 if (cudaMalloc(&aa_device, N * VectorLength * sizeof(value_type *)) != cudaSuccess ||
305 cudaMalloc(&bb_device, N * VectorLength * sizeof(value_type *)) != cudaSuccess) {
306 Kokkos::abort("CUDA memory allocation failed\n");
307 }
308 if (cudaMemcpy(aa_device, aa, sizeof(value_type *) * N * VectorLength, cudaMemcpyHostToDevice) != cudaSuccess ||
309 cudaMemcpy(bb_device, bb, sizeof(value_type *) * N * VectorLength, cudaMemcpyHostToDevice) != cudaSuccess) {
310 Kokkos::abort("CUDA memcpy failed\n");
311 }
312 Kokkos::fence();
313 for (int iter = iter_begin; iter < iter_end; ++iter) {
314 // flush
315 flush.run();
316
317 // initialize matrices
318 Kokkos::deep_copy(a, amat_device);
319 Kokkos::deep_copy(b, bmat_device);
320
321 timer.reset();
322 switch (test) {
323 case 0: {
324 // Left, Lower, NoTrans, UnitDiag
325 stat = cublasDtrsmBatched(handle, CUBLAS_SIDE_LEFT, CUBLAS_FILL_MODE_LOWER, CUBLAS_OP_N, CUBLAS_DIAG_UNIT,
326 BlkSize, NumCols, &one, (const value_type **)aa_device, BlkSize,
327 (value_type **)bb_device, BlkSize, N * VectorLength);
328 break;
329 }
330 case 1: {
331 // Left, Lower, NoTrans, NonUnitDiag
332 stat = cublasDtrsmBatched(handle, CUBLAS_SIDE_LEFT, CUBLAS_FILL_MODE_LOWER, CUBLAS_OP_N,
333 CUBLAS_DIAG_NON_UNIT, BlkSize, NumCols, &one, (const value_type **)aa_device,
334 BlkSize, (value_type **)bb_device, BlkSize, N * VectorLength);
335 break;
336 }
337 case 2: {
338 // Right, Upper, NoTrans, UnitDiag
339 stat = cublasDtrsmBatched(handle, CUBLAS_SIDE_RIGHT, CUBLAS_FILL_MODE_UPPER, CUBLAS_OP_N, CUBLAS_DIAG_UNIT,
340 BlkSize, NumCols, &one, (const value_type **)aa_device, BlkSize,
341 (value_type **)bb_device, BlkSize, N * VectorLength);
342 break;
343 }
344 case 3: {
345 // Right, Upper, NoTrans, NonUnitDiag
346 stat = cublasDtrsmBatched(handle, CUBLAS_SIDE_RIGHT, CUBLAS_FILL_MODE_UPPER, CUBLAS_OP_N,
347 CUBLAS_DIAG_NON_UNIT, BlkSize, NumCols, &one, (const value_type **)aa_device,
348 BlkSize, (value_type **)bb_device, BlkSize, N * VectorLength);
349 break;
350 }
351 case 4: {
352 // Left, Upper, NoTrans, NonUnitDiag
353 stat = cublasDtrsmBatched(handle, CUBLAS_SIDE_LEFT, CUBLAS_FILL_MODE_UPPER, CUBLAS_OP_N,
354 CUBLAS_DIAG_NON_UNIT, BlkSize, NumCols, &one, (const value_type **)aa_device,
355 BlkSize, (value_type **)bb_device, BlkSize, N * VectorLength);
356 break;
357 }
358 }
359
360 if (stat != CUBLAS_STATUS_SUCCESS) {
361 Kokkos::abort("CUBLAS Trsm Batched failed\n");
362 }
363 Kokkos::fence();
364 const double t = timer.seconds();
365 tmin = std::min(tmin, t);
366 tavg += (iter >= 0) * t;
367 }
368 tavg /= iter_end;
369
370 auto bsol = Kokkos::create_mirror_view(typename HostSpaceType::memory_space(), b);
371 Kokkos::deep_copy(bsol, b);
372 Kokkos::deep_copy(bref, bsol);
373
374 if (cudaFree(aa_device) != cudaSuccess || cudaFree(bb_device) != cudaSuccess) {
375 Kokkos::abort("CUDA memory free failed\n");
376 }
377
378 std::cout << std::setw(8) << "CUBLAS" << std::setw(8) << "Batched"
379 << " BlkSize = " << std::setw(3) << BlkSize << " NumCols = " << std::setw(3) << NumCols
380 << " TeamSize = N/A"
381 << " ScratchSize (KB) = N/A"
382 << " time = " << std::scientific << tmin << " avg flop/s = " << (flop / tavg)
383 << " max flop/s = " << (flop / tmin) << std::endl;
384 }
385 cublasDestroy(handle);
386 }
387 #endif
388
389 if (1) {
390 ///
391 /// Range policy version
392 ///
393 typedef Kokkos::View<value_type ***, DeviceSpaceType> view_type;
394 view_type a("a", N * VectorLength, BlkSize, BlkSize), b("b", N * VectorLength, BlkSize, NumCols);
395
396 double tavg = 0, tmin = tmax;
397 {
398 typedef Functor<test, view_type, AlgoTagType> functor_type;
399 const Kokkos::RangePolicy<DeviceSpaceType, ScheduleType, RangeTag> policy(0, N * VectorLength);
400
401 for (int iter = iter_begin; iter < iter_end; ++iter) {
402 // flush
403 flush.run();
404
405 // initialize matrices
406 Kokkos::deep_copy(a, amat);
407 Kokkos::deep_copy(b, bmat);
408
409 Kokkos::fence();
410 timer.reset();
411
412 Kokkos::parallel_for("KokkosBatched::PerfTest::RangeTag", policy, functor_type(a, b));
413
414 Kokkos::fence();
415 const double t = timer.seconds();
416 tmin = std::min(tmin, t);
417 tavg += (iter >= 0) * t;
418 }
419 tavg /= iter_end;
420
421 auto bsol = Kokkos::create_mirror_view(typename HostSpaceType::memory_space(), b);
422 Kokkos::deep_copy(bsol, b);
423
424 double diff = 0;
425 for (int i = 0, iend = bref.extent(0); i < iend; ++i)
426 for (int j = 0, jend = bref.extent(1); j < jend; ++j)
427 for (int k = 0, kend = bref.extent(2); k < kend; ++k)
428 diff += Kokkos::ArithTraits<value_type>::abs(bref(i, j, k) - bsol(i, j, k));
429
430 std::cout << std::setw(8) << "Kokkos" << std::setw(8) << "Range"
431 << " BlkSize = " << std::setw(3) << BlkSize << " NumCols = " << std::setw(3) << NumCols
432 << " TeamSize = N/A"
433 << " ScratchSize (KB) = 0"
434 << " time = " << std::scientific << tmin << " avg flop/s = " << (flop / tavg)
435 << " max flop/s = " << (flop / tmin);
436 #if defined(__KOKKOSKERNELS_NVIDIA_CUBLAS__)
437 std::cout << " diff to ref = " << diff;
438 #endif
439 std::cout << std::endl;
440 }
441 }
442
443 if (1) {
444 ///
445 /// Team policy V1 - almost same scheduling with range policy
446 ///
447 typedef Kokkos::View<value_type ***, DeviceSpaceType> view_type;
448 view_type a("a", N * VectorLength, BlkSize, BlkSize), b("b", N * VectorLength, BlkSize, NumCols);
449
450 double tavg = 0, tmin = tmax;
451 {
452 typedef Kokkos::TeamPolicy<DeviceSpaceType, ScheduleType, TeamTagV1> policy_type;
453 typedef Functor<test, view_type, AlgoTagType, VectorLength> functor_type;
454
455 const int team_size = policy_type(N / 32, Kokkos::AUTO, VectorLength)
456 .team_size_recommended(functor_type(), Kokkos::ParallelForTag());
457
458 const policy_type policy(N / team_size, team_size, VectorLength);
459 for (int iter = iter_begin; iter < iter_end; ++iter) {
460 // flush
461 flush.run();
462
463 // initialize matrices
464 Kokkos::deep_copy(a, amat);
465 Kokkos::deep_copy(b, bmat);
466
467 Kokkos::fence();
468 timer.reset();
469
470 Kokkos::parallel_for("KokkosBatched::PerfTest::TeamTagV1", policy, functor_type(a, b));
471
472 Kokkos::fence();
473 const double t = timer.seconds();
474 tmin = std::min(tmin, t);
475 tavg += (iter >= 0) * t;
476 }
477 tavg /= iter_end;
478
479 auto bsol = Kokkos::create_mirror_view(typename HostSpaceType::memory_space(), b);
480 Kokkos::deep_copy(bsol, b);
481
482 double diff = 0;
483 for (int i = 0, iend = bref.extent(0); i < iend; ++i)
484 for (int j = 0, jend = bref.extent(1); j < jend; ++j)
485 for (int k = 0, kend = bref.extent(2); k < kend; ++k)
486 diff += Kokkos::ArithTraits<value_type>::abs(bref(i, j, k) - bsol(i, j, k));
487
488 std::cout << std::setw(8) << "Kokkos" << std::setw(8) << "Team V1"
489 << " BlkSize = " << std::setw(3) << BlkSize << " NumCols = " << std::setw(3) << NumCols
490 << " TeamSize = " << std::setw(3) << team_size << " ScratchSize (KB) = 0"
491 << " time = " << std::scientific << tmin << " avg flop/s = " << (flop / tavg)
492 << " max flop/s = " << (flop / tmin);
493 #if defined(__KOKKOSKERNELS_NVIDIA_CUBLAS__)
494 std::cout << " diff to ref = " << diff;
495 #endif
496 std::cout << std::endl;
497 }
498 }
499
500 if (1) {
501 ///
502 /// Team policy V2 - team parallel
503 ///
504 typedef Kokkos::View<value_type ***, DeviceSpaceType> view_type;
505 view_type a("a", N * VectorLength, BlkSize, BlkSize), b("b", N * VectorLength, BlkSize, NumCols);
506
507 double tavg = 0, tmin = tmax;
508 {
509 typedef Kokkos::TeamPolicy<DeviceSpaceType, ScheduleType, TeamTagV2> policy_type;
510 typedef Functor<test, view_type, AlgoTagType, VectorLength> functor_type;
511
512 const int is_blocked_algo = (std::is_same<AlgoTagType, Algo::Trsm::Blocked>::value),
513 mb = Algo::Trsm::Blocked::mb<DeviceMemorySpaceType>(), mp = BlkSize % mb > 0;
514
515 const int mblk = is_blocked_algo ? (BlkSize / mb + mp) : BlkSize;
516
517 const int max_team_size =
518 policy_type(N, Kokkos::AUTO, VectorLength).team_size_max(functor_type(), Kokkos::ParallelForTag());
519 const int team_size = std::min(std::max(NumCols, (mblk - 1) * mblk), max_team_size);
520
521 const policy_type policy(N, team_size, VectorLength);
522 for (int iter = iter_begin; iter < iter_end; ++iter) {
523 // flush
524 flush.run();
525
526 // initialize matrices
527 Kokkos::deep_copy(a, amat);
528 Kokkos::deep_copy(b, bmat);
529
530 DeviceSpaceType().fence();
531 timer.reset();
532
533 Kokkos::parallel_for("KokkosBatched::PerfTest::TeamTagV2", policy, functor_type(a, b));
534
535 DeviceSpaceType().fence();
536 const double t = timer.seconds();
537 tmin = std::min(tmin, t);
538 tavg += (iter >= 0) * t;
539 }
540 tavg /= iter_end;
541
542 auto bsol = Kokkos::create_mirror_view(typename HostSpaceType::memory_space(), b);
543 Kokkos::deep_copy(bsol, b);
544
545 double diff = 0;
546 for (int i = 0, iend = bref.extent(0); i < iend; ++i)
547 for (int j = 0, jend = bref.extent(1); j < jend; ++j)
548 for (int k = 0, kend = bref.extent(2); k < kend; ++k)
549 diff += Kokkos::ArithTraits<value_type>::abs(bref(i, j, k) - bsol(i, j, k));
550
551 std::cout << std::setw(8) << "Kokkos" << std::setw(8) << "Team V2"
552 << " BlkSize = " << std::setw(3) << BlkSize << " NumCols = " << std::setw(3) << NumCols
553 << " TeamSize = " << std::setw(3) << team_size << " ScratchSize (KB) = 0"
554 << " time = " << std::scientific << tmin << " avg flop/s = " << (flop / tavg)
555 << " max flop/s = " << (flop / tmin);
556 #if defined(__KOKKOSKERNELS_NVIDIA_CUBLAS__)
557 std::cout << " diff to ref = " << diff;
558 #endif
559 std::cout << std::endl;
560 }
561 }
562
563 if (1) {
564 ///
565 /// Team policy V3 - team parallel + sratch
566 ///
567 typedef Kokkos::View<value_type ***, DeviceSpaceType> view_type;
568 view_type a("a", N * VectorLength, BlkSize, BlkSize), b("b", N * VectorLength, BlkSize, NumCols);
569
570 double tavg = 0, tmin = tmax;
571 {
572 typedef Kokkos::TeamPolicy<DeviceSpaceType, ScheduleType, TeamTagV3> policy_type;
573 typedef Functor<test, view_type, AlgoTagType, VectorLength> functor_type;
574
575 const int lvl = 0, per_team_scratch = ScratchViewType<view_type>::shmem_size(VectorLength, BlkSize, BlkSize);
576
577 if (per_team_scratch / 1024 < 48) {
578 const int is_blocked_algo = (std::is_same<AlgoTagType, Algo::Trsm::Blocked>::value),
579 mb = Algo::Trsm::Blocked::mb<DeviceMemorySpaceType>(), mp = BlkSize % mb > 0;
580
581 const int mblk = is_blocked_algo ? (BlkSize / mb + mp) : BlkSize;
582
583 const int max_team_size = policy_type(N, Kokkos::AUTO, VectorLength)
584 .set_scratch_size(lvl, Kokkos::PerTeam(per_team_scratch))
585 .team_size_max(functor_type(), Kokkos::ParallelForTag());
586 const int team_size = std::min(std::max(NumCols, (mblk - 1) * mblk), max_team_size);
587
588 policy_type policy(N, team_size, VectorLength);
589 for (int iter = iter_begin; iter < iter_end; ++iter) {
590 // flush
591 flush.run();
592
593 // initialize matrices
594 Kokkos::deep_copy(a, amat);
595 Kokkos::deep_copy(b, bmat);
596
597 DeviceSpaceType().fence();
598 timer.reset();
599
600 Kokkos::parallel_for("KokkosBatched::PerfTest::TeamTagV3", policy, functor_type(a, b));
601
602 DeviceSpaceType().fence();
603 const double t = timer.seconds();
604 tmin = std::min(tmin, t);
605 tavg += (iter >= 0) * t;
606 }
607 tavg /= iter_end;
608
609 auto bsol = Kokkos::create_mirror_view(typename HostSpaceType::memory_space(), b);
610 Kokkos::deep_copy(bsol, b);
611
612 double diff = 0;
613 for (int i = 0, iend = bref.extent(0); i < iend; ++i)
614 for (int j = 0, jend = bref.extent(1); j < jend; ++j)
615 for (int k = 0, kend = bref.extent(2); k < kend; ++k)
616 diff += Kokkos::ArithTraits<value_type>::abs(bref(i, j, k) - bsol(i, j, k));
617
618 std::cout << std::setw(8) << "Kokkos" << std::setw(8) << "Team V3"
619 << " BlkSize = " << std::setw(3) << BlkSize << " NumCols = " << std::setw(3) << NumCols
620 << " TeamSize = " << std::setw(3) << team_size << " ScratchSize (KB) = " << std::setw(3)
621 << (per_team_scratch / 1024) << " time = " << std::scientific << tmin
622 << " avg flop/s = " << (flop / tavg) << " max flop/s = " << (flop / tmin);
623 #if defined(__KOKKOSKERNELS_NVIDIA_CUBLAS__)
624 std::cout << " diff to ref = " << diff;
625 #endif
626 std::cout << std::endl;
627 } else {
628 std::cout << std::setw(8) << "Kokkos" << std::setw(8) << "Team V3"
629 << " Scratch per team is too big (KB): " << (per_team_scratch / 1024) << std::endl;
630 }
631 }
632 }
633 std::cout << "\n\n";
634 }
635 } // namespace PerfTest
636 } // namespace KokkosBatched
637
638 using namespace KokkosBatched;
639
640 template <typename AlgoTagType>
641 void run(const int N, const int B, const int R) {
642 typedef Kokkos::DefaultExecutionSpace ExecSpace;
643
644 Kokkos::print_configuration(std::cout, false);
645
646 if (B != 0 && R != 0) {
647 PerfTest::Trsm<0, ExecSpace, AlgoTagType>(N, B, R);
648 } else {
649 std::cout << "\n\n Used for Factorization \n\n";
650
651 /// Left, Lower, NoTrans, UnitDiag (used in LU factorization and LU solve)
652
653 PerfTest::Trsm<0, ExecSpace, AlgoTagType>(N, 3, 3);
654 PerfTest::Trsm<0, ExecSpace, AlgoTagType>(N, 5, 5);
655 PerfTest::Trsm<0, ExecSpace, AlgoTagType>(N, 10, 10);
656 PerfTest::Trsm<0, ExecSpace, AlgoTagType>(N, 15, 15);
657
658 /// Left, Lower, NoTrans, NonUnitDiag
659
660 PerfTest::Trsm<1, ExecSpace, AlgoTagType>(N, 3, 3);
661 PerfTest::Trsm<1, ExecSpace, AlgoTagType>(N, 5, 5);
662 PerfTest::Trsm<1, ExecSpace, AlgoTagType>(N, 10, 10);
663 PerfTest::Trsm<1, ExecSpace, AlgoTagType>(N, 15, 15);
664
665 /// Right, Upper, NoTrans, UnitDiag
666
667 PerfTest::Trsm<2, ExecSpace, AlgoTagType>(N, 3, 3);
668 PerfTest::Trsm<2, ExecSpace, AlgoTagType>(N, 5, 5);
669 PerfTest::Trsm<2, ExecSpace, AlgoTagType>(N, 10, 10);
670 PerfTest::Trsm<2, ExecSpace, AlgoTagType>(N, 15, 15);
671
672 /// Right, Upper, NoTrans, NonUnitDiag (used in LU factorization)
673
674 PerfTest::Trsm<3, ExecSpace, AlgoTagType>(N, 3, 3);
675 PerfTest::Trsm<3, ExecSpace, AlgoTagType>(N, 5, 5);
676 PerfTest::Trsm<3, ExecSpace, AlgoTagType>(N, 10, 10);
677 PerfTest::Trsm<3, ExecSpace, AlgoTagType>(N, 15, 15);
678
679 std::cout << "\n\n Used for Solve \n\n";
680
681 /// Left, Lower, NoTrans, UnitDiag (used in LU solve)
682
683 PerfTest::Trsm<0, ExecSpace, AlgoTagType>(N, 3, 1);
684 PerfTest::Trsm<0, ExecSpace, AlgoTagType>(N, 5, 1);
685 PerfTest::Trsm<0, ExecSpace, AlgoTagType>(N, 10, 1);
686 PerfTest::Trsm<0, ExecSpace, AlgoTagType>(N, 15, 1);
687
688 /// Left, Upper, Notrans, NonUnitDiag (user in LU solve)
689
690 PerfTest::Trsm<4, ExecSpace, AlgoTagType>(N, 3, 1);
691 PerfTest::Trsm<4, ExecSpace, AlgoTagType>(N, 5, 1);
692 PerfTest::Trsm<4, ExecSpace, AlgoTagType>(N, 10, 1);
693 PerfTest::Trsm<4, ExecSpace, AlgoTagType>(N, 15, 1);
694 }
695 }
696
697 int main(int argc, char *argv[]) {
698 Kokkos::initialize(argc, argv);
699
700 int N = 128 * 128, B = 0, R = 0;
701
702 for (int i = 1; i < argc; ++i) {
703 const std::string &token = argv[i];
704 if (token == std::string("-N")) N = std::atoi(argv[++i]);
705 if (token == std::string("-B")) B = std::atoi(argv[++i]);
706 if (token == std::string("-R")) R = std::atoi(argv[++i]);
707 }
708
709 if (R == 0 && B != 0) R = B;
710
711 {
712 std::cout << " N = " << N << std::endl;
713
714 std::cout << "\n Testing LayoutLeft and Algo::Trsm::Unblocked\n";
715 run<Algo::Trsm::Unblocked>(N, B, R);
716
717 std::cout << "\n Testing LayoutLeft Algo::Trsm::Blocked\n";
718 run<Algo::Trsm::Blocked>(N, B, R);
719 }
720
721 Kokkos::finalize();
722
723 return 0;
724 }
725
726 #else
727
728 int main(int argc, char *argv[]) {
729 std::cout << "Kokkos::Cuda is not enabled\n";
730 return -1;
731 }
732
733 #endif