ClGenerators.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #include "ClGenerators.h"
  2. #ifdef WITH_OPENCL
  3. #include <iostream>
  4. #include <iterator>
  5. using namespace cl;
  6. using mnd::ClGenerator;
  7. using mnd::ClGeneratorFloat;
  8. using mnd::ClGeneratorDouble;
  9. using mnd::ClGenerator128;
  10. Platform getPlatform() {
  11. /* Returns the first platform found. */
  12. std::vector<Platform> all_platforms;
  13. Platform::get(&all_platforms);
  14. if (all_platforms.size()==0) {
  15. std::cout << "No platforms found. Check OpenCL installation!\n";
  16. exit(1);
  17. }
  18. for (auto& p : all_platforms) {
  19. std::string name = p.getInfo<CL_PLATFORM_NAME>();
  20. std::string profile = p.getInfo<CL_PLATFORM_PROFILE>();
  21. printf("Platform: %s, %s\n", name.c_str(), profile.c_str());
  22. }
  23. return all_platforms[0];
  24. }
  25. Device getDevice(Platform& platform, int i, bool display = false) {
  26. /* Returns the deviced specified by the index i on platform.
  27. * If display is true, then all of the platforms are listed.
  28. */
  29. std::vector<Device> all_devices;
  30. platform.getDevices(CL_DEVICE_TYPE_ALL, &all_devices);
  31. if (all_devices.size() == 0) {
  32. std::cout << "No devices found. Check OpenCL installation!\n";
  33. exit(1);
  34. }
  35. if (display) {
  36. for (::size_t j = 0; j < all_devices.size(); j++) {
  37. printf("Device %d: %s\n", int(j), all_devices[j].getInfo<CL_DEVICE_NAME>().c_str());
  38. printf("preferred float width: %d\n", all_devices[j].getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT>());
  39. printf("vendor: %s\n", all_devices[j].getInfo<CL_DEVICE_VENDOR>().c_str());
  40. }
  41. }
  42. return all_devices[i];
  43. }
  44. ClGenerator::ClGenerator(cl::Device device) :
  45. device{ device }
  46. {
  47. /*Platform p = getPlatform();
  48. device = getDevice(p, 0, true);
  49. context = Context{ device };
  50. Program::Sources sources;
  51. std::string kcode = this->getKernelCode();
  52. sources.push_back({ kcode.c_str(), kcode.length() });
  53. program = Program{ context, sources };
  54. if (program.build({ device }) != CL_SUCCESS) {
  55. std::cout << "Error building: " << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device) << std::endl;
  56. exit(1);
  57. }
  58. queue = CommandQueue(context, device);*/
  59. }
  60. ClGenerator::~ClGenerator(void)
  61. {
  62. queue.flush();
  63. queue.finish();
  64. }
  65. void ClGenerator::generate(const mnd::MandelInfo& info, float* data)
  66. {
  67. ::size_t bufferSize = info.bWidth * info.bHeight * sizeof(float);
  68. Buffer buffer_A(context, CL_MEM_WRITE_ONLY, bufferSize);
  69. float pixelScaleX = info.view.width / info.bWidth;
  70. float pixelScaleY = info.view.height / info.bHeight;
  71. Kernel iterate = Kernel(program, "iterate");
  72. iterate.setArg(0, buffer_A);
  73. iterate.setArg(1, int(info.bWidth));
  74. iterate.setArg(2, float(info.view.x));
  75. iterate.setArg(3, float(info.view.y));
  76. iterate.setArg(4, float(pixelScaleX));
  77. iterate.setArg(5, float(pixelScaleY));
  78. iterate.setArg(6, int(info.maxIter));
  79. // TODO check for overflow
  80. if (false && device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT>() == 4) {
  81. queue.enqueueNDRangeKernel(iterate, 0, NDRange(info.bWidth * info.bHeight / 4));
  82. } else {
  83. queue.enqueueNDRangeKernel(iterate, 0, NDRange(info.bWidth * info.bHeight));
  84. }
  85. queue.enqueueReadBuffer(buffer_A, CL_TRUE, 0, bufferSize, data);
  86. }
  87. ClGeneratorFloat::ClGeneratorFloat(cl::Device device) :
  88. ClGenerator{ device }
  89. {
  90. /*Platform p = getPlatform();
  91. device = getDevice(p, 0, true);*/
  92. context = Context{ device };
  93. Program::Sources sources;
  94. std::string kcode = this->getKernelCode();
  95. sources.push_back({ kcode.c_str(), kcode.length() });
  96. program = Program{ context, sources };
  97. if (program.build({ device }) != CL_SUCCESS) {
  98. throw std::string(program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device));
  99. }
  100. queue = CommandQueue(context, device);
  101. }
  102. std::string ClGeneratorFloat::getKernelCode(void) const
  103. {
  104. if (false && device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT>() == 4) {
  105. return
  106. "__kernel void iterate(__global float* A, const int width, float xl, float yt, float pixelScaleX, float pixelScaleY, int max) {\n"
  107. " int index = get_global_id(0) * 4;\n"
  108. " int x = index % (width);\n"
  109. " int y = index / (width);\n"
  110. " float4 av = (float4)(x * pixelScaleX + xl, (x + 1) * pixelScaleX + xl, (x + 2) * pixelScaleX + xl, (x + 3) * pixelScaleX + xl);\n"
  111. // "(x + 4) * pixelScale + xl, (x + 5) * pixelScale + xl, (x + 6) * pixelScale + xl, (x + 7) * pixelScale + xl);\n"
  112. " float4 bv = (float4)(y * pixelScaleY + yt);\n"
  113. " float4 ca = av;\n"
  114. " float4 cb = bv;\n"
  115. ""
  116. " int4 counter = (int4) 1;"
  117. " float4 threshold = (float4) 16;"
  118. " int n = 0;\n"
  119. " while (n < max) {\n"
  120. " float4 aa = av * av;\n"
  121. " float4 bb = bv * bv;\n"
  122. " float4 ab = av * bv;\n"
  123. " av = aa - bb + ca;\n"
  124. " bv = 2 * ab + cb;\n"
  125. " counter += -(threshold > (aa + bb));\n"
  126. " if(all(threshold < (aa + bb))) break;\n"
  127. " //if (aa + bb > 16) break;\n"
  128. " n++;\n"
  129. " }\n\n"
  130. " A[index] = (float) counter[0];\n"
  131. " A[index + 1] = (float) counter[1];\n"
  132. " A[index + 2] = (float) counter[2];\n"
  133. " A[index + 3] = (float) counter[3];\n"
  134. /* " A[index + 4] = (float) counter[4];\n"
  135. " A[index + 5] = (float) counter[5];\n"
  136. " A[index + 6] = (float) counter[6];\n"
  137. " A[index + 7] = (float) counter[7];\n"*/
  138. // " A[get_global_id(0)] = 1;\n"
  139. "}\n";
  140. }
  141. else {
  142. return
  143. // "#pragma OPENCL EXTENSION cl_khr_fp64 : enable"
  144. "__kernel void iterate(__global float* A, const int width, float xl, float yt, float pixelScaleX, float pixelScaleY, int max) {"
  145. " int index = get_global_id(0);\n"
  146. " int x = index % width;"
  147. " int y = index / width;"
  148. " float a = x * pixelScaleX + xl;"
  149. " float b = y * pixelScaleY + yt;"
  150. " float ca = a;"
  151. " float cb = b;"
  152. ""
  153. " int n = 0;"
  154. " while (n < max - 1) {"
  155. " float aa = a * a;"
  156. " float bb = b * b;"
  157. " float ab = a * b;"
  158. " if (aa + bb > 16) break;"
  159. " a = aa - bb + ca;"
  160. " b = 2 * ab + cb;"
  161. " n++;"
  162. " }\n"
  163. // N + 1 - log (log |Z(N)|) / log 2
  164. " if (n >= max - 1)\n"
  165. " A[index] = max;\n"
  166. " else"
  167. " A[index] = ((float)n) + 1 - log(log(a * a + b * b) / 2) / log(2.0f);\n"
  168. // " A[index] = ((float)n) + 1 - (a * a + b * b - 16) / (256 - 16);\n"
  169. // " A[get_global_id(0)] = 5;"
  170. "}";
  171. }
  172. }
  173. ClGeneratorDouble::ClGeneratorDouble(cl::Device device) :
  174. ClGenerator{ device }
  175. {
  176. context = Context{ device };
  177. Program::Sources sources;
  178. std::string kcode = this->getKernelCode();
  179. sources.push_back({ kcode.c_str(), kcode.length() });
  180. program = Program{ context, sources };
  181. if (program.build({ device }) != CL_SUCCESS) {
  182. throw std::string(program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device));
  183. }
  184. queue = CommandQueue(context, device);
  185. }
  186. void ClGeneratorDouble::generate(const mnd::MandelInfo& info, float* data)
  187. {
  188. ::size_t bufferSize = info.bWidth * info.bHeight * sizeof(float);
  189. Buffer buffer_A(context, CL_MEM_WRITE_ONLY, bufferSize);
  190. float pixelScaleX = info.view.width / info.bWidth;
  191. float pixelScaleY = info.view.height / info.bHeight;
  192. Kernel iterate = Kernel(program, "iterate");
  193. iterate.setArg(0, buffer_A);
  194. iterate.setArg(1, int(info.bWidth));
  195. iterate.setArg(2, double(info.view.x));
  196. iterate.setArg(3, double(info.view.y));
  197. iterate.setArg(4, double(pixelScaleX));
  198. iterate.setArg(5, double(pixelScaleY));
  199. iterate.setArg(6, int(info.maxIter));
  200. queue.enqueueNDRangeKernel(iterate, 0, NDRange(info.bWidth * info.bHeight));
  201. queue.enqueueReadBuffer(buffer_A, CL_TRUE, 0, bufferSize, data);
  202. }
  203. std::string ClGeneratorDouble::getKernelCode(void) const
  204. {
  205. return
  206. "#pragma OPENCL EXTENSION cl_khr_fp64 : enable\n"
  207. "__kernel void iterate(__global float* A, const int width, double xl, double yt, double pixelScaleX, double pixelScaleY, int max) {\n"
  208. " int index = get_global_id(0);\n"
  209. " int x = index % width;"
  210. " int y = index / width;"
  211. " double a = x * pixelScaleX + xl;"
  212. " double b = y * pixelScaleY + yt;"
  213. " double ca = a;"
  214. " double cb = b;"
  215. ""
  216. " int n = 0;"
  217. " while (n < max - 1) {"
  218. " double aa = a * a;"
  219. " double bb = b * b;"
  220. " double ab = a * b;"
  221. " if (aa + bb > 16) break;"
  222. " a = aa - bb + ca;"
  223. " b = 2 * ab + cb;"
  224. " n++;"
  225. " }\n"
  226. // N + 1 - log (log |Z(N)|) / log 2
  227. " if (n >= max - 1)\n"
  228. " A[index] = max;\n"
  229. " else"
  230. " A[index] = ((float)n) + 1 - log(log(a * a + b * b) / 2) / log(2.0f);\n"
  231. // " A[index] = ((float)n) + 1 - (a * a + b * b - 16) / (256 - 16);\n"
  232. // " A[get_global_id(0)] = 5;"
  233. "}";
  234. }
  235. ClGenerator128::ClGenerator128(cl::Device device) :
  236. ClGenerator{ device }
  237. {
  238. context = Context{ device };
  239. Program::Sources sources;
  240. std::string kcode = this->getKernelCode();
  241. sources.push_back({ kcode.c_str(), kcode.length() });
  242. program = Program{ context, sources };
  243. if (program.build({ device }) != CL_SUCCESS) {
  244. throw std::string(program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device));
  245. }
  246. queue = CommandQueue(context, device);
  247. }
  248. void ClGenerator128::generate(const mnd::MandelInfo& info, float* data)
  249. {
  250. ::size_t bufferSize = info.bWidth * info.bHeight * sizeof(float);
  251. Buffer buffer_A(context, CL_MEM_WRITE_ONLY, bufferSize);
  252. float pixelScaleX = info.view.width / info.bWidth;
  253. float pixelScaleY = info.view.height / info.bHeight;
  254. Kernel iterate = Kernel(program, "iterate");
  255. iterate.setArg(0, buffer_A);
  256. iterate.setArg(1, int(info.bWidth));
  257. iterate.setArg(2, double(info.view.x));
  258. iterate.setArg(3, double(info.view.y));
  259. iterate.setArg(4, double(pixelScaleX));
  260. iterate.setArg(5, double(pixelScaleY));
  261. iterate.setArg(6, int(info.maxIter));
  262. queue.enqueueNDRangeKernel(iterate, 0, NDRange(info.bWidth * info.bHeight));
  263. queue.enqueueReadBuffer(buffer_A, CL_TRUE, 0, bufferSize, data);
  264. }
  265. #include <string>
  266. #include <fstream>
  267. #include <streambuf>
  268. std::string ClGenerator128::getKernelCode(void) const
  269. {
  270. //fprintf(stderr, "starting file read\n");
  271. std::ifstream t("mandel128.cl");
  272. std::string str((std::istreambuf_iterator<char>(t)),
  273. std::istreambuf_iterator<char>());
  274. //fprintf(stderr, "%s\n", str);
  275. return str;
  276. }
  277. #endif // WITH_OPENCL