Debian Codegraph @

packages/muelu/test/scaling/MueLu_PerfModelReporter.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // MueLu: A package for multigrid based preconditioning
4 //
5 // Copyright 2012 NTESS and the MueLu contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #ifndef MUELU_PERFMODEL_REPORTER_HPP
11 #define MUELU_PERFMODEL_REPORTER_HPP
12 #include <vector>
13
14 #include <Teuchos_RCP.hpp>
15 #include <Xpetra_Matrix.hpp>
16
17 #include "MueLu_PerfModels.hpp"
18
19 namespace MueLu {
20
21 // =========================================================================
22 // Performance Routines
23 // =========================================================================
24 // Report bandwidth in GB / sec
25
26 /* @brief Generate performance model reports for SPMV of a given matrix
27 Input:
28 A - Matrix to evaluate
29 nrepeat - # times to repeat the tests in model construction
30 timer_names - Names of Teuchos timers which are SPMVs to compare agains the model
31 verbose - Enable verbose output
32 */
33 template <class Matrix>
34 void report_spmv_performance_models(const Teuchos::RCP<const Matrix> &A, int nrepeat, const std::vector<const char *> &timer_names, Teuchos::RCP<Teuchos::TimeMonitor> &myTimeMonitor, const std::string prefix = "", bool verbose = false) {
35 using Teuchos::RCP;
36 using Teuchos::rcp;
37 const RCP<const Teuchos::Comm<int> > comm = A->getMap()->getComm();
38 using SC = typename Matrix::scalar_type;
39 using LO = typename Matrix::local_ordinal_type;
40 using GO = typename Matrix::global_ordinal_type;
41 using NO = typename Matrix::node_type;
42
43 const double GB = 1024.0 * 1024.0 * 1024.0;
44
45 // Conversion function as a lambda
46 auto convert_time_to_bandwidth_gbs = [=](double time, int num_calls, double memory_per_call_bytes) {
47 double time_per_call = time / num_calls;
48
49 return memory_per_call_bytes / GB / time_per_call;
50 };
51
52 // NOTE: We've hardwired this to size_t for the rowptr. This really should really get read out of a typedef,
53 // if Tpetra actually had one
54 using rowptr_type = size_t;
55
56 // Make a new model if we need one
57 MueLu::PerfModels<SC, LO, GO, NO> PM;
58
59 int rank = comm->getRank();
60 int nproc = comm->getSize();
61 int m = static_cast<int>(A->getLocalNumRows());
62 int n = static_cast<int>(A->getColMap()->getLocalNumElements());
63 int nnz = static_cast<int>(A->getLocalMatrixHost().graph.entries.extent(0));
64
65 // Generate Lookup Tables
66 int v_log_max = ceil(log(nnz) / log(2)) + 1;
67 PM.stream_vector_make_table(nrepeat, v_log_max);
68
69 int m_log_max = 15;
70 PM.pingpong_make_table(nrepeat, m_log_max, comm);
71
72 std::string rank_prefix = prefix + std::to_string(rank) + std::string(": ");
73
74 if (A->hasCrsGraph()) {
75 auto importer = A->getCrsGraph()->getImporter();
76 if (!importer.is_null()) {
77 size_t recv_size = importer->getRemoteLIDs().size() * sizeof(SC);
78 size_t send_size = importer->getExportLIDs().size() * sizeof(SC);
79 int local_log_max = ceil(log(std::max(send_size, recv_size)) / log(2)) + 1;
80 int global_log_max = local_log_max;
81 Teuchos::reduceAll<int>(*comm, Teuchos::REDUCE_MAX, 1, &local_log_max, &global_log_max);
82 PM.halopong_make_table(nrepeat, global_log_max, importer);
83 }
84 }
85
86 if (verbose) {
87 std::cout << prefix << "********************************************************" << std::endl;
88 std::cout << prefix << "Performance model results on " << nproc << " ranks" << std::endl;
89 if (rank == 0) {
90 std::cout << rank_prefix << " ****** Launch Latency Table ******" << std::endl;
91 PM.print_launch_latency_table(std::cout, rank_prefix);
92 std::cout << rank_prefix << "****** Stream Table ******" << std::endl;
93 PM.print_stream_vector_table(std::cout, rank_prefix);
94 std::cout << rank_prefix << "****** Latency Corrected Stream Table ******" << std::endl;
95 PM.print_latency_corrected_stream_vector_table(std::cout, rank_prefix);
96 std::cout << rank_prefix << "****** Pingpong Table ******" << std::endl;
97 PM.print_pingpong_table(std::cout, rank_prefix);
98 std::cout << rank_prefix << "****** Halopong Table ******" << std::endl;
99 PM.print_halopong_table(std::cout, rank_prefix);
100 }
101 }
102
103 // For convenience
104 const int NUM_TIMERS = 6;
105 std::string SPMV_test_names[NUM_TIMERS] = {"colind", "rowptr", "vals", "x", "y", "all"};
106 std::vector<int> SPMV_num_objects(NUM_TIMERS), SPMV_object_size(NUM_TIMERS), SPMV_corrected(NUM_TIMERS);
107
108 // Composite model: Use latency correction
109 SPMV_num_objects[0] = nnz;
110 SPMV_object_size[0] = sizeof(LO);
111 SPMV_corrected[0] = 1; // colind
112 SPMV_num_objects[1] = (m + 1);
113 SPMV_object_size[1] = sizeof(rowptr_type);
114 SPMV_corrected[1] = 1; // rowptr
115 SPMV_num_objects[2] = nnz;
116 SPMV_object_size[2] = sizeof(SC);
117 SPMV_corrected[2] = 1; // vals
118 SPMV_num_objects[3] = n;
119 SPMV_object_size[3] = sizeof(SC);
120 SPMV_corrected[3] = 1; // x
121 SPMV_num_objects[4] = m;
122 SPMV_object_size[4] = sizeof(SC);
123 SPMV_corrected[4] = 1; // y
124
125 // All-Model: Do not use latency correction
126 SPMV_object_size[5] = 1;
127 SPMV_num_objects[5] = (m + 1) * sizeof(rowptr_type) + nnz * sizeof(LO) + nnz * sizeof(SC) +
128 n * sizeof(SC) + m * sizeof(SC);
129 SPMV_corrected[5] = 0;
130
131 std::vector<double> gb_per_sec(NUM_TIMERS);
132 if (verbose && rank == 0)
133 std::cout << rank_prefix << "****** Local Time Model Results ******" << std::endl;
134 for (int i = 0; i < NUM_TIMERS; i++) {
135 double avg_time;
136
137 // Table interpolation - Take the faster of the two
138 int size_in_bytes = SPMV_object_size[i] * SPMV_num_objects[i];
139 if (SPMV_corrected[i] == 1)
140 avg_time = PM.latency_corrected_stream_vector_lookup(size_in_bytes);
141 else
142 avg_time = PM.stream_vector_lookup(size_in_bytes);
143
144 // The lookup divides by transactions-per-element already
145 double memory_traffic = (double)SPMV_object_size[i] * (double)SPMV_num_objects[i];
146 gb_per_sec[i] = convert_time_to_bandwidth_gbs(avg_time, 1, memory_traffic);
147
148 if (verbose) {
149 std::cout << rank_prefix << "Local: " << SPMV_test_names[i] << " # Scalars = " << memory_traffic / sizeof(SC) << " time per call = " << avg_time * 1e6 << " us. GB/sec = " << gb_per_sec[i] << std::endl;
150 }
151 }
152
153 // Get the latency info
154 double avg_latency = PM.launch_latency_lookup();
155
156 /***************************************************************************/
157 // *** Calculate SPMV minimum time (composite) ***
158 // Model:
159 // rowptr = One read per row
160 // colind = One read per entry
161 // values = One read per entry
162 // x = One read per entry in values array (but we assume the cache will work its magic here)
163 // y = One write per row
164
165 long unsigned int spmv_memory_bytes[NUM_TIMERS] = {
166 (m + 1) * sizeof(rowptr_type), // rowptr
167 nnz * sizeof(LO), // colind
168 nnz * sizeof(SC), // values
169 n * sizeof(SC), // x
170 m * sizeof(SC), // y
171 0};
172 for (int i = 0; i < NUM_TIMERS - 1; i++)
173 spmv_memory_bytes[NUM_TIMERS - 1] += spmv_memory_bytes[i];
174
175 double minimum_local_composite_time = avg_latency;
176 for (int i = 0; i < NUM_TIMERS - 1; i++)
177 minimum_local_composite_time += spmv_memory_bytes[i] / (GB * gb_per_sec[i]);
178
179 double minimum_local_all_time = spmv_memory_bytes[NUM_TIMERS - 1] / (GB * gb_per_sec[NUM_TIMERS - 1]);
180
181 if (verbose) {
182 std::cout << rank_prefix << "Local: composite = " << minimum_local_composite_time * 1e6 << " us.\n"
183 << rank_prefix << "Local: all = " << minimum_local_all_time * 1e6 << " us.\n";
184 }
185
186 /***************************************************************************/
187 // *** Calculate Remote part of the SPMV ***
188 double time_pack_unpack_outofplace = 0.0;
189 double time_pack_unpack_inplace = 0.0;
190 double time_communicate_ping = 0.0;
191 double time_communicate_halo = 0.0;
192 // Note: We'll assume that each of the permutes, remotes and exports is a unified
193 // memory transaction, even though that's not strictly speaking correct.
194 if (A->hasCrsGraph()) {
195 auto importer = A->getCrsGraph()->getImporter();
196 if (!importer.is_null()) {
197 // Sames [pack] - 1 read SC, 1 write SC
198 // NOTE: Only if you're out-of-place
199 size_t num_sames = importer->getNumSameIDs();
200 double same_time = (num_sames == 0) ? 0.0 : 2.0 * PM.latency_corrected_stream_vector_lookup(num_sames * sizeof(SC)) + avg_latency;
201
202 // Permutes [pack] - 2 reads LO [to, from] , 1 read SC [values], 1 write SC [values]
203 size_t num_permutes = importer->getNumPermuteIDs();
204 double permute_time = (num_permutes == 0) ? 0.0 : 2.0 * PM.latency_corrected_stream_vector_lookup(num_permutes * sizeof(LO)) + 2.0 * PM.latency_corrected_stream_vector_lookup(num_permutes * sizeof(SC)) + avg_latency;
205
206 // Exports [pack] - 1 read LO [exportLIDs], 1 read SC [values], 1 write SC [buffer]
207 // This is what Epetra does at least
208 size_t num_exports = importer->getNumExportIDs();
209 double export_time = (num_exports == 0) ? 0.0 : PM.latency_corrected_stream_vector_lookup(num_exports * sizeof(LO)) + 2.0 * PM.latency_corrected_stream_vector_lookup(num_exports * sizeof(SC)) + avg_latency;
210
211 // Remotes [unpack] - 1 read LO [remoteLIDs], 1 read SC [buffer], 1 write SC [values]
212 // NOTE: Only if you're out of place
213 size_t num_remotes = importer->getNumRemoteIDs();
214 double remote_time = (num_remotes == 0) ? 0.0 : PM.latency_corrected_stream_vector_lookup(num_remotes * sizeof(LO)) + 2.0 * PM.latency_corrected_stream_vector_lookup(num_remotes * sizeof(SC)) + avg_latency;
215
216 // Total pack / unpack time
217 time_pack_unpack_outofplace = same_time + permute_time + export_time + remote_time;
218 time_pack_unpack_inplace = permute_time + export_time;
219
220 // We now need to get the size of each message for the ping-pong costs.
221 double send_time = 0.0;
222 double recv_time = 0.0;
223 double halo_time = 0.0;
224 size_t total_send_length = 0, total_recv_length = 0;
225 double avg_size_per_msg = 0.0;
226 RCP<const Xpetra::TpetraImport<LO, GO, NO> > t_importer = Teuchos::rcp_dynamic_cast<const Xpetra::TpetraImport<LO, GO, NO> >(importer);
227 if (!t_importer.is_null()) {
228 RCP<const Tpetra::Import<LO, GO, NO> > tt_i = t_importer->getTpetra_Import();
229 Tpetra::Distributor &distor = tt_i->getDistributor();
230 Teuchos::ArrayView<const size_t> recv_lengths = distor.getLengthsFrom();
231 Teuchos::ArrayView<const size_t> send_lengths = distor.getLengthsTo();
232
233 for (int i = 0; i < (int)send_lengths.size(); i++) {
234 send_time += PM.pingpong_device_lookup(send_lengths[i] * sizeof(SC));
235 total_send_length = send_lengths[i] * sizeof(SC);
236 }
237
238 for (int i = 0; i < (int)recv_lengths.size(); i++) {
239 recv_time += PM.pingpong_device_lookup(recv_lengths[i] * sizeof(SC));
240 total_recv_length = recv_lengths[i] * sizeof(SC);
241 }
242
243 avg_size_per_msg = (double)total_send_length / (2.0 * send_lengths.size()) + (double)total_recv_length / (2.0 * recv_lengths.size());
244 halo_time = PM.halopong_device_lookup(avg_size_per_msg);
245 }
246
247 // NOTE: For now we'll do comm time as the larger of send/recv. Not sure this is
248 // really the optimal thing to do, but we'll start here.
249 time_communicate_ping = std::max(send_time, recv_time);
250 time_communicate_halo = halo_time;
251
252 if (verbose) {
253 std::cout << rank_prefix << "****** Remote Time Model Results ******" << std::endl;
254 std::cout << rank_prefix << "Remote: same = " << same_time * 1e6 << " us.\n"
255 << rank_prefix << "Remote: permutes = " << permute_time * 1e6 << " us.\n"
256 << rank_prefix << "Remote: exports = " << export_time * 1e6 << " us.\n"
257 << rank_prefix << "Remote: remotes = " << remote_time * 1e6 << " us.\n"
258 << rank_prefix << "Remote: sends len = " << total_send_length << " time = " << send_time * 1e6 << " us.\n"
259 << rank_prefix << "Remote: recvs len = " << total_recv_length << " time = " << recv_time * 1e6 << " us.\n"
260 << rank_prefix << "Remote: pack/unpack OOP = " << time_pack_unpack_outofplace * 1e6 << " us.\n"
261 << rank_prefix << "Remote: pack/unpack INP = " << time_pack_unpack_inplace * 1e6 << " us.\n"
262 << rank_prefix << "Remote: ping avg = " << (size_t)avg_size_per_msg << " time = " << time_communicate_ping * 1e6 << " us.\n"
263 << rank_prefix << "Remote: halo avg = " << (size_t)avg_size_per_msg << " time = " << time_communicate_halo * 1e6 << " us.\n"
264 << std::endl;
265 }
266 }
267 }
268 double minimum_time_in_place_ping = time_communicate_ping + time_pack_unpack_inplace;
269 double minimum_time_out_of_place_ping = time_communicate_ping + time_pack_unpack_outofplace;
270 double minimum_time_in_place_halo = time_communicate_halo + time_pack_unpack_inplace;
271 double minimum_time_out_of_place_halo = time_communicate_halo + time_pack_unpack_outofplace;
272
273 /***************************************************************************/
274
275 // Get global average/max time sums
276 constexpr int NUM_TIMES = 12;
277 double avg_times[NUM_TIMES];
278 double max_times[NUM_TIMES];
279 {
280 double comp = minimum_local_composite_time;
281 double alls = minimum_local_all_time;
282 double p_inplace = minimum_time_in_place_ping;
283 double p_ooplace = minimum_time_out_of_place_ping;
284 double h_inplace = minimum_time_in_place_halo;
285 double h_ooplace = minimum_time_out_of_place_halo;
286
287 double all_times_local[NUM_TIMES] = {
288 comp, comp + p_inplace, comp + p_ooplace, comp + h_inplace, comp + h_ooplace, // 0-4
289 alls, alls + p_inplace, alls + p_ooplace, alls + h_inplace, alls + h_ooplace, // 5-9
290 time_communicate_ping, time_communicate_halo}; // 10,11
291
292 if (nproc > 1) {
293 Teuchos::reduceAll(*comm, Teuchos::REDUCE_SUM, NUM_TIMES, &all_times_local[0], &avg_times[0]);
294 Teuchos::reduceAll(*comm, Teuchos::REDUCE_MAX, NUM_TIMES, &all_times_local[0], &max_times[0]);
295 for (int i = 0; i < NUM_TIMES; i++)
296 avg_times[i] /= nproc;
297 } else {
298 for (int i = 0; i < NUM_TIMES; i++)
299 avg_times[i] = max_times[i] = all_times_local[i];
300 }
301
302 if (rank == 0)
303 std::cout << "\n\n========================================================\n"
304 << prefix << "Minimum time model (composite) : " << avg_times[0] * 1e6 << " us.\n"
305 << prefix << "Minimum time model (all) : " << avg_times[5] * 1e6 << " us.\n"
306 << prefix << "Pack/unpack in-place : " << (avg_times[1] - avg_times[0] - avg_times[10]) * 1e6 << "us.\n" // comp+ping_pack_inplace - comp - ping
307 << prefix << "Pack/unpack out-of-place : " << (avg_times[2] - avg_times[0] - avg_times[10]) * 1e6 << "us.\n" // comp+ping_pack_ooplace - comp - ping
308 << prefix << "Communication time (ping) : " << avg_times[10] * 1e6 << "us.\n"
309 << prefix << "Communication time (halo) : " << avg_times[11] * 1e6 << "us." << std::endl;
310 }
311
312 // Iterate through all of the "MV" timers
313
314 if (rank == 0) {
315 if (!myTimeMonitor.is_null() && timer_names.size() > 0) {
316 const std::string l[NUM_TIMES] = {"Comp", "Comp+ping+inplace", "Comp+ping+ooplace", "Comp+halo+inplace", "Comp+halo+ooplace",
317 "All", "All+ping+inplace", "All+ping+ooplace", "All+halo+inplace", "All+halo+ooplace"};
318 const std::string div = {"-------------------"};
319 printf("%-60s %20s %20s %20s %20s %20s %20s %20s %20s %20s %20s\n", "Timer", l[0].c_str(), l[1].c_str(), l[2].c_str(), l[3].c_str(), l[4].c_str(),
320 l[5].c_str(), l[6].c_str(), l[7].c_str(), l[8].c_str(), l[9].c_str());
321 printf("%-60s %20s %20s %20s %20s %20s %20s %20s %20s %20s %20s\n", "-----", div.c_str(), div.c_str(), div.c_str(), div.c_str(), div.c_str(),
322 div.c_str(), div.c_str(), div.c_str(), div.c_str(), div.c_str());
323 for (int i = 0; i < (int)timer_names.size(); i++) {
324 Teuchos::RCP<Teuchos::Time> t = myTimeMonitor->lookupCounter(timer_names[i]);
325 if (!t.is_null()) {
326 double time_per_call = t->totalElapsedTime() / t->numCalls();
327 printf("%-60s %20.2f %20.2f %20.2f %20.2f %20.2f %20.2f %20.2f %20.2f %20.2f %20.2f\n", timer_names[i],
328 max_times[0] / time_per_call, max_times[1] / time_per_call, max_times[2] / time_per_call, max_times[3] / time_per_call, max_times[4] / time_per_call,
329 max_times[5] / time_per_call, max_times[6] / time_per_call, max_times[7] / time_per_call, max_times[8] / time_per_call, max_times[9] / time_per_call);
330 }
331 }
332 } else {
333 std::cout << prefix << "Note: Minimum time model individual timers only work with stacked timers off." << std::endl;
334 }
335 }
336 }
337
338 } // namespace MueLu
339
340 #endif