summaryrefslogtreecommitdiff
path: root/compute.cpp
blob: d3f5e42f3361253277f064135b925c73d3841910 (plain)
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
#include "compute.h"
#include <complex>
#include <random>
#include <thread>
#include <memory>
#include <cstdlib>
#include <cassert>
#include <immintrin.h>

struct Pointgen {
	Pointgen(std::mt19937::result_type seed) : randgen{seed} {}
	double operator()() {
		return dist(randgen);
	}

private:
	std::mt19937 randgen;
	std::uniform_real_distribution<double> dist{-1.5, 1.5};
};

std::ostream& operator<<(std::ostream &os, __m256d x) {
	union {
		__m256d vec;
		double arr[4];
	} u;
	u.vec = x;
	return os << '<' << u.arr[0] << ',' << u.arr[1] << ',' << u.arr[2] << ',' << u.arr[3] << '>';
}

std::ostream& operator<<(std::ostream &os, __m256i x) {
	union {
		__m256i vec;
		int64_t arr[4];
	} u;
	u.vec = x;
	return os << '<' << u.arr[0] << ',' << u.arr[1] << ',' << u.arr[2] << ',' << u.arr[3] << '>';
}

union int64_4vec {
	__m256i i;
	int64_t a[4];
};

static HistImage accumulate_brot_simple(const Options &options, int64_t numorbits_override, std::mt19937::result_type seed) {
	HistImage img{(size_t)options.imgwidth.value, (size_t)options.imgheight.value};

	Pointgen gen{seed};

	// trace[0] is never set; considered equal to c
	std::vector<std::pair<double, double>> trace(options.maxiter.value + 1);

	for (int64_t orbiti = 0; orbiti < numorbits_override; orbiti++) {
		const double cx = gen(), cy = gen();
		// std::cout << "cx=" << cx << " cy=" << cy << std::endl;

		int iteri = 0;
		{
			double a = cx, b = cy;
			if (a * a + b * b >= 4) continue;
			while (iteri < options.maxiter.value) {
				const double newb = 2 * a * b + cy;
				a = a * a - b * b + cx;
				b = newb;
				iteri++;
				trace[iteri] = std::make_pair(a, b);

				// std::cout << "a=" << a << " b=" << b << std::endl;
				if (a * a + b * b >= 4) goto escape;
			}
			continue;  // did not escape, so in set, so ignore
		}

	escape:
		if (options.orbit_burnin.value == 0) trace[0] = std::make_pair(cx, cy);
		// std::cout << "iteri=" << iteri << std::endl;
		for (int i = options.orbit_burnin.value; i <= iteri; i++) {
			const double zx = trace[i].first, zy = trace[i].second;
			const int imgx = (zx - options.xmin.value) / options.cplxwidth.value * (options.imgwidth.value - 1);
			const int imgy = (zy - options.ymin.value) / options.cplxheight.value * (options.imgheight.value - 1);
			// std::cerr << " zx=" << zx << " zy=" << zy << " img=(" << imgx << ',' << imgy << ")" << std::endl;
			if (0 <= imgx && imgx < options.imgwidth.value &&
					0 <= imgy && imgy < options.imgheight.value) {
				img.data[options.imgwidth.value * imgy + imgx]++;
			}
		}
	}

	return img;
}

