IterationGenerator.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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::IterationFormula;
  9. IterationGenerator::IterationGenerator(IterationFormula z0, IterationFormula zi,
  10. const mnd::Real& prec) :
  11. mnd::MandelGenerator{ prec },
  12. z0{ std::move(z0) },
  13. zi{ std::move(zi) }
  14. {
  15. }
  16. NaiveGenerator::NaiveGenerator(IterationFormula z0, IterationFormula zi,
  17. const mnd::Real& prec) :
  18. IterationGenerator{ std::move(z0), std::move(zi), prec }
  19. {
  20. this->z0.optimize();
  21. this->zi.optimize();
  22. }
  23. void NaiveGenerator::generate(const mnd::MandelInfo& info, float* data)
  24. {
  25. const MandelViewport& view = info.view;
  26. const bool parallel = true;
  27. using T = double;
  28. T viewx = mnd::convert<T>(view.x);
  29. T viewy = mnd::convert<T>(view.y);
  30. T wpp = mnd::convert<T>(view.width / info.bWidth);
  31. T hpp = mnd::convert<T>(view.height / info.bHeight);
  32. if constexpr (parallel)
  33. omp_set_num_threads(omp_get_num_procs());
  34. //#pragma omp parallel for schedule(static, 1) if (parallel)
  35. for (long j = 0; j < info.bHeight; j++) {
  36. T y = viewy + T(double(j)) * hpp;
  37. long i = 0;
  38. for (i; i < info.bWidth; i++) {
  39. T x = viewx + T(double(i)) * wpp;
  40. T cx = x;
  41. T cy = y;
  42. std::complex<double> z = calc(*z0.expr, { 0, 0 }, { x, y });
  43. std::complex<double> c{ cx, cy };
  44. int k = 0;
  45. for (k = 0; k < info.maxIter; k++) {
  46. z = this->iterate(z, c);
  47. if (std::abs(z) >= 4)
  48. break;
  49. }
  50. data[i + j * info.bWidth] = float(k);
  51. /*if (info.smooth) {
  52. if (k >= info.maxIter)
  53. data[i + j * info.bWidth] = float(info.maxIter);
  54. else {
  55. float aapp = mnd::convert<float>(a);
  56. float bapp = mnd::convert<float>(b);
  57. data[i + j * info.bWidth] = ((float) k) + 1 - ::logf(::logf(aapp * aapp + bapp * bapp) / 2) / ::logf(2.0f);
  58. }
  59. }
  60. else
  61. data[i + j * info.bWidth] = k;*/
  62. }
  63. }
  64. }
  65. std::complex<double> NaiveGenerator::iterate(std::complex<double> z, std::complex<double> c)
  66. {
  67. auto& expr = *zi.expr;
  68. return calc(expr, z, c);
  69. }
  70. std::complex<double> NaiveGenerator::calc(mnd::Expression& expr, std::complex<double> z, std::complex<double> c)
  71. {
  72. std::complex<double> result = 0;
  73. std::visit([this, &result, z, c] (auto&& ex) {
  74. using T = std::decay_t<decltype(ex)>;
  75. if constexpr (std::is_same<T, mnd::Constant>::value) {
  76. result = std::complex{ mnd::convert<double>(ex.re), mnd::convert<double>(ex.im) };
  77. }
  78. else if constexpr (std::is_same<T, mnd::Variable>::value) {
  79. if (ex.name == "z")
  80. result = z;
  81. else if (ex.name == "c")
  82. result = c;
  83. else if (ex.name == "i")
  84. result = std::complex{ 0.0, 1.0 };
  85. }
  86. else if constexpr (std::is_same<T, mnd::Negation>::value) {
  87. result = -calc(*ex.operand, z, c);
  88. }
  89. else if constexpr (std::is_same<T, mnd::Addition>::value) {
  90. if (ex.subtraction)
  91. result = calc(*ex.left, z, c) - calc(*ex.right, z, c);
  92. else
  93. result = calc(*ex.left, z, c) + calc(*ex.right, z, c);
  94. }
  95. else if constexpr (std::is_same<T, mnd::Multiplication>::value) {
  96. result = calc(*ex.left, z, c) * calc(*ex.right, z, c);
  97. }
  98. else if constexpr (std::is_same<T, mnd::Division>::value) {
  99. result = calc(*ex.left, z, c) / calc(*ex.right, z, c);
  100. }
  101. else if constexpr (std::is_same<T, mnd::Pow>::value) {
  102. result = std::pow(calc(*ex.left, z, c), calc(*ex.right, z, c));
  103. }
  104. }, expr);
  105. return result;
  106. }
  107. using mnd::CompiledGenerator;
  108. using mnd::CompiledClGenerator;
  109. CompiledGenerator::CompiledGenerator(std::unique_ptr<mnd::ExecData> execData) :
  110. MandelGenerator{ 1.0e-15 },
  111. execData{ std::move(execData) }
  112. {
  113. }
  114. CompiledGenerator::CompiledGenerator(CompiledGenerator&&) = default;
  115. CompiledGenerator::~CompiledGenerator(void)
  116. {
  117. }
  118. /*__declspec(noinline)
  119. int iter(double x, double y, int maxIter)
  120. {
  121. int k = 0;
  122. double a = x;
  123. double b = y;
  124. for (k = 0; k < maxIter; k++) {
  125. double aa = a * a;
  126. double bb = b * b;
  127. double abab = a * b + a * b;
  128. a = aa - bb + x;
  129. b = abab + y;
  130. if (aa + bb >= 16)
  131. break;
  132. }
  133. return k;
  134. }*/
  135. void CompiledGenerator::generate(const mnd::MandelInfo& info, float* data)
  136. {
  137. using IterFunc = int (*)(double, double, int);
  138. omp_set_num_threads(omp_get_num_procs());
  139. //#pragma omp parallel for schedule(static, 1)
  140. for (int i = 0; i < info.bHeight; i++) {
  141. double y = mnd::convert<double>(info.view.y + info.view.height * i / info.bHeight);
  142. for (int j = 0; j < info.bWidth; j++) {
  143. double x = mnd::convert<double>(info.view.x + info.view.width * j / info.bWidth);
  144. IterFunc iterFunc = asmjit::ptr_as_func<IterFunc>(this->execData->iterationFunc);
  145. int k = iterFunc(x, y, info.maxIter);
  146. data[i * info.bWidth + j] = float(k);
  147. }
  148. }
  149. }
  150. std::string CompiledGenerator::dump(void) const
  151. {
  152. asmjit::String d;
  153. execData->compiler->dump(d);
  154. return d.data();
  155. }
  156. #ifdef WITH_OPENCL
  157. CompiledClGenerator::CompiledClGenerator(const mnd::MandelDevice& device, const std::string& code) :
  158. ClGeneratorFloat{ device.getClDevice().device, code }
  159. {
  160. }
  161. std::string CompiledClGenerator::getKernelCode(bool smooth) const
  162. {
  163. return "";
  164. }
  165. void CompiledClGenerator::generate(const mnd::MandelInfo& info, float* data)
  166. {
  167. ::size_t bufferSize = info.bWidth * info.bHeight * sizeof(float);
  168. cl::Buffer buffer_A(context, CL_MEM_WRITE_ONLY, bufferSize);
  169. float pixelScaleX = float(info.view.width / info.bWidth);
  170. float pixelScaleY = float(info.view.height / info.bHeight);
  171. static cl::Kernel iterate = cl::Kernel(program, "iterate");
  172. iterate.setArg(0, buffer_A);
  173. iterate.setArg(1, int(info.bWidth));
  174. iterate.setArg(2, float(info.view.x));
  175. iterate.setArg(3, float(info.view.y));
  176. iterate.setArg(4, float(pixelScaleX));
  177. iterate.setArg(5, float(pixelScaleY));
  178. iterate.setArg(6, int(info.maxIter));
  179. iterate.setArg(7, int(info.smooth ? 1 : 0));
  180. iterate.setArg(8, int(info.julia ? 1 : 0));
  181. iterate.setArg(9, float(info.juliaX));
  182. iterate.setArg(10, float(info.juliaY));
  183. queue.enqueueNDRangeKernel(iterate, 0, cl::NDRange(info.bWidth * info.bHeight));
  184. queue.enqueueReadBuffer(buffer_A, CL_TRUE, 0, bufferSize, data);
  185. }
  186. #endif // WITH_OPENCL