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