{
float res;
int i, max_elements = 100;
- clock_t t_time_bev, t_time_after, t_clocks_dauer;
double d_zeitdauer;
// Allocate memory and make sure pointers are aligned to 16 byte addresses
float *aa = (float *) ca;
float *ab = (float *) cb;
+ srand(0);
+
printf("Scalar product\n==============\n\n");
//printf("Array Position: %u, %u, %u, %u\n", a, b, aa, ba/*(unsigned int) &aa[0] % 16, (unsigned int) &ba[0] % 16*/);
//printf("(%g * %g) + ", a[i], b[i]);
}
- // Start time measurement
- t_time_bev = clock();
-
- //for(i = 0; i < max_elements - 4; i += 4)
res = scalar_product(aa, ab, max_elements);
- // Stop time measurement
- t_time_after = clock();
- t_clocks_dauer = (t_time_after-t_time_bev);
- d_zeitdauer = (double) (t_time_after-t_time_bev) / CLOCKS_PER_SEC;
-
- #ifdef __GNUC__
- printf("Zeitdauer %g s\n", d_zeitdauer);
- #else
- printf("Zeitdauer %g ms\n", d_zeitdauer);
- #endif
-
printf("\nResult: %g\n", res);
}
float scalar_product(float * a, float * b, unsigned int max_elements)
{
- float res;
+ float res = 0;
int i;
- /*for(i = 0; i < 4; i++)
- {
- a[i] = (float) (rand() % 10);
- b[i] = (float) (rand() % 10);
-
- printf("(%g * %g) + ", a[i], b[i]);
- }*/
-
- for(i = 0; i < max_elements; i += 4)
- res += a[i] * b[i] + a[i + 1] * b[i + 1] + a[i + 2] * b[i + 2] + a[i + 3] * b[i + 3];
+ for(i = 0; i < max_elements; i += 4) {
+ res += a[i] * b[i];
+ res += a[i + 1] * b[i + 1];
+ res += a[i + 2] * b[i + 2];
+ res += a[i + 3] * b[i + 3];
+ }
- return(res);
+ return res;
}