/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/2_dotproduct/dp.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
#include <locale.h>
 
5
 
 
6
#define ITERATIONS 10
 
7
#define DEFAULT_SIZE 1000000
 
8
 
 
9
size_t exercise(float *res, float *a, float *b, size_t size, int iterations);
 
10
int exercise_init(const char *name, size_t size);
 
11
int exercise_allocate(float **a, float **b, size_t size);
 
12
void exercise_free();
 
13
extern int exercise_required_alignment;
 
14
 
 
15
 
 
16
size_t get_padded_size(size_t size, size_t block_size) {
 
17
    size_t blocks = (size / block_size);
 
18
    size_t inc = blocks * block_size - size;
 
19
    if (inc) return (blocks + 1) * block_size;
 
20
    return size;
 
21
}
 
22
 
 
23
 
 
24
int main(int argc, char *argv[]) {
 
25
    int err;
 
26
    long i;
 
27
    size_t size = DEFAULT_SIZE;
 
28
    size_t iterations = ITERATIONS;
 
29
    int standard_memory = 0;
 
30
    float *a = NULL, *b = NULL, res;
 
31
 
 
32
    size_t runtime;
 
33
    size_t us, throughput;
 
34
    struct timeval tv1,tv2;
 
35
 
 
36
    char *name, fname[255];
 
37
    FILE *f;
 
38
 
 
39
    name = strrchr(argv[0], '/');
 
40
    if (name) name += 1;
 
41
    else name = argv[0];
 
42
 
 
43
    if (argc > 1) {
 
44
        size = atoi(argv[1]);
 
45
    }
 
46
    
 
47
    if (argc > 2) {
 
48
        iterations = atoi(argv[2]);
 
49
        if (!iterations) iterations = 1;
 
50
    }
 
51
 
 
52
    if (exercise_required_alignment > 1) {
 
53
        size = get_padded_size(size, exercise_required_alignment);
 
54
    }
 
55
 
 
56
    throughput = 2 * size * sizeof(float);
 
57
    setlocale(LC_NUMERIC, "en_US");
 
58
    printf("Dot product of %'lu elements, Iterations %u, %lu Gthroughput required\n", size, iterations, (throughput/1000000000));
 
59
 
 
60
    err = exercise_init(name, size);
 
61
    if (err) return err;
 
62
 
 
63
    err = exercise_allocate(&a, &b, size);
 
64
    if (err) {
 
65
        if (res) return err;
 
66
        else printf("Allocation of fast memory failed, using standard slow mode...\n");
 
67
    }
 
68
    
 
69
    if (!a) {
 
70
        standard_memory = 1;
 
71
        
 
72
        a = (float *)calloc( size, sizeof( float ) );
 
73
        b = (float *)calloc( size, sizeof( float ) );
 
74
        if(a == NULL || b == NULL) {
 
75
            printf( "Can't allocate memory for arrays\n");
 
76
            return 0;
 
77
        }
 
78
    }
 
79
 
 
80
    srand(1);
 
81
    for (i = 0; i < size; i++) {
 
82
        a[i] = 1. * rand() / RAND_MAX;
 
83
        b[i] = 1. * rand() / RAND_MAX;
 
84
    }
 
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
    printf("Result: %lf\n", res);
 
95
 
 
96
    exercise_free();
 
97
    
 
98
    if (standard_memory) {
 
99
        free(b);
 
100
        free(a);
 
101
    }
 
102
 
 
103
    us = (tv2.tv_sec - tv1.tv_sec)*1000000 + (tv2.tv_usec - tv1.tv_usec);
 
104
    us /= iterations;
 
105
    printf("Throughput: %.2lf GB/s (%lf s)\n", ((0.001 * throughput) / us), us / 1000000.);
 
106
    if (runtime) {
 
107
        runtime /= iterations;
 
108
        printf("Throughput (excluding transfer): %.2lf GB/s (%lf s)\n", ((1. * throughput) / runtime), runtime / 1000000000.);
 
109
    }
 
110
 
 
111
    return 0;
 
112
}
 
113