50673ed5bb92e7903606c262deb2b0fbbb0ce4b3
1 //**************************************************************************
2 // Vector-vector add benchmark
3 //--------------------------------------------------------------------------
4 // Author : Andrew Waterman
5 // TA : Christopher Celio
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
13 // to print out arrays, etc.
16 //--------------------------------------------------------------------------
24 //--------------------------------------------------------------------------
25 // Input/Reference Data
30 #define MIN(X,Y) (X < Y ? X : Y)
31 #define MAX(X,Y) (X > Y ? X : Y)
33 //--------------------------------------------------------------------------
34 // Basic Utilities and Multi-thread Support
36 __thread
unsigned long coreid
;
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(); \
46 _c += rdcycle(), _i += rdinstret(); \
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); \
53 //--------------------------------------------------------------------------
56 void printArray( char name
[], int n
, data_t arr
[] )
62 printf( " %10s :", name
);
63 for ( i
= 0; i
< n
; i
++ )
64 printf( " %4ld ", (long) arr
[i
] );
68 void __attribute__((noinline
)) verify(size_t n
, const data_t
* test
, const data_t
* correct
)
74 for (i
= 0; i
< n
; i
++)
76 if (test
[i
] != correct
[i
])
78 printf("FAILED test[%d]= %4ld, correct[%d]= %4ld\n",
79 i
, (long) test
[i
], i
, (long)correct
[i
]);
87 //--------------------------------------------------------------------------
90 //perform in-place vvadd
91 void __attribute__((noinline
)) vvadd(size_t n
, data_t
* __restrict__ x
, const data_t
* __restrict__ y
)
95 // interleave accesses
96 for (i
= coreid
; i
< n
; i
+=ncores
)
102 void __attribute__((noinline
)) vvadd_opt(size_t n
, data_t
* __restrict__ x
, const data_t
* __restrict__ y
)
104 // ***************************** //
105 // **** ADD YOUR CODE HERE ***** //
106 // ***************************** //
107 size_t i
, start
= coreid
* (n
/ ncores
), end
= (coreid
== ncores
- 1 ? n
: (coreid
+ 1) * (n
/ ncores
));
109 for (i
= start
; i
< end
; i
++) {
114 //--------------------------------------------------------------------------
117 // all threads start executing thread_entry(). Use their "coreid" to
118 // differentiate between threads (each thread is running on a separate core).
120 void thread_entry(int cid
, int nc
)
125 // static allocates data in the binary, which is visible to both threads
126 static data_t results_data
[DATA_SIZE
];
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
135 for (i
= 0; i
< DATA_SIZE
; i
++)
136 results_data
[i
] = input1_data
[i
];
140 // Execute the provided, terrible vvadd
142 stats(vvadd(DATA_SIZE
, results_data
, input2_data
); barrier());
146 verify(DATA_SIZE
, results_data
, verify_data
);
148 // reset results from the first trial
151 for (i
=0; i
< DATA_SIZE
; i
++)
152 results_data
[i
] = input1_data
[i
];
157 // Execute your faster vvadd
159 stats(vvadd_opt(DATA_SIZE
, results_data
, input2_data
); barrier());
162 printArray("results: ", DATA_SIZE
, results_data
);
163 printArray("verify : ", DATA_SIZE
, verify_data
);
167 verify(DATA_SIZE
, results_data
, verify_data
);