IterationGenerator.cpp 8.4 KB

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