benchmarkdialog.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include "benchmarkdialog.h"
  2. #include <chrono>
  3. #include <cmath>
  4. mnd::MandelViewport Benchmarker::benchViewport(void)
  5. {
  6. return mnd::MandelViewport{ -1.250000598933854152929, 0.0001879894057291665530, 0.0000003839916666666565, 0.0000003839916666666565 };
  7. }
  8. const std::vector<mnd::MandelInfo> Benchmarker::benches {
  9. mnd::MandelInfo{ benchViewport(), 50, 50, 250 },
  10. mnd::MandelInfo{ benchViewport(), 50, 50, 500 },
  11. mnd::MandelInfo{ benchViewport(), 50, 100, 500 },
  12. mnd::MandelInfo{ benchViewport(), 100, 100, 500 },
  13. mnd::MandelInfo{ benchViewport(), 100, 100, 1000 },
  14. mnd::MandelInfo{ benchViewport(), 100, 200, 1000 },
  15. mnd::MandelInfo{ benchViewport(), 200, 200, 1000 },
  16. mnd::MandelInfo{ benchViewport(), 200, 200, 2000 },
  17. mnd::MandelInfo{ benchViewport(), 200, 400, 2000 },
  18. mnd::MandelInfo{ benchViewport(), 400, 400, 2000 },
  19. mnd::MandelInfo{ benchViewport(), 400, 400, 4000 },
  20. mnd::MandelInfo{ benchViewport(), 400, 800, 4000 },
  21. mnd::MandelInfo{ benchViewport(), 800, 800, 4000 },
  22. mnd::MandelInfo{ benchViewport(), 800, 800, 8000 },
  23. mnd::MandelInfo{ benchViewport(), 800, 800, 16000 },
  24. mnd::MandelInfo{ benchViewport(), 800, 1600, 16000 },
  25. mnd::MandelInfo{ benchViewport(), 1600, 1600, 16000 },
  26. mnd::MandelInfo{ benchViewport(), 1600, 1600, 32000 },
  27. mnd::MandelInfo{ benchViewport(), 1600, 1600, 64000 },
  28. mnd::MandelInfo{ benchViewport(), 1600, 3200, 64000 },
  29. mnd::MandelInfo{ benchViewport(), 3200, 3200, 64000 },
  30. mnd::MandelInfo{ benchViewport(), 3200, 3200, 128000 },
  31. mnd::MandelInfo{ benchViewport(), 3200, 3200, 256000 },
  32. mnd::MandelInfo{ benchViewport(), 3200, 3200, 512000 },
  33. mnd::MandelInfo{ benchViewport(), 3200, 3200, 1024000 },
  34. mnd::MandelInfo{ benchViewport(), 3200, 3200, 2048000 },
  35. mnd::MandelInfo{ benchViewport(), 3200, 6400, 2048000 },
  36. mnd::MandelInfo{ benchViewport(), 6400, 6400, 2048000 },
  37. mnd::MandelInfo{ benchViewport(), 6400, 6400, 4096000 },
  38. mnd::MandelInfo{ benchViewport(), 6400, 6400, 8192000 },
  39. mnd::MandelInfo{ benchViewport(), 6400, 6400, 16384000 },
  40. mnd::MandelInfo{ benchViewport(), 6400, 6400, 32768000 },
  41. mnd::MandelInfo{ benchViewport(), 6400, 6400, 65536000 },
  42. mnd::MandelInfo{ benchViewport(), 6400, 6400, 131072000 },
  43. mnd::MandelInfo{ benchViewport(), 6400, 6400, 262144000 },
  44. mnd::MandelInfo{ benchViewport(), 6400, 6400, 524288000 },
  45. mnd::MandelInfo{ benchViewport(), 6400, 6400, 1048576000 },
  46. mnd::MandelInfo{ benchViewport(), 6400, 6400, 2097152000 },
  47. };
  48. std::pair<long long, std::chrono::nanoseconds> Benchmarker::measureMips(const std::function<Bitmap<float>*()>& bench) const
  49. {
  50. using namespace std::chrono;
  51. auto before = high_resolution_clock::now();
  52. auto* bitmap = bench();
  53. auto after = high_resolution_clock::now();
  54. long long sum = 0;
  55. for (int i = 0; i < bitmap->width * bitmap->height; i++) {
  56. sum += static_cast<long long>(std::floor(bitmap->pixels[size_t(i)]));
  57. }
  58. return std::make_pair(sum, duration_cast<nanoseconds>(after - before));
  59. }
  60. double Benchmarker::benchmarkResult(mnd::Generator& mg) const
  61. {
  62. size_t testIndex = 0;
  63. for (size_t i = 0; i < benches.size(); i++) {
  64. const mnd::MandelInfo& mi = benches[i];
  65. Bitmap<float> bmp(mi.bWidth, mi.bHeight);
  66. auto [iters, time] = measureMips([&mg, &mi, &bmp]() {
  67. mg.generate(mi, bmp.pixels.get());
  68. return &bmp;
  69. });
  70. if (time > std::chrono::milliseconds(500)) {
  71. testIndex = i + 0;
  72. //printf("testing index %d\n", testIndex);
  73. fflush(stdout);
  74. break;
  75. }
  76. }
  77. const mnd::MandelInfo& mi = benches[(testIndex >= benches.size()) ? (benches.size() - 1) : testIndex];
  78. Bitmap<float> bmp(mi.bWidth, mi.bHeight);
  79. auto [iters, time] = measureMips([&mg, &mi, &bmp]() {
  80. mg.generate(mi, bmp.pixels.get());
  81. return &bmp;
  82. });
  83. return double(iters) / time.count() * 1000;
  84. }
  85. void Benchmarker::start(void)
  86. {
  87. mnd::Generator& cpuf = mndContext.getCpuGeneratorFloat();
  88. mnd::Generator& cpud = mndContext.getCpuGeneratorDouble();
  89. mnd::Generator* cpudd = mndContext.getCpuGeneratorDD();
  90. mnd::Generator* cpuqd = mndContext.getCpuGeneratorQD();
  91. mnd::Generator* cpu128 = mndContext.getCpuGeneratorQuad();
  92. mnd::Generator* cpu256 = mndContext.getCpuGeneratorOct();
  93. double nTests = 2;
  94. if (cpudd)
  95. nTests++;
  96. if (cpuqd)
  97. nTests++;
  98. if (cpu128)
  99. nTests++;
  100. if (cpu256)
  101. nTests++;
  102. auto& devices = mndContext.getDevices();
  103. for (size_t i = 0; i < devices.size(); i++) {
  104. if (mnd::Generator* gpuf; (gpuf = devices[i].getGeneratorFloat())) {
  105. nTests++;
  106. }
  107. if (mnd::Generator* gpud; (gpud = devices[i].getGeneratorDouble())) {
  108. nTests++;
  109. }
  110. if (mnd::Generator* gpudd; (gpudd = devices[i].getGeneratorDoubleDouble())) {
  111. nTests++;
  112. }
  113. }
  114. double progress = 90.0 / nTests;
  115. BenchmarkResult br;
  116. br.values.push_back({});
  117. br.percentage = 10;
  118. emit update(br);
  119. std::vector<double>& cpu = br.values[0];
  120. cpu.push_back(benchmarkResult(cpuf));
  121. br.percentage += progress;
  122. emit update(br);
  123. cpu.push_back(benchmarkResult(cpud));
  124. br.percentage += progress;
  125. emit update(br);
  126. if (cpudd) {
  127. cpu.push_back(benchmarkResult(*cpudd));
  128. br.percentage += progress;
  129. emit update(br);
  130. }
  131. if (cpuqd) {
  132. cpu.push_back(benchmarkResult(*cpuqd));
  133. br.percentage += progress;
  134. emit update(br);
  135. }
  136. if (cpu128) {
  137. cpu.push_back(benchmarkResult(*cpu128));
  138. br.percentage += progress;
  139. emit update(br);
  140. }
  141. if (cpu256) {
  142. cpu.push_back(benchmarkResult(*cpu256));
  143. br.percentage += progress;
  144. emit update(br);
  145. }
  146. for (size_t i = 0; i < devices.size(); i++) {
  147. br.values.push_back({});
  148. std::vector<double>& gpu = br.values[br.values.size() - 1];
  149. if (mnd::Generator* gpuf; (gpuf = devices[i].getGeneratorFloat())) {
  150. gpu.push_back(benchmarkResult(*gpuf));
  151. br.percentage += progress;
  152. emit update(br);
  153. }
  154. if (mnd::Generator* gpud; (gpud = devices[i].getGeneratorDouble())) {
  155. gpu.push_back(benchmarkResult(*gpud));
  156. br.percentage += progress;
  157. emit update(br);
  158. }
  159. if (mnd::Generator* gpudd; (gpudd = devices[i].getGeneratorDoubleDouble())) {
  160. gpu.push_back(benchmarkResult(*gpudd));
  161. br.percentage += progress;
  162. emit update(br);
  163. }
  164. }
  165. emit update(br);
  166. emit finished();
  167. }
  168. BenchmarkDialog::BenchmarkDialog(mnd::MandelContext& mndContext, QWidget *parent) :
  169. QDialog{ parent },
  170. mndContext{ mndContext },
  171. benchmarker{ mndContext }
  172. {
  173. ui.setupUi(this);
  174. printf("bench!\n"); fflush(stdout);
  175. auto& devices = mndContext.getDevices();
  176. size_t nDevices = devices.size() + 1;
  177. ui.tableWidget->setColumnCount(6);
  178. ui.tableWidget->setRowCount(int(nDevices));
  179. ui.tableWidget->setHorizontalHeaderLabels({"Single Precision", "Double Precision", "Double-Double Precision", "Quad-Double Precision", "Quad Precision", "Oct Precision"});
  180. QString cpuDesc = ("CPU [" + mndContext.getCpuInfo().getBrand() + "]").c_str();
  181. ui.tableWidget->setVerticalHeaderItem(0, new QTableWidgetItem(cpuDesc));
  182. for (size_t i = 0; i < devices.size(); i++) {
  183. std::string cpuDescS = std::string("GPU ") + std::to_string(i + 1) + " [" + devices[i].getName().c_str() + "]";
  184. QString cpuDesc = QString::fromLatin1(cpuDescS.c_str());
  185. /*printf("brand [%d]: --> %s <--\n", (int) cpuDescS.size(), cpuDescS.c_str());
  186. for (int x = 0; x < cpuDescS.size(); x++) {
  187. printf("%d\n", cpuDescS[x]);
  188. }
  189. printf("\n");*/
  190. auto label = new QTableWidgetItem(cpuDesc);
  191. label->setStatusTip(QString::fromLatin1(devices[i].getName().c_str()));
  192. ui.tableWidget->setVerticalHeaderItem(int(i + 1), label);
  193. }
  194. qRegisterMetaType<BenchmarkResult>();
  195. benchmarker.moveToThread(&benchThread);
  196. connect(&benchThread, &QThread::started, &benchmarker, &Benchmarker::start);
  197. connect(&benchmarker, SIGNAL (finished()), &benchThread, SLOT (quit()));
  198. connect(&benchmarker, SIGNAL (update(BenchmarkResult)), this, SLOT (update(BenchmarkResult)));
  199. ui.tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
  200. }
  201. void BenchmarkDialog::update(BenchmarkResult br)
  202. {
  203. std::vector<double> cpu = br.values[0];
  204. for (size_t j = 0; j < br.values.size(); j++) {
  205. for (size_t i = 0; i < br.values[j].size(); i++) {
  206. ui.tableWidget->setItem(int(j), int(i), new QTableWidgetItem(QString::number(br.values[j][i])));
  207. }
  208. }
  209. ui.progressBar->setValue(int(br.percentage));
  210. }
  211. void BenchmarkDialog::on_run_clicked()
  212. {
  213. if (!benchThread.isRunning()) {
  214. /*for (int i = 0; i < ui.tableWidget->columnCount(); i++) {
  215. for (int j = 0; j < ui.tableWidget->rowCount(); j++) {
  216. ui.tableWidget->setItem(j, i, new QTableWidgetItem(""));
  217. }
  218. }*/
  219. benchThread.start();
  220. }
  221. // ui.tableWidget->setItem(0, 1, new QTableWidgetItem(benchmarkResult(clg, 4000, 10000)));
  222. }