375569b81646bbc2f28d0d250f65f010fdf4db61
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
31 //--------------------------------------------------------------------------
32 // Basic Utilities and Multi-thread Support
34 __thread
unsigned long coreid
;
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(); \
44 _c += rdcycle(), _i += rdinstret(); \
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); \
51 //--------------------------------------------------------------------------
54 void printArrayMT( char name
[], int n
, data_t arr
[] )
60 printf( " %10s :", name
);
61 for ( i
= 0; i
< n
; i
++ )
62 printf( " %4ld ", (long) arr
[i
] );
66 void __attribute__((noinline
)) verifyMT(size_t n
, const data_t
* test
, const data_t
* correct
)
72 for (i
= 0; i
< n
; i
++)
74 if (test
[i
] != correct
[i
])
76 printf("FAILED test[%d]= %4ld, correct[%d]= %4ld\n",
77 i
, (long) test
[i
], i
, (long)correct
[i
]);
85 //--------------------------------------------------------------------------
88 //perform in-place vvadd
89 void __attribute__((noinline
)) vvadd(size_t n
, data_t
* __restrict__ x
, const data_t
* __restrict__ y
)
93 // interleave accesses
94 for (i
= coreid
; i
< n
; i
+=ncores
)
100 void __attribute__((noinline
)) vvadd_opt(size_t n
, data_t
* __restrict__ x
, const data_t
* __restrict__ y
)
102 // ***************************** //
103 // **** ADD YOUR CODE HERE ***** //
104 // ***************************** //
108 for (i
= coreid
; i
< n
; i
+=ncores
*2)
111 //int x2 = x[i+ncores];
112 //int x3 = x[i+ncores*2];
113 //int x4 = x[i+ncores*4];
115 //int y2 = y[i+ncores];
116 //int y3 = y[i+ncores*2];
117 //int y4 = y[i+ncores*4];
119 int x2
= x
[i
+ncores
];
121 int y2
= y
[i
+ncores
];
123 x
[i
+ncores
] = x2
+ y2
;
124 //x[i+ncores*2] = x[i+ncores*2] + y[i+ncores*2];
125 // x[i+ncores*4] = x[i+ncores*4] + y[i+ncores*4];
127 //x[i+ncores] = x2 + y2;
128 //x[i+ncores*2] = x3 + y3;
129 //x[i+ncores*4] = x4 + y4;
133 //--------------------------------------------------------------------------
136 // all threads start executing thread_entry(). Use their "coreid" to
137 // differentiate between threads (each thread is running on a separate core).
139 void thread_entry(int cid
, int nc
)
144 // static allocates data in the binary, which is visible to both threads
145 static data_t results_data
[DATA_SIZE
];
147 // because we're going to perform an in-place vvadd (and we're going to run
148 // it a couple of times) let's copy the input data to a temporary results
154 for (i
= 0; i
< DATA_SIZE
; i
++)
155 results_data
[i
] = input1_data
[i
];
159 // Execute the provided, terrible vvadd
161 stats(vvadd(DATA_SIZE
, results_data
, input2_data
); barrier(nc
));
165 verifyMT(DATA_SIZE
, results_data
, verify_data
);
167 // reset results from the first trial
170 for (i
=0; i
< DATA_SIZE
; i
++)
171 results_data
[i
] = input1_data
[i
];
176 // Execute your faster vvadd
178 stats(vvadd_opt(DATA_SIZE
, results_data
, input2_data
); barrier(nc
));
181 printArrayMT("results: ", DATA_SIZE
, results_data
);
182 printArrayMT("verify : ", DATA_SIZE
, verify_data
);
186 verifyMT(DATA_SIZE
, results_data
, verify_data
);