ClGenerators.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. #include "ClGenerators.h"
  2. #include "OpenClCode.h"
  3. #ifdef WITH_OPENCL
  4. #include <iostream>
  5. #include <iterator>
  6. #include <utility>
  7. using namespace cl;
  8. using mnd::ClGenerator;
  9. using mnd::ClGeneratorFloat;
  10. using mnd::ClGeneratorDoubleFloat;
  11. using mnd::ClGeneratorDouble;
  12. using mnd::ClGeneratorDoubleDouble;
  13. using mnd::ClGeneratorQuadDouble;
  14. using mnd::ClGenerator128;
  15. using mnd::ClGenerator64;
  16. Platform getPlatform() {
  17. /* Returns the first platform found. */
  18. std::vector<Platform> all_platforms;
  19. Platform::get(&all_platforms);
  20. if (all_platforms.size() == 0) {
  21. std::cout << "No platforms found. Check OpenCL installation!\n";
  22. exit(1);
  23. }
  24. for (auto& p : all_platforms) {
  25. std::string name = p.getInfo<CL_PLATFORM_NAME>();
  26. std::string profile = p.getInfo<CL_PLATFORM_PROFILE>();
  27. printf("Platform: %s, %s\n", name.c_str(), profile.c_str());
  28. }
  29. return all_platforms[0];
  30. }
  31. Device getDevice(Platform& platform, int i, bool display = false) {
  32. /* Returns the deviced specified by the index i on platform.
  33. * If display is true, then all of the platforms are listed.
  34. */
  35. std::vector<Device> all_devices;
  36. platform.getDevices(CL_DEVICE_TYPE_ALL, &all_devices);
  37. if (all_devices.size() == 0) {
  38. std::cout << "No devices found. Check OpenCL installation!\n";
  39. exit(1);
  40. }
  41. if (display) {
  42. for (::size_t j = 0; j < all_devices.size(); j++) {
  43. printf("Device %d: %s\n", int(j), all_devices[j].getInfo<CL_DEVICE_NAME>().c_str());
  44. printf("preferred float width: %d\n", all_devices[j].getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT>());
  45. printf("vendor: %s\n", all_devices[j].getInfo<CL_DEVICE_VENDOR>().c_str());
  46. }
  47. }
  48. return all_devices[i];
  49. }
  50. ClGenerator::ClGenerator(cl::Device device, const mnd::Real& precision) :
  51. MandelGenerator{ precision },
  52. device{ device }
  53. {
  54. /*Platform p = getPlatform();
  55. device = getDevice(p, 0, true);
  56. context = Context{ device };
  57. Program::Sources sources;
  58. std::string kcode = this->getKernelCode();
  59. sources.push_back({ kcode.c_str(), kcode.length() });
  60. program = Program{ context, sources };
  61. if (program.build({ device }) != CL_SUCCESS) {
  62. std::cout << "Error building: " << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device) << std::endl;
  63. exit(1);
  64. }
  65. queue = CommandQueue(context, device);*/
  66. }
  67. ClGenerator::~ClGenerator(void)
  68. {
  69. queue.flush();
  70. queue.finish();
  71. }
  72. void ClGenerator::generate(const mnd::MandelInfo& info, float* data)
  73. {
  74. ::size_t bufferSize = info.bWidth * info.bHeight * sizeof(float);
  75. Buffer buffer_A(context, CL_MEM_WRITE_ONLY, bufferSize);
  76. float pixelScaleX = float(info.view.width / info.bWidth);
  77. float pixelScaleY = float(info.view.height / info.bHeight);
  78. bool useVec = device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT>() >= 4;
  79. Kernel iterate = Kernel(program, useVec ? "iterate_vec4" : "iterate");
  80. iterate.setArg(0, buffer_A);
  81. iterate.setArg(1, int(info.bWidth));
  82. iterate.setArg(2, float(info.view.x));
  83. iterate.setArg(3, float(info.view.y));
  84. iterate.setArg(4, float(pixelScaleX));
  85. iterate.setArg(5, float(pixelScaleY));
  86. iterate.setArg(6, int(info.maxIter));
  87. iterate.setArg(7, int(info.smooth ? 1 : 0));
  88. iterate.setArg(8, int(info.julia ? 1 : 0));
  89. iterate.setArg(9, float(info.juliaX));
  90. iterate.setArg(10, float(info.juliaY));
  91. if (useVec) {
  92. queue.enqueueNDRangeKernel(iterate, 0, NDRange(info.bWidth * info.bHeight / 4));
  93. } else {
  94. queue.enqueueNDRangeKernel(iterate, 0, NDRange(info.bWidth * info.bHeight));
  95. }
  96. queue.enqueueReadBuffer(buffer_A, CL_TRUE, 0, bufferSize, data);
  97. }
  98. ClGeneratorFloat::ClGeneratorFloat(cl::Device device) :
  99. ClGenerator{ device, mnd::getPrecision<float>() }
  100. {
  101. context = Context{ device };
  102. Program::Sources sources;
  103. std::string kcode = this->getKernelCode(false);
  104. sources.push_back({ kcode.c_str(), kcode.length() });
  105. program = Program{ context, sources };
  106. if (program.build({ device }) != CL_SUCCESS) {
  107. throw std::string(program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device));
  108. }
  109. queue = CommandQueue(context, device);
  110. }
  111. std::string ClGeneratorFloat::getKernelCode(bool smooth) const
  112. {
  113. return mnd::getFloat_cl();
  114. }
  115. ClGeneratorDoubleFloat::ClGeneratorDoubleFloat(cl::Device device) :
  116. ClGenerator{ device, mnd::getPrecision(mnd::Precision::DOUBLE_FLOAT) }
  117. {
  118. context = Context{ device };
  119. Program::Sources sources;
  120. std::string kcode = this->getKernelCode(false);
  121. sources.push_back({ kcode.c_str(), kcode.length() });
  122. program = Program{ context, sources };
  123. if (program.build({ device }) != CL_SUCCESS) {
  124. throw std::string(program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device));
  125. }
  126. queue = CommandQueue(context, device);
  127. }
  128. std::pair<float, float> twoSum(float a, float b) {
  129. float s = a + b;
  130. float v = s - a;
  131. float r = (a - (s - v)) + (b - v);
  132. return { s, r };
  133. }
  134. std::pair<float, float> split(float a) {
  135. float c = (4096 + 1) * a;
  136. float abig = c - a;
  137. float ahi = c - abig;
  138. float alo = a - ahi;
  139. return { ahi, alo };
  140. }
  141. std::pair<float, float> twoProd(float a, float b) {
  142. float x = a * b;
  143. auto aex = split(a);
  144. auto bex = split(b);
  145. float errx = x - (aex.first * bex.first);
  146. float erry = errx - (aex.second * bex.first);
  147. float errz = erry - (aex.first * bex.second);
  148. float y = (aex.second * bex.second) - errz;
  149. return { x, y };
  150. }
  151. std::pair<float, float> add(std::pair<float, float> a, std::pair<float, float> b) {
  152. float r = a.first + b.first;
  153. float s;
  154. if (fabs(a.first) >= fabs(b.first)) {
  155. s = (((a.first - r) + b.first) + b.second) + a.second;
  156. }
  157. else {
  158. s = (((b.first - r) + a.first) + a.second) + b.second;
  159. }
  160. return twoSum(r, s);
  161. }
  162. std::pair<float, float> mul(std::pair<float, float> a, std::pair<float, float> b) {
  163. auto t = twoProd(a.first, b.first);
  164. t.second += ((a.first * b.second) + (a.second * b.first));
  165. return twoSum(t.first, t.second);
  166. }
  167. std::pair<float, float> mulFloat(std::pair<float, float> a, float b) {
  168. std::pair<float, float> t = twoProd(a.first, b);
  169. float t3 = (a.second * b) + t.second;
  170. return twoSum(t.first, t.second);
  171. }
  172. void ClGeneratorDoubleFloat::generate(const mnd::MandelInfo& info, float* data)
  173. {
  174. ::size_t bufferSize = info.bWidth * info.bHeight * sizeof(float);
  175. auto splitDouble = [] (double x) {
  176. /*uint64_t xl = *((uint64_t*)&x);
  177. uint64_t mantissa = xl & 0x000FFFFFFFFFFFFFULL;
  178. uint64_t exp = (xl & 0x7FF0000000000000ULL) >> 53;
  179. bool sign = (xl & 0x1000000000000000ULL) != 0;
  180. uint32_t floathi = exp << 23;*/
  181. float hi = float(x);
  182. float lo = float(x - double(hi));
  183. if (abs(lo) >= 1.0e-10f) {
  184. //printf("hi: %.10ef, lo: %.10ef\n", hi, lo);
  185. //fflush(stdout);
  186. }
  187. return std::pair{ hi, lo };
  188. };
  189. Buffer buffer_A(context, CL_MEM_WRITE_ONLY, bufferSize);
  190. double pixelScX = double(info.view.width / info.bWidth);
  191. double pixelScY = double(info.view.height / info.bHeight);
  192. auto[x1, x2] = splitDouble(double(info.view.x));
  193. auto[y1, y2] = splitDouble(double(info.view.y));
  194. auto[w1, w2] = splitDouble(pixelScX);
  195. auto[h1, h2] = splitDouble(pixelScY);
  196. /*
  197. for (int px = 0; px < info.bWidth; px++) {
  198. for (int py = 0; py < info.bHeight; py++) {
  199. std::pair<float, float> xl = { x1, x2 };
  200. std::pair<float, float> yt = { y1, y2 };
  201. std::pair<float, float> pixelScaleX = { w1, w2 };
  202. std::pair<float, float> pixelScaleY = { h1, h2 };
  203. std::pair<float, float> a = add(mulFloat(pixelScaleX, (float) px), xl); // pixelScaleX * px + xl
  204. std::pair<float, float> b = add(mulFloat(pixelScaleY, (float) py), yt); // pixelScaleY * py + yt
  205. std::pair<float, float> ca = a;
  206. std::pair<float, float> cb = b;
  207. int n = 0;
  208. while (n < info.maxIter - 1) {
  209. std::pair<float, float> aa = mul(a, a);
  210. std::pair<float, float> bb = mul(b, b);
  211. std::pair<float, float> ab = mul(a, b);
  212. if (aa.first + bb.first > 16) break;
  213. std::pair<float, float> minusbb = { -bb.first, -bb.second };
  214. a = add(add(aa, minusbb), ca);
  215. b = add(add(ab, ab), cb);
  216. n++;
  217. }
  218. // N + 1 - log (log |Z(N)|) / log 2
  219. if (n >= info.maxIter - 1)
  220. data[px + py * info.bWidth] = info.maxIter;
  221. else {
  222. if (info.smooth)
  223. data[px + py * info.bWidth] = ((float) n) + 1 - log(log(a.first * a.first + b.first * b.first ) / 2) / log(2.0f);
  224. else
  225. data[px + py * info.bWidth] = ((float)n);
  226. }
  227. }
  228. }
  229. return;
  230. */
  231. Kernel iterate = Kernel(program, "iterate");
  232. iterate.setArg(0, buffer_A);
  233. iterate.setArg(1, int(info.bWidth));
  234. iterate.setArg(2, x1);
  235. iterate.setArg(3, x2);
  236. iterate.setArg(4, y1);
  237. iterate.setArg(5, y2);
  238. iterate.setArg(6, w1);
  239. iterate.setArg(7, w2);
  240. iterate.setArg(8, h1);
  241. iterate.setArg(9, h2);
  242. iterate.setArg(10, int(info.maxIter));
  243. iterate.setArg(11, int(info.smooth ? 1 : 0));
  244. cl_int result = queue.enqueueNDRangeKernel(iterate, 0, NDRange(info.bWidth * info.bHeight));
  245. queue.enqueueReadBuffer(buffer_A, CL_TRUE, 0, bufferSize, data);
  246. }
  247. std::string ClGeneratorDoubleFloat::getKernelCode(bool smooth) const
  248. {
  249. return getDoubleFloat_cl();
  250. }
  251. ClGeneratorDouble::ClGeneratorDouble(cl::Device device) :
  252. ClGenerator{ device, mnd::getPrecision<double>() }
  253. {
  254. context = Context{ device };
  255. Program::Sources sources;
  256. std::string kcode = getDouble_cl();
  257. sources.push_back({ kcode.c_str(), kcode.length() });
  258. program = Program{ context, sources };
  259. if (program.build({ device }) != CL_SUCCESS) {
  260. throw std::string(program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device));
  261. }
  262. queue = CommandQueue(context, device);
  263. }
  264. void ClGeneratorDouble::generate(const mnd::MandelInfo& info, float* data)
  265. {
  266. ::size_t bufferSize = info.bWidth * info.bHeight * sizeof(float);
  267. Buffer buffer_A(context, CL_MEM_WRITE_ONLY, bufferSize);
  268. double pixelScaleX = double(info.view.width / info.bWidth);
  269. double pixelScaleY = double(info.view.height / info.bHeight);
  270. Kernel iterate = Kernel(program, "iterate");
  271. iterate.setArg(0, buffer_A);
  272. iterate.setArg(1, int(info.bWidth));
  273. iterate.setArg(2, double(info.view.x));
  274. iterate.setArg(3, double(info.view.y));
  275. iterate.setArg(4, double(pixelScaleX));
  276. iterate.setArg(5, double(pixelScaleY));
  277. iterate.setArg(6, int(info.maxIter));
  278. iterate.setArg(7, int(info.smooth ? 1 : 0));
  279. cl_int result = queue.enqueueNDRangeKernel(iterate, 0, NDRange(info.bWidth * info.bHeight));
  280. queue.enqueueReadBuffer(buffer_A, CL_TRUE, 0, bufferSize, data);
  281. }
  282. std::string ClGeneratorDouble::getKernelCode(bool smooth) const
  283. {
  284. return
  285. "#pragma OPENCL EXTENSION cl_khr_fp64 : enable\n"
  286. "__kernel void iterate(__global float* A, const int width, double xl, double yt, double pixelScaleX, double pixelScaleY, int max, int smooth) {\n"
  287. " int index = get_global_id(0);\n"
  288. " int x = index % width;"
  289. " int y = index / width;"
  290. " double a = x * pixelScaleX + xl;"
  291. " double b = y * pixelScaleY + yt;"
  292. " double ca = a;"
  293. " double cb = b;"
  294. ""
  295. " int n = 0;"
  296. " while (n < max - 1) {"
  297. " double aa = a * a;"
  298. " double bb = b * b;"
  299. " double ab = a * b;"
  300. " if (aa + bb > 16) break;"
  301. " a = aa - bb + ca;"
  302. " b = ab + ab + cb;"
  303. " n++;"
  304. " }\n"
  305. // N + 1 - log (log |Z(N)|) / log 2
  306. " if (n >= max - 1)\n"
  307. " A[index] = max;\n"
  308. " else {"
  309. " if (smooth != 0)\n"
  310. " A[index] = ((float)n) + 1 - log(log((float)(a * a + b * b)) / 2) / log(2.0f);\n"
  311. " else\n"
  312. " A[index] = ((float)n);\n"
  313. " }"
  314. "}";
  315. }
  316. ClGeneratorDoubleDouble::ClGeneratorDoubleDouble(cl::Device device) :
  317. ClGenerator{ device, mnd::getPrecision<DoubleDouble>() }
  318. {
  319. context = Context{ device };
  320. Program::Sources sources;
  321. std::string kcode = this->getKernelCode(false);
  322. sources.push_back({ kcode.c_str(), kcode.length() });
  323. program = Program{ context, sources };
  324. if (program.build({ device }) != CL_SUCCESS) {
  325. throw std::string(program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device));
  326. }
  327. queue = CommandQueue(context, device);
  328. }
  329. void ClGeneratorDoubleDouble::generate(const mnd::MandelInfo& info, float* data)
  330. {
  331. ::size_t bufferSize = info.bWidth * info.bHeight * sizeof(float);
  332. Buffer buffer_A(context, CL_MEM_WRITE_ONLY, bufferSize);
  333. mnd::DoubleDouble x = mnd::convert<mnd::DoubleDouble>(info.view.x);
  334. mnd::DoubleDouble y = mnd::convert<mnd::DoubleDouble>(info.view.y);
  335. mnd::DoubleDouble psx = mnd::convert<mnd::DoubleDouble>(info.view.width / info.bWidth);
  336. mnd::DoubleDouble psy = mnd::convert<mnd::DoubleDouble>(info.view.height / info.bHeight);
  337. Kernel iterate = Kernel(program, "iterate");
  338. iterate.setArg(0, buffer_A);
  339. iterate.setArg(1, int(info.bWidth));
  340. iterate.setArg(2, x.x[0]);
  341. iterate.setArg(3, x.x[1]);
  342. iterate.setArg(4, y.x[0]);
  343. iterate.setArg(5, y.x[1]);
  344. iterate.setArg(6, psx.x[0]);
  345. iterate.setArg(7, psx.x[1]);
  346. iterate.setArg(8, psy.x[0]);
  347. iterate.setArg(9, psy.x[1]);
  348. iterate.setArg(10, int(info.maxIter));
  349. iterate.setArg(11, int(info.smooth ? 1 : 0));
  350. cl_int result = queue.enqueueNDRangeKernel(iterate, 0, NDRange(info.bWidth * info.bHeight));
  351. queue.enqueueReadBuffer(buffer_A, CL_TRUE, 0, bufferSize, data);
  352. }
  353. std::string ClGeneratorDoubleDouble::getKernelCode(bool smooth) const
  354. {
  355. return getDoubleDouble_cl();
  356. }
  357. ClGeneratorQuadDouble::ClGeneratorQuadDouble(cl::Device device) :
  358. ClGenerator{ device, mnd::getPrecision<QuadDouble>() }
  359. {
  360. context = Context{ device };
  361. Program::Sources sources;
  362. std::string kcode = this->getKernelCode(false);
  363. sources.push_back({ kcode.c_str(), kcode.length() });
  364. program = Program{ context, sources };
  365. if (program.build({ device }) != CL_SUCCESS) {
  366. throw std::string(program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device));
  367. }
  368. queue = CommandQueue(context, device);
  369. }
  370. void ClGeneratorQuadDouble::generate(const mnd::MandelInfo& info, float* data)
  371. {
  372. ::size_t bufferSize = info.bWidth * info.bHeight * sizeof(float);
  373. Buffer buffer_A(context, CL_MEM_WRITE_ONLY, bufferSize);
  374. mnd::QuadDouble x = mnd::convert<mnd::QuadDouble>(info.view.x);
  375. mnd::QuadDouble y = mnd::convert<mnd::QuadDouble>(info.view.y);
  376. mnd::QuadDouble psx = mnd::convert<mnd::QuadDouble>(info.view.width / info.bWidth);
  377. mnd::QuadDouble psy = mnd::convert<mnd::QuadDouble>(info.view.height / info.bHeight);
  378. Kernel iterate = Kernel(program, "iterate");
  379. iterate.setArg(0, buffer_A);
  380. iterate.setArg(1, int(info.bWidth));
  381. iterate.setArg(2, x.x[0]);
  382. iterate.setArg(3, x.x[1]);
  383. iterate.setArg(4, x.x[2]);
  384. iterate.setArg(5, x.x[3]);
  385. iterate.setArg(6, y.x[0]);
  386. iterate.setArg(7, y.x[1]);
  387. iterate.setArg(8, y.x[2]);
  388. iterate.setArg(9, y.x[3]);
  389. iterate.setArg(10, psx.x[0]);
  390. iterate.setArg(11, psx.x[1]);
  391. iterate.setArg(12, psx.x[2]);
  392. iterate.setArg(13, psx.x[3]);
  393. iterate.setArg(14, psy.x[0]);
  394. iterate.setArg(15, psy.x[1]);
  395. iterate.setArg(16, psy.x[2]);
  396. iterate.setArg(17, psy.x[3]);
  397. iterate.setArg(18, int(info.maxIter));
  398. iterate.setArg(19, int(info.smooth ? 1 : 0));
  399. cl_int result = queue.enqueueNDRangeKernel(iterate, 0, NDRange(info.bWidth * info.bHeight));
  400. queue.enqueueReadBuffer(buffer_A, CL_TRUE, 0, bufferSize, data);
  401. }
  402. std::string ClGeneratorQuadDouble::getKernelCode(bool smooth) const
  403. {
  404. return getQuadDouble_cl();
  405. }
  406. ClGenerator128::ClGenerator128(cl::Device device) :
  407. ClGenerator{ device, mnd::getPrecision<Fixed128>() }
  408. {
  409. context = Context{ device };
  410. Program::Sources sources;
  411. std::string kcode = this->getKernelCode(false);
  412. sources.push_back({ kcode.c_str(), kcode.length() });
  413. program = Program{ context, sources };
  414. if (program.build({ device }) != CL_SUCCESS) {
  415. throw std::string(program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device));
  416. }
  417. queue = CommandQueue(context, device);
  418. }
  419. void ClGenerator128::generate(const mnd::MandelInfo& info, float* data)
  420. {
  421. ::size_t bufferSize = info.bWidth * info.bHeight * sizeof(float);
  422. Buffer buffer_A(context, CL_MEM_WRITE_ONLY, bufferSize);
  423. float pixelScaleX = float(info.view.width / info.bWidth);
  424. float pixelScaleY = float(info.view.height / info.bHeight);
  425. using ull = unsigned long long;
  426. ull x1 = ull(double(info.view.x) * 0x100000000ULL);
  427. ull x2 = 0;
  428. ull y1 = ull(double(info.view.y) * 0x100000000ULL);
  429. ull y2 = 0;
  430. ull w1 = ull(double(pixelScaleX) * 0x100000000ULL);
  431. ull w2 = 0;
  432. ull h1 = ull(double(pixelScaleY) * 0x100000000ULL);
  433. ull h2 = 0;
  434. Kernel iterate = Kernel(program, "iterate");
  435. iterate.setArg(0, buffer_A);
  436. iterate.setArg(1, int(info.bWidth));
  437. iterate.setArg(2, ull(x1));
  438. iterate.setArg(3, ull(x2));
  439. iterate.setArg(4, ull(y1));
  440. iterate.setArg(5, ull(y2));
  441. iterate.setArg(6, ull(w1));
  442. iterate.setArg(7, ull(w2));
  443. iterate.setArg(8, ull(h1));
  444. iterate.setArg(9, ull(h2));
  445. iterate.setArg(10, int(info.maxIter));
  446. iterate.setArg(11, int(info.smooth ? 1 : 0));
  447. queue.enqueueNDRangeKernel(iterate, 0, NDRange(info.bWidth * info.bHeight));
  448. queue.enqueueReadBuffer(buffer_A, CL_TRUE, 0, bufferSize, data);
  449. }
  450. std::string ClGenerator128::getKernelCode(bool smooth) const
  451. {
  452. /*//fprintf(stderr, "starting file read\n");
  453. std::ifstream t("mandel128.cl");
  454. std::string str((std::istreambuf_iterator<char>(t)),
  455. std::istreambuf_iterator<char>());
  456. //fprintf(stderr, "%s\n", str);
  457. return str;*/
  458. return getFixed512_cl();
  459. }
  460. ClGenerator64::ClGenerator64(cl::Device device) :
  461. ClGenerator{ device, mnd::getPrecision<Fixed64>() }
  462. {
  463. context = Context{ device };
  464. Program::Sources sources;
  465. std::string kcode = this->getKernelCode(false);
  466. sources.push_back({ kcode.c_str(), kcode.length() });
  467. program = Program{ context, sources };
  468. if (program.build({ device }) != CL_SUCCESS) {
  469. throw std::string(program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device));
  470. }
  471. queue = CommandQueue(context, device);
  472. }
  473. #include "CpuGenerators.h"
  474. void ClGenerator64::generate(const mnd::MandelInfo& info, float* data)
  475. {
  476. ::size_t bufferSize = info.bWidth * info.bHeight * sizeof(float);
  477. Buffer buffer_A(context, CL_MEM_WRITE_ONLY, bufferSize);
  478. float pixelScaleX = float(info.view.width / info.bWidth);
  479. float pixelScaleY = float(info.view.height / info.bHeight);
  480. using ull = unsigned long long;
  481. ull x = ull(::round(double(info.view.x) * (1LL << 48)));
  482. ull y = ull(::round(double(info.view.y) * (1LL << 48)));
  483. ull w = ull(::round(double(pixelScaleX) * (1LL << 48)));
  484. ull h = ull(::round(double(pixelScaleY) * (1LL << 48)));
  485. //x = 0;
  486. //y = 0;
  487. Kernel iterate = Kernel(program, "iterate");
  488. iterate.setArg(0, buffer_A);
  489. iterate.setArg(1, int(info.bWidth));
  490. iterate.setArg(2, ull(x));
  491. iterate.setArg(3, ull(y));
  492. iterate.setArg(4, ull(w));
  493. iterate.setArg(5, ull(h));
  494. iterate.setArg(6, int(info.maxIter));
  495. iterate.setArg(7, int(info.smooth ? 1 : 0));
  496. queue.enqueueNDRangeKernel(iterate, 0, NDRange(info.bWidth * info.bHeight));
  497. queue.enqueueReadBuffer(buffer_A, CL_TRUE, 0, bufferSize, data);
  498. //CpuGenerator<Fixed64> fx;
  499. //fx.generate(info, data);
  500. }
  501. std::string ClGenerator64::getKernelCode(bool smooth) const
  502. {
  503. /*//fprintf(stderr, "starting file read\n");
  504. std::ifstream t("mandel128.cl");
  505. std::string str((std::istreambuf_iterator<char>(t)),
  506. std::istreambuf_iterator<char>());
  507. //fprintf(stderr, "%s\n", str);
  508. return str;*/
  509. return getFixed64_cl();
  510. }
  511. #endif // WITH_OPENCL