Debian Codegraph @

packages/kokkos-kernels/perf_test/blas/blas3/KokkosBlas3_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 #include "KokkosBlas3_common.hpp"
17 #include "KokkosBlas3_trmm_perf_test.hpp"
18 #include "KokkosBlas3_gemm_perf_test.hpp"
19
20 #include <cstdlib>
21 #include <memory>
22 #include <unistd.h>
23 #include <getopt.h>
24
25 static struct option long_options[] = {{"help", no_argument, 0, 'h'},
26 {"test", required_argument, 0, 't'},
27 {"warm_up_loop", required_argument, 0, 'w'},
28 {"trmm_options", required_argument, 0, 'o'},
29 {"trmm_alpha", required_argument, 0, 'a'},
30 {"gemm_options", required_argument, 0, 'g'},
31 {"gemm_scalars", required_argument, 0, 'p'},
32 {"team_size", required_argument, 0, 'z'},
33 {"vector_len", required_argument, 0, 'n'},
34 {"use_auto", required_argument, 0, 'u'},
35 {"batch_size", required_argument, 0, 'k'},
36 {"batch_size_last_dim", required_argument, 0, 'd'},
37 {"loop_type", required_argument, 0, 'l'},
38 {"matrix_size_start", required_argument, 0, 'b'},
39 {"matrix_size_stop", required_argument, 0, 'e'},
40 {"matrix_size_step", required_argument, 0, 's'},
41 {"iter", required_argument, 0, 'i'},
42 {"csv", required_argument, 0, 'c'},
43 {"routines", required_argument, 0, 'r'},
44 {"verify", required_argument, 0, 'v'},
45 {"ninter", required_argument, 0, 'j'},
46 {"use_simd", required_argument, 0, 'f'},
47 {0, 0, 0, 0}};
48
49 static void __print_help_blas3_perf_test() {
50 printf("Options:\n");
51
52 printf("\t-h, --help\n");
53 printf("\t\tPrint this help menu.\n");
54
55 printf("\t-t, --test=OPTION\n");
56 printf("\t\tAlgorithm selection.\n");
57 printf("\t\t\tValid values for OPTION:\n");
58 for (int i = 0; i < TEST_N; i++) {
59 printf("%c[1m", 27);
60 printf("\t\t\t\t%s", test_e_str[i].c_str());
61 printf("%c[0m", 27);
62 printf("\n");
63 }
64
65 printf("\t-o, --trmm_options=OPTION_STRING\n");
66 printf("\t\tTRMM side, uplo, trans, and diag options.\n");
67 printf(
68 "\t\t\tValid format for OPTION_STRING is \"%%c%%c%%c%%c\". (default: "
69 "%s)\n",
70 DEFAULT_TRMM_ARGS);
71
72 printf("\t-a, --trmm_alpha=SCALAR_VALUE\n");
73 printf("\t\tTRMM alpha value.\n");
74 printf("\t\t\tThe value of alpha in floating point. (default: %lf)\n", DEFAULT_TRMM_ALPHA);
75
76 printf("\t-g, --gemm_options=OPTION_STRING\n");
77 printf("\t\tGEMM transA, and transB options.\n");
78 printf(
79 "\t\t\tValid format for OPTION_STRING is \"%%c%%c\". (default: "
80 "%s)\n",
81 DEFAULT_GEMM_ARGS);
82
83 printf("\t-p, --gemm_scalars=ALPHA_SCALAR_VALUE,BETA_SCALAR_VALUE\n");
84 printf("\t\tGEMM alpha and beta values.\n");
85 printf(
86 "\t\t\tThe value of alpha and beta in floating point. (default: "
87 "%lf,%lf)\n",
88 DEFAULT_GEMM_ALPHA, DEFAULT_GEMM_BETA);
89
90 printf("\t-z, --team_size=SIZE\n");
91 printf("\t\tKokkos team size.\n");
92 printf("\t\t\tThe value of SIZE as an integer. (default: %d)\n", DEFAULT_TEAM_SIZE);
93
94 printf("\t-n, --vector_len=LEN\n");
95 printf("\t\tKokkos vector length (Heirarchical parallelism).\n");
96 printf("\t\t\tThe value of LEN as an integer. (default: %d)\n", DEFAULT_VECTOR_LEN);
97
98 printf("\t-u, --use_auto=AUTO\n");
99 printf(
100 "\t\tWhether to use Kokkos::AUTO for vector_len and team_size "
101 "(Heirarchical parallelism).\n");
102 printf(
103 "\t\t\tValid values for AUTO are 1 to use Kokkos::AUTO and 0 to use "
104 "--vector_len and --team_size "
105 "instead. (default: %d)\n",
106 DEFAULT_USE_AUTO);
107
108 printf("\t-k, --batch_size=LEN\n");
109 printf("\t\tBatch size. Adds third dimension to matrices A, B, and C.\n");
110 printf("\t\t\tThe value of LEN as an integer. (default: %d)\n", DEFAULT_K);
111
112 printf("\t-d, --batch_size_last_dim=LAST_DIM\n");
113 printf("\t\tHow to allocate the batch_size in the matrices.\n");
114 printf(
115 "\t\t\tValid values for LAST_DIM are 1 make the batch_size the last "
116 "dimension and 0 to make the batch_size "
117 "the first dimension (default: %d)\n",
118 DEFAULT_BATCH_SIZE_LAST_DIM);
119
120 printf("\t-l, --loop_type=OPTION\n");
121 printf("\t\tLoop selection.\n");
122 printf("\t\t\tValid values for OPTION:\n");
123 printf("%c[1m", 27);
124 printf("\t\t\t\tserial:");
125 printf("%c[0m", 27);
126 printf(" invoke blas routine in a serial for-loop. (default)\n");
127 printf("%c[1m", 27);
128 printf("\t\t\t\tparallel:");
129 printf("%c[0m", 27);
130 printf(" invoke blas routine in a Kokkos::parallel_for-loop.\n");
131
132 printf("\t-b, --matrix_size_start=MxN,IxJ,PxQ\n");
133 printf(
134 "\t\tMatrix size selection where A is MxN, B is IxJ, and C is PxQ "
135 "(start)\n");
136 printf(
137 "\t\t\tValid values for M and N are any non-negative 32-bit integers. "
138 "(default: %dx%d,%dx%d,%dx%d)\n",
139 DEFAULT_MATRIX_START, DEFAULT_MATRIX_START, DEFAULT_MATRIX_START, DEFAULT_MATRIX_START, DEFAULT_MATRIX_START,
140 DEFAULT_MATRIX_START);
141
142 printf("\t-e, --matrix_size_stop=SxT,LxK,OxR\n");
143 printf(
144 "\t\tMatrix size selection where A is SxT, B is LxK, and C is OxR "
145 "(stop)\n");
146 printf(
147 "\t\t\tValid dimension values are any non-negative 32-bit integers. "
148 "(default: %dx%d,%dx%d,%dx%d)\n",
149 DEFAULT_MATRIX_STOP, DEFAULT_MATRIX_STOP, DEFAULT_MATRIX_STOP, DEFAULT_MATRIX_STOP, DEFAULT_MATRIX_STOP,
150 DEFAULT_MATRIX_STOP);
151
152 printf("\t-s, --matrix_size_step=K\n");
153 printf("\t\tMatrix step selection.\n");
154 printf(
155 "\t\t\tValid value for K is any non-negative 32-bit integer. (default: "
156 "%d)\n",
157 DEFAULT_STEP);
158
159 printf("\t-w, --warm_up_loop=LOOP\n");
160 printf("\t\tWarm up loop selection. (untimed)\n");
161 printf(
162 "\t\t\tValid value for LOOP is any non-negative 32-bit integer that's <= "
163 "ITER. (default: %d)\n",
164 DEFAULT_WARM_UP_N);
165
166 printf("\t-i, --iter=ITER\n");
167 printf("\t\tIteration selection. (timed)\n");
168 printf(
169 "\t\t\tValid value for ITER is any non-negative 32-bit integer. "
170 "(default: %d)\n",
171 DEFAULT_N);
172
173 printf("\t-c, --csv=/path/to/file.csv\n");
174 printf("\t\tCsv output file selection.\n");
175 printf(
176 "\t\t\tValid value for /path/to/file.csv is any valid file name. "
177 "(default: stdout)\n");
178
179 printf("\t-r, --routines=ROUTINES\n");
180 printf("\t\tRoutine selection.\n");
181 printf(
182 "\t\t\tValid value for ROUTINES is one of more valid blas3 routines "
183 "delimited by a comma. (default: %s)\n",
184 DEFAULT_BLAS_ROUTINES);
185
186 printf("\t-v, --verify=VERIFY\n");
187 printf("\t\tVerification selection. (untimed)\n");
188 printf(
189 "\t\t\tValid values for VERIFY are either 0 to skip verification or 1 to "
190 "verify before timing. "
191 "(default: %d)\n",
192 DEFAULT_VERIFY);
193
194 printf("\t-j, --ninter=NINTER\n");
195 printf("\t\tInterleaving size for armpl. (untimed)\n");
196 printf(
197 "\t\t\tValid values for NINTER is any positive integer "
198 "that evenly divides the batch size. "
199 "(default: %d)\n",
200 DEFAULT_NINTER);
201
202 printf("\t-u, --use_simd=SIMD\n");
203 printf("\t\tWhether to use SIMD views.\n");
204 printf(
205 "\t\t\tValid values for SIMD are 1 to use SIMD views and 0 to use "
206 "non-SIMD"
207 "views instead. (default: %d)\n",
208 DEFAULT_USE_SIMD);
209 }
210
211 static void __blas3_perf_test_input_error(char ** /*argv*/, char short_opt, char *getopt_optarg) {
212 fprintf(stderr, "ERROR: invalid option \"-%c %s\". Try --help.\n", short_opt, getopt_optarg);
213 exit(-EINVAL);
214 }
215
216 int main(int argc, char **argv) {
217 options_t options;
218 int option_idx = 0, ret, i;
219 char *n_str = nullptr, *adim = nullptr, *bdim = nullptr, *cdim = nullptr;
220 std::filebuf fb;
221 std::ostream out(&fb);
222 char *out_file = nullptr;
223 using rt_type = decltype(do_trmm_invoke);
224 rt_type *routine_table[BLAS_ROUTINES_N] = {
225 &do_trmm_invoke, &do_gemm_invoke
226 // ADD MORE BLAS3 ROUTINES HERE
227 };
228
229 /* set default options */
230 options.test = DEFAULT_TEST;
231 options.loop = DEFAULT_LOOP;
232 options.start.a.k = DEFAULT_K;
233 options.start.a.m = DEFAULT_MATRIX_START;
234 options.start.a.n = DEFAULT_MATRIX_START;
235 options.stop.a.k = DEFAULT_K;
236 options.stop.a.m = DEFAULT_MATRIX_STOP;
237 options.stop.a.n = DEFAULT_MATRIX_STOP;
238 options.start.b.k = DEFAULT_K;
239 options.start.b.m = DEFAULT_MATRIX_START;
240 options.start.b.n = DEFAULT_MATRIX_START;
241 options.stop.b.k = DEFAULT_K;
242 options.stop.b.m = DEFAULT_MATRIX_STOP;
243 options.stop.b.n = DEFAULT_MATRIX_STOP;
244 options.start.c.k = DEFAULT_K;
245 options.start.c.m = DEFAULT_MATRIX_START;
246 options.start.c.n = DEFAULT_MATRIX_START;
247 options.stop.c.k = DEFAULT_K;
248 options.stop.c.m = DEFAULT_MATRIX_STOP;
249 options.stop.c.n = DEFAULT_MATRIX_STOP;
250 options.step = DEFAULT_STEP;
251 options.warm_up_n = DEFAULT_WARM_UP_N;
252 options.n = DEFAULT_N;
253 options.out = DEFAULT_OUT;
254 options.blas_routines = std::string(DEFAULT_BLAS_ROUTINES);
255 options.blas_args.team_size = DEFAULT_TEAM_SIZE;
256 options.blas_args.vector_len = DEFAULT_VECTOR_LEN;
257 options.blas_args.use_auto = DEFAULT_USE_AUTO;
258 options.blas_args.batch_size_last_dim = DEFAULT_BATCH_SIZE_LAST_DIM;
259 options.verify = DEFAULT_VERIFY;
260 options.ninter = DEFAULT_NINTER;
261 options.use_simd = DEFAULT_USE_SIMD;
262
263 options.blas_args.trmm.trmm_args = DEFAULT_TRMM_ARGS;
264 options.blas_args.trmm.alpha = DEFAULT_TRMM_ALPHA;
265
266 options.blas_args.gemm.gemm_args = DEFAULT_GEMM_ARGS;
267 options.blas_args.gemm.alpha = DEFAULT_GEMM_ALPHA;
268 options.blas_args.gemm.beta = DEFAULT_GEMM_BETA;
269
270 while ((ret = getopt_long(argc, argv, "ht:l:b:e:s:w:i:o:a:c:r:g:z:n:k:u:p:d:v:j:f:", long_options, &option_idx)) !=
271 -1) {
272 switch (ret) {
273 case 'h': __print_help_blas3_perf_test(); return 0;
274 case 't':
275 for (i = 0; i < TEST_N; i++) {
276 if (!test_e_str[i].compare(optarg)) {
277 options.test = (test_e)i;
278 break;
279 }
280 }
281 if (i == TEST_N) {
282 __blas3_perf_test_input_error(argv, ret, optarg);
283 }
284 break;
285 case 'o':
286 // printf("optarg=%s. %d\n", optarg, strncasecmp(optarg, "blas", 4));
287 if (strlen(optarg) != 4) {
288 __blas3_perf_test_input_error(argv, ret, optarg);
289 }
290 options.blas_args.trmm.trmm_args = optarg;
291 break;
292 case 'g':
293 // printf("optarg=%s. %d\n", optarg, strncasecmp(optarg, "blas", 4));
294 if (strlen(optarg) != 2) {
295 __blas3_perf_test_input_error(argv, ret, optarg);
296 }
297 options.blas_args.gemm.gemm_args = optarg;
298 break;
299 case 'p':
300 // printf("optarg=%s. %d\n", optarg, strncasecmp(optarg, "blas", 4));
301 double alpha, beta;
302 if (sscanf(optarg, "%lf,%lf", &alpha, &beta) != 2) __blas3_perf_test_input_error(argv, ret, optarg);
303
304 options.blas_args.gemm.alpha = static_cast<KokkosKernels::default_scalar>(alpha);
305 options.blas_args.gemm.beta = static_cast<KokkosKernels::default_scalar>(beta);
306 break;
307 case 'a':
308 // printf("optarg=%s. %d\n", optarg, strncasecmp(optarg, "blas", 4));
309 options.blas_args.trmm.alpha = (KokkosKernels::default_scalar)atof(optarg);
310 break;
311 case 'l':
312 for (i = 0; i < LOOP_N; i++) {
313 if (!loop_e_str[i].compare(optarg)) {
314 options.loop = (loop_e)i;
315 break;
316 }
317 }
318 if (i == LOOP_N) {
319 __blas3_perf_test_input_error(argv, ret, optarg);
320 }
321 break;
322 case 'b':
323 adim = optarg;
324 bdim = strcasestr(optarg, ",");
325 bdim[0] = '\0';
326 bdim = &bdim[1];
327 cdim = strcasestr(bdim, ",");
328 cdim[0] = '\0';
329 cdim = &cdim[1];
330
331 n_str = strcasestr(adim, "x");
332 if (n_str == NULL) __blas3_perf_test_input_error(argv, ret, optarg);
333
334 n_str[0] = '\0';
335 options.start.a.m = atoi(adim);
336 options.start.a.n = atoi(&n_str[1]);
337
338 n_str = strcasestr(bdim, "x");
339 if (n_str == NULL) __blas3_perf_test_input_error(argv, ret, optarg);
340
341 n_str[0] = '\0';
342 options.start.b.m = atoi(bdim);
343 options.start.b.n = atoi(&n_str[1]);
344
345 n_str = strcasestr(cdim, "x");
346 if (n_str == NULL) __blas3_perf_test_input_error(argv, ret, optarg);
347
348 n_str[0] = '\0';
349 options.start.c.m = atoi(cdim);
350 options.start.c.n = atoi(&n_str[1]);
351 break;
352 case 'e':
353 adim = optarg;
354 bdim = strcasestr(optarg, ",");
355 bdim[0] = '\0';
356 bdim = &bdim[1];
357 cdim = strcasestr(bdim, ",");
358 cdim[0] = '\0';
359 cdim = &cdim[1];
360
361 n_str = strcasestr(adim, "x");
362 if (n_str == NULL) __blas3_perf_test_input_error(argv, ret, optarg);
363
364 n_str[0] = '\0';
365 options.stop.a.m = atoi(adim);
366 options.stop.a.n = atoi(&n_str[1]);
367
368 n_str = strcasestr(bdim, "x");
369 if (n_str == NULL) __blas3_perf_test_input_error(argv, ret, optarg);
370
371 n_str[0] = '\0';
372 options.stop.b.m = atoi(bdim);
373 options.stop.b.n = atoi(&n_str[1]);
374
375 n_str = strcasestr(cdim, "x");
376 if (n_str == NULL) __blas3_perf_test_input_error(argv, ret, optarg);
377
378 n_str[0] = '\0';
379 options.stop.c.m = atoi(cdim);
380 options.stop.c.n = atoi(&n_str[1]);
381 break;
382 case 's': options.step = atoi(optarg); break;
383 case 'w': options.warm_up_n = atoi(optarg); break;
384 case 'i': options.n = atoi(optarg); break;
385 case 'k':
386 options.start.a.k = options.start.b.k = options.start.c.k = options.stop.a.k = options.stop.b.k =
387 options.stop.c.k = atoi(optarg);
388 break;
389 case 'd': options.blas_args.batch_size_last_dim = atoi(optarg); break;
390 case 'v': options.verify = atoi(optarg); break;
391 case 'j': options.ninter = atoi(optarg); break;
392 case 'z': options.blas_args.team_size = atoi(optarg); break;
393 case 'n': options.blas_args.vector_len = atoi(optarg); break;
394 case 'u': options.blas_args.use_auto = atoi(optarg); break;
395 case 'f': options.use_simd = atoi(optarg); break;
396 case 'c':
397 out_file = optarg;
398 options.out_file = std::string(out_file);
399 break;
400 case 'r': options.blas_routines = optarg; break;
401 case '?':
402 default: __blas3_perf_test_input_error(argv, ret, optarg);
403 }
404 }
405
406 if (out_file != nullptr) {
407 fb.open(out_file, std::ios::out);
408 options.out = &out;
409 }
410
411 if (options.warm_up_n > options.n) {
412 fprintf(stderr, "ERROR: warm_up_n=%d > n=%d. Try --help.\n", options.warm_up_n, options.n);
413 exit(-EINVAL);
414 }
415
416 Kokkos::initialize(argc, argv);
417 atexit(Kokkos::finalize);
418
419 int err = 0;
420 for (i = 0; i < BLAS_ROUTINES_N; i++) {
421 if (options.blas_routines.find(blas_routines_e_str[i]) != std::string::npos) {
422 std::cout << "Testing " << blas_routines_e_str[i] << "..." << std::endl;
423
424 auto routine = routine_table[i];
425
426 if (!routine || !routine[0][options.loop][options.test]) {
427 std::cerr << "do_" << blas_routines_e_str[i] << "_invoke[";
428 err = 1;
429 break;
430 }
431 routine[0][options.loop][options.test](options);
432 }
433 }
434
435 if (err) {
436 std::cerr << loop_e_str[options.loop] << "][" << test_e_str[options.test] << "] not yet implemented!" << std::endl;
437 exit(-EINVAL);
438 }
439
440 if (out_file != nullptr) fb.close();
441
442 return 0;
443 }