aboutsummaryrefslogtreecommitdiff
path: root/benchmarks/vec-matmul/vec_matmul_main.c
blob: 5de377ed8620f747efc99ecfa378fe95a7c9e813 (plain)
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
//**************************************************************************
// Vector-Thread Vector Matrix Multiply benchmark
//--------------------------------------------------------------------------
//
// This benchmark multiplies two 2-D arrays together and writes the results to
// a third vector. The input data (and reference data) should be generated
// using the matmul_gendata.pl perl script and dumped to a file named
// dataset.h. The riscv-gcc toolchain does not support system calls so printf's
// can only be used on a host system, not on the riscv-v processor simulator
// itself. 
//
// HOWEVER: printstr() and printhex() are provided, for a primitive form of
// printing strings and hexadecimal values to stdout.


// Choose which implementation you wish to test... but leave only one on!
// (only the first one will be executed).
//#define SCALAR_C
//#define SCALAR_ASM
#define VT_ASM

//--------------------------------------------------------------------------
// Macros

// Set HOST_DEBUG to 1 if you are going to compile this for a host
// machine (ie Athena/Linux) for debug purposes and set HOST_DEBUG
// to 0 if you are compiling with the smips-gcc toolchain.

#ifndef HOST_DEBUG
#define HOST_DEBUG 0
#endif

// Set PREALLOCATE to 1 if you want to preallocate the benchmark
// function before starting stats. If you have instruction/data
// caches and you don't want to count the overhead of misses, then
// you will need to use preallocation.

#ifndef PREALLOCATE
#define PREALLOCATE 0
#endif

// Set SET_STATS to 1 if you want to carve out the piece that actually
// does the computation.

#ifndef SET_STATS
#define SET_STATS 0
#endif

//--------------------------------------------------------------------------
// Host Platform Includes

#if HOST_DEBUG
   #include <stdio.h>
   #include <stdlib.h>
#else
void printstr(const char*);
void exit();
#endif


//--------------------------------------------------------------------------
// Input/Reference Data

//#include "dataset_test.h"
#include "dataset.h"
  
//--------------------------------------------------------------------------
// Helper functions

int verify( int n, float test[], float correct[] )
{
  int i;
  for ( i = 0; i < n; i++ ) {
    if ( test[i] > 1.02*correct[i] 
       || test[i] < 0.98*correct[i]) {
#if HOST_DEBUG
      printf("    test[%d] : %3.2f\n", i, test[i]);
      printf("    corr[%d] : %3.2f\n", i, correct[i]);
#endif
      // tell us which index fails + 2
      // (so that if i==0,i==1 fails, we don't 
      // think it was a 'not-finished yet' or pass)
      return i+10; 
    }
  }
  return 1;
}

#if HOST_DEBUG
void printArray( char name[], int n, float arr[] )
{
  int i;
  printf( " %10s :", name );
  for ( i = 0; i < n; i++ )
    printf( " %03.2f ", arr[i] );
  printf( "\n" );
}
#endif
 

void finishTest( int correct, long long num_cycles, long long num_retired )
{
   int toHostValue = correct;
#if HOST_DEBUG
  if ( toHostValue == 1 )
    printf( "*** PASSED ***\n" );
  else
    printf( "*** FAILED *** (tohost = %d)\n", toHostValue );
  exit(0);
#else
  // we no longer run in -testrun mode, which means we can't use
  // the tohost register to communicate "test is done" and "test results"
  // so instead we will communicate through print* functions!
  if ( correct == 1 )
  {
    printstr( "*** PASSED *** (num_cycles = 0x" );
    printhex(num_cycles);
    printstr( ", num_inst_retired = 0x");
    printhex(num_retired);
    printstr( ")\n" );
  }
  else
  {
    printstr( "*** FAILED *** (num_cycles = 0x");
    printhex(num_cycles);
    printstr( ", num_inst_retired = 0x");
    printhex(num_retired);
    printstr( ")\n" );
  }
  exit();
#endif
}


// deprecated - cr10/stats-enable register no longer exists
void setStats( int enable )
{
#if ( !HOST_DEBUG && SET_STATS )
  asm( "mtpcr %0, cr10" : : "r" (enable) );
#endif
}
 
long long getCycles()
{
   long long cycles = 1337;
#if ( !HOST_DEBUG && SET_STATS )
  __asm__ __volatile__( "rdcycle %0" : "=r" (cycles) );
#endif
  return cycles;
}
  
long long getInstRetired()
{
   long long inst_retired = 1338;
#if ( !HOST_DEBUG && SET_STATS )
  __asm__ __volatile__( "rdinstret %0" : "=r" (inst_retired) );
#endif
  return inst_retired;
}

//--------------------------------------------------------------------------
// matmul function

// scalar C implementation 
void matmul(const int lda,  const float A[], const float B[], float C[] )
{
  int i, j, k;
  
  for ( j = 0; j < lda; j++ )  
    for ( i = 0; i < lda; i++ )
    {
      float cij = C[i + j*lda];
      for ( k = 0; k < lda; k++ ) 
      {
        cij += A[j*lda + k] * B[k*lda + i];
      }
      C[i + j*lda] = cij;
    }
}


// assembly implementations can be found in *_asm.S

//--------------------------------------------------------------------------
// Main

int main( int argc, char* argv[] )
{
  int i,j;
  long long start_cycles = 0;
  long long stop_cycles = 0;
  long long num_cycles; 
  long long start_retired = 0;
  long long stop_retired = 0;
  long long num_retired;
  
  float results_data[ARRAY_SIZE];
  for ( i = 0; i < DIM_SIZE; i++ )
    for ( j = 0; j < DIM_SIZE; j++ ) 
      results_data[i + j*DIM_SIZE] = 0.0f;

  // Output the input array

#if HOST_DEBUG
  printArray( "input1", ARRAY_SIZE, input1_data );
  printArray( "input2", ARRAY_SIZE, input2_data );
  printArray( "verify", ARRAY_SIZE, verify_data );
  printArray( "results", ARRAY_SIZE, results_data );
#endif

  // --------------------------------------------------
  // If needed we preallocate everything in the caches

#if PREALLOCATE



#endif

  // --------------------------------------------------
  // Do the matmul
  start_cycles = getCycles();
  start_retired = getInstRetired();
  
#ifdef SCALAR_C
  matmul( DIM_SIZE, input1_data, input2_data, results_data );
#else
#ifdef SCALAR_ASM
  #if HOST_DEBUG==0
  scalar_matmul_asm( DIM_SIZE, input1_data, input2_data, results_data );
  #endif
#else
#ifdef VT_ASM
  #if HOST_DEBUG==0
  vt_matmul_asm( DIM_SIZE, input1_data, input2_data, results_data );
  #endif
#endif
#endif
#endif
 
  
  stop_cycles  = getCycles();
  stop_retired = getInstRetired();
  num_cycles = stop_cycles - start_cycles;
  num_retired = stop_retired - start_retired;
     
  
  // --------------------------------------------------
  // Print out the results

#if HOST_DEBUG
  printArray( "results", ARRAY_SIZE, results_data );
#endif


  // --------------------------------------------------
  // Check the results
  int correct = verify( ARRAY_SIZE, results_data, verify_data );
  finishTest(correct, num_cycles, num_retired);
}