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
|
#include <chrono>
#include <cstdint>
#include <cstdlib>
#include <complex>
#include <fstream>
#include <iostream>
#include <thread>
#include <vector>
#include <mutex>
#include <atomic>
//Import things we need from the standard library
using std::chrono::duration_cast;
using std::chrono::milliseconds;
using std::complex;
using std::cout;
using std::endl;
using std::ofstream;
using std::thread;
using std::this_thread::sleep_for;
using std::vector;
using std::mutex;
using std::atomic_size_t;
class MyClass
{
private:
std::thread t1;
std::thread t2;
std::thread t3;
std::thread t4;
std::atomic_size_t counter = 0;
int i;
int left;
int right;
int top;
int bottom;
int y_start;
int y_stop;
public:
MyClass();
~MyClass();
};
// Define the alias "the_clock" for the clock type we're going to use.
typedef std::chrono::steady_clock the_clock;
// The size of the image to generate.
const int WIDTH = 1920;
const int HEIGHT = 1200;
//Splitting Image by an amount
const int thread = 4;
// The number of times to iterate before we assume that a point isn't in the
// Mandelbrot set.
// (You may need to turn this up if you zoom further into the set.)
const int MAX_ITERATIONS = 500;
int NUM_THREADS = 4; // number of chunks for parallel processing
// The image data.
// Each pixel is represented as 0xRRGGBB.
uint32_t image[HEIGHT][WIDTH];
int Colour1 = 0;
int Colour2 = 0;
// Write the image to a TGA file with the given name.
// Format specification: http://www.gamers.org/dEngine/quake3/TGA.txt
void write_tga(const char* filename)
{
ofstream outfile(filename, ofstream::binary);
uint8_t header[18] = {
0, // no image ID
0, // no colour map
2, // uncompressed 24-bit image
0, 0, 0, 0, 0, // empty colour map specification
0, 0, // X origin
0, 0, // Y origin
WIDTH & 0xFF, (WIDTH >> 8) & 0xFF, // width
HEIGHT & 0xFF, (HEIGHT >> 8) & 0xFF, // height
24, // bits per pixel
0, // image descriptor
};
outfile.write((const char*)header, 18);
for (int y = 0; y < HEIGHT; ++y)
{
for (int x = 0; x < WIDTH; ++x)
{
uint8_t pixel[3] = {
image[y][x] & 0xFF, // blue channel
(image[y][x] >> 8) & 0xFF, // green channel
(image[y][x] >> 16) & 0xFF, // red channel
};
outfile.write((const char*)pixel, 3);
}
}
outfile.close();
if (!outfile)
{
// An error has occurred at some point since we opened the file.
cout << "Error writing to " << filename << endl;
exit(1);
}
}
// Render the Mandelbrot set into the image array.
// The parameters specify the region on the complex plane to plot.
std::mutex m;
void compute_mandelbrot(double left, double right, double top, double bottom, unsigned y_start, unsigned y_stop)
{
m.lock();
for (int y = y_start; y < y_stop; ++y)
{
for (int x = 0; x < WIDTH; ++x)
{
// Work out the point in the complex plane that
// corresponds to this pixel in the output image.
complex<double> c(left + (x * (right - left) / WIDTH),
top + (y * (bottom - top) / HEIGHT));
// Start off z at (0, 0).
complex<double> z(0.0, 0.0);
// Iterate z = z^2 + c until z moves more than 2 units
// away from (0, 0), or we've iterated too many times.
int iterations = 0;
while (abs(z) < 2.0 && iterations < MAX_ITERATIONS)
{
z = (z * z) + c;
++iterations;
}
if (iterations == MAX_ITERATIONS)
{
// z didn't escape from the circle.
// This point is in the Mandelbrot set.
image[y][x] = Colour1; // black
}
else
{
// z escaped within less than MAX_ITERATIONS
// iterations. This point isn't in the set.
image[y][x] = Colour2; // white
}
}
}
m.unlock();
}
/*
// compute mandelbrot set on each chunk
MyClass::MyClass() :
for (int i = 0; i < ::NUM_THREADS; ++i)
{
t[i](&compute_mandelbrot, left, right, top, bottom, y_start, y_stop));
}
{}
//cout << " [INFO] Threads created: " << ::NUM_THREADS << endl;
MyClass::~MyClass()
for (int i = 0; i < ::NUM_THREADS; ++i)
{
t[i].join();
}
*/
MyClass::MyClass():
t1(&compute_mandelbrot, left, right, top, bottom, y_start, y_stop)
{}
MyClass::~MyClass()
{
t1.join();
t2.join();
t3.join();
t4.join();
}
int main(int argc, char* argv[])
{
cout << "Please choose your first colour" << endl;
cout << "Type 1 for red, 2 for blue, 3 for green:";
std::cin >> Colour1;
switch (Colour1)
{
case 1: Colour1 = 0xFF0000; //red
break; // exits switch
case 2: Colour1 = 0x0000FF; //blue
break;
case 3: Colour1 = 0x00FF00; //green
break;
}
cout << "Please choose second colour" << endl;
cout << "Enter 1 for cyan, 2 for yellow and 3 for magenta:";
std::cin >> Colour2;
switch (Colour2)
{
case 1: Colour2 = 0x00FFFF; //cyan
break;
case 2: Colour2 = 0xFFFF00; //yellow
break;
case 3: Colour2 = 0xFF00FF; //magenta
break;
}
MyClass myClass;
//auto threads = std::make_unique<std::thread[]>(std::thread::hardware_concurrency());
//std::thread t[number_of_threads];
//std::thread t1(&compute_mandelbrot);
//thread t2(compute_mandelbrot);
//t1.join();
//t2.join();
// compute mandelbrot set on each chunk
//int i = 1;
/*
for (int i = 0; i < ::NUM_THREADS; ++i)
{
t[i] = thread(compute_mandelbrot);
}
cout << " [INFO] Threads created: " << ::NUM_THREADS << endl;
for (int i = 0; i < ::NUM_THREADS; ++i)
{
t[i].join();
}
*/
cout << "Calculating..." << endl;
// Start timing
the_clock::time_point start = the_clock::now();
// This shows the whole set.
for (int y = 0; y < HEIGHT; y += HEIGHT / thread)
{
compute_mandelbrot(-2.0, 1.0, 1.125, -1.125, y, y + HEIGHT / thread);
// This zooms in on an interesting bit of detail.
//compute_mandelbrot(-0.751085, -0.734975, 0.118378, 0.134488, y, y + HEIGHT / thread);
}
// Stop timing
the_clock::time_point end = the_clock::now();
// Compute the difference between the two times in milliseconds
auto time_taken = duration_cast<milliseconds>(end - start).count();
cout << "Computing the Mandelbrot set took " << time_taken << " ms." << endl;
write_tga("output.tga");
return 0;
}
|