mandel.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. MandelContext::MandelContext(void)
  32. {
  33. if (cpuInfo.hasAvx()) {
  34. cpuGeneratorFloat = std::make_unique<CpuGeneratorAvxFloat>();
  35. cpuGeneratorDouble = std::make_unique<CpuGeneratorAvxDouble>();
  36. }
  37. else if (cpuInfo.hasSse2()) {
  38. cpuGeneratorFloat = std::make_unique<CpuGeneratorSse2Float>();
  39. cpuGeneratorDouble = std::make_unique<CpuGeneratorSse2Double>();
  40. }
  41. else {
  42. cpuGeneratorFloat = std::make_unique<CpuGeneratorFloat>();
  43. cpuGeneratorDouble = std::make_unique<CpuGeneratorDouble>();
  44. }
  45. cpuGenerator128 = std::make_unique<CpuGenerator128>();
  46. devices = createDevices();
  47. }
  48. std::vector<MandelDevice> MandelContext::createDevices(void)
  49. {
  50. std::vector<MandelDevice> mandelDevices;
  51. std::vector<cl::Platform> platforms;
  52. cl::Platform::get(&platforms);
  53. platforms.erase(platforms.begin() + 1);
  54. for (auto& platform : platforms) {
  55. std::string name = platform.getInfo<CL_PLATFORM_NAME>();
  56. std::string profile = platform.getInfo<CL_PLATFORM_PROFILE>();
  57. std::string ext = platform.getInfo<CL_PLATFORM_EXTENSIONS>();
  58. printf("Platform extensions: %s\n", ext.c_str());
  59. printf("Platform: %s, %s\n", name.c_str(), profile.c_str());
  60. std::vector<cl::Device> devices;
  61. platform.getDevices(CL_DEVICE_TYPE_GPU, &devices);
  62. for (auto& device : devices) {
  63. //printf("Device: %s\n", device.getInfo<CL_DEVICE_NAME>().c_str());
  64. //printf("preferred float width: %d\n", device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT>());
  65. //printf("vendor: %s\n", device.getInfo<CL_DEVICE_VENDOR>().c_str());
  66. std::string extensions = device.getInfo<CL_DEVICE_EXTENSIONS>();
  67. auto supportsDouble = extensions.find("cl_khr_fp64") != std::string::npos;
  68. printf("Device extensions: %s\n", ext.c_str());
  69. MandelDevice md;
  70. printf("clock: %d", device.getInfo<CL_DEVICE_MAX_CLOCK_FREQUENCY>());
  71. md.name = device.getInfo<CL_DEVICE_NAME>();
  72. md.vendor = device.getInfo<CL_DEVICE_VENDOR>();
  73. try {
  74. md.floatGenerator = std::make_unique<ClGeneratorFloat>(device);
  75. }
  76. catch (const std::string& err) {
  77. printf("err: %s", err.c_str());
  78. }
  79. if (supportsDouble) {
  80. try {
  81. md.doubleGenerator = std::make_unique<ClGeneratorDouble>(device);
  82. }
  83. catch (const std::string& err) {
  84. }
  85. }
  86. mandelDevices.push_back(std::move(md));
  87. }
  88. }
  89. return mandelDevices;
  90. }
  91. const std::string& MandelDevice::getName(void) const
  92. {
  93. return name;
  94. }
  95. Generator& MandelContext::getDefaultGenerator(void)
  96. {
  97. return getCpuGeneratorDouble();
  98. }
  99. const std::vector<MandelDevice>& MandelContext::getDevices(void)
  100. {
  101. return devices;
  102. }
  103. Generator& MandelContext::getCpuGeneratorFloat(void)
  104. {
  105. return *cpuGeneratorFloat;
  106. }
  107. Generator& MandelContext::getCpuGeneratorDouble(void)
  108. {
  109. return *cpuGeneratorDouble;
  110. }
  111. Generator& MandelContext::getCpuGenerator128(void)
  112. {
  113. return *cpuGenerator128;
  114. }