d60f4ec4a3f277bebc5e7cf28e055cb7264f8dd1
[riscv-tests.git] / mt / bm_vvadd / bm_vvadd.c
1 //**************************************************************************
2 // Vector-vector add benchmark
3 //--------------------------------------------------------------------------
4 // Author : Andrew Waterman
5 // TA : Christopher Celio
6 // Student :
7 //
8 // This benchmark adds two vectors and writes the results to a
9 // third vector. The input data (and reference data) should be
10 // generated using the vvadd_gendata.pl perl script and dumped
11 // to a file named dataset.h
12
13 // to print out arrays, etc.
14 //#define DEBUG
15
16 //--------------------------------------------------------------------------
17 // Includes
18
19 #include <string.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22
23
24 //--------------------------------------------------------------------------
25 // Input/Reference Data
26
27 typedef float data_t;
28 #include "dataset.h"
29
30
31 //--------------------------------------------------------------------------
32 // Basic Utilities and Multi-thread Support
33
34 __thread unsigned long coreid;
35 unsigned long ncores;
36
37 #include "util.h"
38
39 #define stringify_1(s) #s
40 #define stringify(s) stringify_1(s)
41 #define stats(code) do { \
42 unsigned long _c = -rdcycle(), _i = -rdinstret(); \
43 code; \
44 _c += rdcycle(), _i += rdinstret(); \
45 if (coreid == 0) \
46 printf("%s: %ld cycles, %ld.%ld cycles/iter, %ld.%ld CPI\n", \
47 stringify(code), _c, _c/DATA_SIZE, 10*_c/DATA_SIZE%10, _c/_i, 10*_c/_i%10); \
48 } while(0)
49
50
51 //--------------------------------------------------------------------------
52 // Helper functions
53
54 void printArray( char name[], int n, data_t arr[] )
55 {
56 int i;
57 if (coreid != 0)
58 return;
59
60 printf( " %10s :", name );
61 for ( i = 0; i < n; i++ )
62 printf( " %4ld ", (long) arr[i] );
63 printf( "\n" );
64 }
65
66 void __attribute__((noinline)) verify(size_t n, const data_t* test, const data_t* correct)
67 {
68 if (coreid != 0)
69 return;
70
71 size_t i;
72 for (i = 0; i < n; i++)
73 {
74 if (test[i] != correct[i])
75 {
76 printf("FAILED test[%d]= %4ld, correct[%d]= %4ld\n",
77 i, (long) test[i], i, (long)correct[i]);
78 exit(-1);
79 }
80 }
81
82 return;
83 }
84
85 //--------------------------------------------------------------------------
86 // vvadd function
87
88 //perform in-place vvadd
89 void __attribute__((noinline)) vvadd(size_t n, data_t* __restrict__ x, const data_t* __restrict__ y)
90 {
91 size_t i;
92
93 // interleave accesses
94 for (i = coreid; i < n; i+=ncores)
95 {
96 x[i] = x[i] + y[i];
97 }
98 }
99
100 void __attribute__((noinline)) vvadd_opt(size_t n, data_t* __restrict__ x, const data_t* __restrict__ y)
101 {
102 // ***************************** //
103 // **** ADD YOUR CODE HERE ***** //
104 // ***************************** //
105
106 size_t i;
107 size_t space=n/ncores;
108 size_t max= space*coreid+space;
109 if (coreid!=ncores-1){
110 for (i=space*coreid;i<max;i+=1)
111 {
112 x[i] = x[i] + y[i];
113 }
114 }
115 else{
116 for(i=space*coreid;i<n;i+=1)
117 {
118 x[i] = x[i] + y[i];
119 }
120 }
121 /*
122 size_t i;
123 size_t space=n/ncores;
124 size_t max= space*coreid+space;
125 if (n%ncores!=0)
126 {
127 space=space+1;
128 }
129 for (i=space*coreid;i<max&& i<n;i+=1)
130 {
131 x[i] = x[i] + y[i];
132 }
133 */
134 }
135
136 //--------------------------------------------------------------------------
137 // Main
138 //
139 // all threads start executing thread_entry(). Use their "coreid" to
140 // differentiate between threads (each thread is running on a separate core).
141
142 void thread_entry(int cid, int nc)
143 {
144 coreid = cid;
145 ncores = nc;
146
147 // static allocates data in the binary, which is visible to both threads
148 static data_t results_data[DATA_SIZE];
149
150 // because we're going to perform an in-place vvadd (and we're going to run
151 // it a couple of times) let's copy the input data to a temporary results
152 // array
153
154 size_t i;
155 if (coreid == 0)
156 {
157 for (i = 0; i < DATA_SIZE; i++)
158 results_data[i] = input1_data[i];
159 }
160
161
162 // Execute the provided, terrible vvadd
163 barrier();
164 stats(vvadd(DATA_SIZE, results_data, input2_data); barrier());
165
166
167 // verify
168 verify(DATA_SIZE, results_data, verify_data);
169
170 // reset results from the first trial
171 if (coreid == 0)
172 {
173 for (i=0; i < DATA_SIZE; i++)
174 results_data[i] = input1_data[i];
175 }
176 barrier();
177
178
179 // Execute your faster vvadd
180 barrier();
181 stats(vvadd_opt(DATA_SIZE, results_data, input2_data); barrier());
182
183 #ifdef DEBUG
184 printArray("results: ", DATA_SIZE, results_data);
185 printArray("verify : ", DATA_SIZE, verify_data);
186 #endif
187
188 // verify
189 verify(DATA_SIZE, results_data, verify_data);
190 barrier();
191
192 exit(0);
193 }
194