Elements  6.0.1
A C++ base framework for the Euclid Software.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
OpenMPExample.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 
30 using std::map;
31 using std::size_t;
32 using std::string;
34 
35 namespace Elements {
36 namespace Examples {
37 
38 static constexpr char CHARSET[] = ".,c8M@jawrpogOQEPGJ";
39 
40 class OpenMPExample : public Program {
41 
42 public:
44 
45  auto log = Logging::getLogger("ProgramExample");
46 
47  const int width = 78, height = 44, num_pixels = width * height;
48 
49  const complex center(-.7, 0), span(2.7, -(4 / 3.0) * 2.7 * height / width);
50  const complex begin = center - span / 2.0; //, end = center+span/2.0;
51  const int maxiter = 100000;
52 
53 #pragma omp parallel for ordered schedule(dynamic)
54  for (int pix = 0; pix < num_pixels; ++pix) {
55 
56  const int x = pix % width, y = pix / width;
57 
58  complex c = begin + complex(x * span.real() / (width + 1.0), y * span.imag() / (height + 1.0));
59 
60  size_t n = mandelbrotCalculate(c, maxiter);
61  if (n == maxiter) {
62  n = 0;
63  }
64 
65 #pragma omp ordered
66  {
67  char c2 = ' ';
68  if (n > 0) {
69  c2 = CHARSET[n % (sizeof(CHARSET) - 1)];
70  }
71  std::putchar(c2);
72  if (x + 1 == width) {
73  std::puts("|");
74  }
75  }
76  }
77 
78  log.info() << "done with test program! ";
79 
80  return ExitCode::OK;
81  }
82 
83 private:
84  static size_t mandelbrotCalculate(const complex c, const size_t maxiter) {
85  // iterates z = z + c until |z| >= 2 or maxiter is reached,
86  // returns the number of iterations.
87  complex z = c;
88  size_t n = 0;
89  for (; n < maxiter; ++n) {
90  if (std::abs(z) >= 2.0) {
91  break;
92  }
93  z = z * z + c;
94  }
95  return n;
96  }
97 };
98 
99 } // namespace Examples
100 } // namespace Elements
101 
ExitCode
Strongly typed exit numbers.
Definition: Exit.h:97
T real(T...args)
T putchar(T...args)
static constexpr char CHARSET[]
Everything is OK.
STL class.
Abstract class for all Elements programs.
Definition: Program.h:52
T imag(T...args)
STL class.
std::complex< double > complex
STL class.
#define MAIN_FOR(ELEMENTS_PROGRAM_NAME)
Definition: Main.h:113
T puts(T...args)
ExitCode mainMethod(map< string, VariableValue > &) override
This is the &quot;main&quot; method of all Elements programs.
static Logging getLogger(const std::string &name="")
Definition: Logging.cpp:63
static size_t mandelbrotCalculate(const complex c, const size_t maxiter)