Generators.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #pragma once
  2. #ifndef MANDELQUEUE_H_
  3. #define MANDELQUEUE_H_
  4. #include "QueueManager.h"
  5. #include "GenericMandelbrot.h"
  6. #include <omp.h>
  7. #include <future>
  8. #include <cstdlib>
  9. #ifdef __APPLE__
  10. #include <OpenCL/cl.hpp>
  11. #else
  12. #include <CL/cl.hpp>
  13. #endif
  14. #include <immintrin.h>
  15. class ClGenerator : public MandelGenerator
  16. {
  17. cl::Device device;
  18. cl::Context context;
  19. cl::Program program;
  20. cl::CommandQueue queue;
  21. public:
  22. ClGenerator(void);
  23. ~ClGenerator(void) = default;
  24. //Bitmap<RGBColor> generate(const MandelInfo& info);
  25. Bitmap<float> generateRaw(const MandelInfo& info);
  26. std::future<Bitmap<RGBColor>> enqueueMandelbrot(long width, long height, float x, float y, float fwidth);
  27. };
  28. std::future<Bitmap<RGBColor>> createHPM();
  29. template<typename T>
  30. class CpuGenerator : public MandelGenerator
  31. {
  32. public:
  33. Bitmap<float> generateRaw(const MandelInfo& info)
  34. {
  35. const MandelViewport& view = info.view;
  36. Bitmap<float> res{ info.bWidth, info.bHeight };
  37. omp_set_num_threads(2 * omp_get_num_procs());
  38. #pragma omp parallel for
  39. for (int j = 0; j < res.height; j++) {
  40. T y = T(view.y) + T(j) * view.height / res.height;
  41. for (::size_t i = 0; i < res.width; i++) {
  42. T x = T(view.x) + T(i) * view.width / res.width;
  43. res.get(i, j) = iterate<T>(x, y, info.maxIter);
  44. }
  45. }
  46. return res;
  47. }
  48. };
  49. template<>
  50. Bitmap<float> CpuGenerator<double>::generateRaw(const MandelInfo& info)
  51. {
  52. using T = double;
  53. const MandelViewport& view = info.view;
  54. Bitmap<float> res{ info.bWidth, info.bHeight };
  55. omp_set_num_threads(2 * omp_get_num_procs());
  56. #pragma omp parallel for
  57. for (long j = 0; j < res.height; j++) {
  58. T y = T(view.y) + T(j) * view.height / res.height;
  59. long i = 0;
  60. for (i; i < res.width; i += 4) {
  61. __m256d xs = {
  62. double(view.x) + double(i) * view.width / res.width,
  63. double(view.x) + double(i + 1) * view.width / res.width,
  64. double(view.x) + double(i + 2) * view.width / res.width,
  65. double(view.x) + double(i + 3) * view.width / res.width
  66. };
  67. int itRes[4] = {0, 0, 0, 0};
  68. __m256d threshold = {16.0, 16.0, 16.0, 16.0};
  69. __m256d counter = {0, 0, 0, 0};
  70. __m256d adder = {1, 1, 1, 1};
  71. __m256d ys = {y, y, y, y};
  72. __m256d a = xs;
  73. __m256d b = ys;
  74. for (int k = 0; k < info.maxIter; k++) {
  75. __m256d aa = _mm256_mul_pd(a, a);
  76. __m256d bb = _mm256_mul_pd(b, b);
  77. __m256d abab = _mm256_mul_pd(a, b); abab = _mm256_add_pd(abab, abab);
  78. a = _mm256_add_pd(_mm256_sub_pd(aa, bb), xs);
  79. b = _mm256_add_pd(abab, ys);
  80. __m256i cmp = _mm256_castpd_si256(_mm256_cmp_pd(_mm256_add_pd(aa, bb), threshold, _CMP_LE_OQ));
  81. adder = _mm256_and_pd(adder, _mm256_castsi256_pd(cmp));
  82. counter = _mm256_add_pd(counter, adder);
  83. if (_mm256_testz_si256(cmp, cmp) != 0) {
  84. break;
  85. }
  86. }
  87. double data[8];
  88. void* aligned = data;
  89. unsigned int length = sizeof data;
  90. std::align(32, 4 * sizeof(double), aligned, length);
  91. double* ftRes = static_cast<double*>(aligned);
  92. _mm256_store_pd(ftRes, counter);
  93. for (int k = 0; k < 4; k++)
  94. res.get(i + k, j) = ftRes[k] > 0 ? float(ftRes[k]) : info.maxIter;
  95. }
  96. }
  97. return res;
  98. }
  99. template<>
  100. Bitmap<float> CpuGenerator<float>::generateRaw(const MandelInfo& info)
  101. {
  102. using T = float;
  103. const MandelViewport& view = info.view;
  104. Bitmap<float> res{ info.bWidth, info.bHeight };
  105. omp_set_num_threads(2 * omp_get_num_procs());
  106. #pragma omp parallel for
  107. for (long j = 0; j < res.height; j++) {
  108. T y = T(view.y) + T(j) * view.height / res.height;
  109. long i = 0;
  110. for (i; i < res.width; i += 8) {
  111. __m256 xs = {
  112. float(view.x + double(i) * view.width / res.width),
  113. float(view.x + double(i + 1) * view.width / res.width),
  114. float(view.x + double(i + 2) * view.width / res.width),
  115. float(view.x + double(i + 3) * view.width / res.width),
  116. float(view.x + double(i + 4) * view.width / res.width),
  117. float(view.x + double(i + 5) * view.width / res.width),
  118. float(view.x + double(i + 6) * view.width / res.width),
  119. float(view.x + double(i + 7) * view.width / res.width)
  120. };
  121. __m256 counter = {0, 0, 0, 0, 0, 0, 0, 0};
  122. __m256 adder = {1, 1, 1, 1, 1, 1, 1, 1};
  123. __m256 threshold = {16.0f, 16.0f, 16.0f, 16.0f, 16.0f, 16.0f, 16.0f, 16.0f};
  124. __m256 ys = {y, y, y, y, y, y, y, y};
  125. __m256 a = xs;
  126. __m256 b = ys;
  127. for (int k = 0; k < info.maxIter; k++) {
  128. __m256 aa = _mm256_mul_ps(a, a);
  129. __m256 bb = _mm256_mul_ps(b, b);
  130. __m256 abab = _mm256_mul_ps(a, b); abab = _mm256_add_ps(abab, abab);
  131. a = _mm256_add_ps(_mm256_sub_ps(aa, bb), xs);
  132. b = _mm256_add_ps(abab, ys);
  133. __m256i cmp = _mm256_castps_si256(_mm256_cmp_ps(_mm256_add_ps(aa, bb), threshold, _CMP_LE_OQ));
  134. adder = _mm256_and_ps(adder, _mm256_castsi256_ps(cmp));
  135. counter = _mm256_add_ps(counter, adder);
  136. if (_mm256_testz_si256(cmp, cmp) != 0) {
  137. break;
  138. }
  139. }
  140. float data[16];
  141. void* aligned = data;
  142. unsigned int length = sizeof data;
  143. std::align(32, 8 * sizeof(float), aligned, length);
  144. float* ftRes = static_cast<float*>(aligned);
  145. _mm256_store_ps(ftRes, counter);
  146. for (int k = 0; k < 8; k++)
  147. res.get(i + k, j) = ftRes[k] > 0 ? ftRes[k] : info.maxIter;
  148. }
  149. }
  150. return res;
  151. }
  152. #endif // MANDELQUEUE_H_