Skip to content

Commit c6211ec

Browse files
committed
Add single and double precision for everything.
1 parent 5c55d81 commit c6211ec

8 files changed

+225
-8
lines changed

src/baseline.cpp renamed to src/baseline_dp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "mandelbrot.hpp"
22

3-
#ifdef BASELINE
3+
#ifdef BASELINE_DP
44
int64_t mandelbrot_computation(ofstream &matrix_out) {
55
uint16_t *const image = new uint16_t[HEIGHT * WIDTH];
66

@@ -18,10 +18,10 @@ int64_t mandelbrot_computation(ofstream &matrix_out) {
1818
{
1919
z = pow(z, 2) + c;
2020

21+
image[pos] = i;
2122
// If it is convergent
2223
if (abs(z) >= 2)
2324
{
24-
image[pos] = i;
2525
break;
2626
}
2727
}

src/baseline_sp.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include "mandelbrot.hpp"
2+
3+
#ifdef BASELINE_SP
4+
int64_t mandelbrot_computation(ofstream &matrix_out) {
5+
uint16_t *const image = new uint16_t[HEIGHT * WIDTH];
6+
7+
const auto start = chrono::steady_clock::now();
8+
for (int pos = 0; pos < HEIGHT * WIDTH; ++pos)
9+
{
10+
11+
const int row = pos / WIDTH;
12+
const int col = pos % WIDTH;
13+
const complex<float> c(col * STEP + MIN_X, row * STEP + MIN_Y);
14+
15+
// z = z^2 + c
16+
complex<float> z(0, 0);
17+
for (int i = 1; i <= ITERATIONS; ++i)
18+
{
19+
z = pow(z, 2) + c;
20+
21+
image[pos] = i;
22+
// If it is convergent
23+
if (abs(z) >= 2)
24+
{
25+
break;
26+
}
27+
}
28+
}
29+
const auto end = chrono::steady_clock::now();
30+
auto difference =chrono::duration_cast<chrono::milliseconds>(end - start).count();
31+
cerr << "Time elapsed: "
32+
<< difference
33+
<< " milliseconds." << endl;
34+
for (int row = 0; row < HEIGHT; row++)
35+
{
36+
for (int col = 0; col < WIDTH; col++)
37+
{
38+
matrix_out << image[row * WIDTH + col];
39+
40+
if (col < WIDTH - 1)
41+
matrix_out << ',';
42+
}
43+
if (row < HEIGHT - 1)
44+
matrix_out << endl;
45+
}
46+
matrix_out.close();
47+
48+
delete[] image; // It's here for coding style, but useless
49+
return difference;
50+
}
51+
#endif

src/dynamic_multithreading.cpp renamed to src/dynamic_multithreading_dp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "mandelbrot.hpp"
22

3-
#ifdef DYNAMIC_MULTITHREADING
3+
#ifdef DYNAMIC_MULTITHREADING_DP
44
int64_t mandelbrot_computation(ofstream &matrix_out)
55
{
66
uint16_t *const image = new uint16_t[HEIGHT * WIDTH];
@@ -21,9 +21,9 @@ int64_t mandelbrot_computation(ofstream &matrix_out)
2121
z = pow(z, 2) + c;
2222

2323
// If it is convergent
24+
image[pos] = i;
2425
if (abs(z) >= 2)
2526
{
26-
image[pos] = i;
2727
break;
2828
}
2929
}

src/dynamic_multithreading_sp.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include "mandelbrot.hpp"
2+
3+
#ifdef DYNAMIC_MULTITHREADING_SP
4+
int64_t mandelbrot_computation(ofstream &matrix_out)
5+
{
6+
uint16_t *const image = new uint16_t[HEIGHT * WIDTH];
7+
8+
const auto start = chrono::steady_clock::now();
9+
#pragma omp parallel for schedule(dynamic, 1)
10+
for (int pos = 0; pos < HEIGHT * WIDTH; ++pos)
11+
{
12+
13+
const int row = pos / WIDTH;
14+
const int col = pos % WIDTH;
15+
const complex<float> c(col * STEP + MIN_X, row * STEP + MIN_Y);
16+
17+
// z = z^2 + c
18+
complex<float> z(0, 0);
19+
for (int i = 1; i <= ITERATIONS; ++i)
20+
{
21+
z = pow(z, 2) + c;
22+
23+
// If it is convergent
24+
image[pos] = i;
25+
if (abs(z) >= 2)
26+
{
27+
break;
28+
}
29+
}
30+
}
31+
const auto end = chrono::steady_clock::now();
32+
auto difference = chrono::duration_cast<chrono::milliseconds>(end - start).count();
33+
cerr << "Time elapsed: "
34+
<< difference
35+
<< " milliseconds." << endl;
36+
for (int row = 0; row < HEIGHT; row++)
37+
{
38+
for (int col = 0; col < WIDTH; col++)
39+
{
40+
matrix_out << image[row * WIDTH + col];
41+
42+
if (col < WIDTH - 1)
43+
matrix_out << ',';
44+
}
45+
if (row < HEIGHT - 1)
46+
matrix_out << endl;
47+
}
48+
matrix_out.close();
49+
50+
delete[] image; // It's here for coding style, but useless
51+
return difference;
52+
}
53+
#endif

src/no_complex.cpp renamed to src/no_complex_dp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "mandelbrot.hpp"
22

3-
#ifdef NO_COMPLEX
3+
#ifdef NO_COMPLEX_DP
44
int64_t mandelbrot_computation(ofstream &matrix_out)
55
{
66
uint16_t *const image = new uint16_t[HEIGHT * WIDTH];
@@ -30,8 +30,8 @@ int64_t mandelbrot_computation(ofstream &matrix_out)
3030
zr2 = zr*zr;
3131
zi2 = zi*zi;
3232
double mag2 = zr2 + zi2;
33+
image[pos] = k;
3334
if(mag2 >= 4.0) {
34-
image[pos] = k;
3535
break;
3636
}
3737
}

src/no_complex_sp.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include "mandelbrot.hpp"
2+
3+
#ifdef NO_COMPLEX_SP
4+
int64_t mandelbrot_computation(ofstream &matrix_out)
5+
{
6+
uint16_t *const image = new uint16_t[HEIGHT * WIDTH];
7+
8+
const auto start = chrono::steady_clock::now();
9+
#pragma omp parallel for schedule(dynamic, 1)
10+
for (int pos = 0; pos < HEIGHT * WIDTH; ++pos)
11+
{
12+
13+
const int row = pos / WIDTH;
14+
const int col = pos % WIDTH;
15+
const float cr = col * STEP + MIN_X;
16+
const float ci = row * STEP + MIN_Y;
17+
18+
19+
// z = z^2 + c
20+
float zr = 0;
21+
float zi = 0;
22+
23+
for (uint16_t k = 1; k <= ITERATIONS; ++k) {
24+
float zr2 = zr*zr;
25+
float zi2 = zi*zi;
26+
float zrzi = zr * zi;
27+
28+
zr = (zr2 - zi2) + cr;
29+
zi = (zrzi + zrzi) + ci;
30+
zr2 = zr*zr;
31+
zi2 = zi*zi;
32+
float mag2 = zr2 + zi2;
33+
image[pos] = k;
34+
if(mag2 >= 4.0) {
35+
break;
36+
}
37+
}
38+
}
39+
const auto end = chrono::steady_clock::now();
40+
auto difference = chrono::duration_cast<chrono::milliseconds>(end - start).count();
41+
cerr << "Time elapsed: "
42+
<< difference
43+
<< " milliseconds." << endl;
44+
for (int row = 0; row < HEIGHT; row++)
45+
{
46+
for (int col = 0; col < WIDTH; col++)
47+
{
48+
matrix_out << image[row * WIDTH + col];
49+
50+
if (col < WIDTH - 1)
51+
matrix_out << ',';
52+
}
53+
if (row < HEIGHT - 1)
54+
matrix_out << endl;
55+
}
56+
matrix_out.close();
57+
58+
delete[] image; // It's here for coding style, but useless
59+
return difference;
60+
}
61+
#endif

src/static_multithreading.cpp renamed to src/static_multithreading_dp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "mandelbrot.hpp"
22

3-
#ifdef STATIC_MULTITHREADING
3+
#ifdef STATIC_MULTITHREADING_DP
44
int64_t mandelbrot_computation(ofstream &matrix_out) {
55
uint16_t *const image = new uint16_t[HEIGHT * WIDTH];
66

@@ -19,10 +19,10 @@ int64_t mandelbrot_computation(ofstream &matrix_out) {
1919
{
2020
z = pow(z, 2) + c;
2121

22+
image[pos] = i;
2223
// If it is convergent
2324
if (abs(z) >= 2)
2425
{
25-
image[pos] = i;
2626
break;
2727
}
2828
}

src/static_multithreading_sp.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "mandelbrot.hpp"
2+
3+
#ifdef STATIC_MULTITHREADING_SP
4+
int64_t mandelbrot_computation(ofstream &matrix_out) {
5+
uint16_t *const image = new uint16_t[HEIGHT * WIDTH];
6+
7+
const auto start = chrono::steady_clock::now();
8+
#pragma omp parallel for
9+
for (int pos = 0; pos < HEIGHT * WIDTH; ++pos)
10+
{
11+
12+
const int row = pos / WIDTH;
13+
const int col = pos % WIDTH;
14+
const complex<float> c(col * STEP + MIN_X, row * STEP + MIN_Y);
15+
16+
// z = z^2 + c
17+
complex<float> z(0, 0);
18+
for (int i = 1; i <= ITERATIONS; ++i)
19+
{
20+
z = pow(z, 2) + c;
21+
22+
image[pos] = i;
23+
// If it is convergent
24+
if (abs(z) >= 2)
25+
{
26+
break;
27+
}
28+
}
29+
}
30+
const auto end = chrono::steady_clock::now();
31+
auto difference =chrono::duration_cast<chrono::milliseconds>(end - start).count();
32+
cerr << "Time elapsed: "
33+
<< difference
34+
<< " milliseconds." << endl;
35+
for (int row = 0; row < HEIGHT; row++)
36+
{
37+
for (int col = 0; col < WIDTH; col++)
38+
{
39+
matrix_out << image[row * WIDTH + col];
40+
41+
if (col < WIDTH - 1)
42+
matrix_out << ',';
43+
}
44+
if (row < HEIGHT - 1)
45+
matrix_out << endl;
46+
}
47+
matrix_out.close();
48+
49+
delete[] image; // It's here for coding style, but useless
50+
return difference;
51+
}
52+
#endif

0 commit comments

Comments
 (0)