1 /************************************************************************
2 * Program: scalar_product.c
3 * Function: Calculates the scalar product of vector lying in memory
4 * Used as a test for the simd optimization.
5 * Author: Andreas Schoesser
7 ************************************************************************/
14 float scalar_product(float *a, float *b, unsigned int max_elements);
19 int i, max_elements = 100000000;
20 clock_t t_time_bev, t_time_after, t_clocks_dauer;
23 // Allocate memory and make sure pointers are aligned to 16 byte addresses
24 char *a = malloc(16 + max_elements * sizeof(float));
25 char *b = malloc(16 + max_elements * sizeof(float));
27 char *ca = &a[0] + 16 - (unsigned int) ((unsigned int) &a[0] % 16);
28 char *cb = &b[0] + 16 - (unsigned int) ((unsigned int) &b[0] % 16);
30 float *aa = (float *) ca;
31 float *ab = (float *) cb;
33 printf("Scalar product\n==============\n\n");
35 //printf("Array Position: %u, %u, %u, %u\n", a, b, aa, ba/*(unsigned int) &aa[0] % 16, (unsigned int) &ba[0] % 16*/);
37 // Fill both arrays with random values
38 for(i = 0; i < max_elements; i++)
40 aa[i] = (float) (rand() % 10);
41 ab[i] = (float) (rand() % 10);
43 //printf("(%g * %g) + ", a[i], b[i]);
46 // Start time measurement
49 //for(i = 0; i < max_elements - 4; i += 4)
50 res = scalar_product(aa, ab, max_elements);
52 // Stop time measurement
53 t_time_after = clock();
54 t_clocks_dauer = (t_time_after-t_time_bev);
55 d_zeitdauer = (double) (t_time_after-t_time_bev) / CLOCKS_PER_SEC;
58 printf("Zeitdauer %g s\n", d_zeitdauer);
60 printf("Zeitdauer %g ms\n", d_zeitdauer);
63 printf("\nResult: %g\n", res);
67 float scalar_product(float * a, float * b, unsigned int max_elements)
72 /*for(i = 0; i < 4; i++)
74 a[i] = (float) (rand() % 10);
75 b[i] = (float) (rand() % 10);
77 printf("(%g * %g) + ", a[i], b[i]);
80 for(i = 0; i < max_elements; i += 4)
81 res += a[i] * b[i] + a[i + 1] * b[i + 1] + a[i + 2] * b[i + 2] + a[i + 3] * b[i + 3];