1
0

mandel.cpp 4.2 KB

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