-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathworker_thread.cpp
More file actions
112 lines (91 loc) · 1.94 KB
/
worker_thread.cpp
File metadata and controls
112 lines (91 loc) · 1.94 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
#include "worker_thread.h"
WorkerThread::WorkerThread(int id, std::vector<Complex>* _hints) :
hints(_hints),
samples_so_far(0),
mt(rd()),
dist(std::uniform_real_distribution<double>(-p.fuzz, p.fuzz)),
hint_dist(std::uniform_int_distribution<int>(0, _hints->size() - 1)),
seed(id)
{
mt.seed(seed);
}
void WorkerThread::start()
{
t = std::thread(&WorkerThread::compute, this);
}
void WorkerThread::record(Complex& o)
{
int d = 0;
Complex c {0.0, 0.0};
while (!escaped(c) && d < p.depth)
{
recurse(c, o);
int i = get_i(c.r);
int j = get_j(c.i);
if (in_bounds(i) && in_bounds(j))
{
int index = p.dim*j + i;
auto got = point_map.find(index);
if (got == point_map.end())
{
point_map.insert({index, 1});
} else
{
got->second++;
}
}
d++;
}
}
Complex WorkerThread::get_rand()
{
int idx = hint_dist(mt);
Complex hint = hints->at(idx);
hint.r += dist(mt);
hint.i += dist(mt);
return hint;
}
void WorkerThread::compute()
{
bool finished = false;
while (not finished)
{
finished = iteration();
}
}
bool WorkerThread::iteration()
{
for (int i=0; i<p.samples_per_iteration; i++)
{
Complex cur {0.0, 0.0};
Complex original = get_rand();
for (int d=0; d<p.depth; d++)
{
recurse(cur, original);
if (escaped(cur))
{
if (d > p.min_depth)
{
record(original);
}
break;
}
}
}
samples_so_far += p.samples_per_iteration;
return samples_so_far >= p.samples;
}
void WorkerThread::finish()
{
if (t.joinable())
{
t.join();
}
}
WorkerThread::~WorkerThread()
{
if (t.joinable())
{
t.join();
}
}