IterationGenerator.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. #include "IterationGenerator.h"
  2. #include "ExecData.h"
  3. #include "Mandel.h"
  4. #include "OpenClInternal.h"
  5. #include <omp.h>
  6. using mnd::IterationGenerator;
  7. using mnd::NaiveGenerator;
  8. using mnd::NaiveIRGenerator;
  9. using mnd::IterationFormula;
  10. IterationGenerator::IterationGenerator(IterationFormula z0, IterationFormula zi,
  11. const mnd::Real& prec) :
  12. mnd::MandelGenerator{ prec },
  13. z0{ std::move(z0) },
  14. zi{ std::move(zi) }
  15. {
  16. }
  17. NaiveGenerator::NaiveGenerator(IterationFormula z0, IterationFormula zi,
  18. const mnd::Real& prec) :
  19. IterationGenerator{ std::move(z0), std::move(zi), prec }
  20. {
  21. this->z0.optimize();
  22. this->zi.optimize();
  23. }
  24. void NaiveGenerator::generate(const mnd::MandelInfo& info, float* data)
  25. {
  26. const MandelViewport& view = info.view;
  27. const bool parallel = true;
  28. using T = double;
  29. T viewx = mnd::convert<T>(view.x);
  30. T viewy = mnd::convert<T>(view.y);
  31. T wpp = mnd::convert<T>(view.width / info.bWidth);
  32. T hpp = mnd::convert<T>(view.height / info.bHeight);
  33. if constexpr (parallel)
  34. omp_set_num_threads(omp_get_num_procs());
  35. //#pragma omp parallel for schedule(static, 1) if (parallel)
  36. for (long j = 0; j < info.bHeight; j++) {
  37. T y = viewy + T(double(j)) * hpp;
  38. long i = 0;
  39. for (i; i < info.bWidth; i++) {
  40. T x = viewx + T(double(i)) * wpp;
  41. T cx = x;
  42. T cy = y;
  43. std::complex<double> z = calc(*z0.expr, { 0, 0 }, { x, y });
  44. std::complex<double> c{ cx, cy };
  45. int k = 0;
  46. for (k = 0; k < info.maxIter; k++) {
  47. z = this->iterate(z, c);
  48. if (std::abs(z) >= 4)
  49. break;
  50. }
  51. data[i + j * info.bWidth] = float(k);
  52. /*if (info.smooth) {
  53. if (k >= info.maxIter)
  54. data[i + j * info.bWidth] = float(info.maxIter);
  55. else {
  56. float aapp = mnd::convert<float>(a);
  57. float bapp = mnd::convert<float>(b);
  58. data[i + j * info.bWidth] = ((float) k) + 1 - ::logf(::logf(aapp * aapp + bapp * bapp) / 2) / ::logf(2.0f);
  59. }
  60. }
  61. else
  62. data[i + j * info.bWidth] = k;*/
  63. }
  64. }
  65. }
  66. std::complex<double> NaiveGenerator::iterate(std::complex<double> z, std::complex<double> c)
  67. {
  68. auto& expr = *zi.expr;
  69. return calc(expr, z, c);
  70. }
  71. std::complex<double> NaiveGenerator::calc(mnd::Expression& expr, std::complex<double> z, std::complex<double> c)
  72. {
  73. std::complex<double> result = 0;
  74. std::visit([this, &result, z, c] (auto&& ex) {
  75. using T = std::decay_t<decltype(ex)>;
  76. if constexpr (std::is_same<T, mnd::Constant>::value) {
  77. result = std::complex{ mnd::convert<double>(ex.re), mnd::convert<double>(ex.im) };
  78. }
  79. else if constexpr (std::is_same<T, mnd::Variable>::value) {
  80. if (ex.name == "z")
  81. result = z;
  82. else if (ex.name == "c")
  83. result = c;
  84. else if (ex.name == "i")
  85. result = std::complex{ 0.0, 1.0 };
  86. }
  87. else if constexpr (std::is_same<T, mnd::Negation>::value) {
  88. result = -calc(*ex.operand, z, c);
  89. }
  90. else if constexpr (std::is_same<T, mnd::Addition>::value) {
  91. if (ex.subtraction)
  92. result = calc(*ex.left, z, c) - calc(*ex.right, z, c);
  93. else
  94. result = calc(*ex.left, z, c) + calc(*ex.right, z, c);
  95. }
  96. else if constexpr (std::is_same<T, mnd::Multiplication>::value) {
  97. result = calc(*ex.left, z, c) * calc(*ex.right, z, c);
  98. }
  99. else if constexpr (std::is_same<T, mnd::Division>::value) {
  100. result = calc(*ex.left, z, c) / calc(*ex.right, z, c);
  101. }
  102. else if constexpr (std::is_same<T, mnd::Pow>::value) {
  103. result = std::pow(calc(*ex.left, z, c), calc(*ex.right, z, c));
  104. }
  105. }, expr);
  106. return result;
  107. }
  108. NaiveIRGenerator::NaiveIRGenerator(const ir::Formula& irf,
  109. const mnd::Real& prec) :
  110. mnd::MandelGenerator{ prec },
  111. form{ irf }
  112. {
  113. }
  114. void NaiveIRGenerator::generate(const mnd::MandelInfo& info, float* data)
  115. {
  116. const MandelViewport& view = info.view;
  117. const bool parallel = true;
  118. using T = double;
  119. T viewx = mnd::convert<T>(view.x);
  120. T viewy = mnd::convert<T>(view.y);
  121. T wpp = mnd::convert<T>(view.width / info.bWidth);
  122. T hpp = mnd::convert<T>(view.height / info.bHeight);
  123. if constexpr (parallel)
  124. omp_set_num_threads(omp_get_num_procs());
  125. //#pragma omp parallel for schedule(static, 1) if (parallel)
  126. for (long j = 0; j < info.bHeight; j++) {
  127. T y = viewy + T(double(j)) * hpp;
  128. long i = 0;
  129. for (i; i < info.bWidth; i++) {
  130. T x = viewx + T(double(i)) * wpp;
  131. T a = calc(form.startA, x, y, 0, 0);
  132. T b = calc(form.startB, x, y, 0, 0);
  133. int k = 0;
  134. for (k = 0; k < info.maxIter; k++) {
  135. double newA = calc(form.newA, a, b, x, y);
  136. double newB = calc(form.newB, a, b, x, y);
  137. a = newA;
  138. b = newB;
  139. if (a * a + b * b >= 16.0)
  140. break;
  141. }
  142. data[i + j * info.bWidth] = float(k);
  143. }
  144. }
  145. }
  146. double NaiveIRGenerator::calc(ir::Node* expr, double a, double b, double x, double y)
  147. {
  148. struct DoubleVisitor
  149. {
  150. double a, b, x, y;
  151. double visitNode(ir::Node* n) {
  152. return std::visit(*this, *n);
  153. }
  154. double operator()(const ir::Constant& c) {
  155. return mnd::convert<double>(c.value);
  156. }
  157. double operator()(const ir::Variable& v) {
  158. if (v.name == "z_re")
  159. return a;
  160. else if (v.name == "z_im")
  161. return b;
  162. else if (v.name == "c_re")
  163. return x;
  164. else if (v.name == "c_im")
  165. return y;
  166. else
  167. return 0.0;
  168. }
  169. double operator()(const ir::Negation& n) {
  170. return -visitNode(n.value);
  171. }
  172. double operator()(const ir::Addition& n) {
  173. return visitNode(n.left) + visitNode(n.right);
  174. }
  175. double operator()(const ir::Subtraction& n) {
  176. return visitNode(n.left) - visitNode(n.right);
  177. }
  178. double operator()(const ir::Multiplication& n) {
  179. return visitNode(n.left) * visitNode(n.right);
  180. }
  181. double operator()(const ir::Division& n) {
  182. return visitNode(n.left) / visitNode(n.right);
  183. }
  184. double operator()(const ir::Atan2& n) {
  185. return ::atan2(visitNode(n.left), visitNode(n.right));
  186. }
  187. double operator()(const ir::Pow& n) {
  188. return ::pow(visitNode(n.left), visitNode(n.right));
  189. }
  190. double operator()(const ir::Cos& n) {
  191. return ::cos(visitNode(n.value));
  192. }
  193. double operator()(const ir::Sin& n) {
  194. return ::sin(visitNode(n.value));
  195. }
  196. double operator()(const ir::Exp& n) {
  197. return ::exp(visitNode(n.value));
  198. }
  199. double operator()(const ir::Ln& n) {
  200. return ::log(visitNode(n.value));
  201. }
  202. };
  203. DoubleVisitor dv{ a, b, x, y };
  204. return dv.visitNode(expr);
  205. }
  206. using mnd::CompiledGenerator;
  207. using mnd::CompiledClGenerator;
  208. CompiledGenerator::CompiledGenerator(std::unique_ptr<mnd::ExecData> execData) :
  209. MandelGenerator{ 1.0e-15 },
  210. execData{ std::move(execData) }
  211. {
  212. }
  213. CompiledGenerator::CompiledGenerator(CompiledGenerator&&) = default;
  214. CompiledGenerator::~CompiledGenerator(void)
  215. {
  216. }
  217. /*__declspec(noinline)
  218. int iter(double x, double y, int maxIter)
  219. {
  220. int k = 0;
  221. double a = x;
  222. double b = y;
  223. for (k = 0; k < maxIter; k++) {
  224. double aa = a * a;
  225. double bb = b * b;
  226. double abab = a * b + a * b;
  227. a = aa - bb + x;
  228. b = abab + y;
  229. if (aa + bb >= 16)
  230. break;
  231. }
  232. return k;
  233. }*/
  234. void CompiledGenerator::generate(const mnd::MandelInfo& info, float* data)
  235. {
  236. using IterFunc = int (*)(double, double, int);
  237. omp_set_num_threads(omp_get_num_procs());
  238. #pragma omp parallel for schedule(static, 1)
  239. for (int i = 0; i < info.bHeight; i++) {
  240. double y = mnd::convert<double>(info.view.y + info.view.height * i / info.bHeight);
  241. for (int j = 0; j < info.bWidth; j++) {
  242. double x = mnd::convert<double>(info.view.x + info.view.width * j / info.bWidth);
  243. IterFunc iterFunc = asmjit::ptr_as_func<IterFunc>(this->execData->iterationFunc);
  244. int k = iterFunc(x, y, info.maxIter);
  245. data[i * info.bWidth + j] = float(k);
  246. }
  247. }
  248. }
  249. std::string CompiledGenerator::dump(void) const
  250. {
  251. asmjit::String d;
  252. execData->compiler->dump(d);
  253. return d.data();
  254. }
  255. #ifdef WITH_OPENCL
  256. CompiledClGenerator::CompiledClGenerator(const mnd::MandelDevice& device, const std::string& code) :
  257. ClGeneratorFloat{ device.getClDevice().device, code }
  258. {
  259. }
  260. std::string CompiledClGenerator::getKernelCode(bool smooth) const
  261. {
  262. return "";
  263. }
  264. void CompiledClGenerator::generate(const mnd::MandelInfo& info, float* data)
  265. {
  266. ::size_t bufferSize = info.bWidth * info.bHeight * sizeof(float);
  267. cl::Buffer buffer_A(context, CL_MEM_WRITE_ONLY, bufferSize);
  268. float pixelScaleX = float(info.view.width / info.bWidth);
  269. float pixelScaleY = float(info.view.height / info.bHeight);
  270. static cl::Kernel iterate = cl::Kernel(program, "iterate");
  271. iterate.setArg(0, buffer_A);
  272. iterate.setArg(1, int(info.bWidth));
  273. iterate.setArg(2, float(info.view.x));
  274. iterate.setArg(3, float(info.view.y));
  275. iterate.setArg(4, float(pixelScaleX));
  276. iterate.setArg(5, float(pixelScaleY));
  277. iterate.setArg(6, int(info.maxIter));
  278. iterate.setArg(7, int(info.smooth ? 1 : 0));
  279. iterate.setArg(8, int(info.julia ? 1 : 0));
  280. iterate.setArg(9, float(info.juliaX));
  281. iterate.setArg(10, float(info.juliaY));
  282. queue.enqueueNDRangeKernel(iterate, 0, cl::NDRange(info.bWidth * info.bHeight));
  283. queue.enqueueReadBuffer(buffer_A, CL_TRUE, 0, bufferSize, data);
  284. }
  285. #endif // WITH_OPENCL