IterationGenerator.cpp 10 KB

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