Debian Codegraph @

packages/kokkos-kernels/perf_test/blas/blas3/KokkosBlas3_gemm_standalone_perf_test.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
17 #include "KokkosBlas3_gemm.hpp"
18 #include <Kokkos_Random.hpp>
19 #include "KokkosKernels_TestStringUtils.hpp"
20
21 struct Params {
22 int use_cuda = 0;
23 int use_openmp = 0;
24 int use_threads = 0;
25 int m = 1000;
26 int n = 1000;
27 int k = 1000;
28 int repeat = 1;
29 };
30
31 void print_options() {
32 std::cerr << "Options\n" << std::endl;
33
34 std::cerr << "\tBACKEND: '--threads[numThreads]' | '--openmp [numThreads]' | "
35 "'--cuda [cudaDeviceIndex]'"
36 << std::endl;
37 std::cerr << "\tIf none selected, serial is used." << std::endl;
38 std::cerr << "\t[Optional] --repeat :: how many times to repeat overall "
39 "spadd (symbolic + repeated numeric)"
40 << std::endl;
41 std::cerr << "\t[Optional] --m :: Rows in A" << std::endl;
42 std::cerr << "\t[Optional] --n :: Columns in A / Rows in B" << std::endl;
43 std::cerr << "\t[Optional] --k :: Columns in B" << std::endl;
44 }
45
46 int parse_inputs(Params& params, int argc, char** argv) {
47 for (int i = 1; i < argc; ++i) {
48 if (0 == Test::string_compare_no_case(argv[i], "--help") || 0 == Test::string_compare_no_case(argv[i], "-h")) {
49 print_options();
50 exit(0); // note: this is before Kokkos::initialize
51 } else if (0 == Test::string_compare_no_case(argv[i], "--threads")) {
52 params.use_threads = atoi(argv[++i]);
53 } else if (0 == Test::string_compare_no_case(argv[i], "--openmp")) {
54 params.use_openmp = atoi(argv[++i]);
55 } else if (0 == Test::string_compare_no_case(argv[i], "--cuda")) {
56 params.use_cuda = atoi(argv[++i]) + 1;
57 } else if (0 == Test::string_compare_no_case(argv[i], "--m")) {
58 params.m = atoi(argv[++i]);
59 } else if (0 == Test::string_compare_no_case(argv[i], "--n")) {
60 params.n = atoi(argv[++i]);
61 } else if (0 == Test::string_compare_no_case(argv[i], "--k")) {
62 params.k = atoi(argv[++i]);
63 } else if (0 == Test::string_compare_no_case(argv[i], "--repeat")) {
64 // if provided, C will be written to given file.
65 // has to have ".bin", or ".crs" extension.
66 params.repeat = atoi(argv[++i]);
67 } else {
68 std::cerr << "Unrecognized command line argument #" << i << ": " << argv[i] << std::endl;
69 print_options();
70 return 1;
71 }
72 }
73 return 0;
74 }
75
76 template <typename ExecSpace, typename ALayout, typename BLayout>
77 void runImpl(int m, int n, int k, int repeat) {
78 using Scalar = double;
79 using MemSpace = typename ExecSpace::memory_space;
80 using Device = Kokkos::Device<ExecSpace, MemSpace>;
81 Kokkos::View<Scalar**, ALayout, Device> A(Kokkos::view_alloc(Kokkos::WithoutInitializing, "A"), m, n);
82 Kokkos::View<Scalar**, BLayout, Device> B(Kokkos::view_alloc(Kokkos::WithoutInitializing, "B"), n, k);
83 Kokkos::View<Scalar**, Kokkos::LayoutLeft, Device> C(Kokkos::view_alloc(Kokkos::WithoutInitializing, "C"), m, k);
84 Kokkos::Random_XorShift64_Pool<ExecSpace> pool(123);
85 Kokkos::fill_random(A, pool, 10.0);
86 Kokkos::fill_random(B, pool, 10.0);
87 // Do a warm-up run
88 KokkosBlas::gemm("N", "N", 1.0, A, B, 0.0, C);
89 // Now, start timing
90 Kokkos::fence();
91 Kokkos::Timer timer;
92 for (int i = 0; i < repeat; i++) {
93 KokkosBlas::gemm("N", "N", 1.0, A, B, 0.0, C);
94 ExecSpace().fence();
95 }
96 double total = timer.seconds();
97 double avg = total / repeat;
98 size_t flopsPerRun = (size_t)2 * m * n * k;
99 printf("Avg GEMM FLOP/s: %.3e --- Avg time: %f\n", flopsPerRun / avg, avg);
100 }
101
102 template <typename ExecSpace>
103 void run(int m, int n, int k, int repeat) {
104 using LL = Kokkos::LayoutLeft;
105 using LR = Kokkos::LayoutRight;
106 std::cout << "** Running GEMM experiments (" << ExecSpace::name() << " m=" << m << " n=" << n << " k=" << k
107 << ") **\n";
108 std::cout << "Running: A LayoutLeft, B LayoutLeft : ";
109 runImpl<ExecSpace, LL, LL>(m, n, k, repeat);
110 std::cout << "Running: A LayoutLeft, B LayoutRight : ";
111 runImpl<ExecSpace, LL, LR>(m, n, k, repeat);
112 std::cout << "Running: A LayoutRight, B LayoutLeft : ";
113 runImpl<ExecSpace, LR, LL>(m, n, k, repeat);
114 std::cout << "Running: A LayoutRight, B LayoutRight: ";
115 runImpl<ExecSpace, LR, LR>(m, n, k, repeat);
116 }
117
118 int main(int argc, char** argv) {
119 Params params;
120
121 if (parse_inputs(params, argc, argv)) {
122 return 1;
123 }
124 const int num_threads = params.use_openmp; // Assumption is that use_openmp variable is provided
125 // as number of threads
126 const int device_id = params.use_cuda - 1;
127
128 Kokkos::initialize(Kokkos::InitializationSettings().set_num_threads(num_threads).set_device_id(device_id));
129
130 bool useOMP = params.use_openmp != 0;
131 bool useCUDA = params.use_cuda != 0;
132
133 bool useSerial = !useOMP && !useCUDA;
134
135 if (useOMP) {
136 #if defined(KOKKOS_ENABLE_OPENMP)
137 run<Kokkos::OpenMP>(params.m, params.n, params.k, params.repeat);
138 #else
139 std::cout << "ERROR: OpenMP requested, but not available.\n";
140 return 1;
141 #endif
142 }
143 if (useCUDA) {
144 #if defined(KOKKOS_ENABLE_CUDA)
145 run<Kokkos::Cuda>(params.m, params.n, params.k, params.repeat);
146 #else
147 std::cout << "ERROR: CUDA requested, but not available.\n";
148 return 1;
149 #endif
150 }
151 if (useSerial) {
152 #if defined(KOKKOS_ENABLE_SERIAL)
153 run<Kokkos::Serial>(params.m, params.n, params.k, params.repeat);
154 #else
155 std::cout << "ERROR: Serial device requested, but not available.\n";
156 return 1;
157 #endif
158 }
159 Kokkos::finalize();
160 return 0;
161 }