522768a4fb11276e4145dc1cf1ed58867c816afb
[riscv-tests.git] / benchmarks / mm / mm_main.c
1 #include "common.h"
2 #include <assert.h>
3 #include <stdlib.h>
4 #include "util.h"
5
6 void thread_entry(int cid, int nc)
7 {
8 const int R = 8;
9 int m, n, p;
10
11 if (have_vec) {
12 m = HCBM;
13 n = HCBN;
14 p = HCBK;
15 } else {
16 m = CBM;
17 n = CBN;
18 p = CBK;
19 }
20
21 t a[m*p];
22 t b[p*n];
23 t c[m*n];
24
25 for (size_t i = 0; i < m; i++)
26 for (size_t j = 0; j < p; j++)
27 a[i*p+j] = i+j;
28 for (size_t i = 0; i < p; i++)
29 for (size_t j = 0; j < n; j++)
30 b[i*n+j] = i-j;
31 memset(c, 0, m*n*sizeof(c[0]));
32
33 size_t instret, cycles;
34 if (have_vec) {
35 for (int i = 0; i < R; i++)
36 {
37 instret = -rdinstret();
38 cycles = -rdcycle();
39 mm_rb_hwacha(m, n, p, a, p, b, n, c, n);
40 instret += rdinstret();
41 cycles += rdcycle();
42 }
43 } else {
44 for (int i = 0; i < R; i++)
45 {
46 instret = -rdinstret();
47 cycles = -rdcycle();
48 mm(m, n, p, a, p, b, n, c, n);
49 instret += rdinstret();
50 cycles += rdcycle();
51 }
52 }
53
54 printf("C%d: reg block %dx%dx%d, cache block %dx%dx%d\n",
55 cid, RBM, RBN, RBK, CBM, CBN, CBK);
56 printf("C%d: %d instructions\n", cid, (int)(instret));
57 printf("C%d: %d cycles\n", cid, (int)(cycles));
58 printf("C%d: %d flops\n", cid, 2*m*n*p);
59 printf("C%d: %d Mflops @ 1 GHz\n", cid, 2000*m*n*p/(cycles));
60
61 #if 1
62 for (size_t i = 0; i < m; i++)
63 {
64 for (size_t j = 0; j < n; j++)
65 {
66 t s = 0;
67 for (size_t aik = i, bkj = -j; aik < i+p; aik++, bkj++)
68 s += (t)aik*(t)bkj;
69 if (fabs(c[i*n+j]-s*R) > 1e-6*s)
70 {
71 printf("C%d: c[%lu][%lu] %f != %f\n", cid, i, j, c[i*n+j], s);
72 exit(1);
73 }
74 }
75 }
76 #endif
77
78 barrier(nc);
79 exit(0);
80 }