From c6211ec16199cdc1361b55e9f2d20eb74aae65a0 Mon Sep 17 00:00:00 2001 From: Alexander Sohn Date: Fri, 6 Dec 2024 15:36:34 +0100 Subject: [PATCH] Add single and double precision for everything. --- src/{baseline.cpp => baseline_dp.cpp} | 4 +- src/baseline_sp.cpp | 51 ++++++++++++++++ ...ding.cpp => dynamic_multithreading_dp.cpp} | 4 +- src/dynamic_multithreading_sp.cpp | 53 ++++++++++++++++ src/{no_complex.cpp => no_complex_dp.cpp} | 4 +- src/no_complex_sp.cpp | 61 +++++++++++++++++++ ...ading.cpp => static_multithreading_dp.cpp} | 4 +- src/static_multithreading_sp.cpp | 52 ++++++++++++++++ 8 files changed, 225 insertions(+), 8 deletions(-) rename src/{baseline.cpp => baseline_dp.cpp} (96%) create mode 100644 src/baseline_sp.cpp rename src/{dynamic_multithreading.cpp => dynamic_multithreading_dp.cpp} (95%) create mode 100644 src/dynamic_multithreading_sp.cpp rename src/{no_complex.cpp => no_complex_dp.cpp} (96%) create mode 100644 src/no_complex_sp.cpp rename src/{static_multithreading.cpp => static_multithreading_dp.cpp} (95%) create mode 100644 src/static_multithreading_sp.cpp diff --git a/src/baseline.cpp b/src/baseline_dp.cpp similarity index 96% rename from src/baseline.cpp rename to src/baseline_dp.cpp index de46d0e..091677f 100644 --- a/src/baseline.cpp +++ b/src/baseline_dp.cpp @@ -1,6 +1,6 @@ #include "mandelbrot.hpp" -#ifdef BASELINE +#ifdef BASELINE_DP int64_t mandelbrot_computation(ofstream &matrix_out) { uint16_t *const image = new uint16_t[HEIGHT * WIDTH]; @@ -18,10 +18,10 @@ int64_t mandelbrot_computation(ofstream &matrix_out) { { z = pow(z, 2) + c; + image[pos] = i; // If it is convergent if (abs(z) >= 2) { - image[pos] = i; break; } } diff --git a/src/baseline_sp.cpp b/src/baseline_sp.cpp new file mode 100644 index 0000000..8ce930c --- /dev/null +++ b/src/baseline_sp.cpp @@ -0,0 +1,51 @@ +#include "mandelbrot.hpp" + +#ifdef BASELINE_SP +int64_t mandelbrot_computation(ofstream &matrix_out) { + uint16_t *const image = new uint16_t[HEIGHT * WIDTH]; + + const auto start = chrono::steady_clock::now(); + for (int pos = 0; pos < HEIGHT * WIDTH; ++pos) + { + + const int row = pos / WIDTH; + const int col = pos % WIDTH; + const complex c(col * STEP + MIN_X, row * STEP + MIN_Y); + + // z = z^2 + c + complex z(0, 0); + for (int i = 1; i <= ITERATIONS; ++i) + { + z = pow(z, 2) + c; + + image[pos] = i; + // If it is convergent + if (abs(z) >= 2) + { + break; + } + } + } + const auto end = chrono::steady_clock::now(); + auto difference =chrono::duration_cast(end - start).count(); + cerr << "Time elapsed: " + << difference + << " milliseconds." << endl; + for (int row = 0; row < HEIGHT; row++) + { + for (int col = 0; col < WIDTH; col++) + { + matrix_out << image[row * WIDTH + col]; + + if (col < WIDTH - 1) + matrix_out << ','; + } + if (row < HEIGHT - 1) + matrix_out << endl; + } + matrix_out.close(); + + delete[] image; // It's here for coding style, but useless + return difference; +} +#endif \ No newline at end of file diff --git a/src/dynamic_multithreading.cpp b/src/dynamic_multithreading_dp.cpp similarity index 95% rename from src/dynamic_multithreading.cpp rename to src/dynamic_multithreading_dp.cpp index f613da3..3d8ff15 100644 --- a/src/dynamic_multithreading.cpp +++ b/src/dynamic_multithreading_dp.cpp @@ -1,6 +1,6 @@ #include "mandelbrot.hpp" -#ifdef DYNAMIC_MULTITHREADING +#ifdef DYNAMIC_MULTITHREADING_DP int64_t mandelbrot_computation(ofstream &matrix_out) { uint16_t *const image = new uint16_t[HEIGHT * WIDTH]; @@ -21,9 +21,9 @@ int64_t mandelbrot_computation(ofstream &matrix_out) z = pow(z, 2) + c; // If it is convergent + image[pos] = i; if (abs(z) >= 2) { - image[pos] = i; break; } } diff --git a/src/dynamic_multithreading_sp.cpp b/src/dynamic_multithreading_sp.cpp new file mode 100644 index 0000000..fee219d --- /dev/null +++ b/src/dynamic_multithreading_sp.cpp @@ -0,0 +1,53 @@ +#include "mandelbrot.hpp" + +#ifdef DYNAMIC_MULTITHREADING_SP +int64_t mandelbrot_computation(ofstream &matrix_out) +{ + uint16_t *const image = new uint16_t[HEIGHT * WIDTH]; + + const auto start = chrono::steady_clock::now(); +#pragma omp parallel for schedule(dynamic, 1) + for (int pos = 0; pos < HEIGHT * WIDTH; ++pos) + { + + const int row = pos / WIDTH; + const int col = pos % WIDTH; + const complex c(col * STEP + MIN_X, row * STEP + MIN_Y); + + // z = z^2 + c + complex z(0, 0); + for (int i = 1; i <= ITERATIONS; ++i) + { + z = pow(z, 2) + c; + + // If it is convergent + image[pos] = i; + if (abs(z) >= 2) + { + break; + } + } + } + const auto end = chrono::steady_clock::now(); + auto difference = chrono::duration_cast(end - start).count(); + cerr << "Time elapsed: " + << difference + << " milliseconds." << endl; + for (int row = 0; row < HEIGHT; row++) + { + for (int col = 0; col < WIDTH; col++) + { + matrix_out << image[row * WIDTH + col]; + + if (col < WIDTH - 1) + matrix_out << ','; + } + if (row < HEIGHT - 1) + matrix_out << endl; + } + matrix_out.close(); + + delete[] image; // It's here for coding style, but useless + return difference; +} +#endif diff --git a/src/no_complex.cpp b/src/no_complex_dp.cpp similarity index 96% rename from src/no_complex.cpp rename to src/no_complex_dp.cpp index 2b20e7f..3e901f8 100644 --- a/src/no_complex.cpp +++ b/src/no_complex_dp.cpp @@ -1,6 +1,6 @@ #include "mandelbrot.hpp" -#ifdef NO_COMPLEX +#ifdef NO_COMPLEX_DP int64_t mandelbrot_computation(ofstream &matrix_out) { uint16_t *const image = new uint16_t[HEIGHT * WIDTH]; @@ -30,8 +30,8 @@ int64_t mandelbrot_computation(ofstream &matrix_out) zr2 = zr*zr; zi2 = zi*zi; double mag2 = zr2 + zi2; + image[pos] = k; if(mag2 >= 4.0) { - image[pos] = k; break; } } diff --git a/src/no_complex_sp.cpp b/src/no_complex_sp.cpp new file mode 100644 index 0000000..dec8de1 --- /dev/null +++ b/src/no_complex_sp.cpp @@ -0,0 +1,61 @@ +#include "mandelbrot.hpp" + +#ifdef NO_COMPLEX_SP +int64_t mandelbrot_computation(ofstream &matrix_out) +{ + uint16_t *const image = new uint16_t[HEIGHT * WIDTH]; + + const auto start = chrono::steady_clock::now(); + #pragma omp parallel for schedule(dynamic, 1) + for (int pos = 0; pos < HEIGHT * WIDTH; ++pos) + { + + const int row = pos / WIDTH; + const int col = pos % WIDTH; + const float cr = col * STEP + MIN_X; + const float ci = row * STEP + MIN_Y; + + + // z = z^2 + c + float zr = 0; + float zi = 0; + + for (uint16_t k = 1; k <= ITERATIONS; ++k) { + float zr2 = zr*zr; + float zi2 = zi*zi; + float zrzi = zr * zi; + + zr = (zr2 - zi2) + cr; + zi = (zrzi + zrzi) + ci; + zr2 = zr*zr; + zi2 = zi*zi; + float mag2 = zr2 + zi2; + image[pos] = k; + if(mag2 >= 4.0) { + break; + } + } + } + const auto end = chrono::steady_clock::now(); + auto difference = chrono::duration_cast(end - start).count(); + cerr << "Time elapsed: " + << difference + << " milliseconds." << endl; + for (int row = 0; row < HEIGHT; row++) + { + for (int col = 0; col < WIDTH; col++) + { + matrix_out << image[row * WIDTH + col]; + + if (col < WIDTH - 1) + matrix_out << ','; + } + if (row < HEIGHT - 1) + matrix_out << endl; + } + matrix_out.close(); + + delete[] image; // It's here for coding style, but useless + return difference; +} +#endif diff --git a/src/static_multithreading.cpp b/src/static_multithreading_dp.cpp similarity index 95% rename from src/static_multithreading.cpp rename to src/static_multithreading_dp.cpp index f563b35..26df4eb 100644 --- a/src/static_multithreading.cpp +++ b/src/static_multithreading_dp.cpp @@ -1,6 +1,6 @@ #include "mandelbrot.hpp" -#ifdef STATIC_MULTITHREADING +#ifdef STATIC_MULTITHREADING_DP int64_t mandelbrot_computation(ofstream &matrix_out) { uint16_t *const image = new uint16_t[HEIGHT * WIDTH]; @@ -19,10 +19,10 @@ int64_t mandelbrot_computation(ofstream &matrix_out) { { z = pow(z, 2) + c; + image[pos] = i; // If it is convergent if (abs(z) >= 2) { - image[pos] = i; break; } } diff --git a/src/static_multithreading_sp.cpp b/src/static_multithreading_sp.cpp new file mode 100644 index 0000000..fc8e99f --- /dev/null +++ b/src/static_multithreading_sp.cpp @@ -0,0 +1,52 @@ +#include "mandelbrot.hpp" + +#ifdef STATIC_MULTITHREADING_SP +int64_t mandelbrot_computation(ofstream &matrix_out) { + uint16_t *const image = new uint16_t[HEIGHT * WIDTH]; + + const auto start = chrono::steady_clock::now(); + #pragma omp parallel for + for (int pos = 0; pos < HEIGHT * WIDTH; ++pos) + { + + const int row = pos / WIDTH; + const int col = pos % WIDTH; + const complex c(col * STEP + MIN_X, row * STEP + MIN_Y); + + // z = z^2 + c + complex z(0, 0); + for (int i = 1; i <= ITERATIONS; ++i) + { + z = pow(z, 2) + c; + + image[pos] = i; + // If it is convergent + if (abs(z) >= 2) + { + break; + } + } + } + const auto end = chrono::steady_clock::now(); + auto difference =chrono::duration_cast(end - start).count(); + cerr << "Time elapsed: " + << difference + << " milliseconds." << endl; + for (int row = 0; row < HEIGHT; row++) + { + for (int col = 0; col < WIDTH; col++) + { + matrix_out << image[row * WIDTH + col]; + + if (col < WIDTH - 1) + matrix_out << ','; + } + if (row < HEIGHT - 1) + matrix_out << endl; + } + matrix_out.close(); + + delete[] image; // It's here for coding style, but useless + return difference; +} +#endif \ No newline at end of file