6
#define DEFAULT_SIZE 1024
8
size_t exercise(float *res, float *a, float *b, size_t size, int iterations);
9
int exercise_init(const char *name, size_t size);
10
int exercise_allocate(float **res, float **a, float **b, size_t size);
12
extern int exercise_required_alignment;
15
size_t get_padded_size(size_t size, size_t block_size) {
16
size_t blocks = (size / block_size);
17
size_t inc = blocks * block_size - size;
18
if (inc) return (blocks + 1) * block_size;
23
int main(int argc, char *argv[]) {
26
size_t size = DEFAULT_SIZE;
27
int standard_memory = 0;
28
float *a = NULL, *b = NULL, *res = NULL;
32
struct timeval tv1,tv2;
34
char *name, fname[255];
37
name = strrchr(argv[0], '/');
45
if (exercise_required_alignment > 1) {
46
size = get_padded_size(size, exercise_required_alignment);
49
flops = 2 * size * size * size;
50
printf("Matrix multiplication %lux%lu by %lux%lu, Iterations %u, %lu GFlops required\n", size, size, size, size, ITERATIONS, (flops/1000000000));
52
err = exercise_init(name, size);
55
err = exercise_allocate(&res, &a, &b, size);
61
a = (float *)calloc( size * size, sizeof( float ) );
62
b = (float *)calloc( size * size, sizeof( float ) );
63
res = (float *)calloc( size * size, sizeof( float ) );
64
if(a == NULL || b == NULL || res == NULL) {
65
printf( "\n Can't allocate memory for arrays\n");
71
for (i = 0; i < size * size; i++) {
72
a[i] = 1. * rand() / RAND_MAX;
73
b[i] = 1. * rand() / RAND_MAX;
75
memset(res, 0, size * size * sizeof(float));
78
gettimeofday(&tv1, NULL);
79
runtime = exercise(res, a, b, size, ITERATIONS);
80
gettimeofday(&tv2, NULL);
82
if (runtime == (size_t)-1) {
86
sprintf(fname, "result-%s.out", name);
87
f = fopen(fname, "w");
89
fwrite(res, sizeof(float), size * size, f);
95
if (standard_memory) {
101
us = (tv2.tv_sec - tv1.tv_sec)*1000000 + (tv2.tv_usec - tv1.tv_usec);
103
printf("GFlops: %.2lf (%lf s)\n", ((0.001 * flops) / us), us / 1000000.);
105
runtime /= ITERATIONS;
106
printf("GFlops (excluding transfer): %.2lf (%lf s)\n", ((1. * flops) / runtime), runtime / 1000000000.);