forked from snakster/cpp.react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBenchmarkLifeSim.h
More file actions
300 lines (233 loc) · 7.56 KB
/
BenchmarkLifeSim.h
File metadata and controls
300 lines (233 loc) · 7.56 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// 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 <iostream>
#include <fstream>
#include <memory>
#include <random>
#include <type_traits>
#include "BenchmarkBase.h"
#include "react/ReactiveObject.h"
#include "react/logging/EventLog.h"
using namespace react;
using std::vector;
using std::atomic;
using std::tuple;
using std::unique_ptr;
using std::move;
using std::make_tuple;
using std::cout;
using std::string;
using std::pair;
using std::make_pair;
using std::get;
enum class Seasons { summer, winter };
enum class Migration { enter, leave };
typedef pair<int,int> PositionT;
template <typename D>
class Time : public ReactiveObject<D>
{
public:
EventSourceT<bool> NewDay = MakeEventSource<bool>();
SignalT<int> TotalDays = Iterate(0, NewDay, Incrementer<int>());
SignalT<int> DayOfYear = TotalDays % 365;
SignalT<Seasons> Season = DayOfYear ->* [] (int day) {
return day < 180 ? Seasons::winter : Seasons::summer;
};
};
template <typename D>
class Region : public ReactiveObject<D>
{
Time<D>& theTime;
public:
using BoundsT = tuple<int,int,int,int>;
BoundsT Bounds;
EventSourceT<Migration> EnterOrLeave = MakeEventSource<Migration>();
SignalT<int> AnimalCount = Fold(0, EnterOrLeave, [] (int count, Migration m) {
return m == Migration::enter ? count + 1 : count - 1;
});
SignalT<int> FoodPerDay = theTime.Season ->* [] (Seasons season) {
return season == Seasons::summer ? 20 : 10;
};
SignalT<int> FoodOutputPerDay =
(FoodPerDay, AnimalCount) ->* [] (int food, int count) {
return count > 0 ? food/count : 0;
};
EventsT<int> FoodOutput = Pulse(FoodOutputPerDay, theTime.NewDay);
Region(Time<D>& time, int x, int y):
theTime { time },
Bounds { make_tuple(x*10, x*10+9, y*10, y*10+9) }
{
}
PositionT Center()
{
return make_pair(get<0>(Bounds) + 5, get<2>(Bounds) + 5);
}
PositionT Clamp(PositionT pos)
{
pos.first = get<0>(Bounds) + (abs(pos.first) % 10);
pos.second = get<2>(Bounds) + (abs(pos.second) % 10);
return pos;
}
bool IsInRegion(PositionT pos)
{
return get<0>(Bounds) <= pos.first && pos.first <= get<1>(Bounds) &&
get<2>(Bounds) <= pos.second && pos.second <= get<3>(Bounds);
}
};
template <typename D>
class World : public ReactiveObject<D>
{
int w_;
public:
vector<unique_ptr<Region<D>>> Regions;
World(Time<D>& time, int w) :
w_( w )
{
for (int x=0; x<w; x++)
for (int y=0; y<w; y++)
Regions.push_back(unique_ptr<Region<D>>(new Region<D>(time, x, y)));
}
Region<D>* GetRegion(PositionT pos)
{
for (auto& r : Regions)
{
if (r->IsInRegion(pos))
return r.get();
}
printf("FATAL ERROR %d %d\n", pos.first, pos.second);
return nullptr;
}
PositionT Clamp(PositionT pos)
{
pos.first = abs(pos.first) % 10*w_;
pos.second = abs(pos.second) % 10*w_;
return pos;
}
};
template <typename D>
class Animal : public ReactiveObject<D>
{
Time<D>& theTime;
World<D>& theWorld;
std::mt19937 generator;
public:
SignalT<PositionT> Position;
VarSignalT<Region<D>*> CurrentRegion;
SignalT<Region<D>*> NewRegion;
Animal(Time<D>& time, World<D>& world, Region<D>* initRegion, unsigned seed) :
theTime( time ),
theWorld( world ),
CurrentRegion( MakeVar(initRegion) ),
generator( seed )
{
Position = Fold(initRegion->Center(), Moving, [this] (PositionT position, bool shouldMigrate) {
std::uniform_int_distribution<int> dist(-1,1);
// Wander randomly
for (int i=0; i<100; i++)
{
position.first += dist(generator);
position.second += dist(generator);
}
if (shouldMigrate)
return theWorld.Clamp(position);
else
return CurrentRegion.Value()->Clamp(position);
});
NewRegion = (Position) ->* [this] (PositionT pos)
{
return theWorld.GetRegion(pos);
};
initRegion->EnterOrLeave << Migration::enter;
Observe(NewRegion, [this] (Region<D>* newRegion) {
CurrentRegion.Value()->EnterOrLeave << Migration::leave;
newRegion->EnterOrLeave << Migration::enter;
CurrentRegion <<= newRegion;
//Migrating << true;
});
}
EventsT<int> FoodReceived = REACTIVE_PTR(CurrentRegion, FoodOutput);
SignalT<int> Age = Iterate(0, theTime.NewDay, Incrementer<int>());
SignalT<int> Health = Fold(100, FoodReceived, [] (int health, int food) {
auto newHealth = health + food - 10;
return newHealth < 0 ? 0 : newHealth > 10000 ? 10000 : newHealth;
});
//Signal<bool> YoungAndHealthy = Age < 1000 && Health > 0;
//Event<bool> Migrating = EventSource<bool>();
SignalT<bool> ShouldMigrate = Hold(0, FoodReceived) < 10;
EventsT<bool> Moving = Pulse(ShouldMigrate, theTime.NewDay);
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Run simulation
///////////////////////////////////////////////////////////////////////////////////////////////////
struct BenchmarkParams_LifeSim
{
BenchmarkParams_LifeSim(int n, int w, int k) :
N( n ),
W( w ),
K( k )
{
}
const int N;
const int W;
const int K;
void Print(std::ostream& out) const
{
out << "N = " << N
<< ", K = " << K
<< ", W = " << W;
}
};
template <typename D>
struct Benchmark_LifeSim : public BenchmarkBase<D>
{
double Run(const BenchmarkParams_LifeSim& params)
{
auto theTime = Time<D>();
auto theWorld = World<D>(theTime, params.W);
auto animals = vector<unique_ptr<Animal<D>>>();
std::mt19937 gen(2015);
std::uniform_int_distribution<int> dist(0,theWorld.Regions.size()-1);
for (int i=0; i<params.N; i++)
{
auto r = theWorld.Regions[dist(gen)].get();
animals.push_back(unique_ptr<Animal<D>>(new Animal<D>(theTime, theWorld, r, i+1)));
}
atomic<int> c(0);
int s = 0;
//for (const auto& e : animals)
//{
// Observe(e->Migrating, [&c] (bool _) {
// c++;
// });
//}
//Observe(theEnvironment.SummerIsComing, [] (int v) {
// printf("=== SUMMER ===\n");
//});
//Observe(theEnvironment.WinterIsComing, [] (int v) {
// printf("=== WINTER ===\n");
//});
// WHEEL IN THE SKY KEEPS ON TURNING
auto t0 = tbb::tick_count::now();
for (int i=0; i<params.K; i++)
{
theTime.NewDay << true;
//s = s + c;
//printf("c %d\n", c);
//c = 0;
}
auto t1 = tbb::tick_count::now();
double d = (t1 - t0).seconds();
//s = s / params.K;
//printf("s %d\n", s);
s = s / params.K;
printf("s/k %d\n", s);
/* std::ofstream logfile;
logfile.open("log.txt");
D::Log().Write(logfile);
logfile.close()*/;
return d;
}
};