| 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 |
#include <fstream> |
| 17 |
|
| 18 |
/// Kokkos headers |
| 19 |
#include "Kokkos_Core.hpp" |
| 20 |
|
| 21 |
#include "Kokkos_ArithTraits.hpp" |
| 22 |
#include "KokkosBatched_Util.hpp" |
| 23 |
#include "KokkosBatched_Vector.hpp" |
| 24 |
|
| 25 |
#include "KokkosBatched_Test_Sparse_Helper.hpp" |
| 26 |
|
| 27 |
#include "KokkosBatched_Spmv.hpp" |
| 28 |
#include "KokkosBatched_CrsMatrix.hpp" |
| 29 |
#include "KokkosBatched_Krylov_Handle.hpp" |
| 30 |
#include "KokkosBatched_CG.hpp" |
| 31 |
|
| 32 |
typedef Kokkos::DefaultExecutionSpace exec_space; |
| 33 |
typedef typename exec_space::memory_space memory_space; |
| 34 |
typedef Kokkos::DefaultHostExecutionSpace host_space; |
| 35 |
typedef typename Kokkos::Device<exec_space, memory_space> device; |
| 36 |
|
| 37 |
#include "Functor_TestBatchedTeamVectorCG_1.hpp" |
| 38 |
#include "Functor_TestBatchedTeamVectorCG_2.hpp" |
| 39 |
#include "Functor_TestBatchedTeamVectorCG_3.hpp" |
| 40 |
|
| 41 |
int main(int argc, char *argv[]) { |
| 42 |
Kokkos::initialize(argc, argv); |
| 43 |
{ |
| 44 |
#if defined(KOKKOS_ENABLE_CUDA) && defined(KOKKOSBATCHED_PROFILE) |
| 45 |
cudaProfilerStop(); |
| 46 |
#endif |
| 47 |
Kokkos::print_configuration(std::cout); |
| 48 |
|
| 49 |
// typedef Kokkos::ArithTraits<value_type> ats; |
| 50 |
Kokkos::Timer timer; |
| 51 |
|
| 52 |
/// |
| 53 |
/// input arguments parsing |
| 54 |
/// |
| 55 |
int n_rep_1 = 10; // # of repetitions |
| 56 |
int n_rep_2 = 1000; // # of repetitions |
| 57 |
int team_size = 8; |
| 58 |
int N_team_potential = 8; |
| 59 |
int n_impl = 1; |
| 60 |
bool layout_left = true; |
| 61 |
bool layout_right = false; |
| 62 |
bool monitor_convergence = false; |
| 63 |
int vector_length = 8; |
| 64 |
|
| 65 |
std::string name_A = "A.mm"; |
| 66 |
std::string name_B = "B.mm"; |
| 67 |
|
| 68 |
std::string name_timer = "timers"; |
| 69 |
std::string name_X = "X"; |
| 70 |
std::string name_conv = "res"; |
| 71 |
|
| 72 |
std::vector<int> impls; |
| 73 |
for (int i = 1; i < argc; ++i) { |
| 74 |
const std::string &token = argv[i]; |
| 75 |
if (token == std::string("--help") || token == std::string("-h")) { |
| 76 |
std::cout << "Kokkos Batched CG performance test options:" << std::endl |
| 77 |
<< "-A : Filename of the input batched matrix." << std::endl |
| 78 |
<< "-B : Filename of the input batched right-hand " |
| 79 |
"side." |
| 80 |
<< std::endl |
| 81 |
<< "-X : Filename of the output batched solution." << std::endl |
| 82 |
<< "-res : Filename of the output residual history." << std::endl |
| 83 |
<< "-timers : Filename of the output timers." << std::endl |
| 84 |
<< "-n1 : Number of repetitions of the experience." << std::endl |
| 85 |
<< "-n2 : Number of the kernel calls inside one " |
| 86 |
"experience." |
| 87 |
<< std::endl |
| 88 |
<< "-team_size : Used team size." << std::endl |
| 89 |
<< "-n_implementations: Number of implementations to use: test " |
| 90 |
"all " |
| 91 |
"implementations [0, specified number -1]." |
| 92 |
<< std::endl |
| 93 |
<< "-implementation : Specify only one implementation at a time." << std::endl |
| 94 |
<< " Note: implementation 0 : use scratch pad " |
| 95 |
"only for scalar temporary variable." |
| 96 |
<< std::endl |
| 97 |
<< " Note: implementation 1 : use scratch pad " |
| 98 |
"for scalar temporary variables and for the graph of the " |
| 99 |
"matrices." |
| 100 |
<< std::endl |
| 101 |
<< " Note: implementation 2 : use scratch pad " |
| 102 |
"for scalar and vector temporary variables and for the graph of " |
| 103 |
"the matrices." |
| 104 |
<< std::endl |
| 105 |
<< "-l : Specify left layout." << std::endl |
| 106 |
<< "-r : Specify right layout." << std::endl |
| 107 |
<< "-C : Specify if the convergence is monitored." << std::endl |
| 108 |
<< "-N_team : Specify the number of systems per team." << std::endl |
| 109 |
<< "-vector_length : Specify the vector length." << std::endl |
| 110 |
<< std::endl; |
| 111 |
return 0; |
| 112 |
} |
| 113 |
if (token == std::string("-A")) name_A = argv[++i]; |
| 114 |
if (token == std::string("-B")) name_B = argv[++i]; |
| 115 |
|
| 116 |
if (token == std::string("-X")) name_X = argv[++i]; |
| 117 |
|
| 118 |
if (token == std::string("-res")) name_conv = argv[++i]; |
| 119 |
|
| 120 |
if (token == std::string("-timers")) name_timer = argv[++i]; |
| 121 |
|
| 122 |
if (token == std::string("-n1")) n_rep_1 = std::atoi(argv[++i]); |
| 123 |
if (token == std::string("-n2")) n_rep_2 = std::atoi(argv[++i]); |
| 124 |
if (token == std::string("-team_size")) team_size = std::atoi(argv[++i]); |
| 125 |
if (token == std::string("-n_implementations")) n_impl = std::atoi(argv[++i]); |
| 126 |
if (token == std::string("-implementation")) impls.push_back(std::atoi(argv[++i])); |
| 127 |
if (token == std::string("-l")) { |
| 128 |
layout_left = true; |
| 129 |
layout_right = false; |
| 130 |
} |
| 131 |
if (token == std::string("-r")) { |
| 132 |
layout_left = false; |
| 133 |
layout_right = true; |
| 134 |
} |
| 135 |
if (token == std::string("-C")) monitor_convergence = true; |
| 136 |
if (token == std::string("-N_team")) N_team_potential = std::atoi(argv[++i]); |
| 137 |
if (token == std::string("-vector_length")) vector_length = std::atoi(argv[++i]); |
| 138 |
} |
| 139 |
|
| 140 |
int N, Blk, nnz, ncols; |
| 141 |
|
| 142 |
readSizesFromMM(name_A, Blk, ncols, nnz, N); |
| 143 |
|
| 144 |
if (impls.size() == 0) |
| 145 |
for (int i = 0; i < n_impl; ++i) impls.push_back(i); |
| 146 |
|
| 147 |
std::cout << "N_team_potential = " << N_team_potential << ", n = " << Blk << ", N = " << N |
| 148 |
<< ", team_size = " << team_size << ", vector_length = " << vector_length << std::endl; |
| 149 |
|
| 150 |
// V100 L2 cache 6MB per core |
| 151 |
constexpr size_t LLC_CAPACITY = 80 * 6 * 1024 * 1024; |
| 152 |
KokkosBatched::Flush<LLC_CAPACITY, exec_space> flush; |
| 153 |
|
| 154 |
printf(" :::: CG Testing (N = %d, Blk = %d, nnz = %d, vl = %d, n = %d)\n", N, Blk, nnz, vector_length, n_rep_1); |
| 155 |
|
| 156 |
typedef Kokkos::LayoutRight LR; |
| 157 |
typedef Kokkos::LayoutLeft LL; |
| 158 |
|
| 159 |
using IntView = Kokkos::View<int *, LR>; |
| 160 |
using AMatrixValueViewLR = Kokkos::View<double **, LR>; |
| 161 |
using AMatrixValueViewLL = Kokkos::View<double **, LL>; |
| 162 |
using XYTypeLR = Kokkos::View<double **, LR>; |
| 163 |
using XYTypeLL = Kokkos::View<double **, LL>; |
| 164 |
|
| 165 |
using alphaViewType = Kokkos::View<double *>; |
| 166 |
alphaViewType alphaV("alpha", N); |
| 167 |
alphaViewType betaV("alpha", N); |
| 168 |
|
| 169 |
IntView rowOffsets("values", Blk + 1); |
| 170 |
IntView colIndices("values", nnz); |
| 171 |
AMatrixValueViewLR valuesLR("values", N, nnz); |
| 172 |
AMatrixValueViewLL valuesLL("values", N, nnz); |
| 173 |
|
| 174 |
XYTypeLR xLR("values", N, Blk); |
| 175 |
XYTypeLR yLR("values", N, Blk); |
| 176 |
|
| 177 |
XYTypeLL xLL("values", N, Blk); |
| 178 |
XYTypeLL yLL("values", N, Blk); |
| 179 |
|
| 180 |
if (layout_left) |
| 181 |
printf(" :::: Testing left layout (team_size = %d, vector_length = %d)\n", team_size, vector_length); |
| 182 |
if (layout_right) |
| 183 |
printf(" :::: Testing right layout (team_size = %d, vector_length = %d)\n", team_size, vector_length); |
| 184 |
|
| 185 |
if (layout_left) { |
| 186 |
readCRSFromMM(name_A, valuesLL, rowOffsets, colIndices); |
| 187 |
readArrayFromMM(name_B, yLL); |
| 188 |
} |
| 189 |
if (layout_right) { |
| 190 |
readCRSFromMM(name_A, valuesLR, rowOffsets, colIndices); |
| 191 |
readArrayFromMM(name_B, yLR); |
| 192 |
} |
| 193 |
|
| 194 |
for (auto i_impl : impls) { |
| 195 |
std::vector<double> timers; |
| 196 |
|
| 197 |
int n_skip = 2; |
| 198 |
|
| 199 |
int N_team = N_team_potential; |
| 200 |
|
| 201 |
using ScalarType = typename AMatrixValueViewLL::non_const_value_type; |
| 202 |
using Layout = typename AMatrixValueViewLL::array_layout; |
| 203 |
using EXSP = typename AMatrixValueViewLL::execution_space; |
| 204 |
|
| 205 |
using MagnitudeType = typename Kokkos::ArithTraits<ScalarType>::mag_type; |
| 206 |
|
| 207 |
using Norm2DViewType = Kokkos::View<MagnitudeType **, Layout, EXSP>; |
| 208 |
using Scalar3DViewType = Kokkos::View<ScalarType ***, Layout, EXSP>; |
| 209 |
using IntViewType = Kokkos::View<int *, Layout, EXSP>; |
| 210 |
|
| 211 |
using KrylovHandleType = KokkosBatched::KrylovHandle<Norm2DViewType, IntViewType, Scalar3DViewType>; |
| 212 |
KrylovHandleType handle(N, N_team); |
| 213 |
handle.set_scratch_pad_level(0); |
| 214 |
|
| 215 |
for (int i_rep = 0; i_rep < n_rep_1 + n_skip; ++i_rep) { |
| 216 |
double t_spmv = 0; |
| 217 |
for (int j_rep = 0; j_rep < n_rep_2; ++j_rep) { |
| 218 |
#if defined(KOKKOS_ENABLE_CUDA) && defined(KOKKOSBATCHED_PROFILE) |
| 219 |
cudaProfilerStart(); |
| 220 |
#endif |
| 221 |
exec_space().fence(); |
| 222 |
Kokkos::deep_copy(xLL, 0.0); |
| 223 |
Kokkos::deep_copy(xLR, 0.0); |
| 224 |
flush.run(); |
| 225 |
exec_space().fence(); |
| 226 |
|
| 227 |
if (i_impl == 0 && layout_left) { |
| 228 |
t_spmv += |
| 229 |
Functor_TestBatchedTeamVectorCG_1<exec_space, AMatrixValueViewLL, IntView, XYTypeLL, KrylovHandleType>( |
| 230 |
valuesLL, rowOffsets, colIndices, xLL, yLL, N_team, team_size, vector_length, handle) |
| 231 |
.run(); |
| 232 |
} |
| 233 |
if (i_impl == 1 && layout_left) { |
| 234 |
t_spmv += |
| 235 |
Functor_TestBatchedTeamVectorCG_2<exec_space, AMatrixValueViewLL, IntView, XYTypeLL, KrylovHandleType>( |
| 236 |
valuesLL, rowOffsets, colIndices, xLL, yLL, N_team, team_size, vector_length, handle) |
| 237 |
.run(); |
| 238 |
} |
| 239 |
if (i_impl == 2 && layout_left) { |
| 240 |
t_spmv += |
| 241 |
Functor_TestBatchedTeamVectorCG_3<exec_space, AMatrixValueViewLL, IntView, XYTypeLL, KrylovHandleType>( |
| 242 |
valuesLL, rowOffsets, colIndices, xLL, yLL, N_team, team_size, vector_length, handle) |
| 243 |
.run(); |
| 244 |
} |
| 245 |
if (i_impl == 0 && layout_right) { |
| 246 |
t_spmv += |
| 247 |
Functor_TestBatchedTeamVectorCG_1<exec_space, AMatrixValueViewLR, IntView, XYTypeLR, KrylovHandleType>( |
| 248 |
valuesLR, rowOffsets, colIndices, xLR, yLR, N_team, team_size, vector_length, handle) |
| 249 |
.run(); |
| 250 |
} |
| 251 |
if (i_impl == 1 && layout_right) { |
| 252 |
t_spmv += |
| 253 |
Functor_TestBatchedTeamVectorCG_2<exec_space, AMatrixValueViewLR, IntView, XYTypeLR, KrylovHandleType>( |
| 254 |
valuesLR, rowOffsets, colIndices, xLR, yLR, N_team, team_size, vector_length, handle) |
| 255 |
.run(); |
| 256 |
} |
| 257 |
if (i_impl == 2 && layout_right) { |
| 258 |
t_spmv += |
| 259 |
Functor_TestBatchedTeamVectorCG_3<exec_space, AMatrixValueViewLR, IntView, XYTypeLR, KrylovHandleType>( |
| 260 |
valuesLR, rowOffsets, colIndices, xLR, yLR, N_team, team_size, vector_length, handle) |
| 261 |
.run(); |
| 262 |
} |
| 263 |
exec_space().fence(); |
| 264 |
|
| 265 |
#if defined(KOKKOS_ENABLE_CUDA) && defined(KOKKOSBATCHED_PROFILE) |
| 266 |
cudaProfilerStop(); |
| 267 |
#endif |
| 268 |
} |
| 269 |
if (i_rep > n_skip) timers.push_back(t_spmv / n_rep_2); |
| 270 |
} |
| 271 |
|
| 272 |
{ |
| 273 |
std::ofstream myfile; |
| 274 |
std::string name; |
| 275 |
if (layout_left) name = name_timer + "_" + std::to_string(i_impl) + "_left.txt"; |
| 276 |
if (layout_right) name = name_timer + "_" + std::to_string(i_impl) + "_right.txt"; |
| 277 |
|
| 278 |
myfile.open(name); |
| 279 |
|
| 280 |
for (size_t i = 0; i < timers.size(); ++i) myfile << timers[i] << " "; |
| 281 |
|
| 282 |
myfile << std::endl; |
| 283 |
|
| 284 |
myfile.close(); |
| 285 |
} |
| 286 |
|
| 287 |
double average_time = 0.; |
| 288 |
|
| 289 |
for (size_t i = 0; i < timers.size(); ++i) average_time += timers[i] / timers.size(); |
| 290 |
|
| 291 |
if (layout_left) printf("Left layout: Implementation %d: solve time = %f\n", i_impl, average_time); |
| 292 |
if (layout_right) printf("Right layout: Implementation %d: solve time = %f\n", i_impl, average_time); |
| 293 |
|
| 294 |
if (layout_left) { |
| 295 |
writeArrayToMM(name_X + std::to_string(i_impl) + "_l.mm", xLL); |
| 296 |
} |
| 297 |
if (layout_right) { |
| 298 |
writeArrayToMM(name_X + std::to_string(i_impl) + "_r.mm", xLR); |
| 299 |
} |
| 300 |
if (monitor_convergence) { |
| 301 |
writeArrayToMM(name_conv + std::to_string(i_impl) + ".mm", handle.residual_norms); |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
Kokkos::finalize(); |
| 306 |
|
| 307 |
return 0; |
| 308 |
} |