| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#include "magazine.h" |
| 12 |
#if !defined(SHUFFLE) |
| 13 |
# include <libxsmm.h> |
| 14 |
#endif |
| 15 |
|
| 16 |
#if 0 |
| 17 |
# define AUTO |
| 18 |
#endif |
| 19 |
#if 0 |
| 20 |
# define NOPREFETCH |
| 21 |
#endif |
| 22 |
|
| 23 |
|
| 24 |
int main(int argc, char* argv[]) |
| 25 |
{ |
| 26 |
|
| 27 |
const int batchsize = (1 < argc ? atoi(argv[1]) : 0); |
| 28 |
|
| 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 |
|
| 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 |
|
| 37 |
const char transa = 'n', transb = 'n'; |
| 38 |
const TYPE alpha = 1, beta = 1; |
| 39 |
|
| 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 |
|
| 44 |
const int size = (0 >= batchsize ? (int)((2ULL << 30) / (sizeof(TYPE) * (na + nb + nc))) : batchsize); |
| 45 |
#if defined(SHUFFLE) |
| 46 |
const size_t shuffle = libxsmm_shuffle((unsigned int)size); |
| 47 |
#endif |
| 48 |
|
| 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 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
#if !defined(AUTO) |
| 63 |
const int flags = LIBXSMM_GEMM_FLAGS(transa, transb); |
| 64 |
# if !defined(NOPREFETCH) && (STREAM_A(1) || STREAM_B(1) || STREAM_C(1)) |
| 65 |
const int prefetch = LIBXSMM_PREFETCH_AUTO; |
| 66 |
# else |
| 67 |
const int prefetch = LIBXSMM_PREFETCH_NONE; |
| 68 |
# endif |
| 69 |
union { |
| 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 |
|
| 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)) { |
| 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 |
{ |
| 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)) |
| 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)) |
| 112 |
const int p = i + 1; |
| 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)) |
| 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)) |
| 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) |
| 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 |
{ |
| 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 |
|