IterationGenerator.cpp 8.3 KB

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