Debian Codegraph @

packages/kokkos-kernels/perf_test/batched/dense/do-not-use/KokkosBatched_Test_Gemm_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_Gemm_Decl.hpp"
38 #include "KokkosBatched_Gemm_Serial_Impl.hpp"
39 #include "KokkosBatched_Gemm_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 FlopCount(int mm, int nn, int kk) {
51 double m = (double)mm;
52 double n = (double)nn;
53 double k = (double)kk;
54 return (FLOP_MUL * (m * n * k) + FLOP_ADD * (m * n * k));
55 }
56
57 struct RangeTag {};
58 struct TeamTagV1 {};
59 struct TeamTagV2 {};
60 struct TeamTagV3 {};
61 struct TeamTagHandmade {};
62
63 template <typename ViewType, typename AlgoTagType, int VectorLength = 0>
64 struct Functor {
65 ConstUnmanagedViewType<ViewType> _a, _b;
66 UnmanagedViewType<ViewType> _c;
67
68 KOKKOS_INLINE_FUNCTION
69 Functor() = default;
70
71 KOKKOS_INLINE_FUNCTION
72 Functor(const ViewType &a, const ViewType &b, const ViewType &c) : _a(a), _b(b), _c(c) {}
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 auto bb = Kokkos::subview(_b, k, Kokkos::ALL(), Kokkos::ALL());
78 auto cc = Kokkos::subview(_c, k, Kokkos::ALL(), Kokkos::ALL());
79
80 SerialGemm<Trans::NoTranspose, Trans::NoTranspose, AlgoTagType>::invoke(1.0, aa, bb, 1.0, cc);
81 }
82
83 template <typename MemberType>
84 KOKKOS_INLINE_FUNCTION void operator()(const TeamTagV1 &, const MemberType &member) const {
85 const int kbeg = (member.league_rank() * (member.team_size() * VectorLength) + member.team_rank() * VectorLength);
86 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, VectorLength), [&](const int &k) {
87 const int kk = kbeg + k;
88 if (kk < int(_c.extent(0))) {
89 auto aa = Kokkos::subview(_a, kk, Kokkos::ALL(), Kokkos::ALL());
90 auto bb = Kokkos::subview(_b, kk, Kokkos::ALL(), Kokkos::ALL());
91 auto cc = Kokkos::subview(_c, kk, Kokkos::ALL(), Kokkos::ALL());
92
93 SerialGemm<Trans::NoTranspose, Trans::NoTranspose, AlgoTagType>::invoke(1.0, aa, bb, 1.0, cc);
94 }
95 });
96 }
97
98 template <typename MemberType>
99 KOKKOS_INLINE_FUNCTION void operator()(const TeamTagV2 &, const MemberType &member) const {
100 const int kbeg = member.league_rank() * VectorLength;
101 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, VectorLength), [&](const int &k) {
102 const int kk = kbeg + k;
103 if (kk < int(_c.extent(0))) {
104 auto aa = Kokkos::subview(_a, kk, Kokkos::ALL(), Kokkos::ALL());
105 auto bb = Kokkos::subview(_b, kk, Kokkos::ALL(), Kokkos::ALL());
106 auto cc = Kokkos::subview(_c, kk, Kokkos::ALL(), Kokkos::ALL());
107
108 TeamGemm<MemberType, Trans::NoTranspose, Trans::NoTranspose, AlgoTagType>::invoke(member, 1.0, aa, bb, 1.0, cc);
109 }
110 });
111 }
112
113 template <typename MemberType>
114 KOKKOS_INLINE_FUNCTION void operator()(const TeamTagV3 &, const MemberType &member) const {
115 const int lvl = 0;
116 ScratchViewType<ViewType> sa(member.team_scratch(lvl), VectorLength, _a.extent(1), _a.extent(2));
117 ScratchViewType<ViewType> sb(member.team_scratch(lvl), VectorLength, _b.extent(1), _b.extent(2));
118
119 const int kbeg = member.league_rank() * VectorLength;
120 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, VectorLength), [&](const int &k) {
121 const int kk = kbeg + k;
122 if (kk < int(_c.extent(0))) {
123 auto aa = Kokkos::subview(_a, kk, Kokkos::ALL(), Kokkos::ALL());
124 auto bb = Kokkos::subview(_b, kk, Kokkos::ALL(), Kokkos::ALL());
125 auto cc = Kokkos::subview(_c, kk, Kokkos::ALL(), Kokkos::ALL());
126
127 auto saa = Kokkos::subview(sa, k, Kokkos::ALL(), Kokkos::ALL());
128 auto sbb = Kokkos::subview(sb, k, Kokkos::ALL(), Kokkos::ALL());
129
130 TeamCopy<MemberType, Trans::NoTranspose>::invoke(member, aa, saa);
131 TeamCopy<MemberType, Trans::NoTranspose>::invoke(member, bb, sbb);
132 member.team_barrier();
133
134 TeamGemm<MemberType, Trans::NoTranspose, Trans::NoTranspose, AlgoTagType>::invoke(member, 1.0, saa, sbb, 1.0,
135 cc);
136 }
137 });
138 }
139
140 template <typename MemberType>
141 KOKKOS_INLINE_FUNCTION void operator()(const TeamTagHandmade &, const MemberType &member) const {
142 const int kbeg = member.league_rank() * VectorLength;
143 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, VectorLength), [&](const int &k) {
144 const int kk = kbeg + k;
145 if (kk < int(_c.extent(0))) {
146 const int m = _c.extent(1), n = _c.extent(2), q = _a.extent(2);
147 Kokkos::parallel_for(Kokkos::TeamThreadRange(member, 0, m * n), [&](const int &ij) {
148 const int i = ij % m, j = ij / m;
149 typename ViewType::non_const_value_type cval = 0;
150 for (int p = 0; p < q; ++p) cval += _a(kk, i, p) * _b(kk, p, j);
151 _c(kk, i, j) += cval;
152 });
153 }
154 });
155 }
156 };
157
158 template <typename DeviceSpaceType, typename AlgoTagType>
159 void Gemm(const int NN, const int BlkSize) {
160 typedef Kokkos::Schedule<Kokkos::Static> ScheduleType;
161
162 constexpr int VectorLength = DefaultVectorLength<value_type, typename DeviceSpaceType::memory_space>::value;
163 const int N = NN / VectorLength;
164
165 {
166 std::string value_type_name;
167 if (std::is_same<value_type, double>::value) value_type_name = "double";
168 if (std::is_same<value_type, Kokkos::complex<double> >::value) value_type_name = "Kokkos::complex<double>";
169
170 std::cout << "SIMD is defined: datatype " << value_type_name << " a vector length " << VectorLength << "\n";
171 }
172
173 const double flop = (N * VectorLength) * FlopCount(BlkSize, BlkSize, BlkSize);
174 const double tmax = 1.0e15;
175
176 typedef Kokkos::DefaultHostExecutionSpace HostSpaceType;
177 typedef typename DeviceSpaceType::memory_space DeviceMemorySpaceType;
178
179 const int iter_begin = -3, iter_end = 30;
180 Kokkos::Timer timer;
181
182 Kokkos::View<value_type ***, Kokkos::LayoutLeft, HostSpaceType> amat("amat", N * VectorLength, BlkSize, BlkSize),
183 bmat("bmat", N * VectorLength, BlkSize, BlkSize), cref("cref", N * VectorLength, BlkSize, BlkSize);
184
185 {
186 Random<value_type> random;
187 for (int k = 0; k < N * VectorLength; ++k)
188 for (int i = 0; i < BlkSize; ++i)
189 for (int j = 0; j < BlkSize; ++j) {
190 amat(k, i, j) = random.value();
191 bmat(k, i, j) = random.value();
192 }
193 }
194
195 // P100 L2 cache 4MB per core
196 constexpr size_t LLC_CAPACITY = 56 * 4 * 1024 * 1024;
197 Flush<LLC_CAPACITY, DeviceSpaceType> flush;
198
199 #if defined(__KOKKOSKERNELS_NVIDIA_CUBLAS__)
200 if (1) {
201 ///
202 /// CUBLAS Strided version
203 ///
204 const Kokkos::LayoutStride stride(N * VectorLength, BlkSize * BlkSize, BlkSize, 1, BlkSize, BlkSize);
205
206 Kokkos::View<value_type ***, Kokkos::LayoutStride, DeviceSpaceType> a("a", stride), b("b", stride), c("c", stride);
207
208 double tavg = 0, tmin = tmax;
209
210 cublasStatus_t stat;
211 cublasHandle_t handle;
212
213 stat = cublasCreate(&handle);
214 if (stat != CUBLAS_STATUS_SUCCESS) Kokkos::abort("CUBLAS initialization failed\n");
215
216 auto amat_device = Kokkos::create_mirror_view(DeviceMemorySpaceType(), amat);
217 auto bmat_device = Kokkos::create_mirror_view(DeviceMemorySpaceType(), bmat);
218
219 Kokkos::deep_copy(amat_device, amat);
220 Kokkos::deep_copy(bmat_device, bmat);
221
222 Kokkos::fence();
223
224 const double one(1.0), zero(0.0);
225 {
226 tavg = 0;
227 tmin = tmax;
228
229 for (int iter = iter_begin; iter < iter_end; ++iter) {
230 // flush
231 flush.run();
232
233 // initialize matrices
234 Kokkos::deep_copy(a, amat_device);
235 Kokkos::deep_copy(b, bmat_device);
236 Kokkos::deep_copy(c, 0);
237
238 Kokkos::fence();
239 timer.reset();
240
241 stat = cublasDgemmStridedBatched(handle, CUBLAS_OP_N, CUBLAS_OP_N, BlkSize, BlkSize, BlkSize, &one,
242 (const value_type *)a.data(), BlkSize, BlkSize * BlkSize,
243 (const value_type *)b.data(), BlkSize, BlkSize * BlkSize, &zero,
244 (value_type *)c.data(), BlkSize, BlkSize * BlkSize, N * VectorLength);
245
246 Kokkos::fence();
247 const double t = timer.seconds();
248 tmin = std::min(tmin, t);
249 tavg += (iter >= 0) * t;
250 }
251 tavg /= iter_end;
252
253 auto csol = Kokkos::create_mirror_view(typename HostSpaceType::memory_space(), c);
254 Kokkos::deep_copy(csol, c);
255 Kokkos::deep_copy(cref, csol);
256
257 std::cout << std::setw(8) << "CUBLAS" << std::setw(8) << "Strided"
258 << " BlkSize = " << std::setw(3) << BlkSize << " TeamSize = N/A"
259 << " ScratchSize (KB) = 0"
260 << " time = " << std::scientific << tmin << " avg flop/s = " << (flop / tavg)
261 << " max flop/s = " << (flop / tmin) << std::endl;
262 }
263 cublasDestroy(handle);
264 }
265 #endif
266
267 if (1) {
268 ///
269 /// Range policy version
270 ///
271 typedef Kokkos::View<value_type ***, DeviceSpaceType> view_type;
272 view_type a("a", N * VectorLength, BlkSize, BlkSize), b("b", N * VectorLength, BlkSize, BlkSize),
273 c("c", N * VectorLength, BlkSize, BlkSize);
274
275 double tavg = 0, tmin = tmax;
276 {
277 typedef Functor<view_type, AlgoTagType> functor_type;
278 const Kokkos::RangePolicy<DeviceSpaceType, ScheduleType, RangeTag> policy(0, N * VectorLength);
279
280 for (int iter = iter_begin; iter < iter_end; ++iter) {
281 // flush
282 flush.run();
283
284 // initialize matrices
285 Kokkos::deep_copy(a, amat);
286 Kokkos::deep_copy(b, bmat);
287 Kokkos::deep_copy(c, 0);
288
289 Kokkos::fence();
290 timer.reset();
291
292 Kokkos::parallel_for("KokkosBatched::PerfTest::GemmCuda::RangeTag", policy, functor_type(a, b, c));
293
294 Kokkos::fence();
295 const double t = timer.seconds();
296 tmin = std::min(tmin, t);
297 tavg += (iter >= 0) * t;
298 }
299 tavg /= iter_end;
300
301 auto csol = Kokkos::create_mirror_view(typename HostSpaceType::memory_space(), c);
302 Kokkos::deep_copy(csol, c);
303
304 double diff = 0;
305 for (int i = 0, iend = cref.extent(0); i < iend; ++i)
306 for (int j = 0, jend = cref.extent(1); j < jend; ++j)
307 for (int k = 0, kend = cref.extent(2); k < kend; ++k)
308 diff += Kokkos::ArithTraits<value_type>::abs(cref(i, j, k) - csol(i, j, k));
309
310 std::cout << std::setw(8) << "Kokkos" << std::setw(8) << "Range"
311 << " BlkSize = " << std::setw(3) << BlkSize << " TeamSize = N/A"
312 << " ScratchSize (KB) = 0"
313 << " time = " << std::scientific << tmin << " avg flop/s = " << (flop / tavg)
314 << " max flop/s = " << (flop / tmin);
315 #if defined(__KOKKOSKERNELS_NVIDIA_CUBLAS__)
316 std::cout << " diff to ref = " << diff;
317 #endif
318 std::cout << std::endl;
319 }
320 }
321
322 if (1) {
323 ///
324 /// Team policy V1 - almost same scheduling with range policy;
325 /// expect the same performance as range policy
326 ///
327 typedef Kokkos::View<value_type ***, DeviceSpaceType> view_type;
328 view_type a("a", N * VectorLength, BlkSize, BlkSize), b("b", N * VectorLength, BlkSize, BlkSize),
329 c("c", N * VectorLength, BlkSize, BlkSize);
330
331 double tavg = 0, tmin = tmax;
332 {
333 typedef Kokkos::TeamPolicy<DeviceSpaceType, ScheduleType, TeamTagV1> policy_type;
334
335 typedef Functor<view_type, AlgoTagType, VectorLength> functor_type;
336
337 // 128 is rough estimates
338 const int team_size = policy_type(N / 32, Kokkos::AUTO, VectorLength)
339 .team_size_recommended(functor_type(), Kokkos::ParallelForTag());
340
341 const policy_type policy(N / team_size, team_size, VectorLength);
342 for (int iter = iter_begin; iter < iter_end; ++iter) {
343 // flush
344 flush.run();
345
346 // initialize matrices
347 Kokkos::deep_copy(a, amat);
348 Kokkos::deep_copy(b, bmat);
349 Kokkos::deep_copy(c, 0);
350
351 Kokkos::fence();
352 timer.reset();
353
354 Kokkos::parallel_for("KokkosBatched::PerfTest::GemmCuda::TeamPolicyV1", policy, functor_type(a, b, c));
355
356 Kokkos::fence();
357 const double t = timer.seconds();
358 tmin = std::min(tmin, t);
359 tavg += (iter >= 0) * t;
360 }
361 tavg /= iter_end;
362
363 auto csol = Kokkos::create_mirror_view(typename HostSpaceType::memory_space(), c);
364 Kokkos::deep_copy(csol, c);
365
366 double diff = 0;
367 for (int i = 0, iend = cref.extent(0); i < iend; ++i)
368 for (int j = 0, jend = cref.extent(1); j < jend; ++j)
369 for (int k = 0, kend = cref.extent(2); k < kend; ++k)
370 diff += Kokkos::ArithTraits<value_type>::abs(cref(i, j, k) - csol(i, j, k));
371
372 std::cout << std::setw(8) << "Kokkos" << std::setw(8) << "Team V1"
373 << " BlkSize = " << std::setw(3) << BlkSize << " TeamSize = " << std::setw(3) << team_size
374 << " ScratchSize (KB) = 0"
375 << " time = " << std::scientific << tmin << " avg flop/s = " << (flop / tavg)
376 << " max flop/s = " << (flop / tmin);
377 #if defined(__KOKKOSKERNELS_NVIDIA_CUBLAS__)
378 std::cout << " diff to ref = " << diff;
379 #endif
380 std::cout << std::endl;
381 }
382 }
383
384 if (1) {
385 ///
386 /// Team policy V2 - team parallel
387 ///
388 typedef Kokkos::View<value_type ***, DeviceSpaceType> view_type;
389 view_type a("a", N * VectorLength, BlkSize, BlkSize), b("b", N * VectorLength, BlkSize, BlkSize),
390 c("c", N * VectorLength, BlkSize, BlkSize);
391
392 double tavg = 0, tmin = tmax;
393 {
394 typedef Kokkos::TeamPolicy<DeviceSpaceType, ScheduleType, TeamTagV2> policy_type;
395 typedef Functor<view_type, AlgoTagType, VectorLength> functor_type;
396
397 const int is_blocked_algo = (std::is_same<AlgoTagType, Algo::Gemm::Blocked>::value),
398 mb = Algo::Gemm::Blocked::mb<DeviceMemorySpaceType>(), mp = BlkSize % mb > 0;
399
400 const int mblk = is_blocked_algo ? (BlkSize / mb + mp) : BlkSize;
401
402 const int max_team_size =
403 policy_type(N, Kokkos::AUTO, VectorLength).team_size_max(functor_type(), Kokkos::ParallelForTag());
404 const int team_size = std::min(std::max(mblk * mblk, 4), max_team_size);
405
406 policy_type policy(N, team_size, VectorLength);
407 for (int iter = iter_begin; iter < iter_end; ++iter) {
408 // flush
409 flush.run();
410
411 // initialize matrices
412 Kokkos::deep_copy(a, amat);
413 Kokkos::deep_copy(b, bmat);
414 Kokkos::deep_copy(c, 0);
415
416 Kokkos::fence();
417 timer.reset();
418
419 Kokkos::parallel_for("KokkosBatched::PerfTest::GemmCuda::TeamPolicyV2", policy, functor_type(a, b, c));
420
421 Kokkos::fence();
422 const double t = timer.seconds();
423 tmin = std::min(tmin, t);
424 tavg += (iter >= 0) * t;
425 }
426 tavg /= iter_end;
427
428 auto csol = Kokkos::create_mirror_view(typename HostSpaceType::memory_space(), c);
429 Kokkos::deep_copy(csol, c);
430
431 double diff = 0;
432 for (int i = 0, iend = cref.extent(0); i < iend; ++i)
433 for (int j = 0, jend = cref.extent(1); j < jend; ++j)
434 for (int k = 0, kend = cref.extent(2); k < kend; ++k)
435 diff += Kokkos::ArithTraits<value_type>::abs(cref(i, j, k) - csol(i, j, k));
436
437 std::cout << std::setw(8) << "Kokkos" << std::setw(8) << "Team V2"
438 << " BlkSize = " << std::setw(3) << BlkSize << " TeamSize = " << std::setw(3) << team_size
439 << " ScratchSize (KB) = 0"
440 << " time = " << std::scientific << tmin << " avg flop/s = " << (flop / tavg)
441 << " max flop/s = " << (flop / tmin);
442 #if defined(__KOKKOSKERNELS_NVIDIA_CUBLAS__)
443 std::cout << " diff to ref = " << diff;
444 #endif
445 std::cout << std::endl;
446 }
447 }
448
449 if (1) {
450 ///
451 /// Team policy V3 - team parallel + scratch
452 ///
453 typedef Kokkos::View<value_type ***, DeviceSpaceType> view_type;
454 view_type a("a", N * VectorLength, BlkSize, BlkSize), b("b", N * VectorLength, BlkSize, BlkSize),
455 c("c", N * VectorLength, BlkSize, BlkSize);
456
457 double tavg = 0, tmin = tmax;
458 {
459 typedef Kokkos::TeamPolicy<DeviceSpaceType, ScheduleType, TeamTagV3> policy_type;
460 typedef Functor<view_type, AlgoTagType, VectorLength> functor_type;
461
462 const int lvl = 0, per_team_scratch = 2 * ScratchViewType<view_type>::shmem_size(VectorLength, BlkSize, BlkSize);
463 // std::cout << "per team scratch " << per_team_scratch << "\n";
464 if (per_team_scratch / 1024 < 48) {
465 const int is_blocked_algo = (std::is_same<AlgoTagType, Algo::Gemm::Blocked>::value),
466 mb = Algo::Gemm::Blocked::mb<DeviceMemorySpaceType>(), mp = BlkSize % mb > 0;
467
468 const int mblk = is_blocked_algo ? (BlkSize / mb + mp) : BlkSize;
469
470 const int max_team_size = policy_type(N, Kokkos::AUTO, VectorLength)
471 .set_scratch_size(lvl, Kokkos::PerTeam(per_team_scratch))
472 .team_size_max(functor_type(), Kokkos::ParallelForTag());
473 const int team_size = std::min(std::max(mblk * mblk, 4), max_team_size);
474
475 policy_type policy =
476 policy_type(N, team_size, VectorLength).set_scratch_size(lvl, Kokkos::PerTeam(per_team_scratch));
477 for (int iter = iter_begin; iter < iter_end; ++iter) {
478 // flush
479 flush.run();
480
481 // initialize matrices
482 Kokkos::deep_copy(a, amat);
483 Kokkos::deep_copy(b, bmat);
484 Kokkos::deep_copy(c, 0);
485
486 Kokkos::fence();
487 timer.reset();
488
489 Kokkos::parallel_for("KokkosBatched::PerfTest::GemmCuda::TeamPolicyV3", policy, functor_type(a, b, c));
490
491 Kokkos::fence();
492 const double t = timer.seconds();
493 tmin = std::min(tmin, t);
494 tavg += (iter >= 0) * t;
495 }
496 tavg /= iter_end;
497
498 auto csol = Kokkos::create_mirror_view(typename HostSpaceType::memory_space(), c);
499 Kokkos::deep_copy(csol, c);
500
501 double diff = 0;
502 for (int i = 0, iend = cref.extent(0); i < iend; ++i)
503 for (int j = 0, jend = cref.extent(1); j < jend; ++j)
504 for (int k = 0, kend = cref.extent(2); k < kend; ++k)
505 diff += Kokkos::ArithTraits<value_type>::abs(cref(i, j, k) - csol(i, j, k));
506
507 std::cout << std::setw(8) << "Kokkos" << std::setw(8) << "Team V3"
508 << " BlkSize = " << std::setw(3) << BlkSize << " TeamSize = " << std::setw(3) << team_size
509 << " ScratchSize (KB) = " << std::setw(3) << (per_team_scratch / 1024)
510 << " time = " << std::scientific << tmin << " avg flop/s = " << (flop / tavg)
511 << " max flop/s = " << (flop / tmin);
512 #if defined(__KOKKOSKERNELS_NVIDIA_CUBLAS__)
513 std::cout << " diff to ref = " << diff;
514 #endif
515 std::cout << std::endl;
516 } else {
517 std::cout << std::setw(8) << "Kokkos" << std::setw(8) << "Team V3"
518 << " Scratch per team is too big:" << std::setw(3) << (per_team_scratch / 1024) << std::endl;
519 }
520 }
521 }
522
523 if (1) {
524 ///
525 /// Team policy - handmade
526 ///
527 typedef Kokkos::View<value_type ***, DeviceSpaceType> view_type;
528 view_type a("a", N * VectorLength, BlkSize, BlkSize), b("b", N * VectorLength, BlkSize, BlkSize),
529 c("c", N * VectorLength, BlkSize, BlkSize);
530
531 double tavg = 0, tmin = tmax;
532 {
533 typedef Kokkos::TeamPolicy<DeviceSpaceType, ScheduleType, TeamTagHandmade> policy_type;
534 typedef Functor<view_type, AlgoTagType, VectorLength> functor_type;
535
536 const int max_team_size =
537 policy_type(N, Kokkos::AUTO, VectorLength).team_size_max(functor_type(), Kokkos::ParallelForTag());
538
539 const int team_size = std::min(max_team_size, BlkSize * BlkSize);
540
541 const policy_type policy(N, team_size, VectorLength);
542 for (int iter = iter_begin; iter < iter_end; ++iter) {
543 // flush
544 flush.run();
545
546 // initialize matrices
547 Kokkos::deep_copy(a, amat);
548 Kokkos::deep_copy(b, bmat);
549 Kokkos::deep_copy(c, 0);
550
551 Kokkos::fence();
552 timer.reset();
553
554 Kokkos::parallel_for("KokkosBatched::PerfTest::GemmCuda::TeamPolicyHandmade", policy, functor_type(a, b, c));
555
556 Kokkos::fence();
557 const double t = timer.seconds();
558 tmin = std::min(tmin, t);
559 tavg += (iter >= 0) * t;
560 }
561 tavg /= iter_end;
562
563 auto csol = Kokkos::create_mirror_view(typename HostSpaceType::memory_space(), c);
564 Kokkos::deep_copy(csol, c);
565
566 double diff = 0;
567 for (int i = 0, iend = cref.extent(0); i < iend; ++i)
568 for (int j = 0, jend = cref.extent(1); j < jend; ++j)
569 for (int k = 0, kend = cref.extent(2); k < kend; ++k)
570 diff += Kokkos::ArithTraits<value_type>::abs(cref(i, j, k) - csol(i, j, k));
571
572 std::cout << std::setw(8) << "Kokkos" << std::setw(8) << "Team HM"
573 << " BlkSize = " << std::setw(3) << BlkSize << " TeamSize = " << std::setw(3) << team_size
574 << " ScratchSize (KB) = 0"
575 << " time = " << std::scientific << tmin << " avg flop/s = " << (flop / tavg)
576 << " max flop/s = " << (flop / tmin);
577 #if defined(__KOKKOSKERNELS_NVIDIA_CUBLAS__)
578 std::cout << " diff to ref = " << diff;
579 #endif
580 std::cout << std::endl;
581 }
582 }
583
584 std::cout << std::endl;
585 }
586 } // namespace PerfTest
587 } // namespace KokkosBatched
588
589 using namespace KokkosBatched;
590
591 template <typename AlgoTagType>
592 void run(const int N, const int B) {
593 typedef Kokkos::DefaultExecutionSpace ExecSpace;
594
595 Kokkos::print_configuration(std::cout);
596
597 if (B != 0) {
598 PerfTest::Gemm<ExecSpace, AlgoTagType>(N, B);
599 } else {
600 PerfTest::Gemm<ExecSpace, AlgoTagType>(N, 3);
601 PerfTest::Gemm<ExecSpace, AlgoTagType>(N, 5);
602 PerfTest::Gemm<ExecSpace, AlgoTagType>(N, 10);
603 PerfTest::Gemm<ExecSpace, AlgoTagType>(N, 15);
604
605 // PerfTest::Gemm<ExecSpace, AlgoTagType>(N, 4);
606 // PerfTest::Gemm<ExecSpace, AlgoTagType>(N, 8);
607 // PerfTest::Gemm<ExecSpace, AlgoTagType>(N, 16);
608 // PerfTest::Gemm<ExecSpace, AlgoTagType>(N, 18);
609 }
610 }
611
612 int main(int argc, char *argv[]) {
613 Kokkos::initialize(argc, argv);
614
615 int N = 128 * 128, B = 0;
616
617 for (int i = 1; i < argc; ++i) {
618 const std::string &token = argv[i];
619 if (token == std::string("-N")) N = std::atoi(argv[++i]);
620 if (token == std::string("-B")) B = std::atoi(argv[++i]);
621 }
622
623 {
624 std::cout << " N = " << N << std::endl;
625
626 std::cout << "\n Testing LayoutLeft Algo::Gemm::Unblocked\n";
627 run<Algo::Gemm::Unblocked>(N, B);
628
629 std::cout << "\n Testing LayoutLeft Algo::Gemm::Blocked\n";
630 run<Algo::Gemm::Blocked>(N, B);
631 }
632
633 Kokkos::finalize();
634
635 return 0;
636 }
637
638 #else
639
640 int main(int argc, char *argv[]) {
641 std::cout << "Kokkos::Cuda is not enabled\n";
642 return -1;
643 }
644 #endif