Skip to content

Commit

Permalink
Add single and double precision for everything.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sohn123 committed Dec 6, 2024
1 parent 5c55d81 commit c6211ec
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/baseline.cpp → src/baseline_dp.cpp
Original file line number Diff line number Diff line change
@@ -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];

Expand All @@ -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;
}
}
Expand Down
51 changes: 51 additions & 0 deletions src/baseline_sp.cpp
Original file line number Diff line number Diff line change
@@ -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<float> c(col * STEP + MIN_X, row * STEP + MIN_Y);

// z = z^2 + c
complex<float> 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<chrono::milliseconds>(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
Original file line number Diff line number Diff line change
@@ -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];
Expand All @@ -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;
}
}
Expand Down
53 changes: 53 additions & 0 deletions src/dynamic_multithreading_sp.cpp
Original file line number Diff line number Diff line change
@@ -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<float> c(col * STEP + MIN_X, row * STEP + MIN_Y);

// z = z^2 + c
complex<float> 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<chrono::milliseconds>(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
4 changes: 2 additions & 2 deletions src/no_complex.cpp → src/no_complex_dp.cpp
Original file line number Diff line number Diff line change
@@ -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];
Expand Down Expand Up @@ -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;
}
}
Expand Down
61 changes: 61 additions & 0 deletions src/no_complex_sp.cpp
Original file line number Diff line number Diff line change
@@ -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<chrono::milliseconds>(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
Original file line number Diff line number Diff line change
@@ -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];

Expand All @@ -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;
}
}
Expand Down
52 changes: 52 additions & 0 deletions src/static_multithreading_sp.cpp
Original file line number Diff line number Diff line change
@@ -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<float> c(col * STEP + MIN_X, row * STEP + MIN_Y);

// z = z^2 + c
complex<float> 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<chrono::milliseconds>(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

0 comments on commit c6211ec

Please sign in to comment.