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