/perf/kseta

To get this branch, use:
bzr branch http://darksoft.org/webbzr/perf/kseta

« back to all changes in this revision

Viewing changes to sources/mm/mm.c

  • Committer: Suren A. Chilingaryan
  • Date: 2013-09-30 06:47:09 UTC
  • Revision ID: csa@dside.dyndns.org-20130930064709-55cde0k5ci76t8z5
Simple matrix multiplication

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdlib.h>
 
3
#include <string.h>
 
4
 
 
5
#define ITERATIONS 10
 
6
#define DEFAULT_SIZE 1024
 
7
 
 
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);
 
11
void exercise_free();
 
12
extern int exercise_required_alignment;
 
13
 
 
14
 
 
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;
 
19
    return size;
 
20
}
 
21
 
 
22
 
 
23
int main(int argc, char *argv[]) {
 
24
    int err;
 
25
    long i;
 
26
    size_t size = DEFAULT_SIZE;
 
27
    int standard_memory = 0;
 
28
    float *a = NULL, *b = NULL, *res = NULL;
 
29
 
 
30
    size_t runtime;
 
31
    size_t us, flops;
 
32
    struct timeval tv1,tv2;
 
33
 
 
34
    char *name, fname[255];
 
35
    FILE *f;
 
36
 
 
37
    name = strrchr(argv[0], '/');
 
38
    if (name) name += 1;
 
39
    else name = argv[0];
 
40
 
 
41
    if (argc > 1) {
 
42
        size = atoi(argv[1]);
 
43
    }
 
44
    
 
45
    if (exercise_required_alignment > 1) {
 
46
        size = get_padded_size(size, exercise_required_alignment);
 
47
    }
 
48
 
 
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));
 
51
 
 
52
    err = exercise_init(name, size);
 
53
    if (err) return err;
 
54
 
 
55
    err = exercise_allocate(&res, &a, &b, size);
 
56
    if (err) return err;
 
57
 
 
58
    if (!res) {
 
59
        standard_memory = 1;
 
60
        
 
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");
 
66
            return 0;
 
67
        }
 
68
    }
 
69
 
 
70
    srand(1);
 
71
    for (i = 0; i < size * size; i++) {
 
72
        a[i] = 1. * rand() / RAND_MAX;
 
73
        b[i] = 1. * rand() / RAND_MAX;
 
74
    }
 
75
    memset(res, 0, size * size * sizeof(float));
 
76
 
 
77
    
 
78
    gettimeofday(&tv1, NULL);
 
79
    runtime = exercise(res, a, b, size, ITERATIONS);
 
80
    gettimeofday(&tv2, NULL);
 
81
 
 
82
    if (runtime == (size_t)-1) {
 
83
        return 1;
 
84
    }
 
85
 
 
86
    sprintf(fname, "result-%s.out", name);
 
87
    f = fopen(fname, "w");
 
88
    if (f) {
 
89
        fwrite(res, sizeof(float), size * size, f);
 
90
        fclose(f);
 
91
    }
 
92
 
 
93
    exercise_free();
 
94
 
 
95
    if (standard_memory) {
 
96
        free(res);
 
97
        free(b);
 
98
        free(a);
 
99
    }
 
100
 
 
101
    us = (tv2.tv_sec - tv1.tv_sec)*1000000 + (tv2.tv_usec - tv1.tv_usec);
 
102
    us /= ITERATIONS;
 
103
    printf("GFlops: %.2lf (%lf s)\n", ((0.001 * flops) / us), us / 1000000.);
 
104
    if (runtime) {
 
105
        runtime /= ITERATIONS;
 
106
        printf("GFlops (excluding transfer): %.2lf (%lf s)\n", ((1. * flops) / runtime), runtime / 1000000000.);
 
107
    }
 
108
 
 
109
    return 0;
 
110
}
 
111