Skip to content

Commit 18754af

Browse files
authored
First upload
0 parents  commit 18754af

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+17943
-0
lines changed

LICENCE

Lines changed: 637 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Mandelbrot generation experiments with MPI and OpenMP
2+
3+
This project include:
4+
- C++ programs for the generation of the Mandelbrot set using MPI and OpenMP, as well as a sequential version.
5+
- A test running Python program to make performance testing to the C++ programs.
6+
7+
Author: Ernesto Soto Gómez.
8+
<esto.yinyang@gmail.com>, <esoto@uci.cu>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "compute_mandelbrot_subset.h"
2+
#include <complex>
3+
4+
using namespace std;
5+
6+
const double x_begin = -2.0, x_end = 0.6;
7+
const double y_begin = -1.2, y_end = 1.2;
8+
const double x_step = (x_end-x_begin) / (x_resolution-1);
9+
const double y_step = (y_end-y_begin) / (y_resolution-1);
10+
11+
void compute_mandelbrot_subset(int* result, int iter_limit, int start, int end) {
12+
int i, j;
13+
complex<double> c, z;
14+
15+
#pragma omp parallel shared(result, iter_limit, start, end) private(i, j, c, z)
16+
#pragma omp for schedule(runtime)
17+
for (i = start; i < end; i++) {
18+
c = complex<double>(
19+
x_begin + (i % x_resolution) * x_step,
20+
y_begin + (i / x_resolution) * y_step
21+
);
22+
z = 0; j = 0;
23+
while (norm(z) <= 4 && j < iter_limit) {
24+
z = z*z + c;
25+
j++;
26+
}
27+
result[i-start] = j;
28+
}
29+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
3+
const int x_resolution = 1024;
4+
const int y_resolution = 1024;
5+
const int result_size = x_resolution * y_resolution;
6+
7+
void compute_mandelbrot_subset(int* result, int iter_limit,
8+
int start = 0, int end = result_size);

code/common/misc.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "misc.h"
2+
#include <iostream>
3+
4+
using namespace std;
5+
6+
//process command arguments
7+
void get_iter_limit(int argc, char **argv, int &iter_limit, bool &print) {
8+
try {
9+
switch (argc) {
10+
case 2:
11+
iter_limit = stoi(argv[--argc]);
12+
print = false;
13+
return;
14+
case 3:
15+
iter_limit = stoi(argv[--argc]);
16+
print = stoi(argv[--argc]);
17+
return;
18+
case 1:
19+
cout << endl;
20+
cout << "Pass at least one argument" << endl;
21+
}
22+
}
23+
catch (exception) {
24+
cout << endl;
25+
cout << "Wrong arguments" << endl;
26+
}
27+
cout << "Parameters syntax after program name: [<print>] <iter_limit>" << endl;
28+
cout << "<print>: Whether to print the set to a file or not. This must be somethig that can be converted to boolean" << endl;
29+
cout << "<iter_limit>: The iteration limit. This must be a positive integer" << endl;
30+
cout << endl;
31+
exit(1);
32+
}
33+
34+
void output_execution_time(double time) {
35+
cout << time << endl;
36+
}

code/common/misc.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
3+
4+
void get_iter_limit(int argc, char **argv, int &iter_limit, bool &print);
5+
6+
void output_execution_time(double time);

code/common/now.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "now.h"
2+
#include <ctime>
3+
#include <cstdlib>
4+
5+
double now() {
6+
struct timespec tp;
7+
if (clock_gettime(CLOCK_MONOTONIC_RAW, &tp) != 0)
8+
exit(1);
9+
return tp.tv_sec + tp.tv_nsec / (double)1000000000;
10+
}

code/common/now.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#pragma once
2+
3+
4+
double now();

code/common/print_result.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "print_result.h"
2+
#include "compute_mandelbrot_subset.h"
3+
#include <fstream>
4+
#include <iostream>
5+
6+
using namespace std;
7+
8+
const unsigned short max_color = 65535;
9+
const string mandelbrot_file_name ="../../../images/mandelbrot";
10+
11+
// Used to store mandelbrot set into an image file.
12+
void print_result(int* result, int iter_limit, const char* name) {
13+
ofstream file;
14+
file.open(mandelbrot_file_name);
15+
file << "P3" << endl;
16+
file << x_resolution << " " << y_resolution << endl;
17+
file << max_color << endl;
18+
for (int i = 1; i <= y_resolution; i++) {
19+
for (int j = 0; j < x_resolution; j++) {
20+
int k = result[(i - 1) * x_resolution + j];
21+
file << " ";
22+
if (k == iter_limit)
23+
file << 0 << " " << 0 << " " << 0;
24+
else {
25+
unsigned short x = k * max_color / iter_limit;
26+
file << x / 4 << " ";
27+
file << x / 2 + max_color / 2 << " ";
28+
file << x;
29+
}
30+
file << " ";
31+
}
32+
file << endl;
33+
}
34+
file.close();
35+
36+
string command = "pnmtojpeg -quality=100 -smooth=100 -optimize ./"+mandelbrot_file_name+" > "+mandelbrot_file_name+"_limit="+to_string(iter_limit)+"_"+name+".jpeg";
37+
38+
if (system(command.c_str()) != 0)
39+
exit(1);
40+
41+
system(("rm "+mandelbrot_file_name).c_str());
42+
}

code/common/print_result.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
void print_result(int* result, int iter_limit, const char* name);

0 commit comments

Comments
 (0)