static HistImage accumulate_brot_vectorised(const Options &options, int64_t numorbits_override, std::mt19937::result_type seed) {
	HistImage img{(size_t)options.imgwidth.value, (size_t)options.imgheight.value};

	Pointgen gen{seed};

	using F4 = __m256d;
	using I4 = __m256i;

	// trace[0] is never set; considered equal to c
	std::vector<std::pair<F4, F4>> trace(options.maxiter.value + 1);

	for (int64_t orbiti = 0; orbiti < numorbits_override; orbiti += 4) {
		const F4 cx = _mm256_set_pd(gen(), gen(), gen(), gen());
		const F4 cy = _mm256_set_pd(gen(), gen(), gen(), gen());
		// std::cout << "cx=" << cx << " cy=" << cy << std::endl;

		I4 escaped_mask = _mm256_setzero_si256();
		I4 escaped_at = _mm256_setzero_si256();

		int iteri = 0;
		{
			F4 a = cx, b = cy;
			if (_mm256_movemask_pd(a * a + b * b < 4) == 0) continue;
			while (iteri < options.maxiter.value) {
				const F4 newb = 2 * a * b + cy;
				a = a * a - b * b + cx;
				b = newb;
				iteri++;
				trace[iteri] = std::make_pair(a, b);

				const I4 remaining = a * a + b * b < 4;
				const I4 escaped = ~remaining;
				escaped_at |= _mm256_set1_epi64x(iteri) & ~escaped_mask & escaped;
				escaped_mask |= escaped;
				// std::cout << "a=" << a << " b=" << b << " rem=" << remaining << " esc=" << escaped << " at=" << escaped_at << " mask=" << escaped_mask << std::endl;
				if (_mm256_movemask_epi8(remaining) == 0) goto escape;
				// a = _mm256_castsi256_pd(_mm256_castpd_si256(a) & remaining);
				// b = _mm256_castsi256_pd(_mm256_castpd_si256(b) & remaining);
			}
		}
		if (_mm256_movemask_epi8(escaped_mask) == 0) continue;  // if none escaped, nothing to do

	escape:
		if (options.orbit_burnin.value == 0) trace[0] = std::make_pair(cx, cy);
		union int64_4vec escaped_at_un;
		_mm256_store_si256(&escaped_at_un.i, escaped_at);
		for (int tracei = 0; tracei < 4; tracei++) {
			const int limit = escaped_at_un.a[tracei];
			// std::cout << "tracei=" << tracei << " limit=" << limit << std::endl;
			for (int i = options.orbit_burnin.value; i <= limit; i++) {
				const double zx = ((const double*)&trace[i].first)[tracei];
				const double zy = ((const double*)&trace[i].second)[tracei];
				const int imgx = (zx - options.xmin.value) / options.cplxwidth.value * (options.imgwidth.value - 1);
				const int imgy = (zy - options.ymin.value) / options.cplxheight.value * (options.imgheight.value - 1);
				// std::cerr << " zx=" << zx << " zy=" << zy << " img=(" << imgx << ',' << imgy << ")" << std::endl;
				if (0 <= imgx && imgx < options.imgwidth.value &&
						0 <= imgy && imgy < options.imgheight.value) {
					img.data[options.imgwidth.value * imgy + imgx]++;
				}
			}
		}
	}

	return img;
}

static HistImage accumulate_brot(const Options &options, int64_t numorbits_override, std::mt19937::result_type seed) {
	if (options.algorithm.value == "simple") return accumulate_brot_simple(options, numorbits_override, seed);
	if (options.algorithm.value == "vectorised") return accumulate_brot_vectorised(options, numorbits_override, seed);
	std::cerr << "Invalid algorithm '" << options.algorithm.value << "'" << std::endl;
	exit(1);
}

HistImage compute_brot(const Options &options) {
	std::vector<std::unique_ptr<HistImage>> images(options.num_threads.value);

	std::mt19937 randgen(std::random_device{}());

	std::vector<std::thread> ths(options.num_threads.value);
	for (size_t i = 0; i < (size_t)options.num_threads.value; i++) {
		std::mt19937::result_type seed = randgen();
		ths[i] = std::thread{[&options, &images, i, seed]() {
			images[i] = std::make_unique<HistImage>(
				accumulate_brot(options, options.numorbits.value / options.num_threads.value, seed));
		}};
	}

	for (std::thread &th : ths) th.join();

	HistImage first = std::move(*images[0]);
	images[0].reset();

	for (int i = 1; i < options.num_threads.value; i++) {
		first.add(*images[i]);
		images[i].reset();
	}

	return first;
}

// vim: set sw=4 ts=4 noet: