/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 tutorials/3_simd/diff.c

  • Committer: Suren A. Chilingaryan
  • Date: 2013-10-08 23:53:50 UTC
  • Revision ID: csa@dside.dyndns.org-20131008235350-hsu8oukzkh05gtcm
Add tutorials

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(unsigned char *res, unsigned char *a, unsigned char *b, size_t size, int iterations);
 
9
int exercise_init(const char *name, size_t size);
 
10
int exercise_allocate(unsigned char **res, unsigned char **a, unsigned char **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
    size_t iterations = ITERATIONS;
 
28
    int standard_memory = 0;
 
29
    unsigned char *a = NULL, *b = NULL, *res = NULL;
 
30
 
 
31
    size_t runtime;
 
32
    size_t us, flops;
 
33
    struct timeval tv1,tv2;
 
34
 
 
35
    char *name, fname[255];
 
36
    FILE *f;
 
37
 
 
38
    name = strrchr(argv[0], '/');
 
39
    if (name) name += 1;
 
40
    else name = argv[0];
 
41
 
 
42
    if (argc > 1) {
 
43
        size = atoi(argv[1]);
 
44
    }
 
45
    
 
46
    if (argc > 2) {
 
47
        iterations = atoi(argv[2]);
 
48
        if (!iterations) iterations = 1;
 
49
    }
 
50
    
 
51
    if (exercise_required_alignment > 1) {
 
52
        size = get_padded_size(size, exercise_required_alignment);
 
53
    }
 
54
 
 
55
    flops = 2 * size * size * sizeof(char);
 
56
    printf("Matrix multiplication %lux%lu by %lux%lu, Iterations %u\n", size, size, size, size, iterations);
 
57
 
 
58
    err = exercise_init(name, size);
 
59
    if (err) return err;
 
60
 
 
61
    err = exercise_allocate(&res, &a, &b, size);
 
62
    if (err) {
 
63
        if (res) return err;
 
64
        else printf("Allocation of fast memory failed, using standard slow mode...\n");
 
65
    }
 
66
    
 
67
    if (!res) {
 
68
        standard_memory = 1;
 
69
        
 
70
        a = (unsigned char *)calloc( size * size, sizeof( unsigned char ) );
 
71
        b = (unsigned char *)calloc( size * size, sizeof( unsigned char ) );
 
72
        res = (unsigned char *)calloc( size * size, sizeof( unsigned char ) );
 
73
        if(a == NULL || b == NULL || res == NULL) {
 
74
            printf( "Can't allocate memory for arrays\n");
 
75
            return 0;
 
76
        }
 
77
    }
 
78
 
 
79
    srand(1);
 
80
    for (i = 0; i < size * size; i++) {
 
81
        a[i] = 127. * rand() / RAND_MAX;
 
82
        b[i] = 127. * rand() / RAND_MAX;
 
83
    }
 
84
    memset(res, 0, size * size * sizeof(unsigned char));
 
85
 
 
86
    gettimeofday(&tv1, NULL);
 
87
    runtime = exercise(res, a, b, size, iterations);
 
88
    gettimeofday(&tv2, NULL);
 
89
 
 
90
    if (runtime == (size_t)-1) {
 
91
        return 1;
 
92
    }
 
93
 
 
94
    sprintf(fname, "result-%s.out", name);
 
95
    f = fopen(fname, "w");
 
96
    if (f) {
 
97
        fwrite(res, sizeof(unsigned char), size * size, f);
 
98
        fclose(f);
 
99
    }
 
100
 
 
101
    exercise_free();
 
102
    
 
103
    if (standard_memory) {
 
104
        free(res);
 
105
        free(b);
 
106
        free(a);
 
107
    }
 
108
 
 
109
    us = (tv2.tv_sec - tv1.tv_sec)*1000000 + (tv2.tv_usec - tv1.tv_usec);
 
110
    us /= iterations;
 
111
    printf("Bandwidth: %.2lf GB/s (%lf s)\n", ((0.001 * flops) / us), us / 1000000.);
 
112
    if (runtime) {
 
113
        runtime /= iterations;
 
114
        printf("Bandwidth (excluding transfer): %.2lf GB/s (%lf s)\n", ((1. * flops) / runtime), runtime / 1000000000.);
 
115
    }
 
116
 
 
117
    return 0;
 
118
}
 
119