Mandel.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #include "Mandel.h"
  2. #include "Fixed.h"
  3. #include "CpuGenerators.h"
  4. #include "ClGenerators.h"
  5. #include "OpenClInternal.h"
  6. #include "OpenClCode.h"
  7. #ifdef WITH_ASMJIT
  8. #include <asmjit/asmjit.h>
  9. #endif // WITH_ASMJIT
  10. #include <map>
  11. using mnd::MandelDevice;
  12. using mnd::MandelContext;
  13. using mnd::MandelGenerator;
  14. using mnd::AdaptiveGenerator;
  15. template<typename T, typename U>
  16. static std::map<U, T> invertMap(const std::map<T, U>& m)
  17. {
  18. std::map<U, T> res;
  19. std::transform(m.begin(), m.end(), std::inserter(res, res.end()), [](auto& pair) {
  20. return std::pair{ pair.second, pair.first };
  21. });
  22. return res;
  23. }
  24. static const std::map<mnd::GeneratorType, std::string> typeNames =
  25. {
  26. { mnd::GeneratorType::FLOAT, "float" },
  27. { mnd::GeneratorType::FLOAT_SSE2, "float SSE2" },
  28. { mnd::GeneratorType::FLOAT_AVX, "float AVX" },
  29. { mnd::GeneratorType::FLOAT_AVX_FMA, "float AVX+FMA" },
  30. { mnd::GeneratorType::FLOAT_AVX512, "float AVX512" },
  31. { mnd::GeneratorType::FLOAT_NEON, "float NEON" },
  32. { mnd::GeneratorType::DOUBLE_FLOAT, "double float" },
  33. { mnd::GeneratorType::DOUBLE, "double" },
  34. { mnd::GeneratorType::DOUBLE_SSE2, "double SSE2" },
  35. { mnd::GeneratorType::DOUBLE_AVX, "double AVX" },
  36. { mnd::GeneratorType::DOUBLE_AVX_FMA, "double AVX+FMA" },
  37. { mnd::GeneratorType::DOUBLE_AVX512, "double AVX512" },
  38. { mnd::GeneratorType::DOUBLE_NEON, "double NEON" },
  39. { mnd::GeneratorType::DOUBLE_DOUBLE, "double double" },
  40. { mnd::GeneratorType::DOUBLE_DOUBLE_AVX, "double double AVX" },
  41. { mnd::GeneratorType::DOUBLE_DOUBLE_AVX_FMA, "double double AVX+FMA" },
  42. { mnd::GeneratorType::DOUBLE_DOUBLE_NEON, "double double NEON" },
  43. { mnd::GeneratorType::QUAD_DOUBLE, "quad double" },
  44. { mnd::GeneratorType::QUAD_DOUBLE_AVX_FMA, "quad double AVX+FMA" },
  45. { mnd::GeneratorType::FLOAT128, "float128" },
  46. { mnd::GeneratorType::FLOAT256, "float256" },
  47. { mnd::GeneratorType::FIXED64, "fixed64" },
  48. { mnd::GeneratorType::FIXED128, "fixed128" },
  49. { mnd::GeneratorType::FIXED512, "fixed512" },
  50. };
  51. static const std::map<std::string, mnd::GeneratorType> nameTypes = invertMap(typeNames);
  52. namespace mnd
  53. {
  54. const std::string& getGeneratorName(mnd::GeneratorType type)
  55. {
  56. return typeNames.at(type);
  57. }
  58. mnd::GeneratorType getTypeFromName(const std::string& name)
  59. {
  60. return nameTypes.at(name);
  61. }
  62. }
  63. MandelContext mnd::initializeContext(void)
  64. {
  65. return MandelContext();
  66. }
  67. MandelDevice::MandelDevice(mnd::ClDeviceWrapper device) :
  68. clDevice{ std::make_unique<ClDeviceWrapper>(std::move(device)) }
  69. {
  70. extensions = clDevice->device.getInfo<CL_DEVICE_EXTENSIONS>();
  71. name = clDevice->device.getInfo<CL_DEVICE_NAME>();
  72. vendor = clDevice->device.getInfo<CL_DEVICE_VENDOR>();
  73. }
  74. mnd::MandelGenerator* MandelDevice::getGenerator(mnd::GeneratorType type) const
  75. {
  76. auto it = mandelGenerators.find(type);
  77. if (it != mandelGenerators.end())
  78. return it->second.get();
  79. else
  80. return nullptr;
  81. }
  82. std::vector<mnd::GeneratorType> MandelDevice::getSupportedTypes(void) const
  83. {
  84. std::vector<GeneratorType> types;
  85. for (auto& [type, gen] : mandelGenerators) {
  86. types.push_back(type);
  87. }
  88. return types;
  89. }
  90. bool MandelDevice::supportsDouble(void) const
  91. {
  92. return extensions.find("cl_khr_fp64") != std::string::npos;
  93. }
  94. MandelContext::MandelContext(void)
  95. #ifdef WITH_ASMJIT
  96. : jitRuntime{ std::make_unique<asmjit::JitRuntime>() }
  97. #endif // WITH_ASMJIT
  98. {
  99. #if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86)
  100. # if defined(WITH_AVX512)
  101. if (cpuInfo.hasAvx512()) {
  102. auto fl = std::make_unique<CpuGenerator<float, mnd::X86_AVX_512, true>>();
  103. auto db = std::make_unique<CpuGenerator<double, mnd::X86_AVX_512, true>>();
  104. cpuGenerators.insert({ GeneratorType::FLOAT_AVX512, std::move(fl) });
  105. cpuGenerators.insert({ GeneratorType::DOUBLE_AVX512, std::move(db) });
  106. }
  107. # endif
  108. if (cpuInfo.hasAvx()) {
  109. auto fl = std::make_unique<CpuGenerator<float, mnd::X86_AVX, true>>();
  110. auto db = std::make_unique<CpuGenerator<double, mnd::X86_AVX, true>>();
  111. auto ddb = std::make_unique<CpuGenerator<DoubleDouble, mnd::X86_AVX, true>>();
  112. cpuGenerators.insert({ GeneratorType::FLOAT_AVX, std::move(fl) });
  113. cpuGenerators.insert({ GeneratorType::DOUBLE_AVX, std::move(db) });
  114. cpuGenerators.insert({ GeneratorType::DOUBLE_DOUBLE_AVX, std::move(ddb) });
  115. }
  116. if (cpuInfo.hasAvx2() && cpuInfo.hasFma()) {
  117. auto favxfma = std::make_unique<CpuGenerator<float, mnd::X86_AVX_FMA, true>>();
  118. auto davxfma = std::make_unique<CpuGenerator<double, mnd::X86_AVX_FMA, true>>();
  119. auto ddavxfma = std::make_unique<CpuGenerator<DoubleDouble, mnd::X86_AVX_FMA, true>>();
  120. auto qdavxfma = std::make_unique<CpuGenerator<QuadDouble, mnd::X86_AVX_FMA, true>>();
  121. cpuGenerators.insert({ GeneratorType::FLOAT_AVX_FMA, std::move(favxfma) });
  122. cpuGenerators.insert({ GeneratorType::DOUBLE_AVX_FMA, std::move(davxfma) });
  123. cpuGenerators.insert({ GeneratorType::DOUBLE_DOUBLE_AVX_FMA, std::move(ddavxfma) });
  124. cpuGenerators.insert({ GeneratorType::QUAD_DOUBLE_AVX_FMA, std::move(qdavxfma) });
  125. }
  126. if (cpuInfo.hasSse2()) {
  127. auto fl = std::make_unique<CpuGenerator<float, mnd::X86_SSE2, true>>();
  128. auto db = std::make_unique<CpuGenerator<double, mnd::X86_SSE2, true>>();
  129. cpuGenerators.insert({ GeneratorType::FLOAT_SSE2, std::move(fl) });
  130. cpuGenerators.insert({ GeneratorType::DOUBLE_SSE2, std::move(db) });
  131. }
  132. #elif defined(__arm__) || defined(__aarch64__) || defined(_M_ARM)
  133. if (cpuInfo.hasNeon()) {
  134. auto fl = std::make_unique<CpuGenerator<float, mnd::ARM_NEON, true>>();
  135. auto db = std::make_unique<CpuGenerator<double, mnd::ARM_NEON, true>>();
  136. auto ddb = std::make_unique<CpuGenerator<mnd::DoubleDouble, mnd::ARM_NEON, true>>();
  137. cpuGenerators.insert({ GeneratorType::FLOAT_NEON, std::move(fl) });
  138. cpuGenerators.insert({ GeneratorType::DOUBLE_NEON, std::move(db) });
  139. cpuGenerators.insert({ GeneratorType::DOUBLE_DOUBLE_NEON, std::move(ddb) });
  140. }
  141. #endif
  142. {
  143. auto fl = std::make_unique<CpuGenerator<float, mnd::NONE, true>>();
  144. auto db = std::make_unique<CpuGenerator<double, mnd::NONE, true>>();
  145. cpuGenerators.insert({ GeneratorType::FLOAT, std::move(fl) });
  146. cpuGenerators.insert({ GeneratorType::DOUBLE, std::move(db) });
  147. auto fx64 = std::make_unique<CpuGenerator<Fixed64, mnd::NONE, true>>();
  148. auto fx128 = std::make_unique<CpuGenerator<Fixed128, mnd::NONE, true>>();
  149. cpuGenerators.insert({ GeneratorType::FIXED64, std::move(fx64) });
  150. cpuGenerators.insert({ GeneratorType::FIXED128, std::move(fx128) });
  151. }
  152. #ifdef WITH_BOOST
  153. auto quad = std::make_unique<CpuGenerator<Float128, mnd::NONE, true>>();
  154. auto oct = std::make_unique<CpuGenerator<Float256, mnd::NONE, true>>();
  155. cpuGenerators.insert({ GeneratorType::FLOAT128, std::move(quad) });
  156. cpuGenerators.insert({ GeneratorType::FLOAT256, std::move(oct) });
  157. #endif // WITH_BOOST
  158. auto dd = std::make_unique<CpuGenerator<DoubleDouble, mnd::NONE, true>>();
  159. auto qd = std::make_unique<CpuGenerator<QuadDouble, mnd::NONE, true>>();
  160. cpuGenerators.insert({ GeneratorType::DOUBLE_DOUBLE, std::move(dd) });
  161. cpuGenerators.insert({ GeneratorType::QUAD_DOUBLE, std::move(qd) });
  162. auto fix512 = std::make_unique<CpuGenerator<Fixed512, mnd::NONE, true>>();
  163. cpuGenerators.insert({ GeneratorType::FIXED512, std::move(fix512) });
  164. devices = createDevices();
  165. adaptiveGenerator = createAdaptiveGenerator();
  166. }
  167. std::unique_ptr<mnd::AdaptiveGenerator> MandelContext::createAdaptiveGenerator(void)
  168. {
  169. auto* floatGen = getCpuGenerator(GeneratorType::FLOAT);
  170. auto* doubleGen = getCpuGenerator(GeneratorType::DOUBLE);
  171. auto* doubleDoubleGen = getCpuGenerator(GeneratorType::DOUBLE_DOUBLE);
  172. auto* quadDoubleGen = getCpuGenerator(GeneratorType::QUAD_DOUBLE);
  173. auto* f256Gen = getCpuGenerator(GeneratorType::FLOAT256);
  174. auto* fix512 = getCpuGenerator(GeneratorType::FIXED512);
  175. if (cpuInfo.hasAvx()) {
  176. floatGen = getCpuGenerator(GeneratorType::FLOAT_AVX);
  177. doubleGen = getCpuGenerator(GeneratorType::DOUBLE_AVX);
  178. }
  179. else if (cpuInfo.hasSse2()) {
  180. floatGen = getCpuGenerator(GeneratorType::FLOAT_SSE2);
  181. doubleGen = getCpuGenerator(GeneratorType::DOUBLE_SSE2);
  182. }
  183. if (cpuInfo.hasAvx2() && cpuInfo.hasFma()) {
  184. floatGen = getCpuGenerator(GeneratorType::FLOAT_AVX_FMA);
  185. doubleGen = getCpuGenerator(GeneratorType::DOUBLE_AVX_FMA);
  186. doubleDoubleGen = getCpuGenerator(GeneratorType::DOUBLE_DOUBLE_AVX_FMA);
  187. quadDoubleGen = getCpuGenerator(GeneratorType::QUAD_DOUBLE_AVX_FMA);
  188. }
  189. if (cpuInfo.hasAvx512()) {
  190. floatGen = getCpuGenerator(GeneratorType::FLOAT_AVX512);
  191. doubleGen = getCpuGenerator(GeneratorType::DOUBLE_AVX512);
  192. }
  193. if (cpuInfo.hasNeon()) {
  194. floatGen = getCpuGenerator(GeneratorType::FLOAT_NEON);
  195. doubleGen = getCpuGenerator(GeneratorType::DOUBLE_NEON);
  196. doubleDoubleGen = getCpuGenerator(GeneratorType::DOUBLE_DOUBLE_NEON);
  197. }
  198. if (!devices.empty()) {
  199. auto& device = devices[0];
  200. auto* fGen = device->getGenerator(GeneratorType::FLOAT);
  201. auto* dGen = device->getGenerator(GeneratorType::DOUBLE);
  202. auto* ddGen = device->getGenerator(GeneratorType::DOUBLE_DOUBLE);
  203. auto* qdGen = device->getGenerator(GeneratorType::QUAD_DOUBLE);
  204. if (fGen)
  205. floatGen = fGen;
  206. if (dGen)
  207. doubleGen = dGen;
  208. if (ddGen)
  209. doubleDoubleGen = ddGen;
  210. if (qdGen)
  211. quadDoubleGen = qdGen;
  212. }
  213. auto ag = std::make_unique<AdaptiveGenerator>();
  214. ag->addGenerator(getPrecision<float>(), *floatGen);
  215. ag->addGenerator(getPrecision<double>(), *doubleGen);
  216. ag->addGenerator(getPrecision<DoubleDouble>(), *doubleDoubleGen);
  217. ag->addGenerator(getPrecision<QuadDouble>(), *quadDoubleGen);
  218. ag->addGenerator(getPrecision<Float256>(), *f256Gen);
  219. ag->addGenerator(Precision::INF_PREC, *fix512);
  220. return ag;
  221. }
  222. std::vector<std::unique_ptr<MandelDevice>> MandelContext::createDevices(void)
  223. {
  224. std::vector<std::unique_ptr<MandelDevice>> mandelDevices;
  225. #ifdef WITH_OPENCL
  226. std::vector<cl::Platform> platforms;
  227. cl::Platform::get(&platforms);
  228. //platforms.erase(platforms.begin() + 1);
  229. for (auto& platform : platforms) {
  230. std::string name = platform.getInfo<CL_PLATFORM_NAME>();
  231. std::string profile = platform.getInfo<CL_PLATFORM_PROFILE>();
  232. //printf("using opencl platform: %s\n", name.c_str());
  233. //std::string ext = platform.getInfo<CL_PLATFORM_EXTENSIONS>();
  234. //printf("Platform extensions: %s\n", ext.c_str());
  235. //printf("Platform: %s, %s\n", name.c_str(), profile.c_str());
  236. std::vector<cl::Device> devices;
  237. platform.getDevices(CL_DEVICE_TYPE_GPU, &devices);
  238. for (auto& device : devices) {
  239. //printf("Device: %s\n", device.getInfo<CL_DEVICE_NAME>().c_str());
  240. //printf("preferred float width: %d\n", device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT>());
  241. //printf("vendor: %s\n", device.getInfo<CL_DEVICE_VENDOR>().c_str());
  242. //printf("Device extensions: %s\n", ext.c_str());
  243. auto mandelDevice = std::make_unique<mnd::MandelDevice>(
  244. ClDeviceWrapper{ device, cl::Context{ device } });
  245. MandelDevice& md = *mandelDevice;
  246. auto supportsDouble = md.supportsDouble();
  247. //printf("clock: %d", device.getInfo<CL_DEVICE_MAX_CLOCK_FREQUENCY>());
  248. //printf(" using opencl device: %s\n", md.name.c_str());
  249. try {
  250. md.mandelGenerators.insert({ GeneratorType::FLOAT, std::make_unique<ClGeneratorFloat>(md) });
  251. md.mandelGenerators.insert({ GeneratorType::FIXED64, std::make_unique<ClGenerator64>(md) });
  252. //md.mandelGenerators.insert({ GeneratorType::FIXED128, std::make_unique<ClGenerator128>(md) });
  253. md.mandelGenerators.insert({ GeneratorType::DOUBLE_FLOAT, std::make_unique<ClGeneratorDoubleFloat>(md) });
  254. }
  255. catch (const std::string& err) {
  256. printf("err: %s", err.c_str());
  257. }
  258. if (supportsDouble) {
  259. try {
  260. md.mandelGenerators.insert({ GeneratorType::DOUBLE, std::make_unique<ClGeneratorDouble>(md) });
  261. md.mandelGenerators.insert({ GeneratorType::DOUBLE_DOUBLE, std::make_unique<ClGeneratorDoubleDouble>(md) });
  262. md.mandelGenerators.insert({ GeneratorType::QUAD_DOUBLE, std::make_unique<ClGeneratorQuadDouble>(md) });
  263. }
  264. catch (const std::string& err) {
  265. printf("err: %s", err.c_str());
  266. fflush(stdout);
  267. }
  268. }
  269. try {
  270. //md.generator128 = std::make_unique<ClGenerator128>(device);
  271. }
  272. catch (const std::string& /*err*/) {
  273. //fprintf(stderr, "error creating 128bit cl generator: %s\n", err.c_str());
  274. }
  275. mandelDevices.push_back(std::move(mandelDevice));
  276. }
  277. }
  278. #endif // WITH_OPENCL
  279. return mandelDevices;
  280. }
  281. MandelContext::~MandelContext(void)
  282. {
  283. }
  284. AdaptiveGenerator& MandelContext::getDefaultGenerator(void)
  285. {
  286. return *adaptiveGenerator;
  287. }
  288. std::vector<std::unique_ptr<mnd::MandelDevice>>& MandelContext::getDevices(void)
  289. {
  290. return devices;
  291. }
  292. asmjit::JitRuntime& MandelContext::getJitRuntime(void)
  293. {
  294. return *jitRuntime;
  295. }
  296. MandelGenerator* MandelContext::getCpuGenerator(mnd::GeneratorType type)
  297. {
  298. auto it = cpuGenerators.find(type);
  299. if (it != cpuGenerators.end())
  300. return it->second.get();
  301. else
  302. return nullptr;
  303. }
  304. std::vector<mnd::GeneratorType> MandelContext::getSupportedTypes(void) const
  305. {
  306. std::vector<GeneratorType> types;
  307. for (auto& [type, gen] : cpuGenerators) {
  308. types.push_back(type);
  309. }
  310. return types;
  311. }