forked from degauden/Elements
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOpenMP.cpp
More file actions
107 lines (87 loc) · 3.06 KB
/
Copy pathOpenMP.cpp
File metadata and controls
107 lines (87 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* @file OpenMP.cpp
* @date March 17th, 2020
* @author Hubert Degaudenzi
*
* @copyright 2012-2020 Euclid Science Ground Segment
*
* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General
* Public License as published by the Free Software Foundation; either version 3.0 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <complex> // for operator+, abs, complex, operator*, operator-, operator/
#include <cstdio> // for putchar, puts, size_t
#include <map> // for map
#include <string> // for allocator, string
#include "ElementsKernel/Main.h" // for MAIN_FOR
#include "ElementsKernel/Program.h" // for Program
using std::map;
using std::size_t;
using std::string;
using complex = std::complex<double>;
namespace Elements::Examples {
static constexpr char CHARSET[] = ".,c8M@jawrpogOQEPGJ";
class OpenMP final : public Program {
public:
ExitCode mainMethod(map<string, VariableValue>& /*args*/) override {
const auto log = Logging::getLogger("OpenMP");
constexpr int width = 78;
constexpr int height = 44;
constexpr int num_pixels = width * height;
const complex center(-.7, 0);
const complex span(2.7, -(4 / 3.0) * 2.7 * height / width);
const complex begin = center - span / 2.0; //, end = center+span/2.0;
#pragma omp parallel for ordered schedule(dynamic)
for (int pix = 0; pix < num_pixels; ++pix) {
constexpr int maxiter = 100000;
const int x = pix % width;
const int y = pix / width;
const complex c = begin + complex(x * span.real() / (width + 1.0), y * span.imag() / (height + 1.0));
size_t n = mandelbrotCalculate(c, maxiter);
if (n == maxiter) {
n = 0;
}
#pragma omp ordered
{
char c2 = ' ';
if (n > 0) {
c2 = CHARSET[n % (sizeof(CHARSET) - 1)];
}
std::putchar(c2);
if (x + 1 == width) {
std::puts("|");
}
}
}
log.info() << "done with test program! ";
return ExitCode::OK;
}
private:
static size_t mandelbrotCalculate(const complex c, const size_t max_iterations) {
// iterates z = z + c until |z| >= 2 or max_iterations is reached,
// returns the number of iterations.
complex z = c;
size_t n = 0;
for (; n < max_iterations; ++n) {
if (std::abs(z) >= 2.0) {
break;
}
z = z * z + c;
}
return n;
}
};
} // namespace Elements::Examples
/**
* Implementation of a main using a base class macro
* This must be present in all Elements programs
*/
MAIN_FOR(Elements::Examples::OpenMP)