Mandel.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #include "Mandel.h"
  2. #include "Fixed.h"
  3. #include "CpuGenerators.h"
  4. #include "ClGenerators.h"
  5. using mnd::MandelDevice;
  6. using mnd::MandelContext;
  7. using mnd::Generator;
  8. using mnd::AdaptiveGenerator;
  9. MandelContext mnd::initializeContext(void)
  10. {
  11. MandelContext context = MandelContext();
  12. return context;
  13. }
  14. MandelDevice::MandelDevice(void) :
  15. floatGenerator{ nullptr },
  16. doubleGenerator{ nullptr },
  17. floatGeneratorSmooth{ nullptr },
  18. doubleGeneratorSmooth{ nullptr }
  19. {
  20. }
  21. mnd::Generator* MandelDevice::getGeneratorFloat(bool smooth) const
  22. {
  23. if (smooth)
  24. return floatGeneratorSmooth.get();
  25. else
  26. return floatGenerator.get();
  27. }
  28. mnd::Generator* MandelDevice::getGeneratorDouble(bool smooth) const
  29. {
  30. if (smooth)
  31. return doubleGeneratorSmooth.get();
  32. else
  33. return doubleGenerator.get();
  34. }
  35. /*
  36. mnd::Generator* MandelDevice::getGeneratorQuad(bool smooth) const
  37. {
  38. if (smooth)
  39. return quadGeneratorSmooth.get();
  40. else
  41. return quadGenerator.get();
  42. }*/
  43. /*
  44. mnd::Generator* MandelDevice::getGenerator128(bool smooth) const
  45. {
  46. if (smooth)
  47. return generator128Smooth.get();
  48. else
  49. return generator128.get();
  50. }
  51. */
  52. MandelContext::MandelContext(void) :
  53. cpuGeneratorQuad{ nullptr },
  54. cpuGeneratorQuadSmooth{ nullptr }
  55. {
  56. #if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86)
  57. if (cpuInfo.hasAvx()) {
  58. cpuGeneratorFloat = std::make_unique<CpuGenerator<float, mnd::X86_AVX, true, false>>();
  59. cpuGeneratorDouble = std::make_unique<CpuGenerator<double, mnd::X86_AVX, true, false>>();
  60. cpuGeneratorFloatSmooth = std::make_unique<CpuGenerator<float, mnd::X86_AVX, true, true>>();
  61. cpuGeneratorDoubleSmooth = std::make_unique<CpuGenerator<double, mnd::X86_AVX, true, true>>();
  62. }
  63. else if (cpuInfo.hasSse2()) {
  64. cpuGeneratorFloat = std::make_unique<CpuGenerator<float, mnd::X86_SSE2, true, false>>();
  65. cpuGeneratorDouble = std::make_unique<CpuGenerator<double, mnd::X86_SSE2, true, false>>();
  66. cpuGeneratorFloatSmooth = std::make_unique<CpuGenerator<float, mnd::X86_SSE2, true, true>>();
  67. cpuGeneratorDoubleSmooth = std::make_unique<CpuGenerator<double, mnd::X86_SSE2, true, true>>();
  68. }
  69. else
  70. #elif defined(__aarch64__)
  71. if (true) {
  72. cpuGeneratorFloat = std::make_unique<CpuGenerator<float, mnd::ARM_NEON, true, false>>();
  73. cpuGeneratorDouble = std::make_unique<CpuGenerator<double, mnd::ARM_NEON, true, false>>();
  74. cpuGeneratorFloatSmooth = std::make_unique<CpuGenerator<float, mnd::ARM_NEON>>();
  75. cpuGeneratorDoubleSmooth = std::make_unique<CpuGenerator<double, mnd::ARM_NEON>>();
  76. }
  77. else
  78. #endif
  79. {
  80. cpuGeneratorFloat = std::make_unique<CpuGenerator<float, mnd::NONE, true, false>>();
  81. cpuGeneratorDouble = std::make_unique<CpuGenerator<double, mnd::NONE, true, false>>();
  82. cpuGeneratorFloatSmooth = std::make_unique<CpuGenerator<float, mnd::NONE, true, true>>();
  83. cpuGeneratorDoubleSmooth = std::make_unique<CpuGenerator<double, mnd::NONE, true, true>>();
  84. }
  85. //cpuGenerator128Smooth = std::make_unique<CpuGenerator<Fixed128>>();
  86. //cpuGeneratorFixedp = std::make_unique<CpuGenerator<fixed<1, 3>>>();
  87. #ifdef WITH_BOOST
  88. cpuGeneratorQuad = std::make_unique<CpuGenerator<Float128, mnd::NONE, true, false>>();
  89. cpuGeneratorQuadSmooth = std::make_unique<CpuGenerator<Float128, mnd::NONE, true, true>>();
  90. cpuGeneratorOct = std::make_unique<CpuGenerator<Float256, mnd::NONE, true, false>>();
  91. cpuGenerator128 = std::make_unique<CpuGenerator<Fixed128, mnd::NONE, true, false>>();
  92. cpuGenerator128Smooth = std::make_unique<CpuGenerator<Fixed128, mnd::NONE, true, true>>();
  93. #endif // WITH_BOOST
  94. #ifdef WITH_QD
  95. cpuGeneratorDD = std::make_unique<CpuGenerator<DoubleDouble, mnd::NONE, true, false>>();
  96. cpuGeneratorDDSmooth = std::make_unique<CpuGenerator<DoubleDouble, mnd::NONE, true, true>>();
  97. #endif
  98. devices = createDevices();
  99. if (devices.empty()) {
  100. #ifdef WITH_BOOST
  101. adaptiveGenerator = std::make_unique<AdaptiveGenerator>(
  102. cpuGeneratorFloat.get(), cpuGeneratorDouble.get(), cpuGeneratorQuad.get());
  103. adaptiveGeneratorSmooth = std::make_unique<AdaptiveGenerator>(
  104. cpuGeneratorFloatSmooth.get(), cpuGeneratorDoubleSmooth.get(),
  105. cpuGeneratorQuadSmooth.get());
  106. #else
  107. adaptiveGenerator = std::make_unique<AdaptiveGenerator>(
  108. cpuGeneratorFloat.get(), cpuGeneratorDouble.get());
  109. adaptiveGeneratorSmooth = std::make_unique<AdaptiveGenerator>(
  110. cpuGeneratorFloatSmooth.get(), cpuGeneratorDoubleSmooth.get());
  111. #endif
  112. }
  113. else {
  114. auto& device1 = devices[0];
  115. Generator* floatGenerator = device1.getGeneratorFloat(false);
  116. Generator* doubleGenerator = device1.getGeneratorDouble(false);
  117. Generator* floatGeneratorSmooth = device1.getGeneratorFloat(true);
  118. Generator* doubleGeneratorSmooth = device1.getGeneratorDouble(true);
  119. if (floatGenerator == nullptr)
  120. floatGenerator = cpuGeneratorFloat.get();
  121. if (doubleGenerator == nullptr)
  122. doubleGenerator = cpuGeneratorDouble.get();
  123. if (floatGeneratorSmooth == nullptr)
  124. floatGeneratorSmooth = cpuGeneratorFloatSmooth.get();
  125. if (doubleGeneratorSmooth == nullptr)
  126. doubleGeneratorSmooth = cpuGeneratorDoubleSmooth.get();
  127. #ifdef WITH_BOOST
  128. adaptiveGeneratorSmooth = std::make_unique<AdaptiveGenerator>(floatGeneratorSmooth, doubleGeneratorSmooth, cpuGeneratorDDSmooth.get());
  129. adaptiveGenerator = std::make_unique<AdaptiveGenerator>(floatGenerator, doubleGenerator, cpuGeneratorDD.get());
  130. #else
  131. adaptiveGeneratorSmooth = std::make_unique<AdaptiveGenerator>(floatGeneratorSmooth, doubleGeneratorSmooth);
  132. adaptiveGenerator = std::make_unique<AdaptiveGenerator>(floatGenerator, doubleGenerator);
  133. #endif
  134. }
  135. }
  136. std::vector<MandelDevice> MandelContext::createDevices(void)
  137. {
  138. std::vector<MandelDevice> mandelDevices;
  139. #ifdef WITH_OPENCL
  140. std::vector<cl::Platform> platforms;
  141. cl::Platform::get(&platforms);
  142. //platforms.erase(platforms.begin() + 1);
  143. for (auto& platform : platforms) {
  144. std::string name = platform.getInfo<CL_PLATFORM_NAME>();
  145. std::string profile = platform.getInfo<CL_PLATFORM_PROFILE>();
  146. //std::string ext = platform.getInfo<CL_PLATFORM_EXTENSIONS>();
  147. //printf("Platform extensions: %s\n", ext.c_str());
  148. //printf("Platform: %s, %s\n", name.c_str(), profile.c_str());
  149. std::vector<cl::Device> devices;
  150. platform.getDevices(CL_DEVICE_TYPE_GPU, &devices);
  151. for (auto& device : devices) {
  152. //printf("Device: %s\n", device.getInfo<CL_DEVICE_NAME>().c_str());
  153. //printf("preferred float width: %d\n", device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT>());
  154. //printf("vendor: %s\n", device.getInfo<CL_DEVICE_VENDOR>().c_str());
  155. std::string extensions = device.getInfo<CL_DEVICE_EXTENSIONS>();
  156. auto supportsDouble = extensions.find("cl_khr_fp64") != std::string::npos;
  157. //printf("Device extensions: %s\n", ext.c_str());
  158. MandelDevice md;
  159. //printf("clock: %d", device.getInfo<CL_DEVICE_MAX_CLOCK_FREQUENCY>());
  160. md.name = device.getInfo<CL_DEVICE_NAME>();
  161. md.vendor = device.getInfo<CL_DEVICE_VENDOR>();
  162. try {
  163. md.floatGenerator = std::make_unique<ClGeneratorFloat>(device, false);
  164. md.floatGeneratorSmooth = std::make_unique<ClGeneratorFloat>(device, true);
  165. }
  166. catch (const std::string& err) {
  167. printf("err: %s", err.c_str());
  168. }
  169. if (supportsDouble) {
  170. try {
  171. md.doubleGenerator = std::make_unique<ClGeneratorDouble>(device, false);
  172. md.doubleGeneratorSmooth = std::make_unique<ClGeneratorDouble>(device, true);
  173. }
  174. catch (const std::string& err) {
  175. }
  176. }
  177. try {
  178. //md.generator128 = std::make_unique<ClGenerator128>(device);
  179. }
  180. catch (const std::string& err) {
  181. //fprintf(stderr, "error creating 128bit cl generator: %s\n", err.c_str());
  182. }
  183. mandelDevices.push_back(std::move(md));
  184. }
  185. }
  186. #endif // WITH_OPENCL
  187. return mandelDevices;
  188. }
  189. const std::string& MandelDevice::getName(void) const
  190. {
  191. return name;
  192. }
  193. Generator& MandelContext::getDefaultGenerator(bool smooth)
  194. {
  195. if (smooth)
  196. return *adaptiveGeneratorSmooth;
  197. else
  198. return *adaptiveGenerator;
  199. }
  200. const std::vector<MandelDevice>& MandelContext::getDevices(void)
  201. {
  202. return devices;
  203. }
  204. Generator& MandelContext::getCpuGeneratorFloat(void)
  205. {
  206. return *cpuGeneratorFloat;
  207. }
  208. Generator& MandelContext::getCpuGeneratorDouble(void)
  209. {
  210. return *cpuGeneratorDouble;
  211. }
  212. Generator* MandelContext::getCpuGeneratorQuad(void)
  213. {
  214. return cpuGeneratorQuad.get();
  215. }
  216. Generator* MandelContext::getCpuGeneratorOct(void)
  217. {
  218. return cpuGeneratorOct.get();
  219. }
  220. Generator* MandelContext::getCpuGenerator128(void)
  221. {
  222. return cpuGenerator128.get();
  223. }
  224. Generator* MandelContext::getCpuGeneratorDD(void)
  225. {
  226. return cpuGeneratorDD.get();
  227. }