multithreading tests from 152 lab 5
[riscv-tests.git] / mt / bc_vvadd / bc_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 #define MIN(X,Y) (X < Y ? X : Y)
31 #define MAX(X,Y) (X > Y ? X : Y)
32
33 //--------------------------------------------------------------------------
34 // Basic Utilities and Multi-thread Support
35
36 __thread unsigned long coreid;
37 unsigned long ncores;
38
39 #include "util.h"
40
41 #define stringify_1(s) #s
42 #define stringify(s) stringify_1(s)
43 #define stats(code) do { \
44 unsigned long _c = -rdcycle(), _i = -rdinstret(); \
45 code; \
46 _c += rdcycle(), _i += rdinstret(); \
47 if (coreid == 0) \
48 printf("%s: %ld cycles, %ld.%ld cycles/iter, %ld.%ld CPI\n", \
49 stringify(code), _c, _c/DATA_SIZE, 10*_c/DATA_SIZE%10, _c/_i, 10*_c/_i%10); \
50 } while(0)
51
52
53 //--------------------------------------------------------------------------
54 // Helper functions
55
56 void printArray( char name[], int n, data_t arr[] )
57 {
58 int i;
59 if (coreid != 0)
60 return;
61
62 printf( " %10s :", name );
63 for ( i = 0; i < n; i++ )
64 printf( " %4ld ", (long) arr[i] );
65 printf( "\n" );
66 }
67
68 void __attribute__((noinline)) verify(size_t n, const data_t* test, const data_t* correct)
69 {
70 if (coreid != 0)
71 return;
72
73 size_t i;
74 for (i = 0; i < n; i++)
75 {
76 if (test[i] != correct[i])
77 {
78 printf("FAILED test[%d]= %4ld, correct[%d]= %4ld\n",
79 i, (long) test[i], i, (long)correct[i]);
80 exit(-1);
81 }
82 }
83
84 return;
85 }
86
87 //--------------------------------------------------------------------------
88 // vvadd function
89
90 //perform in-place vvadd
91 void __attribute__((noinline)) vvadd(size_t n, data_t* __restrict__ x, const data_t* __restrict__ y)
92 {
93 size_t i;
94
95 // interleave accesses
96 for (i = coreid; i < n; i+=ncores)
97 {
98 x[i] = x[i] + y[i];
99 }
100 }
101
102 void __attribute__((noinline)) vvadd_opt(size_t n, data_t* __restrict__ x, const data_t* __restrict__ y)
103 {
104 // ***************************** //
105 // **** ADD YOUR CODE HERE ***** //
106 // ***************************** //
107 size_t i, start = coreid * (n / ncores), end = (coreid == ncores - 1 ? n : (coreid + 1) * (n / ncores));
108
109 for (i = start; i < end; i++) {
110 x[i] += y[i];
111 }
112 }
113
114 //--------------------------------------------------------------------------
115 // Main
116 //
117 // all threads start executing thread_entry(). Use their "coreid" to
118 // differentiate between threads (each thread is running on a separate core).
119
120 void thread_entry(int cid, int nc)
121 {
122 coreid = cid;
123 ncores = nc;
124
125 // static allocates data in the binary, which is visible to both threads
126 static data_t results_data[DATA_SIZE];
127
128 // because we're going to perform an in-place vvadd (and we're going to run
129 // it a couple of times) let's copy the input data to a temporary results
130 // array
131
132 size_t i;
133 if (coreid == 0)
134 {
135 for (i = 0; i < DATA_SIZE; i++)
136 results_data[i] = input1_data[i];
137 }
138
139
140 // Execute the provided, terrible vvadd
141 barrier();
142 stats(vvadd(DATA_SIZE, results_data, input2_data); barrier());
143
144
145 // verify
146 verify(DATA_SIZE, results_data, verify_data);
147
148 // reset results from the first trial
149 if (coreid == 0)
150 {
151 for (i=0; i < DATA_SIZE; i++)
152 results_data[i] = input1_data[i];
153 }
154 barrier();
155
156
157 // Execute your faster vvadd
158 barrier();
159 stats(vvadd_opt(DATA_SIZE, results_data, input2_data); barrier());
160
161 #ifdef DEBUG
162 printArray("results: ", DATA_SIZE, results_data);
163 printArray("verify : ", DATA_SIZE, verify_data);
164 #endif
165
166 // verify
167 verify(DATA_SIZE, results_data, verify_data);
168 barrier();
169
170 exit(0);
171 }
172