Debian Codegraph @

samples/magazine/magazine_xsmm.c

raw symbols SCIP
1 /******************************************************************************
2 * Copyright (c) Intel Corporation - All rights reserved. *
3 * This file is part of the LIBXSMM library. *
4 * *
5 * For information on the license, see the LICENSE file. *
6 * Further information: https://github.com/hfp/libxsmm/ *
7 * SPDX-License-Identifier: BSD-3-Clause *
8 ******************************************************************************/
9 /* Hans Pabst (Intel Corp.)
10 ******************************************************************************/
11 #include "magazine.h"
12 #if !defined(SHUFFLE)
13 # include <libxsmm.h>
14 #endif
15
16 #if 0 /* auto-dispatch SMM kernel */
17 # define AUTO
18 #endif
19 #if 0 /* disable auto-prefetch */
20 # define NOPREFETCH
21 #endif
22
23
24 int main(int argc, char* argv[])
25 {
26 /* batch-size is used to stream matrix-operands from memory */
27 const int batchsize = (1 < argc ? atoi(argv[1]) : 0/*auto*/);
28 /* default: M, N, and K are 13, 5, and 7 respectively */
29 const libxsmm_blasint m = (2 < argc ? atoi(argv[2]) : 13);
30 const libxsmm_blasint n = (3 < argc ? atoi(argv[3]) : 5);
31 const libxsmm_blasint k = (4 < argc ? atoi(argv[4]) : 7);
32 /* leading dimensions are made multiples of the size of a cache-line */
33 const libxsmm_blasint lda = (5 < argc ? LIBXSMM_MAX(atoi(argv[5]), m) : (libxsmm_blasint)(LIBXSMM_UP2(sizeof(TYPE) * m, PAD) / sizeof(TYPE)));
34 const libxsmm_blasint ldb = (6 < argc ? LIBXSMM_MAX(atoi(argv[6]), k) : (libxsmm_blasint)(LIBXSMM_UP2(sizeof(TYPE) * k, PAD) / sizeof(TYPE)));
35 const libxsmm_blasint ldc = (7 < argc ? LIBXSMM_MAX(atoi(argv[7]), m) : (libxsmm_blasint)(LIBXSMM_UP2(sizeof(TYPE) * m, PAD) / sizeof(TYPE)));
36 /* micro-kernels are limited to certain alpha- and beta-values */
37 const char transa = 'n', transb = 'n';
38 const TYPE alpha = 1, beta = 1;
39 /* calculate matrix sizes incl. padded elements */
40 const size_t na = LIBXSMM_UP2(sizeof(TYPE) * lda * k, PAD) / sizeof(TYPE);
41 const size_t nb = LIBXSMM_UP2(sizeof(TYPE) * ldb * n, PAD) / sizeof(TYPE);
42 const size_t nc = LIBXSMM_UP2(sizeof(TYPE) * ldc * n, PAD) / sizeof(TYPE);
43 /* calculate default batch-size to hit work-set size of approx. 2 GB */
44 const int size = (0 >= batchsize ? (int)((2ULL << 30/*2 GB*/) / (sizeof(TYPE) * (na + nb + nc))) : batchsize);
45 #if defined(SHUFFLE)
46 const size_t shuffle = libxsmm_shuffle((unsigned int)size);
47 #endif
48 /* allocate A, B, and C matrix buffers */
49 TYPE *const a = (TYPE*)libxsmm_aligned_malloc(sizeof(TYPE) * na * size, LIBXSMM_CACHELINE);
50 TYPE *const b = (TYPE*)libxsmm_aligned_malloc(sizeof(TYPE) * nb * size, LIBXSMM_CACHELINE);
51 TYPE *const c = (TYPE*)libxsmm_aligned_malloc(sizeof(TYPE) * nc * size, LIBXSMM_CACHELINE);
52 const double scale = 1.0 / size;
53 libxsmm_timer_tickint start;
54 double duration;
55 int i, j;
56
57 /**
58 * LIBXSMM's C interface really is type-specific, and the helper macros (such as LIBXSMM_MMFUNCTION_TYPE)
59 * are only for "entertainment". The C++ interface on the other hand provides overloaded functions
60 * and some helpers for more type-generic programming tasks (e.g., libxsmm_mmfunction<T>).
61 */
62 #if !defined(AUTO) /* explicitly dispatch a kernel according to parameters */
63 const int flags = LIBXSMM_GEMM_FLAGS(transa, transb);
64 # if !defined(NOPREFETCH) && (STREAM_A(1) || STREAM_B(1) || STREAM_C(1)) /* prefetch */
65 const int prefetch = LIBXSMM_PREFETCH_AUTO;
66 # else
67 const int prefetch = LIBXSMM_PREFETCH_NONE;
68 # endif
69 union { /* convert between fn.ptr and (data)pointer */
70 LIBXSMM_MMFUNCTION_TYPE(TYPE) fun;
71 const void* ptr;
72 } xmm;
73 xmm.fun = LIBXSMM_MMDISPATCH_SYMBOL(TYPE)(m, n, k, &lda, &ldb, &ldc, &alpha, &beta, &flags, &prefetch);
74 #endif
75
76 /* initialize data according to touch-first policy */
77 #if defined(_OPENMP)
78 # pragma omp parallel for private(i, j)
79 #endif
80 for (i = 0; i < size; ++i) {
81 #if defined(SHUFFLE)
82 j = (i * shuffle) % size;
83 #else
84 j = i;
85 #endif
86 init(25 + i, a + j * na, (int)m, (int)k, (int)lda, scale);
87 init(75 + i, b + j * nb, (int)k, (int)n, (int)ldb, scale);
88 if (LIBXSMM_NEQ(0, beta)) { /* no need to initialize for beta=0 */
89 init(42 + i, c + j * nc, (int)m, (int)n, (int)ldc, scale);
90 }
91 }
92
93 #if defined(_OPENMP)
94 # pragma omp parallel
95 #endif
96 { /* OpenMP thread pool is already populated (parallel region) */
97 #if defined(_OPENMP)
98 # pragma omp single
99 #endif
100 start = libxsmm_timer_tick();
101 #if defined(_OPENMP)
102 # pragma omp for private(i, j)
103 #endif
104 for (i = 0; i < size - 1; ++i) {
105 #if defined(SHUFFLE)
106 # if !defined(AUTO) && !defined(NOPREFETCH) && (STREAM_A(1) || STREAM_B(1) || STREAM_C(1)) /* prefetch */
107 const int p = ((i + 1) * shuffle) % size;
108 # endif
109 j = (i * shuffle) % size;
110 #else
111 # if !defined(AUTO) && !defined(NOPREFETCH) && (STREAM_A(1) || STREAM_B(1) || STREAM_C(1)) /* prefetch */
112 const int p = i + 1; /* next location */
113 # endif
114 j = i;
115 #endif
116 #if defined(AUTO)
117 libxsmm_dgemm(&transa, &transb, &m, &n, &k,
118 &alpha, a + STREAM_A(j * na), &lda, b + STREAM_B(j * nb), &ldb,
119 &beta, c + STREAM_C(SYNC(j, nc, size)), &ldc);
120 #elif !defined(NOPREFETCH) && (STREAM_A(1) || STREAM_B(1) || STREAM_C(1)) /* prefetch */
121 xmm.fun(a + STREAM_A(j * na), b + STREAM_B(j * nb), c + STREAM_C(SYNC(j, nc, size)),
122 a + STREAM_A(p * na), b + STREAM_B(p * nb), c + STREAM_C(SYNC(p, nc, size)));
123 #else
124 xmm.fun(a + STREAM_A(j * na), b + STREAM_B(j * nb), c + STREAM_C(SYNC(j, nc, size)));
125 #endif
126 }
127 }
128 #if defined(SHUFFLE)
129 j = ((size - 1) * shuffle) % size;
130 #else
131 j = size - 1;
132 #endif
133 #if defined(AUTO)
134 libxsmm_dgemm(&transa, &transb, &m, &n, &k,
135 &alpha, a + STREAM_A(j * na), &lda, b + STREAM_B(j * nb), &ldb,
136 &beta, c + STREAM_C(SYNC(j, nc, size)), &ldc);
137 #elif !defined(NOPREFETCH) && (STREAM_A(1) || STREAM_B(1) || STREAM_C(1)) /* prefetch */
138 xmm.fun(a + STREAM_A(j * na), b + STREAM_B(j * nb), c + STREAM_C(SYNC(j, nc, size)),
139 a + STREAM_A(j * na), b + STREAM_B(j * nb), c + STREAM_C(SYNC(j, nc, size)));
140 #else
141 xmm.fun(a + STREAM_A(j * na), b + STREAM_B(j * nb), c + STREAM_C(SYNC(j, nc, size)));
142 #endif
143 duration = libxsmm_timer_duration(start, libxsmm_timer_tick());
144
145 if (0 < duration) {
146 libxsmm_kernel_info info;
147 #if defined(AUTO) /* no explicit kernel hence no query */
148 info.nflops = 2 * m * n * k;
149 #else
150 libxsmm_get_kernel_info(xmm.ptr, &info);
151 #endif
152 printf("%.1f GFLOPS/s\n", (1E-9 * info.nflops) / duration * size);
153 }
154 printf("%.1f ms\n", 1000.0 * duration);
155
156 { /* calculate checksum */
157 double check = 0;
158 for (i = 0; i < size; ++i) {
159 const double cn = norm(c + STREAM_C(i * nc), (int)m, (int)n, (int)ldc);
160 if (check < cn) check = cn;
161 }
162 printf("\n%f (check)\n", check);
163 }
164 libxsmm_free(a);
165 libxsmm_free(b);
166 libxsmm_free(c);
167
168 return EXIT_SUCCESS;
169 }
170