Merge branch 'master' of github.com:ucb-bar/riscv-tests
[riscv-tests.git] / benchmarks / mt-vvadd / mt-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 double 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 #define ncores ncores
37
38 #include "util.h"
39
40 #define stringify_1(s) #s
41 #define stringify(s) stringify_1(s)
42 #define stats(code) do { \
43 unsigned long _c = -rdcycle(), _i = -rdinstret(); \
44 code; \
45 _c += rdcycle(), _i += rdinstret(); \
46 if (coreid == 0) \
47 printf("%s: %ld cycles, %ld.%ld cycles/iter, %ld.%ld CPI\n", \
48 stringify(code), _c, _c/DATA_SIZE, 10*_c/DATA_SIZE%10, _c/_i, 10*_c/_i%10); \
49 } while(0)
50
51 //--------------------------------------------------------------------------
52 // vvadd function
53
54 //perform in-place vvadd
55 void __attribute__((noinline)) vvadd(size_t n, data_t* __restrict__ x, const data_t* __restrict__ y)
56 {
57 size_t i;
58
59 // interleave accesses
60 for (i = coreid; i < n; i+=ncores)
61 {
62 x[i] = x[i] + y[i];
63 }
64 }
65
66 void __attribute__((noinline)) vvadd_opt(size_t n, data_t* __restrict__ x, const data_t* __restrict__ y)
67 {
68 // ***************************** //
69 // **** ADD YOUR CODE HERE ***** //
70 // ***************************** //
71 }
72
73 //--------------------------------------------------------------------------
74 // Main
75 //
76 // all threads start executing thread_entry(). Use their "coreid" to
77 // differentiate between threads (each thread is running on a separate core).
78
79 void thread_entry(int cid, int nc)
80 {
81 coreid = cid;
82 ncores = nc;
83
84 // static allocates data in the binary, which is visible to both threads
85 static data_t results_data[DATA_SIZE];
86
87 // because we're going to perform an in-place vvadd (and we're going to run
88 // it a couple of times) let's copy the input data to a temporary results
89 // array
90
91 size_t i;
92 if (coreid == 0)
93 {
94 for (i = 0; i < DATA_SIZE; i++)
95 results_data[i] = input1_data[i];
96 }
97
98
99 // Execute the provided, terrible vvadd
100 barrier();
101 stats(vvadd(DATA_SIZE, results_data, input2_data); barrier());
102
103
104 // verify
105 int res = verifyDouble(DATA_SIZE, results_data, verify_data);
106 if (res)
107 exit(res);
108
109 #if 0
110 // reset results from the first trial
111 if (coreid == 0)
112 {
113 for (i=0; i < DATA_SIZE; i++)
114 results_data[i] = input1_data[i];
115 }
116 barrier();
117
118 // Execute your faster vvadd
119 barrier();
120 stats(vvadd_opt(DATA_SIZE, results_data, input2_data); barrier());
121
122 #ifdef DEBUG
123 printDoubleArray("results: ", DATA_SIZE, results_data);
124 printDoubleArray("verify : ", DATA_SIZE, verify_data);
125 #endif
126
127 // verify
128 res = verifyDouble(DATA_SIZE, results_data, verify_data);
129 if (res)
130 exit(res);
131 barrier();
132 #endif
133
134 exit(0);
135 }