1
0

IterationGenerator.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. struct EvalNode
  157. {
  158. };
  159. template<typename T>
  160. double NaiveIRGenerator<T>::calc(mnd::ir::Node* expr, double a, double b, double x, double y)
  161. {
  162. struct DoubleVisitor
  163. {
  164. double a, b, x, y;
  165. double visitNode(ir::Node* n) {
  166. auto& nodeData = getNodeData(n);
  167. //if (std::get_a)
  168. return std::visit(*this, *n);
  169. }
  170. const std::any& getNodeData(ir::Node* n) {
  171. return std::visit([](auto& n) {
  172. return n.nodeData;
  173. }, *n);
  174. }
  175. double operator()(const ir::Constant& c) {
  176. return mnd::convert<double>(c.value);
  177. }
  178. double operator()(const ir::Variable& v) {
  179. if (v.name == "z_re")
  180. return a;
  181. else if (v.name == "z_im")
  182. return b;
  183. else if (v.name == "c_re")
  184. return x;
  185. else if (v.name == "c_im")
  186. return y;
  187. else
  188. return 0.0;
  189. }
  190. double operator()(const ir::Negation& n) {
  191. return -visitNode(n.value);
  192. }
  193. double operator()(const ir::Addition& n) {
  194. return visitNode(n.left) + visitNode(n.right);
  195. }
  196. double operator()(const ir::Subtraction& n) {
  197. return visitNode(n.left) - visitNode(n.right);
  198. }
  199. double operator()(const ir::Multiplication& n) {
  200. return visitNode(n.left) * visitNode(n.right);
  201. }
  202. double operator()(const ir::Division& n) {
  203. return visitNode(n.left) / visitNode(n.right);
  204. }
  205. double operator()(const ir::Atan2& n) {
  206. return ::atan2(visitNode(n.left), visitNode(n.right));
  207. }
  208. double operator()(const ir::Pow& n) {
  209. return ::pow(visitNode(n.left), visitNode(n.right));
  210. }
  211. double operator()(const ir::Cos& n) {
  212. return ::cos(visitNode(n.value));
  213. }
  214. double operator()(const ir::Sin& n) {
  215. return ::sin(visitNode(n.value));
  216. }
  217. double operator()(const ir::Exp& n) {
  218. return ::exp(visitNode(n.value));
  219. }
  220. double operator()(const ir::Ln& n) {
  221. return ::log(visitNode(n.value));
  222. }
  223. };
  224. DoubleVisitor dv{ a, b, x, y };
  225. return dv.visitNode(expr);
  226. }
  227. using mnd::CompiledGenerator;
  228. using mnd::CompiledGeneratorVec;
  229. using mnd::CompiledClGenerator;
  230. using mnd::CompiledClGeneratorDouble;
  231. CompiledGenerator::CompiledGenerator(std::unique_ptr<mnd::ExecData> execData,
  232. mnd::Precision prec, mnd::CpuExtension ex) :
  233. MandelGenerator{ prec, ex },
  234. execData{ std::move(execData) }
  235. {
  236. }
  237. CompiledGenerator::CompiledGenerator(CompiledGenerator&&) = default;
  238. CompiledGenerator::~CompiledGenerator(void)
  239. {
  240. }
  241. /*__declspec(noinline)
  242. int iter(double x, double y, int maxIter)
  243. {
  244. int k = 0;
  245. double a = x;
  246. double b = y;
  247. for (k = 0; k < maxIter; k++) {
  248. double aa = a * a;
  249. double bb = b * b;
  250. double abab = a * b + a * b;
  251. a = aa - bb + x;
  252. b = abab + y;
  253. if (aa + bb >= 16)
  254. break;
  255. }
  256. return k;
  257. }*/
  258. void CompiledGenerator::generate(const mnd::MandelInfo& info, float* data)
  259. {
  260. using IterFunc = int (*)(double, double, int);
  261. omp_set_num_threads(omp_get_num_procs());
  262. #pragma omp parallel for schedule(static, 1)
  263. for (int i = 0; i < info.bHeight; i++) {
  264. double y = mnd::convert<double>(info.view.y + info.view.height * i / info.bHeight);
  265. for (int j = 0; j < info.bWidth; j++) {
  266. double x = mnd::convert<double>(info.view.x + info.view.width * j / info.bWidth);
  267. IterFunc iterFunc = asmjit::ptr_as_func<IterFunc>(this->execData->iterationFunc);
  268. int k = iterFunc(x, y, info.maxIter);
  269. data[i * info.bWidth + j] = float(k);
  270. }
  271. }
  272. }
  273. std::string CompiledGenerator::dump(void) const
  274. {
  275. asmjit::String d;
  276. execData->compiler->dump(d);
  277. return d.data();
  278. }
  279. CompiledGeneratorVec::CompiledGeneratorVec(std::unique_ptr<mnd::ExecData> execData) :
  280. CompiledGenerator{ std::move(execData), mnd::Precision::FLOAT, mnd::CpuExtension::X86_AVX }
  281. {
  282. }
  283. CompiledGeneratorVec::CompiledGeneratorVec(CompiledGeneratorVec&&) = default;
  284. CompiledGeneratorVec::~CompiledGeneratorVec(void)
  285. {
  286. }
  287. void CompiledGeneratorVec::generate(const mnd::MandelInfo& info, float* data)
  288. {
  289. using IterFunc = int (*)(float, float, float, int, float*);
  290. double dx = mnd::convert<double>(info.view.width / info.bWidth);
  291. omp_set_num_threads(omp_get_num_procs());
  292. #pragma omp parallel for schedule(static, 1)
  293. for (int i = 0; i < info.bHeight; i++) {
  294. double y = mnd::convert<double>(info.view.y + info.view.height * i / info.bHeight);
  295. for (int j = 0; j < info.bWidth; j += 8) {
  296. double x = mnd::convert<double>(info.view.x + info.view.width * j / info.bWidth);
  297. float result[8];
  298. IterFunc iterFunc = asmjit::ptr_as_func<IterFunc>(this->execData->iterationFunc);
  299. int k = iterFunc(x, y, dx, info.maxIter-1, result);
  300. for (int k = 0; k < 8 && j + k < info.bWidth; k++)
  301. data[i * info.bWidth + j + k] = result[k];
  302. }
  303. }
  304. }
  305. #ifdef WITH_OPENCL
  306. CompiledClGenerator::CompiledClGenerator(mnd::MandelDevice& device, const std::string& code) :
  307. ClGeneratorFloat{ device, code }
  308. {
  309. kernel = cl::Kernel(program, "iterate");
  310. }
  311. void CompiledClGenerator::generate(const mnd::MandelInfo& info, float* data)
  312. {
  313. ::size_t bufferSize = info.bWidth * info.bHeight * sizeof(float);
  314. cl::Buffer buffer_A(context, CL_MEM_WRITE_ONLY, bufferSize);
  315. float pixelScaleX = float(info.view.width / info.bWidth);
  316. float pixelScaleY = float(info.view.height / info.bHeight);
  317. //static cl::Kernel iterate = cl::Kernel(program, "iterate");
  318. kernel.setArg(0, buffer_A);
  319. kernel.setArg(1, int(info.bWidth));
  320. kernel.setArg(2, float(info.view.x));
  321. kernel.setArg(3, float(info.view.y));
  322. kernel.setArg(4, float(pixelScaleX));
  323. kernel.setArg(5, float(pixelScaleY));
  324. kernel.setArg(6, int(info.maxIter));
  325. kernel.setArg(7, int(info.smooth ? 1 : 0));
  326. kernel.setArg(8, int(info.julia ? 1 : 0));
  327. kernel.setArg(9, float(info.juliaX));
  328. kernel.setArg(10, float(info.juliaY));
  329. queue.enqueueNDRangeKernel(kernel, 0, cl::NDRange(info.bWidth * info.bHeight));
  330. queue.enqueueReadBuffer(buffer_A, CL_TRUE, 0, bufferSize, data);
  331. }
  332. CompiledClGeneratorDouble::CompiledClGeneratorDouble(mnd::MandelDevice& device, const std::string& code) :
  333. ClGeneratorDouble{ device, code }
  334. {
  335. }
  336. #endif // WITH_OPENCL