mandel.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "Mandel.h"
  2. #include "CpuGenerators.h"
  3. #include "ClGenerators.h"
  4. using mnd::MandelDevice;
  5. using mnd::MandelContext;
  6. using mnd::Generator;
  7. MandelContext mnd::initializeContext(void)
  8. {
  9. MandelContext context = MandelContext();
  10. return context;
  11. }
  12. MandelDevice::MandelDevice(void) :
  13. floatGenerator{ nullptr },
  14. doubleGenerator{ nullptr }
  15. {
  16. }
  17. mnd::Generator* MandelDevice::getGeneratorFloat(void) const
  18. {
  19. if (floatGenerator)
  20. return floatGenerator.get();
  21. else
  22. return nullptr;
  23. }
  24. mnd::Generator* MandelDevice::getGeneratorDouble(void) const
  25. {
  26. if (doubleGenerator)
  27. return doubleGenerator.get();
  28. else
  29. return nullptr;
  30. }
  31. mnd::Generator* MandelDevice::getGenerator128(void) const
  32. {
  33. if (generator128)
  34. return generator128.get();
  35. else
  36. return nullptr;
  37. }
  38. MandelContext::MandelContext(void)
  39. {
  40. #if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86)
  41. if (cpuInfo.hasAvx()) {
  42. cpuGeneratorFloat = std::make_unique<CpuGeneratorAvxFloat>();
  43. cpuGeneratorDouble = std::make_unique<CpuGeneratorAvxDouble>();
  44. }
  45. else if (cpuInfo.hasSse2()) {
  46. cpuGeneratorFloat = std::make_unique<CpuGeneratorSse2Float>();
  47. cpuGeneratorDouble = std::make_unique<CpuGeneratorSse2Double>();
  48. }
  49. else
  50. #endif
  51. {
  52. cpuGeneratorFloat = std::make_unique<CpuGeneratorFloat>();
  53. cpuGeneratorDouble = std::make_unique<CpuGeneratorDouble>();
  54. }
  55. cpuGenerator128 = std::make_unique<CpuGenerator128>();
  56. devices = createDevices();
  57. }
  58. std::vector<MandelDevice> MandelContext::createDevices(void)
  59. {
  60. std::vector<MandelDevice> mandelDevices;
  61. std::vector<cl::Platform> platforms;
  62. cl::Platform::get(&platforms);
  63. //platforms.erase(platforms.begin() + 1);
  64. for (auto& platform : platforms) {
  65. std::string name = platform.getInfo<CL_PLATFORM_NAME>();
  66. std::string profile = platform.getInfo<CL_PLATFORM_PROFILE>();
  67. //std::string ext = platform.getInfo<CL_PLATFORM_EXTENSIONS>();
  68. //printf("Platform extensions: %s\n", ext.c_str());
  69. //printf("Platform: %s, %s\n", name.c_str(), profile.c_str());
  70. std::vector<cl::Device> devices;
  71. platform.getDevices(CL_DEVICE_TYPE_GPU, &devices);
  72. for (auto& device : devices) {
  73. //printf("Device: %s\n", device.getInfo<CL_DEVICE_NAME>().c_str());
  74. //printf("preferred float width: %d\n", device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT>());
  75. //printf("vendor: %s\n", device.getInfo<CL_DEVICE_VENDOR>().c_str());
  76. std::string extensions = device.getInfo<CL_DEVICE_EXTENSIONS>();
  77. auto supportsDouble = extensions.find("cl_khr_fp64") != std::string::npos;
  78. //printf("Device extensions: %s\n", ext.c_str());
  79. MandelDevice md;
  80. //printf("clock: %d", device.getInfo<CL_DEVICE_MAX_CLOCK_FREQUENCY>());
  81. md.name = device.getInfo<CL_DEVICE_NAME>();
  82. md.vendor = device.getInfo<CL_DEVICE_VENDOR>();
  83. try {
  84. md.floatGenerator = std::make_unique<ClGeneratorFloat>(device);
  85. }
  86. catch (const std::string& err) {
  87. printf("err: %s", err.c_str());
  88. }
  89. if (supportsDouble) {
  90. try {
  91. md.doubleGenerator = std::make_unique<ClGeneratorDouble>(device);
  92. }
  93. catch (const std::string& err) {
  94. }
  95. }
  96. try {
  97. //md.generator128 = std::make_unique<ClGenerator128>(device);
  98. }
  99. catch (const std::string& err) {
  100. //fprintf(stderr, "error creating 128bit cl generator: %s\n", err.c_str());
  101. }
  102. mandelDevices.push_back(std::move(md));
  103. }
  104. }
  105. return mandelDevices;
  106. }
  107. const std::string& MandelDevice::getName(void) const
  108. {
  109. return name;
  110. }
  111. Generator& MandelContext::getDefaultGenerator(void)
  112. {
  113. return getCpuGeneratorDouble();
  114. }
  115. const std::vector<MandelDevice>& MandelContext::getDevices(void)
  116. {
  117. return devices;
  118. }
  119. Generator& MandelContext::getCpuGeneratorFloat(void)
  120. {
  121. return *cpuGeneratorFloat;
  122. }
  123. Generator& MandelContext::getCpuGeneratorDouble(void)
  124. {
  125. return *cpuGeneratorDouble;
  126. }
  127. Generator& MandelContext::getCpuGenerator128(void)
  128. {
  129. return *cpuGenerator128;
  130. }