Elements  6.2
A C++ base framework for the Euclid Software.
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 
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 OpenMP : public Program {
41 
42 public:
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 
87 private:
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 
Elements::Examples::OpenMP::mandelbrotCalculate
static size_t mandelbrotCalculate(const complex c, const size_t maxiter)
Definition: OpenMP.cpp:88
Elements::ExitCode::OK
@ OK
Everything is OK.
std::string
STL class.
Elements::ExitCode
ExitCode
Strongly typed exit numbers.
Definition: Exit.h:97
Elements::Examples::Program
Simple example of an Elements program.
Definition: Program.cpp:79
Elements::Examples::OpenMP::mainMethod
ExitCode mainMethod(map< string, VariableValue > &) override
This is the "main" method of all Elements programs.
Definition: OpenMP.cpp:43
Elements::Examples::OpenMP
Definition: OpenMP.cpp:40
std::complex::real
T real(T... args)
std::map
STL class.
ProgramHeaders.h
Elements::Examples::log
auto log
Definition: BackTrace.cpp:36
Elements::Logging::getLogger
static Logging getLogger(const std::string &name="")
Definition: Logging.cpp:63
MAIN_FOR
#define MAIN_FOR(ELEMENTS_PROGRAM_NAME)
Definition: Main.h:113
std::complex
STL class.
std::complex::imag
T imag(T... args)
std::size_t
Elements::Examples::CHARSET
static constexpr char CHARSET[]
Definition: OpenMP.cpp:38
complex
std::complex< double > complex
Definition: OpenMP.cpp:33
std::putchar
T putchar(T... args)
std::puts
T puts(T... args)
Elements
Definition: callBackExample.h:35