benchmarkdialog.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include "benchmarkdialog.h"
  2. #include <chrono>
  3. #include <cmath>
  4. mnd::MandelViewport Benchmarker::benchViewport(void) const
  5. {
  6. return mnd::MandelViewport{ -0.758267525104592591494, -0.066895616551111110830, 0.000000043217777777655, 0.000000043217777777655 };
  7. }
  8. double Benchmarker::measureMips(const std::function<Bitmap<float>()>& bench) const
  9. {
  10. using namespace std::chrono;
  11. auto before = high_resolution_clock::now();
  12. auto bitmap = bench();
  13. auto after = high_resolution_clock::now();
  14. long long sum = 0;
  15. for (int i = 0; i < bitmap.width * bitmap.height; i++) {
  16. sum += std::floor(bitmap.pixels[size_t(i)]);
  17. }
  18. double iterPerNanos = double(sum) / duration_cast<nanoseconds>(after - before).count();
  19. printf("test took %lld millis\n", duration_cast<milliseconds>(after - before).count());
  20. printf("test did %lld iters\n", sum);
  21. double megaItersPerSecond = iterPerNanos * 1000.0;
  22. return megaItersPerSecond;
  23. }
  24. double Benchmarker::benchmarkResult(mnd::Generator& mg) const
  25. {
  26. // create testbenchmark
  27. mnd::MandelInfo mi;
  28. mi.bWidth = 250;
  29. mi.bHeight = 250;
  30. mi.maxIter = 4000;
  31. mi.view = benchViewport();
  32. double testValue = measureMips([&mg, &mi] () {
  33. Bitmap<float> bmp(mi.bWidth, mi.bHeight);
  34. mg.generate(mi, bmp.pixels.get());
  35. return bmp;
  36. });
  37. printf("testbench: %lf\n", testValue);
  38. std::vector<std::pair<double, mnd::MandelInfo>> benches {
  39. { 200, mnd::MandelInfo{ benchViewport(), 750, 750, 5000} },
  40. { 500, mnd::MandelInfo{ benchViewport(), 2000, 1000, 7500} },
  41. { 2000, mnd::MandelInfo{ benchViewport(), 2000, 2000, 15000} },
  42. { 5000, mnd::MandelInfo{ benchViewport(), 3000, 3000, 30000} },
  43. { 10000, mnd::MandelInfo{ benchViewport(), 4000, 4000, 75000} },
  44. { 100000, mnd::MandelInfo{ benchViewport(), 6000, 6000, 750000} },
  45. { std::numeric_limits<double>::max(), mnd::MandelInfo{ benchViewport(), 7000, 7000, 1000000} }
  46. };
  47. double megaItersPerSecond = 0.0;
  48. if (testValue < 100) {
  49. megaItersPerSecond = testValue;
  50. }
  51. else {
  52. for (auto& [thresh, info] : benches) {
  53. auto& m = info;
  54. if (testValue < thresh) {
  55. megaItersPerSecond = measureMips([&mg, &m] () {
  56. Bitmap<float> bmp(m.bWidth, m.bHeight);
  57. mg.generate(m, bmp.pixels.get());
  58. return bmp;
  59. });
  60. break;
  61. }
  62. }
  63. }
  64. return megaItersPerSecond;
  65. }
  66. void Benchmarker::start(void)
  67. {
  68. mnd::Generator& cpuf = mndContext.getCpuGeneratorFloat();
  69. mnd::Generator& cpud = mndContext.getCpuGeneratorDouble();
  70. mnd::Generator& cpu128 = mndContext.getCpuGenerator128();
  71. double nTests = 3;
  72. auto& devices = mndContext.getDevices();
  73. for (int i = 0; i < devices.size(); i++) {
  74. if (mnd::Generator* gpuf; gpuf = devices[i].getGeneratorFloat()) {
  75. nTests++;
  76. }
  77. if (mnd::Generator* gpud; gpud = devices[i].getGeneratorDouble()) {
  78. nTests++;
  79. }
  80. if (mnd::Generator* gpu128; gpu128 = devices[i].getGenerator128()) {
  81. nTests++;
  82. }
  83. }
  84. double progress = 90.0 / nTests;
  85. BenchmarkResult br;
  86. br.values.push_back({});
  87. br.percentage = 10;
  88. emit update(br);
  89. std::vector<double>& cpu = br.values[0];
  90. cpu.push_back(benchmarkResult(cpuf));
  91. br.percentage += progress;
  92. emit update(br);
  93. cpu.push_back(benchmarkResult(cpud));
  94. br.percentage += progress;
  95. emit update(br);
  96. cpu.push_back(benchmarkResult(cpu128));
  97. br.percentage += progress;
  98. emit update(br);
  99. for (int i = 0; i < devices.size(); i++) {
  100. br.values.push_back({});
  101. std::vector<double>& gpu = br.values[br.values.size() - 1];
  102. if (mnd::Generator* gpuf; gpuf = devices[i].getGeneratorFloat()) {
  103. gpu.push_back(benchmarkResult(*gpuf));
  104. br.percentage += progress;
  105. emit update(br);
  106. }
  107. if (mnd::Generator* gpud; gpud = devices[i].getGeneratorDouble()) {
  108. gpu.push_back(benchmarkResult(*gpud));
  109. br.percentage += progress;
  110. emit update(br);
  111. }
  112. if (mnd::Generator* gpu128; gpu128 = devices[i].getGenerator128()) {
  113. gpu.push_back(benchmarkResult(*gpu128));
  114. br.percentage += progress;
  115. emit update(br);
  116. }
  117. }
  118. printf("benchmark finished\n");
  119. emit update(br);
  120. emit finished();
  121. }
  122. BenchmarkDialog::BenchmarkDialog(mnd::MandelContext& mndContext, QWidget *parent) :
  123. QDialog(parent),
  124. mndContext{ mndContext },
  125. benchmarker{ mndContext }
  126. {
  127. ui.setupUi(this);
  128. auto& devices = mndContext.getDevices();
  129. int nDevices = devices.size() + 1;
  130. ui.tableWidget->setColumnCount(3);
  131. ui.tableWidget->setRowCount(nDevices);
  132. ui.tableWidget->setHorizontalHeaderLabels({"Single Precision", "Double Precision", "128-bit Fixed Point"});
  133. QString cpuDesc = ("CPU [" + mndContext.getCpuInfo().getBrand() + "]").c_str();
  134. ui.tableWidget->setVerticalHeaderItem(0, new QTableWidgetItem(cpuDesc));
  135. for (int i = 0; i < devices.size(); i++) {
  136. std::string cpuDescS = std::string("GPU ") + std::to_string(i + 1) + " [" + devices[i].getName().c_str() + "]";
  137. QString cpuDesc = QString::fromLatin1(cpuDescS.c_str());
  138. /*printf("brand [%d]: --> %s <--\n", (int) cpuDescS.size(), cpuDescS.c_str());
  139. for (int x = 0; x < cpuDescS.size(); x++) {
  140. printf("%d\n", cpuDescS[x]);
  141. }
  142. printf("\n");*/
  143. auto label = new QTableWidgetItem(cpuDesc);
  144. label->setStatusTip(QString::fromLatin1(devices[i].getName().c_str()));
  145. ui.tableWidget->setVerticalHeaderItem(i + 1, label);
  146. }
  147. qRegisterMetaType<BenchmarkResult>();
  148. benchmarker.moveToThread(&benchThread);
  149. connect(&benchThread, &QThread::started, &benchmarker, &Benchmarker::start);
  150. connect(&benchmarker, SIGNAL (finished()), &benchThread, SLOT (quit()));
  151. connect(&benchmarker, SIGNAL (update(BenchmarkResult)), this, SLOT (update(BenchmarkResult)));
  152. }
  153. void BenchmarkDialog::update(BenchmarkResult br)
  154. {
  155. std::vector<double> cpu = br.values[0];
  156. for (int j = 0; j < int(br.values.size()); j++) {
  157. for (int i = 0; i < int(br.values[j].size()); i++) {
  158. ui.tableWidget->setItem(j, i, new QTableWidgetItem(QString::number(br.values[j][i])));
  159. }
  160. }
  161. ui.progressBar->setValue(int(br.percentage));
  162. }
  163. void BenchmarkDialog::on_run_clicked()
  164. {
  165. if (!benchThread.isRunning()) {
  166. /*for (int i = 0; i < ui.tableWidget->columnCount(); i++) {
  167. for (int j = 0; j < ui.tableWidget->rowCount(); j++) {
  168. ui.tableWidget->setItem(j, i, new QTableWidgetItem(""));
  169. }
  170. }*/
  171. benchThread.start();
  172. }
  173. // ui.tableWidget->setItem(0, 1, new QTableWidgetItem(benchmarkResult(clg, 4000, 10000)));
  174. }