NaiveIRGenerator.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #include "NaiveIRGenerator.h"
  2. #include <omp.h>
  3. using mnd::NaiveIRGenerator;
  4. template class mnd::NaiveIRGenerator<float>;
  5. template class mnd::NaiveIRGenerator<double>;
  6. template class mnd::NaiveIRGenerator<mnd::DoubleDouble>;
  7. template class mnd::NaiveIRGenerator<mnd::QuadDouble>;
  8. namespace mnd::eval
  9. {
  10. using namespace mnd;
  11. using namespace mnd::ir;
  12. template<typename T>
  13. struct ToEvalVisitor
  14. {
  15. EvalStruct<T>& es;
  16. std::unique_ptr<EvalNode> visit(ir::Node* f)
  17. {
  18. std::any& nodeData = getNodeData(f);
  19. if (EvalNode** en = std::any_cast<EvalNode*>(&nodeData)) {
  20. size_t tmpStore;
  21. if (Store* s = std::get_if<Store>(*en)) {
  22. tmpStore = s->index;
  23. }
  24. else {
  25. tmpStore = createTemp();
  26. EvalNode store = Store{ tmpStore, std::make_unique<EvalNode>(std::move(**en)) };
  27. **en = std::move(store);
  28. }
  29. auto l = std::make_unique<EvalNode>(Load{ tmpStore });
  30. setNodeData(f, l.get());
  31. return l;
  32. }
  33. EvalNode n = std::visit(*this, *f);
  34. auto r = std::make_unique<EvalNode>(std::move(n));
  35. setNodeData(f, r.get());
  36. return r;
  37. }
  38. std::any& getNodeData(ir::Node* n)
  39. {
  40. return std::visit([](auto& x) -> std::any& { return x.nodeData; }, *n);
  41. }
  42. void setNodeData(ir::Node* n, EvalNode* en)
  43. {
  44. std::visit([en](auto& x) { x.nodeData = en; }, *n);
  45. }
  46. size_t createTemp(void)
  47. {
  48. es.variables.push_back(0);
  49. return es.variables.size() - 1;
  50. }
  51. size_t createConstant(mnd::Real& value)
  52. {
  53. es.variables.push_back(mnd::convert<T>(value));
  54. return es.variables.size() - 1;
  55. }
  56. size_t createVariable(std::string& value)
  57. {
  58. es.variables.push_back(0);
  59. es.variableNames.emplace(value, es.variables.size() - 1);
  60. return es.variables.size() - 1;
  61. }
  62. EvalNode operator()(ir::Constant& x) {
  63. return Load{ createConstant(x.value) };
  64. }
  65. EvalNode operator()(ir::Variable& x) {
  66. return Load{ createVariable(x.name) };
  67. }
  68. EvalNode operator()(ir::Addition& x) {
  69. return Add{ visit(x.left), visit(x.right) };
  70. }
  71. EvalNode operator()(ir::Subtraction& x) {
  72. return Sub{ visit(x.left), visit(x.right) };
  73. }
  74. EvalNode operator()(ir::Multiplication& x) {
  75. return Mul{ visit(x.left), visit(x.right) };
  76. }
  77. EvalNode operator()(ir::Division& x) {
  78. return Div{ visit(x.left), visit(x.right) };
  79. }
  80. EvalNode operator()(ir::Negation& x) {
  81. return Neg{ visit(x.value) };
  82. }
  83. EvalNode operator()(ir::Atan2& x) {
  84. return Atan2{ visit(x.left), visit(x.right) };
  85. }
  86. EvalNode operator()(ir::Pow& x) {
  87. return Pow{ visit(x.left), visit(x.right) };
  88. }
  89. EvalNode operator()(ir::Cos& x) {
  90. return Cos{ visit(x.value) };
  91. }
  92. EvalNode operator()(ir::Sin& x) {
  93. return Sin{ visit(x.value) };
  94. }
  95. EvalNode operator()(ir::Exp& x) {
  96. return Exp{ visit(x.value) };
  97. }
  98. EvalNode operator()(ir::Ln& x) {
  99. return Ln{ visit(x.value) };
  100. }
  101. };
  102. template<typename T>
  103. struct EvalVisitor
  104. {
  105. mnd::eval::EvalStruct<T>& es;
  106. T visit(const EvalNode& en) {
  107. return std::visit(*this, en);
  108. }
  109. T operator()(const Load& x) {
  110. return es.variables[x.index];
  111. }
  112. T operator()(const Store& x) {
  113. T r = visit(*x.v);
  114. es.variables[x.index] = r;
  115. return r;
  116. }
  117. T operator()(const Add& x) {
  118. return visit(*x.a) + visit(*x.b);
  119. }
  120. T operator()(const Sub& x) {
  121. return visit(*x.a) - visit(*x.b);
  122. }
  123. T operator()(const Mul& x) {
  124. return visit(*x.a) * visit(*x.b);
  125. }
  126. T operator()(const Div& x) {
  127. return visit(*x.a) / visit(*x.b);
  128. }
  129. T operator()(const Neg& x) {
  130. return -visit(*x.a);
  131. }
  132. T operator()(const Atan2& x) {
  133. return mnd::atan2(visit(*x.a), visit(*x.b));
  134. }
  135. T operator()(const Pow& x) {
  136. return mnd::pow(visit(*x.a), visit(*x.b));
  137. }
  138. T operator()(const Cos& x) {
  139. return mnd::cos(visit(*x.a));
  140. }
  141. T operator()(const Sin& x) {
  142. return mnd::sin(visit(*x.a));
  143. }
  144. T operator()(const Exp& x) {
  145. return mnd::exp(visit(*x.a));
  146. }
  147. T operator()(const Ln& x) {
  148. return mnd::log(visit(*x.a));
  149. }
  150. };
  151. }
  152. template<typename T>
  153. NaiveIRGenerator<T>::NaiveIRGenerator(const mnd::ir::Formula& irf,
  154. mnd::Precision prec) :
  155. mnd::MandelGenerator{ prec },
  156. form{ irf }
  157. {
  158. eval::ToEvalVisitor<T> tev{ es };
  159. newz_re = tev.visit(irf.newA);
  160. newz_im = tev.visit(irf.newB);
  161. start_re = tev.visit(irf.startA);
  162. start_im = tev.visit(irf.startB);
  163. }
  164. template<typename T>
  165. void NaiveIRGenerator<T>::generate(const mnd::MandelInfo& info, float* data)
  166. {
  167. const MandelViewport& view = info.view;
  168. const bool parallel = true;
  169. T viewx = mnd::convert<T>(view.x);
  170. T viewy = mnd::convert<T>(view.y);
  171. T wpp = mnd::convert<T>(view.width / info.bWidth);
  172. T hpp = mnd::convert<T>(view.height / info.bHeight);
  173. #if defined(_OPENMP)
  174. if constexpr (parallel)
  175. omp_set_num_threads(omp_get_num_procs());
  176. # pragma omp parallel for schedule(static, 1) if (parallel)
  177. #endif
  178. for (long j = 0; j < info.bHeight; j++) {
  179. T y = viewy + T(double(j)) * hpp;
  180. for (long i = 0; i < info.bWidth; i++) {
  181. T x = viewx + T(double(i)) * wpp;
  182. es.prepare(0, 0, x, y);
  183. eval::EvalVisitor<T> visitor{ es };
  184. T a = visitor.visit(*start_re);
  185. T b = visitor.visit(*start_im);
  186. es.prepare(a, b, x, y);
  187. int k = 0;
  188. for (k = 0; k < info.maxIter; k++) {
  189. T newA = visitor.visit(*newz_re);
  190. T newB = visitor.visit(*newz_im);
  191. a = newA;
  192. b = newB;
  193. if (a * a + b * b >= 16.0)
  194. break;
  195. }
  196. data[i + j * info.bWidth] = float(k);
  197. }
  198. }
  199. }