Anyways enough talk, first up we have a C++11 concurrency project that I've been working on over the past few days.
As a part of an exercise in understanding futures in the C++11 standard I parallelised the Mandelbrot set. The Mandelbrot set is famous for being used in the generation of the Mandelbrot fractal which can be seen in the image below.Mandelbrot Set
Figure 1 - Mandelbrot Fractal |
int main() { auto num_threads = thread::hardware_concurrency(); // Calculate the range of pixels each thread will work on auto strip_height = dimension / num_threads; // Create futures for each thread vector<future<vector<float>>> futures; // We give each thread the calculation and the data range // with which we want it to work on. We also make it // asynchronous so that we get a std::future reference back // so that we may retrieve the value we want later for(int i = 0; i < num_threads; ++i) futures.push_back(async(mandelbrotcalculation, i * strip_height, (i + 1) * strip_height)); // The results of the calculation vector<vector<float>> results; // Get the results from the future! // Bear in mind that f.get() can only be called once for (auto& f : futures) results.push_back(f.get()); return 0; }
The code above shows the main for the application generating the fractals and how we would operate the futures for this mandelbrot generation. Bear in mind instead of creating std::thread objects we are using std::futures which allow us to get the returning value from the thread later. "Mandelbrotcalculation" is simply out function for calculating the colour for a single pixel, the maths of which will be covered in a later post. A more indepth discussion on how it works can be read here on Wolfram Mathworld.
The resulting application still takes some time and I still need to measure timings to see how much faster it is than just calculating everything on a single thread would be but results in the following image.
Figure 2 - Part of the Mandelbrot Rendered |
Email : markmmiller@hotmail.co.uk
Xbox Live : Dr Death MK 2
Steam : 7thsanctum
Follow @7thsanctum