forked from snakster/cpp.react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBenchmarkGrid.h
More file actions
167 lines (127 loc) · 4.01 KB
/
BenchmarkGrid.h
File metadata and controls
167 lines (127 loc) · 4.01 KB
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
167
// Copyright Sebastian Jeckel 2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#pragma once
#include <functional>
#include <iostream>
#include <vector>
#include "tbb/tick_count.h"
#include "BenchmarkBase.h"
#include "react/Signal.h"
using namespace react;
///////////////////////////////////////////////////////////////////////////////////////////////////
/// GridGraphGenerator
///////////////////////////////////////////////////////////////////////////////////////////////////
template
<
typename D,
typename TValue
>
class GridGraphGenerator
{
public:
using MySignal = Signal<D,TValue>;
using Func1T = std::function<TValue(TValue)>;
using Func2T = std::function<TValue(TValue,TValue)>;
using SignalVectT = std::vector<MySignal>;
using WidthVectT = std::vector<size_t>;
SignalVectT InputSignals;
SignalVectT OutputSignals;
Func1T Function1;
Func2T Function2;
WidthVectT Widths;
void Generate()
{
assert(InputSignals.size() >= 1);
assert(Widths.size() >= 1);
SignalVectT buf1 = InputSignals;
SignalVectT buf2;
SignalVectT* curBuf = &buf1;
SignalVectT* nextBuf = &buf2;
size_t curWidth = InputSignals.size();
size_t nodeCount = 1;
nodeCount += curWidth;
for (auto targetWidth : Widths)
{
while (curWidth != targetWidth)
{
// Grow or shrink?
bool shouldGrow = targetWidth > curWidth;
auto l = curBuf->begin();
auto r = curBuf->begin();
if (r != curBuf->end())
++r;
if (shouldGrow)
{
auto s = (*l) ->* Function1;
nextBuf->push_back(s);
}
while (r != curBuf->end())
{
auto s = (*l,*r) ->* Function2;
nextBuf->push_back(s);
nodeCount++;
++l; ++r;
}
if (shouldGrow)
{
auto s = (*l) ->* Function1;
nextBuf->push_back(s);
nodeCount++;
}
curBuf->clear();
// Swap buffer pointers
SignalVectT* t = curBuf;
curBuf = nextBuf;
nextBuf = t;
if (shouldGrow)
curWidth++;
else
curWidth--;
}
}
//printf ("NODE COUNT %d\n", nodeCount);
OutputSignals.clear();
OutputSignals.insert(OutputSignals.begin(), curBuf->begin(), curBuf->end());
}
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Benchmark_Grid
///////////////////////////////////////////////////////////////////////////////////////////////////
struct BenchmarkParams_Grid
{
BenchmarkParams_Grid(int n, int k) :
N(n),
K(k)
{}
void Print(std::ostream& out) const
{
out << "N = " << N
<< ", K = " << K;
}
const int N;
const int K;
};
template <typename D>
struct Benchmark_Grid : public BenchmarkBase<D>
{
double Run(const BenchmarkParams_Grid& params)
{
auto in = MakeVar<D>(1);
GridGraphGenerator<D,int> generator;
generator.InputSignals.push_back(in);
generator.Widths.push_back(params.N);
generator.Widths.push_back(1);
generator.Function1 = [] (int a) { return a; };
generator.Function2 = [] (int a, int b) { return a + b; };
generator.Generate();
auto t0 = tbb::tick_count::now();
for (int i=0; i<params.K; i++)
in <<= 10+i;
auto t1 = tbb::tick_count::now();
double d = (t1 - t0).seconds();
//printf("Time %g\n", d);
return d;
}
};