1 //**************************************************************************
2 // Vector-Thread Vector Matrix Multiply benchmark
3 //--------------------------------------------------------------------------
5 // This benchmark multiplies two 2-D arrays together and writes the results to
6 // a third vector. The input data (and reference data) should be generated
7 // using the matmul_gendata.pl perl script and dumped to a file named
12 // Choose which implementation you wish to test... but leave only one on!
13 // (only the first one will be executed).
18 //--------------------------------------------------------------------------
19 // Input/Reference Data
21 //#include "dataset_test.h"
24 //--------------------------------------------------------------------------
27 int verify( int n
, float test
[], float correct
[] )
30 for ( i
= 0; i
< n
; i
++ ) {
31 if ( test
[i
] > 1.02*correct
[i
]
32 || test
[i
] < 0.98*correct
[i
]) {
34 printf(" test[%d] : %3.2f\n", i
, test
[i
]);
35 printf(" corr[%d] : %3.2f\n", i
, correct
[i
]);
37 // tell us which index fails + 2
38 // (so that if i==0,i==1 fails, we don't
39 // think it was a 'not-finished yet' or pass)
46 void finishTest( int correct
, long long num_cycles
, long long num_retired
)
48 int toHostValue
= correct
;
50 if ( toHostValue
== 1 )
51 printf( "*** PASSED ***\n" );
53 printf( "*** FAILED *** (tohost = %d)\n", toHostValue
);
56 // we no longer run in -testrun mode, which means we can't use
57 // the tohost register to communicate "test is done" and "test results"
58 // so instead we will communicate through print* functions!
61 printstr( "*** PASSED *** (num_cycles = 0x" );
63 printstr( ", num_inst_retired = 0x");
64 printhex(num_retired
);
69 printstr( "*** FAILED *** (num_cycles = 0x");
71 printstr( ", num_inst_retired = 0x");
72 printhex(num_retired
);
80 // deprecated - cr10/stats-enable register no longer exists
81 void setStats( int enable
)
83 #if ( !HOST_DEBUG && SET_STATS )
84 asm( "mtpcr %0, cr10" : : "r" (enable
) );
90 long long cycles
= 1337;
91 #if ( !HOST_DEBUG && SET_STATS )
92 __asm__
__volatile__( "rdcycle %0" : "=r" (cycles
) );
97 long long getInstRetired()
99 long long inst_retired
= 1338;
100 #if ( !HOST_DEBUG && SET_STATS )
101 __asm__
__volatile__( "rdinstret %0" : "=r" (inst_retired
) );
106 //--------------------------------------------------------------------------
109 // scalar C implementation
110 void matmul(const int lda
, const float A
[], const float B
[], float C
[] )
114 for ( j
= 0; j
< lda
; j
++ )
115 for ( i
= 0; i
< lda
; i
++ )
117 float cij
= C
[i
+ j
*lda
];
118 for ( k
= 0; k
< lda
; k
++ )
120 cij
+= A
[j
*lda
+ k
] * B
[k
*lda
+ i
];
127 // assembly implementations can be found in *_asm.S
129 //--------------------------------------------------------------------------
132 int main( int argc
, char* argv
[] )
135 long long start_cycles
= 0;
136 long long stop_cycles
= 0;
137 long long num_cycles
;
138 long long start_retired
= 0;
139 long long stop_retired
= 0;
140 long long num_retired
;
142 float results_data
[ARRAY_SIZE
];
143 for ( i
= 0; i
< DIM_SIZE
; i
++ )
144 for ( j
= 0; j
< DIM_SIZE
; j
++ )
145 results_data
[i
+ j
*DIM_SIZE
] = 0.0f
;
147 // Output the input array
150 printArray( "input1", ARRAY_SIZE
, input1_data
);
151 printArray( "input2", ARRAY_SIZE
, input2_data
);
152 printArray( "verify", ARRAY_SIZE
, verify_data
);
153 printArray( "results", ARRAY_SIZE
, results_data
);
156 // --------------------------------------------------
157 // If needed we preallocate everything in the caches
165 // --------------------------------------------------
167 start_cycles
= getCycles();
168 start_retired
= getInstRetired();
171 matmul( DIM_SIZE
, input1_data
, input2_data
, results_data
);
175 scalar_matmul_asm( DIM_SIZE
, input1_data
, input2_data
, results_data
);
180 vt_matmul_asm( DIM_SIZE
, input1_data
, input2_data
, results_data
);
187 stop_cycles
= getCycles();
188 stop_retired
= getInstRetired();
189 num_cycles
= stop_cycles
- start_cycles
;
190 num_retired
= stop_retired
- start_retired
;
193 // --------------------------------------------------
194 // Print out the results
197 printArray( "results", ARRAY_SIZE
, results_data
);
201 // --------------------------------------------------
203 int correct
= verify( ARRAY_SIZE
, results_data
, verify_data
);
204 finishTest(correct
, num_cycles
, num_retired
);