Debian Codegraph @

packages/kokkos-kernels/perf_test/batched/sparse/SPMV/KokkosBatched_Test_SPMV.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 <fstream>
18
19 /// Kokkos headers
20 #include "Kokkos_Core.hpp"
21 #include "Kokkos_ArithTraits.hpp"
22 #include "KokkosBatched_Util.hpp"
23
24 #include "KokkosBatched_Test_Sparse_Helper.hpp"
25
26 #include "KokkosBatched_SPMV_View.hpp"
27 #include "KokkosBatched_Spmv.hpp"
28
29 typedef Kokkos::DefaultExecutionSpace exec_space;
30 typedef typename exec_space::memory_space memory_space;
31 typedef Kokkos::DefaultHostExecutionSpace host_space;
32 typedef typename Kokkos::Device<exec_space, memory_space> device;
33
34 template <typename PolicyType, typename DViewType, typename IntView, typename xViewType, typename yViewType,
35 typename alphaViewType, typename betaViewType, int dobeta>
36 struct Functor_TestBatchedTeamVectorSpmv {
37 PolicyType _policy;
38 const alphaViewType _alpha;
39 const DViewType _D;
40 const IntView _r;
41 const IntView _c;
42 const xViewType _X;
43 const betaViewType _beta;
44 const yViewType _Y;
45 int _matrices_per_team;
46
47 KOKKOS_INLINE_FUNCTION
48 Functor_TestBatchedTeamVectorSpmv(PolicyType policy, const alphaViewType &alpha, const DViewType &D, const IntView &r,
49 const IntView &c, const xViewType &X, const betaViewType &beta, const yViewType &Y,
50 const int matrices_per_team)
51 : _policy(policy),
52 _alpha(alpha),
53 _D(D),
54 _r(r),
55 _c(c),
56 _X(X),
57 _beta(beta),
58 _Y(Y),
59 _matrices_per_team(matrices_per_team) {}
60
61 template <typename MemberType>
62 KOKKOS_INLINE_FUNCTION void operator()(const MemberType &member) const {
63 const int first_matrix = static_cast<int>(member.league_rank()) * _matrices_per_team;
64 const int N = _D.extent(0);
65 const int last_matrix = (static_cast<int>(member.league_rank() + 1) * _matrices_per_team < N
66 ? static_cast<int>(member.league_rank() + 1) * _matrices_per_team
67 : N);
68
69 auto alpha_team = Kokkos::subview(_alpha, Kokkos::make_pair(first_matrix, last_matrix));
70 auto D_team = Kokkos::subview(_D, Kokkos::make_pair(first_matrix, last_matrix), Kokkos::ALL);
71 auto X_team = Kokkos::subview(_X, Kokkos::make_pair(first_matrix, last_matrix), Kokkos::ALL);
72 auto beta_team = Kokkos::subview(_beta, Kokkos::make_pair(first_matrix, last_matrix));
73 auto Y_team = Kokkos::subview(_Y, Kokkos::make_pair(first_matrix, last_matrix), Kokkos::ALL);
74
75 using ScratchPadIntView = Kokkos::View<int *, Kokkos::DefaultExecutionSpace::scratch_memory_space>;
76
77 const int n_rows = _r.extent(0) - 1;
78 const int nnz = _c.extent(0);
79
80 ScratchPadIntView cols(member.team_scratch(0), nnz);
81 ScratchPadIntView row_map(member.team_scratch(0), n_rows + 1);
82
83 Kokkos::parallel_for(Kokkos::TeamVectorRange(member, 0, n_rows + 1), [&](const int &i) { row_map(i) = _r(i); });
84
85 Kokkos::parallel_for(Kokkos::TeamVectorRange(member, 0, nnz), [&](const int &i) { cols(i) = _c(i); });
86
87 member.team_barrier();
88
89 if (last_matrix != N && _matrices_per_team == 8)
90 KokkosBatched::TeamVectorSpmv<MemberType, KokkosBatched::Trans::NoTranspose, 8>::template invoke<
91 DViewType, ScratchPadIntView, xViewType, yViewType, alphaViewType, betaViewType, dobeta>(
92 member, alpha_team, D_team, row_map, cols, X_team, beta_team, Y_team);
93 else
94 KokkosBatched::TeamVectorSpmv<MemberType, KokkosBatched::Trans::NoTranspose, 1>::template invoke<
95 DViewType, ScratchPadIntView, xViewType, yViewType, alphaViewType, betaViewType, dobeta>(
96 member, alpha_team, D_team, row_map, cols, X_team, beta_team, Y_team);
97 }
98
99 inline void run() { Kokkos::parallel_for("KokkosSparse::PerfTest::BSpMV", _policy, *this); }
100 };
101
102 int main(int argc, char *argv[]) {
103 Kokkos::initialize(argc, argv);
104 {
105 #if defined(KOKKOS_ENABLE_CUDA) && defined(KOKKOSBATCHED_PROFILE)
106 cudaProfilerStop();
107 #endif
108 Kokkos::print_configuration(std::cout);
109
110 // typedef Kokkos::ArithTraits<value_type> ats;
111 Kokkos::Timer timer;
112
113 ///
114 /// input arguments parsing
115 ///
116 int n_rep_1 = 10; // # of repetitions
117 int n_rep_2 = 1000; // # of repetitions
118 int team_size = 8;
119 int vector_length = 8;
120 int N_team_potential = 8;
121 int n_impl = 1;
122 bool layout_left = true;
123 bool layout_right = false;
124
125 std::string name_A = "A.mm";
126 std::string name_B = "B.mm";
127
128 std::string name_timer = "timers";
129 std::string name_X = "X";
130
131 std::vector<int> impls;
132 for (int i = 1; i < argc; ++i) {
133 const std::string &token = argv[i];
134 if (token == std::string("--help") || token == std::string("-h")) {
135 std::cout << "Kokkos Batched SPMV performance test options:" << std::endl
136 << "-A : Filename of the input batched matrix." << std::endl
137 << "-B : Filename of the input batched right-hand "
138 "side."
139 << std::endl
140 << "-X : Filename of the output batched solution." << std::endl
141 << "-timers : Filename of the output timers." << std::endl
142 << "-n1 : Number of repetitions of the experience." << std::endl
143 << "-n2 : Number of the kernel calls inside one "
144 "experience."
145 << std::endl
146 << "-team_size : Used team size." << std::endl
147 << "-n_implementations: Number of implementations to use: test "
148 "all "
149 "implementations [0, specified number -1]."
150 << std::endl
151 << "-implementation : Specify only one implementation at a time." << std::endl
152 << " Note: implementation 0 : use a Team "
153 "approach where a Team have to apply N_team SPMV. A given team "
154 "applies N_team SPMV sequentially and uses a ThreadRange over "
155 "the row and a VectorRange over the non zero entries of a given "
156 "row."
157 << std::endl
158 << " Note: implementation 1 : use a Team "
159 "approach where a Team have to apply N_team SPMV. A given team "
160 "uses a fused thread vector range policy to loop over the "
161 "independent fibers."
162 << std::endl
163 << " Note: implementation 2 : same as "
164 "implementation 1 but using scratch pad for the graph."
165 << std::endl
166 << " Note: implementation 3 : same as "
167 "implementation 1 but using the kernels from "
168 "batched/sparse/impl/*."
169 << std::endl
170 << "-l : Specify left layout." << std::endl
171 << "-r : Specify right layout." << std::endl
172 << "-N_team : Specify the number of systems per team." << std::endl
173 << "-vector_length : Specify the vector length." << std::endl
174 << std::endl;
175 return 0;
176 }
177 if (token == std::string("-A")) name_A = argv[++i];
178 if (token == std::string("-B")) name_B = argv[++i];
179
180 if (token == std::string("-X")) name_X = argv[++i];
181
182 if (token == std::string("-timers")) name_timer = argv[++i];
183
184 if (token == std::string("-n1")) n_rep_1 = std::atoi(argv[++i]);
185 if (token == std::string("-n2")) n_rep_2 = std::atoi(argv[++i]);
186 if (token == std::string("-vector_length")) vector_length = std::atoi(argv[++i]);
187 if (token == std::string("-N_team")) N_team_potential = std::atoi(argv[++i]);
188 if (token == std::string("-team_size")) team_size = std::atoi(argv[++i]);
189 if (token == std::string("-n_implementations")) n_impl = std::atoi(argv[++i]);
190 if (token == std::string("-implementation")) impls.push_back(std::atoi(argv[++i]));
191 if (token == std::string("-l")) {
192 layout_left = true;
193 layout_right = false;
194 }
195 if (token == std::string("-r")) {
196 layout_left = false;
197 layout_right = true;
198 }
199 }
200
201 int N, Blk, nnz, ncols;
202
203 int internal_vector_length = 2;
204
205 readSizesFromMM(name_A, Blk, ncols, nnz, N);
206
207 if (impls.size() == 0)
208 for (int i = 0; i < n_impl; ++i) impls.push_back(i);
209
210 // V100 L2 cache 6MB per core
211 constexpr size_t LLC_CAPACITY = 80 * 6 * 1024 * 1024;
212 KokkosBatched::Flush<LLC_CAPACITY, exec_space> flush;
213
214 printf(
215 " :::: Testing (N = %d, Blk = %d, nnz = %d, vl = %d, vi = %d, n = "
216 "%d, N_team_potential = %d)\n",
217 N, Blk, nnz, vector_length, internal_vector_length, n_rep_1, N_team_potential);
218
219 typedef Kokkos::LayoutRight LR;
220 typedef Kokkos::LayoutLeft LL;
221
222 using IntView = Kokkos::View<int *, LR>;
223 using AMatrixValueViewLR = Kokkos::View<double **, LR>;
224 using AMatrixValueViewLL = Kokkos::View<double **, LL>;
225 using XYTypeLR = Kokkos::View<double **, LR>;
226 using XYTypeLL = Kokkos::View<double **, LL>;
227
228 using alphaViewType = Kokkos::View<double *>;
229 alphaViewType alphaV("alpha", N);
230 alphaViewType betaV("alpha", N);
231
232 IntView rowOffsets("values", Blk + 1);
233 IntView colIndices("values", nnz);
234 AMatrixValueViewLR valuesLR("values", N, nnz);
235 AMatrixValueViewLL valuesLL("values", N, nnz);
236
237 XYTypeLR xLR("values", N, Blk);
238 XYTypeLR yLR("values", N, Blk);
239
240 XYTypeLL xLL("values", N, Blk);
241 XYTypeLL yLL("values", N, Blk);
242
243 double *s_a = new double[N];
244 double *s_b = new double[N];
245
246 if (layout_left) printf(" :::: Testing left layout (team_size = %d)\n", team_size);
247 if (layout_right) printf(" :::: Testing right layout (team_size = %d)\n", team_size);
248
249 if (layout_left) {
250 readCRSFromMM(name_A, valuesLL, rowOffsets, colIndices);
251 readArrayFromMM(name_B, xLL);
252 }
253 if (layout_right) {
254 readCRSFromMM(name_A, valuesLR, rowOffsets, colIndices);
255 readArrayFromMM(name_B, xLR);
256 }
257
258 auto alphaV_h = Kokkos::create_mirror_view(alphaV);
259 auto betaV_h = Kokkos::create_mirror_view(betaV);
260
261 for (int i = 0; i < N; ++i) {
262 s_a[i] = 1.;
263 s_b[i] = 0.;
264 alphaV_h(i) = s_a[i];
265 betaV_h(i) = s_b[i];
266 }
267
268 Kokkos::deep_copy(alphaV, alphaV_h);
269 Kokkos::deep_copy(betaV, betaV_h);
270
271 using ScratchPadIntView = Kokkos::View<int *, exec_space::scratch_memory_space>;
272
273 for (auto i_impl : impls) {
274 std::vector<double> timers;
275
276 int n_skip = 2;
277
278 for (int i_rep = 0; i_rep < n_rep_1 + n_skip; ++i_rep) {
279 double t_spmv = 0;
280 for (int j_rep = 0; j_rep < n_rep_2; ++j_rep) {
281 #if defined(KOKKOS_ENABLE_CUDA) && defined(KOKKOSBATCHED_PROFILE)
282 cudaProfilerStart();
283 #endif
284 exec_space().fence();
285 if (n_rep_2 != 1) flush.run();
286 exec_space().fence();
287
288 timer.reset();
289 exec_space().fence();
290
291 int N_team = N_team_potential;
292 int number_of_teams = ceil(static_cast<double>(N) / N_team);
293
294 if (layout_left) {
295 using policy_type = Kokkos::TeamPolicy<exec_space>;
296 policy_type auto_policy(number_of_teams, Kokkos::AUTO(), Kokkos::AUTO());
297 policy_type tuned_policy(number_of_teams, team_size, Kokkos::AUTO());
298 policy_type tuned_policy_2(number_of_teams, team_size, vector_length);
299 policy_type policy;
300
301 if (team_size < 1)
302 policy = auto_policy;
303 else if (vector_length < 1)
304 policy = tuned_policy;
305 else
306 policy = tuned_policy_2;
307
308 // std::cout << "auto_policy.team_size() = " <<
309 // auto_policy.team_size() << std::endl;
310
311 size_t bytes_0 = ScratchPadIntView::shmem_size(Blk + 1);
312 size_t bytes_1 = ScratchPadIntView::shmem_size(nnz);
313 if (i_impl > 1) policy.set_scratch_size(0, Kokkos::PerTeam(bytes_0 + bytes_1));
314 // policy.set_scratch_size(1, Kokkos::PerTeam(bytes_1));
315 if (i_impl == 3) {
316 Functor_TestBatchedTeamVectorSpmv<policy_type, AMatrixValueViewLL, IntView, XYTypeLL, XYTypeLL,
317 alphaViewType, alphaViewType, 0>(policy, alphaV, valuesLL, rowOffsets,
318 colIndices, xLL, betaV, yLL, N_team)
319 .run();
320 } else {
321 Kokkos::parallel_for("KokkosSparse::PerfTest::BSpMV", policy,
322 BSPMV_Functor_View<AMatrixValueViewLL, IntView, XYTypeLL, XYTypeLL, 0>(
323 s_a, valuesLL, rowOffsets, colIndices, xLL, s_b, yLL, N_team, N, i_impl));
324 }
325 }
326 if (layout_right) {
327 using policy_type = Kokkos::TeamPolicy<exec_space>;
328 policy_type auto_policy(number_of_teams, Kokkos::AUTO(), Kokkos::AUTO());
329 policy_type tuned_policy(number_of_teams, team_size, Kokkos::AUTO());
330 policy_type tuned_policy_2(number_of_teams, team_size, vector_length);
331 policy_type policy;
332
333 if (team_size < 1)
334 policy = auto_policy;
335 else if (vector_length < 1)
336 policy = tuned_policy;
337 else
338 policy = tuned_policy_2;
339
340 size_t bytes_0 = ScratchPadIntView::shmem_size(Blk + 1);
341 size_t bytes_1 = ScratchPadIntView::shmem_size(nnz);
342 if (i_impl > 1) policy.set_scratch_size(0, Kokkos::PerTeam(bytes_0 + bytes_1));
343 // policy.set_scratch_size(1, Kokkos::PerTeam(bytes_1));
344 if (i_impl == 3) {
345 Functor_TestBatchedTeamVectorSpmv<policy_type, AMatrixValueViewLR, IntView, XYTypeLR, XYTypeLR,
346 alphaViewType, alphaViewType, 0>(policy, alphaV, valuesLR, rowOffsets,
347 colIndices, xLR, betaV, yLR, N_team)
348 .run();
349 } else {
350 Kokkos::parallel_for("KokkosSparse::PerfTest::BSpMV", policy,
351 BSPMV_Functor_View<AMatrixValueViewLR, IntView, XYTypeLR, XYTypeLR, 0>(
352 s_a, valuesLR, rowOffsets, colIndices, xLR, s_b, yLR, N_team, N, i_impl));
353 }
354 }
355 exec_space().fence();
356 t_spmv += timer.seconds();
357 #if defined(KOKKOS_ENABLE_CUDA) && defined(KOKKOSBATCHED_PROFILE)
358 cudaProfilerStop();
359 #endif
360 }
361 if (i_rep > n_skip) timers.push_back(t_spmv / n_rep_2);
362 }
363
364 {
365 std::ofstream myfile;
366 std::string name;
367 if (layout_left) name = name_timer + "_" + std::to_string(i_impl) + "_left.txt";
368 if (layout_right) name = name_timer + "_" + std::to_string(i_impl) + "_right.txt";
369
370 myfile.open(name);
371
372 for (size_t i = 0; i < timers.size(); ++i) myfile << timers[i] << " ";
373
374 myfile << std::endl;
375
376 myfile.close();
377 }
378
379 double average_time = 0.;
380
381 for (size_t i = 0; i < timers.size(); ++i) average_time += timers[i] / timers.size();
382
383 if (layout_left)
384 printf(
385 "Left layout: Implementation %d: solve time = %f , # of SPMV per "
386 "min = %f\n",
387 i_impl, average_time, 1.0 / average_time * 60 * N);
388 if (layout_right)
389 printf(
390 "Right layout: Implementation %d: solve time = %f , # of SPMV per "
391 "min = %f\n",
392 i_impl, average_time, 1.0 / average_time * 60 * N);
393
394 if (layout_left) {
395 writeArrayToMM(name_X + std::to_string(i_impl) + "_l.mm", xLL);
396 }
397 if (layout_right) {
398 writeArrayToMM(name_X + std::to_string(i_impl) + "_r.mm", xLR);
399 }
400 }
401 }
402 Kokkos::finalize();
403
404 return 0;
405 }