File: main.cpp

package info (click to toggle)
vc 1.4.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,552 kB
  • sloc: cpp: 19,220; ansic: 15,669; sh: 453; xml: 186; makefile: 30
file content (166 lines) | stat: -rw-r--r-- 6,112 bytes parent folder | download | duplicates (3)
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
157
158
159
160
161
162
163
164
165
166
/*{{{
    Copyright (C) 2013 Matthias Kretz <kretz@kde.org>

    Permission to use, copy, modify, and distribute this software
    and its documentation for any purpose and without fee is hereby
    granted, provided that the above copyright notice appear in all
    copies and that both that the copyright notice and this
    permission notice and warranty disclaimer appear in supporting
    documentation, and that the name of the author not be used in
    advertising or publicity pertaining to distribution of the
    software without specific, written prior permission.

    The author disclaim all warranties with regard to this
    software, including all implied warranties of merchantability
    and fitness.  In no event shall the author be liable for any
    special, indirect or consequential damages or any damages
    whatsoever resulting from loss of use, data or profits, whether
    in an action of contract, negligence or other tortious action,
    arising out of or in connection with the use or performance of
    this software.

}}}*/

#include <array>
#include <memory>

#include <Vc/Vc>
#include "../tsc.h"

using Vc::float_v;

/*
 * This example shows how an arbitrary problem scales depending on working-set size and FLOPs per
 * load/store. Understanding this can help to create better implementations.
 */

/*
 * The Runner is a method to generate the different scenarios with all parameters to the Work
 * available as constant expressions.
 * The idea is to have the compiler able to optimize as much as possible so that the actual workload
 * alone is benchmarked.
 *
 * The Runner recursively calls operator() on the Work template class with varying arguments for N
 * and FLOPs.
 */
template<template<std::size_t N, std::size_t M, int, int> class Work, std::size_t N = 256, std::size_t M = 4> struct Runner
{
    static void run() {
        Work<N, M, (N > 4096 ? 1 : 4096 / N), 2>()();
        Work<N, M, (N > 4096 ? 1 : 4096 / N), 3>()();
        Work<N, M, (N > 4096 ? 1 : 4096 / N), 4>()();
        Work<N, M, (N > 4096 ? 1 : 4096 / N), 6>()();
        Work<N, M, (N > 4096 ? 1 : 4096 / N), 9>()();
        Work<N, M, (N > 4096 ? 1 : 4096 / N), 13>()();
        Work<N, M, (N > 4096 ? 1 : 4096 / N), 19>()();
        Work<N, M, (N > 4096 ? 1 : 4096 / N), 28>()();
        Work<N, M, (N > 4096 ? 1 : 4096 / N), 42>()();
        Work<N, M, (N > 4096 ? 1 : 4096 / N), 63>()();
        Work<N, M, (N > 4096 ? 1 : 4096 / N), 94>()();
        Work<N, M, (N > 4096 ? 1 : 4096 / N), 141>()();
        Work<N, M, (N > 4096 ? 1 : 4096 / N), 211>()();
        Runner<Work, N * 2, M>::run();
    }
};

template <template <std::size_t N, std::size_t M, int, int> class Work, std::size_t M>
struct Runner<Work, 256 * 1024 * 32,  // don't make this number larger, otherwise GCC6
                                      // blows up with Vc::Scalar to 10GB of memory usage
              M> {
    static void run() {
    }
};

/*
 * The Flops helper struct generates code that executes FLOPs many floating-point SIMD instructions
 * (add, sub, and mul)
 */
template<int FLOPs> struct Flops
{
    inline float_v operator()(float_v a, float_v b, float_v c)
    {
        typedef Flops<(FLOPs - 5) / 2> F1;
        typedef Flops<(FLOPs - 4) / 2> F2;
        return F1()(a + b, a * b, c) + F2()(a * c, b + c, a);
    }
};

template<> inline float_v Flops<2>::operator()(float_v a, float_v b, float_v c)
{
    return a * b + c;
}
template<> inline float_v Flops<3>::operator()(float_v a, float_v b, float_v c)
{
    return a * b + (c - a);
}
template<> inline float_v Flops<4>::operator()(float_v a, float_v b, float_v c)
{
    return (a * b + c) + a * c;
}
template<> inline float_v Flops<5>::operator()(float_v a, float_v b, float_v c)
{
    return a * b + (a + c) + a * c;
}
template<> inline float_v Flops<6>::operator()(float_v a, float_v b, float_v c)
{
    return (a * b + (a + c)) + (a * c - b);
}
template<> inline float_v Flops<7>::operator()(float_v a, float_v b, float_v c)
{
    return (a * b + (a + c)) + (a * c - (b + c));
}
template<> inline float_v Flops<8>::operator()(float_v a, float_v b, float_v c)
{
    return (a * b + (a + c) + b) + (a * c - (b + c));
}

/*
 * This is the benchmark code. It is called from Runner and uses Flops to do the work.
 */
template<std::size_t _N, std::size_t M, int Repetitions, int FLOPs>
struct ScaleWorkingSetSize
{
    void operator()()
    {
        constexpr std::size_t N = _N / sizeof(float_v) + 3 * 16 / float_v::Size;
        typedef std::array<std::array<float_v, N>, M> Cont;
        auto data = Vc::make_unique<Cont, Vc::AlignOnPage>();
        for (auto &arr : *data) {
            for (auto &value : arr) {
                value = float_v::Random();
            }
        }

        TimeStampCounter tsc;
        double throughput = 0.;
        for (std::size_t i = 0; i < 2 + 512 / N; ++i) {
            tsc.start();
            // ------------- start of the benchmarked code ---------------
            for (int repetitions = 0; repetitions < Repetitions; ++repetitions) {
                for (std::size_t m = 0; m < M; ++m) {
                    for (std::size_t n = 0; n < N; ++n) {
                        (*data)[m][n] = Flops<FLOPs>()((*data)[(m + 1) % M][n],
                                (*data)[(m + 2) % M][n],
                                (*data)[(m + 3) % M][n]);
                    }
                }
            }
            // -------------- end of the benchmarked code ----------------
            tsc.stop();

            throughput = std::max(throughput, (Repetitions * M * N * float_v::Size * FLOPs) / static_cast<double>(tsc.cycles()));
        }

        const long bytes = N * M * sizeof(float_v);
        printf("%10lu Byte | %4.2f FLOP/Byte | %4.1f FLOP/cycle\n", bytes, static_cast<double>(float_v::Size * FLOPs) / (4 * sizeof(float_v)), throughput
                );
    }
};

int Vc_CDECL main()
{
    ScaleWorkingSetSize<256, 4, 10, 2>()();
    printf("%10s | %4s | %4s\n", "Working-Set Size", "FLOPs per Byte", "Throughput (FLOPs/Cycle)");
    Runner<ScaleWorkingSetSize>::run();
    return 0;
}