Commit fa94b49b authored by Andreas Kempe's avatar Andreas Kempe

The programme now creates one image instead of several parts.

parent 037b9943
......@@ -11,6 +11,6 @@ link_directories(/home/kempe/lib/lib)
set(CMAKE_C_COMPILER mpicc)
set(CMAKE_CXX_COMPILER mpicxx)
set(CMAKE_CXX_FLAGS "-DMAGICKCORE_HDRI_ENABLE=0 -DMAGICKCORE_QUANTUM_DEPTH=16")
set(CMAKE_CXX_FLAGS "-DMAGICKCORE_HDRI_ENABLE=0 -DMAGICKCORE_QUANTUM_DEPTH=16 -O3 -std=c++11")
target_link_libraries(mpi-mandel mpi Magick++-6.Q16)
......@@ -4,28 +4,26 @@
#include <complex>
#include <cmath>
#include <sstream>
#include <iomanip>
#define IMG_WIDTH 800
#define IMG_HEIGHT 600
#define IMG_SIZE "800x600"
#define ITERATIONS 100
#define IMG_WIDTH 10640
#define IMG_HEIGHT 8000
#define ITERATIONS 2000
#define RE_MAX 1
#define RE_MIN -2
#define IM_MAX 2.2
#define IM_MIN -1.7
#define IM_MAX 1.4
#define IM_MIN -1.3
using namespace std;
using namespace Magick;
int effective_height = IMG_HEIGHT;
struct MandelResult
{
unsigned int end_iterations;
unsigned long end_iterations;
bool belongs;
MandelResult(unsigned int end_iterations, bool belongs) :
MandelResult(unsigned long end_iterations, bool belongs) :
end_iterations(end_iterations), belongs(belongs) {}
};
......@@ -42,15 +40,15 @@ complex<double> GetPixelCoordinate(int x, int y)
}
MandelResult IsMandelbrotPoint(int x, int y, unsigned int iterations)
MandelResult IsMandelbrotPoint(unsigned long x, unsigned long y, unsigned int iterations)
{
complex<double> point = GetPixelCoordinate(x, y);
complex<double> series_value(0, 0);
for (unsigned int i = 0; i < iterations; ++i)
for (unsigned long i = 0; i < iterations; ++i)
{
if (real(series_value) * real(series_value) + imag(series_value) * imag(series_value) >= 4)
return MandelResult(i, false);
return MandelResult(abs(series_value), false);
series_value= pow(series_value, 2) + point;
}
......@@ -62,36 +60,84 @@ int main(int argc, char** argv)
{
int id;
int procs;
unsigned long *calculated_values;
unsigned long *receive_buffer = nullptr;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &procs);
MPI_Comm_rank(MPI_COMM_WORLD, &id);
int effective_height = IMG_HEIGHT;
// Calculate this threads working area.
effective_height /= procs;
Image my_image(Geometry(IMG_WIDTH, effective_height), "white");
calculated_values = new unsigned long[IMG_WIDTH * effective_height];
for (int x = 0; x < IMG_WIDTH; ++x)
for (unsigned long y = effective_height * id; y < effective_height * (id + 1); ++y)
{
for (int y = effective_height * id; y < effective_height * (id + 1); ++y)
for (unsigned long x = 0; x < IMG_WIDTH; ++x)
{
MandelResult result = IsMandelbrotPoint(x, y, ITERATIONS);
if (result.belongs)
my_image.pixelColor(x, y - effective_height * id, Color("purple"));
else
my_image.pixelColor(x, y - effective_height * id, Color(0, 0, MaxRGB / ITERATIONS * result.end_iterations));
calculated_values[(y - effective_height * id) * IMG_WIDTH + x] = result.end_iterations;
}
}
if (id == 0)
receive_buffer = new unsigned long[IMG_WIDTH * IMG_HEIGHT];
cout << "Process: " << id << ", Gathering!" << endl;
MPI_Gather(calculated_values, IMG_WIDTH * effective_height, MPI_UNSIGNED_LONG, receive_buffer,
IMG_WIDTH * effective_height, MPI_UNSIGNED_LONG, 0, MPI_COMM_WORLD);
cout << "Process: " << id << ", done gathering!" << endl;
if (id == 0)
{
Image my_image(Geometry(IMG_WIDTH, IMG_WIDTH), "white");
my_image.modifyImage();
stringstream ss;
ss << id << ".png";
Pixels pixel_cache(my_image);
PixelPacket *pixels = pixel_cache.get(0, 0, IMG_WIDTH, IMG_HEIGHT);
my_image.write(ss.str());
float max = 0;
for (unsigned long i = 0; i < IMG_WIDTH * IMG_HEIGHT; ++i)
{
if (receive_buffer[i] > max && receive_buffer[i] != ITERATIONS)
max = receive_buffer[i];
}
cout << max << endl;
cout << "Assembling image!" << endl;
for (unsigned long y = 0; y < IMG_HEIGHT; ++y)
{
cout << "Progress: " << setprecision(3) <<
static_cast<float>(y * 100) / IMG_HEIGHT << " % \r" << flush;
for (unsigned long x = 0; x < IMG_WIDTH; ++x)
{
int iter_res = receive_buffer[y * IMG_WIDTH + x];
if (iter_res == ITERATIONS)
pixels[y * IMG_WIDTH + x] = Color("purple");
else
pixels[y * IMG_WIDTH + x] = Color(0, 0, MaxRGB * static_cast<float>(iter_res) / max);
}
}
pixel_cache.sync();
cout << "Progress: 100 % " << endl;
stringstream ss;
ss << "output.png";
cout << "Saving image!" << endl;
my_image.write(ss.str());
}
delete[] calculated_values;
delete[] receive_buffer;
MPI_Finalize();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment