File: chunky.cpp

package info (click to toggle)
blitz%2B%2B 1%3A0.10-3.2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 13,276 kB
  • ctags: 12,037
  • sloc: cpp: 70,465; sh: 11,116; fortran: 1,510; python: 1,246; f90: 852; makefile: 701
file content (156 lines) | stat: -rw-r--r-- 3,152 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <blitz/timer.h>

using namespace blitz;

void report(const char* name, Timer& timer, int N,
    long int iterations);
void unfused(int N);
void fused(int N);
void chunky(int N);

float* _bz_restrict a;
float* _bz_restrict b;
float* _bz_restrict c;
float* _bz_restrict d;
float* _bz_restrict e;
float* _bz_restrict f;

/*
 * Code to support the "fuse" macro
 */

int _chunk;
bool _done_chunks;
int _chunk_size = 512;

#define  fuse    _chunk = 0; _done_chunks = false;    \
                 for (; !_done_chunks; ++_chunk)

int main(int argc, char** argv)
{
    if (argc == 2)
        _chunk_size = atoi(argv[1]);

    cout << "Using chunk size " << _chunk_size << endl;

    const int N = 100000;

    a = new float[N];
    b = new float[N];
    c = new float[N];
    d = new float[N];
    e = new float[N];
    f = new float[N];

    for (int i=0; i < N; ++i)
    {
        a[i] = i;
        b[i] = i;
        c[i] = i;
        d[i] = i;
    }

    Timer timer;
    long int iterations = 100;

    timer.start();
    for (long i=0; i < iterations; ++i)
        unfused(N);
    timer.stop();

    report("Unfused", timer, N, iterations);

    timer.start();
    for (long i=0; i < iterations; ++i)
        fused(N);
    timer.stop();

    report("Fused", timer, N, iterations);

    timer.start();
    for (long i=0; i < iterations; ++i)
        chunky(N);
    timer.stop();

    report("Chunky", timer, N, iterations);

    return 0;
}

void report(const char* name, Timer& timer, int N,
    long int iterations)
{
    float flops = float(N) * iterations * 2;
    float Mflops = flops / timer.elapsedSeconds() / 1e+6;
    cout << setw(20) << name << " " << Mflops << " Mflops/s" << endl;
}

void __sink() { }

void unfused(int N)
{
    for (int i=0; i < N; ++i)
        e[i] = a[i] * b[i] + c[i] * d[i];

    __sink();

    for (int i=0; i < N; ++i)
        f[i] = c[i] * b[i] + a[i] * d[i];
}

void fused(int N)
{
    for (int i=0; i < N; ++i)
    {
        e[i] = a[i] * b[i] + c[i] * d[i];
        f[i] = c[i] * b[i] + a[i] * d[i];
    }
}


// This "chunky" routine is a simulated implementation of
// expression templates with tiling across multiple statements
// (the "chunky fusion" approach).  This code would be
// generated by:
//
//     fuse {
//         E = A*B + C*D;
//         F = C*B + A*D;
//     }

void chunky(int N)
{
    fuse {

        {   // Code generated by E = A*B + C*D;
            int lbound = _chunk * _chunk_size;
            int uboundp1 = lbound + _chunk_size;

            if (uboundp1 > N)
            {
                _done_chunks = true;
                uboundp1 = N;
            }

            for (int i=lbound; i < uboundp1; ++i)
                e[i] = a[i] * b[i] + c[i] * d[i];
        }

        __sink();

        {   // Code generated by F = C*B + A*D;
            int lbound = _chunk * _chunk_size;
            int uboundp1 = lbound + _chunk_size; 

            if (uboundp1 > N)
            {
                _done_chunks = true;
                uboundp1 = N;
            }

            for (int i=lbound; i < uboundp1; ++i)
                f[i] = c[i] * b[i] + a[i] * d[i];
        }
    }
}