benchmarks initial commit
[riscv-tests.git] / benchmarks / spmv / spmv_main.c
1 //**************************************************************************
2 // Double-precision general matrix multiplication benchmark
3 //--------------------------------------------------------------------------
4
5 //--------------------------------------------------------------------------
6 // Macros
7
8 // Set HOST_DEBUG to 1 if you are going to compile this for a host
9 // machine (ie Athena/Linux) for debug purposes and set HOST_DEBUG
10 // to 0 if you are compiling with the smips-gcc toolchain.
11
12 #ifndef HOST_DEBUG
13 #define HOST_DEBUG 0
14 #endif
15
16 // Set PREALLOCATE to 1 if you want to preallocate the benchmark
17 // function before starting stats. If you have instruction/data
18 // caches and you don't want to count the overhead of misses, then
19 // you will need to use preallocation.
20
21 #ifndef PREALLOCATE
22 #define PREALLOCATE 0
23 #endif
24
25 // Set SET_STATS to 1 if you want to carve out the piece that actually
26 // does the computation.
27
28 #ifndef SET_STATS
29 #define SET_STATS 0
30 #endif
31
32 //--------------------------------------------------------------------------
33 // Input/Reference Data
34
35 #include "dataset1.h"
36
37 //--------------------------------------------------------------------------
38 // Helper functions
39
40 int verify( long n, const double test[], const double correct[] )
41 {
42 int i;
43 for ( i = 0; i < n; i++ ) {
44 if ( test[i] != correct[i] ) {
45 return 2;
46 }
47 }
48 return 1;
49 }
50
51 #if HOST_DEBUG
52 #include <stdio.h>
53 #include <stdlib.h>
54 void printArray( char name[], long n, const double arr[] )
55 {
56 int i;
57 printf( " %10s :", name );
58 for ( i = 0; i < n; i++ )
59 printf( " %8.1f ", arr[i] );
60 printf( "\n" );
61 }
62 #endif
63
64 void finishTest( int toHostValue )
65 {
66 #if HOST_DEBUG
67 if ( toHostValue == 1 )
68 printf( "*** PASSED ***\n" );
69 else
70 printf( "*** FAILED *** (tohost = %d)\n", toHostValue );
71 exit(0);
72 #else
73 asm( "mtpcr %0, cr30" : : "r" (toHostValue) );
74 while ( 1 ) { }
75 #endif
76 }
77
78 void setStats( int enable )
79 {
80 #if ( !HOST_DEBUG && SET_STATS )
81 asm( "mtpcr %0, cr10" : : "r" (enable) );
82 #endif
83 }
84
85 void spmv(int r, const double* val, const int* idx, const double* x,
86 const int* ptr, double* y)
87 {
88 for (int i = 0; i < r; i++)
89 {
90 int k;
91 double yi0 = 0, yi1 = 0, yi2 = 0, yi3 = 0;
92 for (k = ptr[i]; k < ptr[i+1]-3; k+=4)
93 {
94 yi0 += val[k+0]*x[idx[k+0]];
95 yi1 += val[k+1]*x[idx[k+1]];
96 yi2 += val[k+2]*x[idx[k+2]];
97 yi3 += val[k+3]*x[idx[k+3]];
98 }
99 for ( ; k < ptr[i+1]; k++)
100 {
101 yi0 += val[k]*x[idx[k]];
102 }
103 y[i] = (yi0+yi1)+(yi2+yi3);
104 }
105 }
106
107 //--------------------------------------------------------------------------
108 // Main
109
110 int main( int argc, char* argv[] )
111 {
112 double y[R];
113
114 #if PREALLOCATE
115 spmv(R, val, idx, x, ptr, y);
116 #endif
117
118 setStats(1);
119 spmv(R, val, idx, x, ptr, y);
120 setStats(0);
121
122 finishTest(verify(R, y, verify_data));
123 }