1584a5dc9d784e327b3534dfec20645ca8c786df
[riscv-tests.git] / benchmarks / mt-matmul / mt-matmul.c
1 //**************************************************************************
2 // Multi-threaded Matrix Multiply benchmark
3 //--------------------------------------------------------------------------
4 // TA : Christopher Celio
5 // Student:
6 //
7 //
8 // This benchmark multiplies two 2-D arrays together and writes the results to
9 // a third vector. The input data (and reference data) should be generated
10 // using the matmul_gendata.pl perl script and dumped to a file named
11 // dataset.h.
12
13
14 // print out arrays, etc.
15 //#define DEBUG
16
17 //--------------------------------------------------------------------------
18 // Includes
19
20 #include <string.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23
24
25 //--------------------------------------------------------------------------
26 // Input/Reference Data
27
28 typedef double data_t;
29 #include "dataset.h"
30
31
32 //--------------------------------------------------------------------------
33 // Basic Utilities and Multi-thread Support
34
35 __thread unsigned long coreid;
36
37 #include "util.h"
38
39 #define stringify_1(s) #s
40 #define stringify(s) stringify_1(s)
41 #define stats(code) do { \
42 unsigned long _c = -rdcycle(), _i = -rdinstret(); \
43 code; \
44 _c += rdcycle(), _i += rdinstret(); \
45 if (coreid == 0) \
46 printf("%s: %ld cycles, %ld.%ld cycles/iter, %ld.%ld CPI\n", \
47 stringify(code), _c, _c/DIM_SIZE/DIM_SIZE/DIM_SIZE, 10*_c/DIM_SIZE/DIM_SIZE/DIM_SIZE%10, _c/_i, 10*_c/_i%10); \
48 } while(0)
49
50 //--------------------------------------------------------------------------
51 // matmul function
52
53 // single-thread, naive version
54 void __attribute__((noinline)) matmul_naive(const int lda, const data_t A[], const data_t B[], data_t C[] )
55 {
56 int i, j, k;
57
58 if (coreid > 0)
59 return;
60
61 for ( i = 0; i < lda; i++ )
62 for ( j = 0; j < lda; j++ )
63 {
64 for ( k = 0; k < lda; k++ )
65 {
66 C[i + j*lda] += A[j*lda + k] * B[k*lda + i];
67 }
68 }
69
70 }
71
72
73
74 void __attribute__((noinline)) matmul(const int lda, const data_t A[], const data_t B[], data_t C[] )
75 {
76
77 // ***************************** //
78 // **** ADD YOUR CODE HERE ***** //
79 // ***************************** //
80 //
81 // feel free to make a separate function for MI and MSI versions.
82
83 }
84
85 //--------------------------------------------------------------------------
86 // Main
87 //
88 // all threads start executing thread_entry(). Use their "coreid" to
89 // differentiate between threads (each thread is running on a separate core).
90
91 void thread_entry(int cid, int nc)
92 {
93 coreid = cid;
94
95 // static allocates data in the binary, which is visible to both threads
96 static data_t results_data[ARRAY_SIZE];
97
98
99 // Execute the provided, naive matmul
100 barrier(nc);
101 stats(matmul_naive(DIM_SIZE, input1_data, input2_data, results_data); barrier(nc));
102
103
104 // verify
105 int res = verifyDouble(ARRAY_SIZE, results_data, verify_data);
106 if (res)
107 exit(res);
108
109 #if 0
110 // clear results from the first trial
111 size_t i;
112 if (coreid == 0)
113 for (i=0; i < ARRAY_SIZE; i++)
114 results_data[i] = 0;
115 barrier(nc);
116
117
118 // Execute your faster matmul
119 barrier(nc);
120 stats(matmul(DIM_SIZE, input1_data, input2_data, results_data); barrier(nc));
121
122 #ifdef DEBUG
123 printArray("results:", ARRAY_SIZE, results_data);
124 printArray("verify :", ARRAY_SIZE, verify_data);
125 #endif
126
127 // verify
128 res = verify(ARRAY_SIZE, results_data, verify_data);
129 if (res)
130 exit(res);
131 barrier(nc);
132 #endif
133
134 exit(0);
135 }