aboutsummaryrefslogtreecommitdiff
path: root/benchmarks/vec-matmul/vec_matmul_main.c
blob: 6613902a5435a2d9d49713ed128016881f3f7470 (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
//**************************************************************************
// 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.

#include "util.h"

// 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

//--------------------------------------------------------------------------
// 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;
}

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);
}