1
0

ClGenerators.cpp 11 KB

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