1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
|
/******************************************************************************
* Copyright (c) Intel Corporation - All rights reserved. *
* This file is part of the LIBXSMM library. *
* *
* For information on the license, see the LICENSE file. *
* Further information: https://github.com/hfp/libxsmm/ *
* SPDX-License-Identifier: BSD-3-Clause *
******************************************************************************/
/* Hans Pabst (Intel Corp.)
******************************************************************************/
#include <libxsmm_source.h>
#if defined(LIBXSMM_OFFLOAD_TARGET)
# pragma offload_attribute(push,target(LIBXSMM_OFFLOAD_TARGET))
#endif
#include <algorithm>
#include <stdexcept>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <vector>
#include <cmath>
#if defined(__MKL) || defined(MKL_DIRECT_CALL_SEQ) || defined(MKL_DIRECT_CALL)
# include <mkl_service.h>
# include <mkl.h>
#endif
#if defined(_OPENMP)
# include <omp.h>
#endif
#if defined(LIBXSMM_OFFLOAD_TARGET)
# pragma offload_attribute(pop)
#endif
#if 0 /* enable padding on a per-matrix basis */
# define PAD(TYPE, VALUE) (LIBXSMM_UP2((VALUE) * sizeof(TYPE), LIBXSMM_ALIGNMENT) / sizeof(TYPE))
#else
# define PAD(TYPE, VALUE) (VALUE)
#endif
#if !defined(RANDOMIZED) && 0
# define RANDOMIZED
#endif
#if !defined(ITYPE)
# define ITYPE double
#endif
#if !defined(OTYPE)
# define OTYPE ITYPE
#endif
#if (LIBXSMM_EQUAL(ITYPE, float) || LIBXSMM_EQUAL(ITYPE, double)) \
&& !defined(MKL_DIRECT_CALL_SEQ) && !defined(MKL_DIRECT_CALL)
LIBXSMM_BLAS_SYMBOL_DECL(ITYPE, gemm)
#endif
int main(int argc, char* argv[])
{
int result = EXIT_SUCCESS;
try {
#if defined(__BLAS) && (0 == __BLAS)
LIBXSMM_UNUSED(argc); LIBXSMM_UNUSED(argv);
throw "LAPACK/BLAS library must be available for this sample code!";
#else /* BLAS available */
const libxsmm_blasint benchmark = (1 < argc ? std::atoi(argv[1]) : 0);
LIBXSMM_BLAS_CONST libxsmm_blasint m = (2 < argc ? std::atoi(argv[2]) : 23);
LIBXSMM_BLAS_CONST libxsmm_blasint k = (4 < argc ? std::atoi(argv[4]) : m);
LIBXSMM_BLAS_CONST libxsmm_blasint n = (3 < argc ? std::atoi(argv[3]) : k);
const libxsmm_blasint q = (5 < argc ? std::atoi(argv[5]) : 0/*auto*/);
const libxsmm_blasint nrepeat = (6 < argc ? std::atoi(argv[6]) : (0 >= q ? 13 : 1));
LIBXSMM_BLAS_CONST libxsmm_blasint lda = m, ldb = k, ldc = m;
LIBXSMM_BLAS_CONST char transa = 'N', transb = 'N';
LIBXSMM_BLAS_CONST OTYPE alpha = 1, beta = 1;
const libxsmm_blasint asize = PAD(ITYPE, lda * k), bsize = PAD(ITYPE, ldb * n), csize = PAD(OTYPE, ldc * n);
const libxsmm_blasint max_size = ((2ULL << 30/*2 GB*/) / ((static_cast<size_t>(asize) + bsize) * sizeof(ITYPE) + csize * sizeof(OTYPE)));
const libxsmm_blasint s = LIBXSMM_MIN(0 < q ? q : max_size, max_size);
const libxsmm_blasint aspace = LIBXSMM_ALIGNMENT / sizeof(ITYPE);
const size_t bwsize = (static_cast<size_t>(asize)/*load*/ + static_cast<size_t>(bsize)/*load*/) * sizeof(ITYPE)
+ (sizeof(OTYPE) * static_cast<size_t>(csize) * 2/*RFO*/);
const double gflops = 2E-9 * s * m * n * k;
#if LIBXSMM_TYPEINFO(ITYPE, FP)
const char ops[] = "FLOPS";
const double scale = 1.0 / s;
#else
const char ops[] = "OPS";
const double scale = 1;
#endif
#if defined(_OPENMP) && !defined(_DEBUG)
const char *const env_check = getenv("CHECK");
const int check = (NULL == env_check ? 0 : atoi(env_check));
#elif (defined(__MKL) || defined(MKL_DIRECT_CALL_SEQ) || defined(MKL_DIRECT_CALL)) && (LIBXSMM_VERSION2(11, 3) <= INTEL_MKL_VERSION)
/*const*/ int check = 1;
#endif
#if defined(LIBXSMM_OFFLOAD_TARGET)
# pragma offload target(LIBXSMM_OFFLOAD_TARGET)
#endif
{
#if defined(_OPENMP)
const libxsmm_blasint chunksize = s / omp_get_max_threads();
#endif
struct raii { // avoid std::vector (first-touch init. causes NUMA issue)
ITYPE *a, *b;
OTYPE *c, *d;
size_t m_size, m_shuffle;
raii(libxsmm_blasint asize_, libxsmm_blasint bsize_, libxsmm_blasint csize_, libxsmm_blasint size_)
: a(new ITYPE[static_cast<size_t>(asize_)]), b(new ITYPE[static_cast<size_t>(bsize_)])
, c(new OTYPE[static_cast<size_t>(csize_)]), d(new OTYPE[static_cast<size_t>(csize_)])
, m_size(static_cast<size_t>(size_)), m_shuffle(libxsmm_shuffle(static_cast<unsigned int>(size_)))
{}
~raii() { delete[] a; delete[] b; delete[] c; delete[] d; }
#if defined(RANDOMIZED)
libxsmm_blasint shuffle(libxsmm_blasint i) const { return (i * m_shuffle) % m_size; }
#else
libxsmm_blasint shuffle(libxsmm_blasint i) const { return i; }
#endif
} helper(s * asize + aspace - 1, s * bsize + aspace - 1, s * csize + aspace - 1, s);
ITYPE *const a = LIBXSMM_ALIGN(helper.a, LIBXSMM_ALIGNMENT);
ITYPE *const b = LIBXSMM_ALIGN(helper.b, LIBXSMM_ALIGNMENT);
OTYPE *const c = LIBXSMM_ALIGN(helper.c, LIBXSMM_ALIGNMENT);
OTYPE *const d = LIBXSMM_ALIGN(helper.d, LIBXSMM_ALIGNMENT);
#if defined(_OPENMP)
const int nthreads = omp_get_max_threads();
# pragma omp parallel for num_threads(nthreads) schedule(static)
#endif
for (libxsmm_blasint i = 0; i < s; ++i) {
LIBXSMM_MATINIT(ITYPE, 42 + helper.shuffle(i), a + static_cast<size_t>(asize) * helper.shuffle(i), m, k, lda, scale);
LIBXSMM_MATINIT(ITYPE, 24 + helper.shuffle(i), b + static_cast<size_t>(bsize) * helper.shuffle(i), k, n, ldb, scale);
LIBXSMM_MATINIT(OTYPE, 22 + i, c + static_cast<size_t>(csize) * i, m, n, ldc, scale);
LIBXSMM_MATINIT(OTYPE, 22 + i, d + static_cast<size_t>(csize) * i, m, n, ldc, scale);
}
#if defined(MKL_ENABLE_AVX512)
mkl_enable_instructions(MKL_ENABLE_AVX512);
#endif
// initialize LIBXSMM
libxsmm_init();
fprintf(stdout, "m=%lli n=%lli k=%lli size=%lli memory=%.1f MB (input=%s output=%s)\n\n",
static_cast<long long>(m), static_cast<long long>(n), static_cast<long long>(k), static_cast<long long>(s),
1.0 * (s * ((static_cast<size_t>(asize) + bsize) * sizeof(ITYPE) + csize * sizeof(OTYPE))) / (1ULL << 20),
LIBXSMM_TYPENAME(ITYPE), LIBXSMM_TYPENAME(OTYPE));
// LAPACK/BLAS3 (warm-up BLAS Library)
#if defined(_OPENMP)
# pragma omp parallel for schedule(static)
#endif
for (libxsmm_blasint i = 0; i < s; ++i) {
LIBXSMM_GEMM_SYMBOL(ITYPE)(&transa, &transb, &m, &n, &k,
&alpha, a + static_cast<size_t>(asize) * helper.shuffle(i), &lda, b + static_cast<size_t>(bsize) * helper.shuffle(i), &ldb,
&beta, c + static_cast<size_t>(csize) * i, &ldc);
}
#if (defined(__MKL) || defined(MKL_DIRECT_CALL_SEQ) || defined(MKL_DIRECT_CALL)) && (LIBXSMM_VERSION2(11, 3) <= INTEL_MKL_VERSION)
std::vector<const ITYPE*> va_array(static_cast<size_t>(s)), vb_array(static_cast<size_t>(s));
std::vector<OTYPE*> vc_array(static_cast<size_t>(s));
const ITYPE* *const a_array = &va_array[0];
const ITYPE* *const b_array = &vb_array[0];
OTYPE* *const c_array = &vc_array[0];
const libxsmm_blasint group_count = 1;
for (libxsmm_blasint i = 0; i < s; ++i) { // setup batched (A,B,C)
a_array[i] = a + static_cast<size_t>(asize) * helper.shuffle(i);
b_array[i] = b + static_cast<size_t>(bsize) * helper.shuffle(i);
c_array[i] = d + static_cast<size_t>(csize) * i;
}
// additional warm-up (also to eventually match the Gold result)
LIBXSMM_TPREFIX(ITYPE,gemm_batch)(&transa, &transb, &m, &n, &k,
&alpha, &a_array[0], &lda, &b_array[0], &ldb,
&beta, &c_array[0], &ldc, &group_count, &s);
#endif
switch (benchmark) {
case 0: { // batched
fprintf(stdout, "Batched (A,B,C)...\n");
const unsigned long long start = libxsmm_timer_tick();
for (libxsmm_blasint r = 0; r < nrepeat; ++r) {
#if defined(_OPENMP)
# pragma omp parallel for num_threads(nthreads) schedule(static)
#endif
for (libxsmm_blasint i = 0; i < s; ++i) {
LIBXSMM_GEMM_SYMBOL(ITYPE)(&transa, &transb, &m, &n, &k,
&alpha, a + static_cast<size_t>(asize) * helper.shuffle(i), &lda, b + static_cast<size_t>(bsize) * helper.shuffle(i), &ldb,
&beta, c + static_cast<size_t>(csize) * i, &ldc);
}
}
const unsigned long long ncycles = libxsmm_timer_ncycles(start, libxsmm_timer_tick());
const double duration = libxsmm_timer_duration(0, ncycles) / nrepeat;
if (0 < duration && 0 != ncycles) {
fprintf(stdout, "\tpseudo-perf.: %.1f %s/cycle\n", (2.0 * k - 1.0) * (static_cast<double>(s) * m * n) / ncycles, ops);
fprintf(stdout, "\tperformance: %.1f G%s/s\n", gflops / duration, ops);
fprintf(stdout, "\tbandwidth: %.1f GB/s\n", s * bwsize / (duration * (1ULL << 30)));
}
fprintf(stdout, "\tduration: %.0f ms\n", 1000.0 * duration);
} /* fallthrough */
#if (defined(__MKL) || defined(MKL_DIRECT_CALL_SEQ) || defined(MKL_DIRECT_CALL)) && (LIBXSMM_VERSION2(11, 3) <= INTEL_MKL_VERSION)
case 1: { // batched indirect
fprintf(stdout, "Indirect (A,B,C)...\n");
const unsigned long long start = libxsmm_timer_tick();
for (libxsmm_blasint r = 0; r < nrepeat; ++r) {
LIBXSMM_TPREFIX(ITYPE,gemm_batch)(&transa, &transb, &m, &n, &k,
&alpha, &a_array[0], &lda, &b_array[0], &ldb,
&beta, &c_array[0], &ldc, &group_count, &s);
}
const unsigned long long ncycles = libxsmm_timer_ncycles(start, libxsmm_timer_tick());
const double duration = libxsmm_timer_duration(0, ncycles) / nrepeat;
if (0 < duration && 0 != ncycles) {
fprintf(stdout, "\tpseudo-perf.: %.1f %s/cycle\n", (2.0 * k - 1.0) * (static_cast<double>(s) * m * n) / ncycles, ops);
fprintf(stdout, "\tperformance: %.1f G%s/s\n", gflops / duration, ops);
fprintf(stdout, "\tbandwidth: %.1f GB/s\n", s * bwsize / (duration * (1ULL << 30)));
}
fprintf(stdout, "\tduration: %.0f ms\n", 1000.0 * duration);
if (0 == (benchmark & 1) && 0 != check) { /* Gold result is available */
libxsmm_matdiff_info diff;
libxsmm_matdiff_clear(&diff);
for (libxsmm_blasint h = 0; h < s; ++h) {
const OTYPE *const u = c + static_cast<size_t>(csize) * h, *const v = c_array[h];
libxsmm_matdiff_info dv;
result = libxsmm_matdiff(&dv, LIBXSMM_DATATYPE(OTYPE), m, n, u, v, &ldc, &ldc);
if (EXIT_SUCCESS == result) libxsmm_matdiff_reduce(&diff, &dv);
}
fprintf(stdout, "\tdiff: L2abs=%f Linf=%f\n", diff.l2_abs, diff.linf_abs);
if (check < diff.l2_rel) {
fprintf(stderr, "FAILED.\n");
result = EXIT_FAILURE;
}
}
}
#endif
break;
case 2: { // streaming A and C
fprintf(stdout, "Streamed (A,C)...\n");
const unsigned long long start = libxsmm_timer_tick();
for (libxsmm_blasint r = 0; r < nrepeat; ++r) {
#if defined(_OPENMP)
# pragma omp parallel for num_threads(nthreads) schedule(static)
#endif
for (libxsmm_blasint i = 0; i < s; ++i) {
LIBXSMM_GEMM_SYMBOL(ITYPE)(&transa, &transb, &m, &n, &k,
&alpha, a + static_cast<size_t>(asize) * helper.shuffle(i), &lda, b, &ldb,
&beta, c + static_cast<size_t>(csize) * i, &ldc);
}
}
const unsigned long long ncycles = libxsmm_timer_ncycles(start, libxsmm_timer_tick());
const double duration = libxsmm_timer_duration(0, ncycles) / nrepeat;
if (0 < duration && 0 != ncycles) {
fprintf(stdout, "\tpseudo-perf.: %.1f %s/cycle\n", (2.0 * k - 1.0) * (static_cast<double>(s) * m * n) / ncycles, ops);
fprintf(stdout, "\tperformance: %.1f G%s/s\n", gflops / duration, ops);
fprintf(stdout, "\tbandwidth: %.1f GB/s\n", s * (bwsize - bsize * sizeof(ITYPE)) / (duration * (1ULL << 30)));
}
fprintf(stdout, "\tduration: %.0f ms\n", 1000.0 * duration);
} /* fallthrough */
#if (defined(__MKL) || defined(MKL_DIRECT_CALL_SEQ) || defined(MKL_DIRECT_CALL)) && (LIBXSMM_VERSION2(11, 3) <= INTEL_MKL_VERSION)
case 3: { // indirect A and C
fprintf(stdout, "Indirect (A,C)...\n");
for (libxsmm_blasint i = 0; i < s; ++i) {
a_array[i] = a + static_cast<size_t>(asize) * helper.shuffle(i);
b_array[i] = b;
c_array[i] = d + static_cast<size_t>(csize) * i;
}
const unsigned long long start = libxsmm_timer_tick();
for (libxsmm_blasint r = 0; r < nrepeat; ++r) {
LIBXSMM_TPREFIX(ITYPE, gemm_batch)(&transa, &transb, &m, &n, &k,
&alpha, &a_array[0], &lda, &b_array[0], &ldb,
&beta, &c_array[0], &ldc, &group_count, &s);
}
const unsigned long long ncycles = libxsmm_timer_ncycles(start, libxsmm_timer_tick());
const double duration = libxsmm_timer_duration(0, ncycles) / nrepeat;
if (0 < duration && 0 != ncycles) {
fprintf(stdout, "\tpseudo-perf.: %.1f %s/cycle\n", (2.0 * k - 1.0) * (static_cast<double>(s) * m * n) / ncycles, ops);
fprintf(stdout, "\tperformance: %.1f G%s/s\n", gflops / duration, ops);
fprintf(stdout, "\tbandwidth: %.1f GB/s\n", s * (bwsize - bsize * sizeof(ITYPE)) / (duration * (1ULL << 30)));
}
fprintf(stdout, "\tduration: %.0f ms\n", 1000.0 * duration);
if (0 == (benchmark & 1) && 0 != check) { /* Gold result is available */
libxsmm_matdiff_info diff;
libxsmm_matdiff_clear(&diff);
for (libxsmm_blasint h = 0; h < s; ++h) {
const OTYPE *const u = c + static_cast<size_t>(csize) * h, *const v = c_array[h];
libxsmm_matdiff_info dv;
result = libxsmm_matdiff(&dv, LIBXSMM_DATATYPE(OTYPE), m, n, u, v, &ldc, &ldc);
if (EXIT_SUCCESS == result) libxsmm_matdiff_reduce(&diff, &dv);
}
fprintf(stdout, "\tdiff: L2abs=%f Linf=%f\n", diff.l2_abs, diff.linf_abs);
if (check < diff.l2_rel) {
fprintf(stderr, "FAILED.\n");
result = EXIT_FAILURE;
}
}
}
#endif
break;
case 4: { // streaming B and C
fprintf(stdout, "Streamed (B,C)...\n");
const unsigned long long start = libxsmm_timer_tick();
for (libxsmm_blasint r = 0; r < nrepeat; ++r) {
#if defined(_OPENMP)
# pragma omp parallel for num_threads(nthreads) schedule(static)
#endif
for (libxsmm_blasint i = 0; i < s; ++i) {
LIBXSMM_GEMM_SYMBOL(ITYPE)(&transa, &transb, &m, &n, &k,
&alpha, a, &lda, b + static_cast<size_t>(bsize) * helper.shuffle(i), &ldb,
&beta, c + static_cast<size_t>(csize) * i, &ldc);
}
}
const unsigned long long ncycles = libxsmm_timer_ncycles(start, libxsmm_timer_tick());
const double duration = libxsmm_timer_duration(0, ncycles) / nrepeat;
if (0 < duration && 0 != ncycles) {
fprintf(stdout, "\tpseudo-perf.: %.1f %s/cycle\n", (2.0 * k - 1.0) * (static_cast<double>(s) * m * n) / ncycles, ops);
fprintf(stdout, "\tperformance: %.1f G%s/s\n", gflops / duration, ops);
fprintf(stdout, "\tbandwidth: %.1f GB/s\n", s * (bwsize - asize * sizeof(ITYPE)) / (duration * (1ULL << 30)));
}
fprintf(stdout, "\tduration: %.0f ms\n", 1000.0 * duration);
} /* fallthrough */
#if (defined(__MKL) || defined(MKL_DIRECT_CALL_SEQ) || defined(MKL_DIRECT_CALL)) && (LIBXSMM_VERSION2(11, 3) <= INTEL_MKL_VERSION)
case 5: { // indirect B and C
fprintf(stdout, "Indirect (B,C)...\n");
for (libxsmm_blasint i = 0; i < s; ++i) {
a_array[i] = a;
b_array[i] = b + static_cast<size_t>(bsize) * helper.shuffle(i);
c_array[i] = d + static_cast<size_t>(csize) * i;
}
const unsigned long long start = libxsmm_timer_tick();
for (libxsmm_blasint r = 0; r < nrepeat; ++r) {
LIBXSMM_TPREFIX(ITYPE, gemm_batch)(&transa, &transb, &m, &n, &k,
&alpha, &a_array[0], &lda, &b_array[0], &ldb,
&beta, &c_array[0], &ldc, &group_count, &s);
}
const unsigned long long ncycles = libxsmm_timer_ncycles(start, libxsmm_timer_tick());
const double duration = libxsmm_timer_duration(0, ncycles) / nrepeat;
if (0 < duration && 0 != ncycles) {
fprintf(stdout, "\tpseudo-perf.: %.1f %s/cycle\n", (2.0 * k - 1.0) * (static_cast<double>(s) * m * n) / ncycles, ops);
fprintf(stdout, "\tperformance: %.1f G%s/s\n", gflops / duration, ops);
fprintf(stdout, "\tbandwidth: %.1f GB/s\n", s * (bwsize - asize * sizeof(ITYPE)) / (duration * (1ULL << 30)));
}
fprintf(stdout, "\tduration: %.0f ms\n", 1000.0 * duration);
if (0 == (benchmark & 1) && 0 != check) { /* Gold result is available */
libxsmm_matdiff_info diff;
libxsmm_matdiff_clear(&diff);
for (libxsmm_blasint h = 0; h < s; ++h) {
const OTYPE *const u = c + static_cast<size_t>(csize) * h, *const v = c_array[h];
libxsmm_matdiff_info dv;
result = libxsmm_matdiff(&dv, LIBXSMM_DATATYPE(OTYPE), m, n, u, v, &ldc, &ldc);
if (EXIT_SUCCESS == result) libxsmm_matdiff_reduce(&diff, &dv);
}
fprintf(stdout, "\tdiff: L2abs=%f Linf=%f\n", diff.l2_abs, diff.linf_abs);
if (check < diff.l2_rel) {
fprintf(stderr, "FAILED.\n");
result = EXIT_FAILURE;
}
}
}
#endif
break;
case 6: { // streaming A and B
fprintf(stdout, "Streamed (A,B)...\n");
const unsigned long long start = libxsmm_timer_tick();
for (libxsmm_blasint r = 0; r < nrepeat; ++r) {
#if defined(_OPENMP)
# pragma omp parallel for num_threads(0 == check ? nthreads : 1) schedule(static)
#endif
for (libxsmm_blasint i = 0; i < s; ++i) {
libxsmm_blasint j = 0;
#if defined(_OPENMP) /* attempt to write to disjunct cachelines */
if (0 == check) j = omp_get_thread_num() * chunksize * csize;
#endif
LIBXSMM_GEMM_SYMBOL(ITYPE)(&transa, &transb, &m, &n, &k,
&alpha, a + static_cast<size_t>(asize) * helper.shuffle(i), &lda, b + static_cast<size_t>(bsize) * helper.shuffle(i), &ldb,
&beta, c + j, &ldc);
}
}
const unsigned long long ncycles = libxsmm_timer_ncycles(start, libxsmm_timer_tick());
const double duration = libxsmm_timer_duration(0, ncycles) / nrepeat;
if (0 < duration && 0 != ncycles) {
fprintf(stdout, "\tpseudo-perf.: %.1f %s/cycle\n", (2.0 * k - 1.0) * (static_cast<double>(s) * m * n) / ncycles, ops);
fprintf(stdout, "\tperformance: %.1f G%s/s\n", gflops / duration, ops);
fprintf(stdout, "\tbandwidth: %.1f GB/s\n", s * (bwsize - sizeof(OTYPE) * csize * 2) / (duration * (1ULL << 30)));
}
fprintf(stdout, "\tduration: %.0f ms\n", 1000.0 * duration);
} /* fallthrough */
#if (defined(__MKL) || defined(MKL_DIRECT_CALL_SEQ) || defined(MKL_DIRECT_CALL)) && (LIBXSMM_VERSION2(11, 3) <= INTEL_MKL_VERSION)
case 7: { // indirect A and B
fprintf(stdout, "Indirect (A,B)...\n");
#if defined(_OPENMP)
# pragma omp parallel for num_threads(0 == check ? nthreads : 1) schedule(static)
#endif
for (libxsmm_blasint i = 0; i < s; ++i) {
a_array[i] = a + static_cast<size_t>(asize) * helper.shuffle(i);
b_array[i] = b + static_cast<size_t>(bsize) * helper.shuffle(i);
#if defined(_OPENMP) /* attempt to write to disjunct cachelines */
if (0 == check) {
c_array[i] = d + static_cast<size_t>(csize) * chunksize * omp_get_thread_num();
}
else
#endif
c_array[i] = d;
}
#if defined(_OPENMP)
omp_set_num_threads(0 == check ? nthreads : 1);
#endif
const unsigned long long start = libxsmm_timer_tick();
for (libxsmm_blasint r = 0; r < nrepeat; ++r) {
LIBXSMM_TPREFIX(ITYPE, gemm_batch)(&transa, &transb, &m, &n, &k,
&alpha, &a_array[0], &lda, &b_array[0], &ldb,
&beta, &c_array[0], &ldc, &group_count, &s);
}
const unsigned long long ncycles = libxsmm_timer_ncycles(start, libxsmm_timer_tick());
const double duration = libxsmm_timer_duration(0, ncycles) / nrepeat;
if (0 < duration && 0 != ncycles) {
fprintf(stdout, "\tpseudo-perf.: %.1f %s/cycle\n", (2.0 * k - 1.0) * (static_cast<double>(s) * m * n) / ncycles, ops);
fprintf(stdout, "\tperformance: %.1f G%s/s\n", gflops / duration, ops);
fprintf(stdout, "\tbandwidth: %.1f GB/s\n", s * (bwsize - sizeof(OTYPE) * csize * 2) / (duration * (1ULL << 30)));
}
fprintf(stdout, "\tduration: %.0f ms\n", 1000.0 * duration);
if (0 == (benchmark & 1) && 0 != check) { /* Gold result is available */
libxsmm_matdiff_info diff;
result = libxsmm_matdiff(&diff, LIBXSMM_DATATYPE(OTYPE), m, n, c, d, &ldc, &ldc);
fprintf(stdout, "\tdiff: L2abs=%f Linf=%f\n", diff.l2_abs, diff.linf_abs);
if (check < diff.l2_rel) {
fprintf(stderr, "FAILED.\n");
result = EXIT_FAILURE;
}
}
}
#endif
break;
case 8: { // cached
fprintf(stdout, "Cached...\n");
const unsigned long long start = libxsmm_timer_tick();
for (libxsmm_blasint r = 0; r < nrepeat; ++r) {
#if defined(_OPENMP)
# pragma omp parallel for num_threads(0 == check ? nthreads : 1) schedule(static)
#endif
for (libxsmm_blasint i = 0; i < s; ++i) {
libxsmm_blasint j = 0;
#if defined(_OPENMP) /* attempt to write to disjunct cachelines */
if (0 == check) j = omp_get_thread_num() * chunksize * csize;
#endif
LIBXSMM_GEMM_SYMBOL(ITYPE)(&transa, &transb, &m, &n, &k,
&alpha, a, &lda, b, &ldb, &beta, c + j, &ldc);
}
}
const unsigned long long ncycles = libxsmm_timer_ncycles(start, libxsmm_timer_tick());
const double duration = libxsmm_timer_duration(0, ncycles) / nrepeat;
if (0 < duration && 0 != ncycles) {
fprintf(stdout, "\tpseudo-perf.: %.1f %s/cycle\n", (2.0 * k - 1.0) * (static_cast<double>(s) * m * n) / ncycles, ops);
fprintf(stdout, "\tperformance: %.1f G%s/s\n", gflops / duration, ops);
}
fprintf(stdout, "\tduration: %.0f ms\n", 1000.0 * duration);
} /* fallthrough */
#if (defined(__MKL) || defined(MKL_DIRECT_CALL_SEQ) || defined(MKL_DIRECT_CALL)) && (LIBXSMM_VERSION2(11, 3) <= INTEL_MKL_VERSION)
case 9: { // indirect cached
fprintf(stdout, "Indirect cached...\n");
#if defined(_OPENMP)
# pragma omp parallel for num_threads(0 == check ? nthreads : 1) schedule(static)
#endif
for (libxsmm_blasint i = 0; i < s; ++i) {
a_array[i] = a; b_array[i] = b;
#if defined(_OPENMP) /* attempt to write to disjunct cachelines */
if (0 == check) {
c_array[i] = d + static_cast<size_t>(csize) * chunksize * omp_get_thread_num();
}
else
#endif
c_array[i] = d;
}
#if defined(_OPENMP)
omp_set_num_threads(0 == check ? nthreads : 1);
#endif
const unsigned long long start = libxsmm_timer_tick();
for (libxsmm_blasint r = 0; r < nrepeat; ++r) {
LIBXSMM_TPREFIX(ITYPE, gemm_batch)(&transa, &transb, &m, &n, &k,
&alpha, &a_array[0], &lda, &b_array[0], &ldb,
&beta, &c_array[0], &ldc, &group_count, &s);
}
const unsigned long long ncycles = libxsmm_timer_ncycles(start, libxsmm_timer_tick());
const double duration = libxsmm_timer_duration(0, ncycles) / nrepeat;
if (0 < duration && 0 != ncycles) {
fprintf(stdout, "\tpseudo-perf.: %.1f %s/cycle\n", (2.0 * k - 1.0) * (static_cast<double>(s) * m * n) / ncycles, ops);
fprintf(stdout, "\tperformance: %.1f G%s/s\n", gflops / duration, ops);
}
fprintf(stdout, "\tduration: %.0f ms\n", 1000.0 * duration);
if (0 == (benchmark & 1) && 0 != check) { /* Gold result is available */
libxsmm_matdiff_info diff;
result = libxsmm_matdiff(&diff, LIBXSMM_DATATYPE(OTYPE), m, n, c, d, &ldc, &ldc);
fprintf(stdout, "\tdiff: L2abs=%f Linf=%f\n", diff.l2_abs, diff.linf_abs);
if (check < diff.l2_rel) {
fprintf(stderr, "FAILED.\n");
result = EXIT_FAILURE;
}
}
}
#endif
break;
default: throw "invalid case selected!";
} /*switch*/
// finalize LIBXSMM
libxsmm_finalize();
fprintf(stdout, "Finished\n");
}
#endif
}
catch(const std::exception& e) {
fprintf(stderr, "Error: %s\n", e.what());
result = EXIT_FAILURE;
}
catch(const char* message) {
fprintf(stderr, "Error: %s\n", message);
result = EXIT_FAILURE;
}
catch(...) {
fprintf(stderr, "Error: unknown exception caught!\n");
result = EXIT_FAILURE;
}
return result;
}
|