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
165 lines (127 loc) · 4.03 KB
/
BenchmarkGrid.h
File metadata and controls
165 lines (127 loc) · 4.03 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
// 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 <random>
#include <vector>
#include "BenchmarkBase.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
/// GridGraphGenerator
///////////////////////////////////////////////////////////////////////////////////////////////////
template
<
typename D,
typename TValue
>
class GridGraphGenerator
{
public:
typedef typename D::template SignalT<TValue> MyHandle;
typedef std::function<TValue(TValue)> Func1T;
typedef std::function<TValue(TValue,TValue)> Func2T;
typedef std::vector<MyHandle> HandleVect;
typedef std::vector<int> WidthVect;
HandleVect InputSignals;
HandleVect OutputSignals;
Func1T Function1;
Func2T Function2;
WidthVect Widths;
void Generate()
{
assert(InputSignals.size() >= 1);
assert(Widths.size() >= 1);
HandleVect buf1 = InputSignals;
HandleVect buf2;
HandleVect* curBuf = &buf1;
HandleVect* nextBuf = &buf2;
int curWidth = InputSignals.size();
int 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
HandleVect* 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)
{
}
const int N;
const int K;
void Print(std::ostream& out) const
{
out << "N = " << N
<< ", K = " << K;
}
};
template <typename D>
struct Benchmark_Grid : public BenchmarkBase<D>
{
double Run(const BenchmarkParams_Grid& params)
{
using MyDomain = D;
auto in = MyDomain::MakeVar(1);
GridGraphGenerator<MyDomain,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;
}
};