Elements 6.1.2
A C++ base framework for the Euclid Software.
Loading...
Searching...
No Matches
OpenMP.cpp
Go to the documentation of this file.
1
21#include <complex> // for complex
22#include <cstdio> // for size_t
23#include <map> // for map
24#include <string> // for string
25
26#include <boost/current_function.hpp> // for BOOST_CURRENT_FUNCTION
27
28#include "ElementsKernel/ProgramHeaders.h" // for including all Program/related headers
29
30using std::map;
31using std::size_t;
32using std::string;
34
35namespace Elements {
36namespace Examples {
37
38static constexpr char CHARSET[] = ".,c8M@jawrpogOQEPGJ";
39
40class OpenMP : public Program {
41
42public:
44
45 auto log = Logging::getLogger("OpenMP");
46
47 const int width = 78;
48 const int height = 44;
49 const int num_pixels = width * height;
50
51 const complex center(-.7, 0);
52 const complex span(2.7, -(4 / 3.0) * 2.7 * height / width);
53 const complex begin = center - span / 2.0; //, end = center+span/2.0;
54 const int maxiter = 100000;
55
56#pragma omp parallel for ordered schedule(dynamic)
57 for (int pix = 0; pix < num_pixels; ++pix) {
58
59 const int x = pix % width;
60 const int y = pix / width;
61
62 complex c = begin + complex(x * span.real() / (width + 1.0), y * span.imag() / (height + 1.0));
63
64 size_t n = mandelbrotCalculate(c, maxiter);
65 if (n == maxiter) {
66 n = 0;
67 }
68
69#pragma omp ordered
70 {
71 char c2 = ' ';
72 if (n > 0) {
73 c2 = CHARSET[n % (sizeof(CHARSET) - 1)];
74 }
75 std::putchar(c2);
76 if (x + 1 == width) {
77 std::puts("|");
78 }
79 }
80 }
81
82 log.info() << "done with test program! ";
83
84 return ExitCode::OK;
85 }
86
87private:
88 static size_t mandelbrotCalculate(const complex c, const size_t maxiter) {
89 // iterates z = z + c until |z| >= 2 or maxiter is reached,
90 // returns the number of iterations.
91 complex z = c;
92 size_t n = 0;
93 for (; n < maxiter; ++n) {
94 if (std::abs(z) >= 2.0) {
95 break;
96 }
97 z = z * z + c;
98 }
99 return n;
100 }
101};
102
103} // namespace Examples
104} // namespace Elements
105
std::complex< double > complex
Definition: OpenMP.cpp:33
static size_t mandelbrotCalculate(const complex c, const size_t maxiter)
Definition: OpenMP.cpp:88
ExitCode mainMethod(map< string, VariableValue > &) override
This is the "main" method of all Elements programs.
Definition: OpenMP.cpp:43
Simple example of an Elements program.
Definition: Program.cpp:79
static Logging getLogger(const std::string &name="")
Definition: Logging.cpp:63
ExitCode
Strongly typed exit numbers.
Definition: Exit.h:97
#define MAIN_FOR(ELEMENTS_PROGRAM_NAME)
Definition: Main.h:113
@ OK
Everything is OK.
T imag(T... args)
static constexpr char CHARSET[]
Definition: OpenMP.cpp:38
T putchar(T... args)
T puts(T... args)
T real(T... args)