benchmarkdialog.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. }
  81. double progress = 90.0 / nTests;
  82. BenchmarkResult br;
  83. br.values.push_back({});
  84. br.percentage = 10;
  85. emit update(br);
  86. std::vector<double>& cpu = br.values[0];
  87. cpu.push_back(benchmarkResult(cpuf));
  88. br.percentage += progress;
  89. emit update(br);
  90. cpu.push_back(benchmarkResult(cpud));
  91. br.percentage += progress;
  92. emit update(br);
  93. cpu.push_back(benchmarkResult(cpu128));
  94. br.percentage += progress;
  95. emit update(br);
  96. for (int i = 0; i < devices.size(); i++) {
  97. br.values.push_back({});
  98. std::vector<double>& gpu = br.values[br.values.size() - 1];
  99. if (mnd::Generator* gpuf; gpuf = devices[i].getGeneratorFloat()) {
  100. gpu.push_back(benchmarkResult(*gpuf));
  101. br.percentage += progress;
  102. emit update(br);
  103. }
  104. if (mnd::Generator* gpud; gpud = devices[i].getGeneratorDouble()) {
  105. gpu.push_back(benchmarkResult(*gpud));
  106. br.percentage += progress;
  107. emit update(br);
  108. }
  109. }
  110. printf("benchmark finished\n");
  111. emit update(br);
  112. emit finished();
  113. }
  114. BenchmarkDialog::BenchmarkDialog(mnd::MandelContext& mndContext, QWidget *parent) :
  115. QDialog(parent),
  116. mndContext{ mndContext },
  117. benchmarker{ mndContext }
  118. {
  119. ui.setupUi(this);
  120. auto& devices = mndContext.getDevices();
  121. int nDevices = devices.size() + 1;
  122. ui.tableWidget->setColumnCount(3);
  123. ui.tableWidget->setRowCount(nDevices);
  124. ui.tableWidget->setHorizontalHeaderLabels({"Single Precision", "Double Precision", "128-bit Fixed Point"});
  125. QString cpuDesc = ("CPU [" + mndContext.getCpuInfo().getBrand() + "]").c_str();
  126. ui.tableWidget->setVerticalHeaderItem(0, new QTableWidgetItem(cpuDesc));
  127. for (int i = 0; i < devices.size(); i++) {
  128. std::string cpuDescS = std::string("GPU ") + std::to_string(i + 1) + " [" + devices[i].getName().c_str() + "]";
  129. QString cpuDesc = QString::fromLatin1(cpuDescS.c_str());
  130. /*printf("brand [%d]: --> %s <--\n", (int) cpuDescS.size(), cpuDescS.c_str());
  131. for (int x = 0; x < cpuDescS.size(); x++) {
  132. printf("%d\n", cpuDescS[x]);
  133. }
  134. printf("\n");*/
  135. auto label = new QTableWidgetItem(cpuDesc);
  136. label->setStatusTip(QString::fromLatin1(devices[i].getName().c_str()));
  137. ui.tableWidget->setVerticalHeaderItem(i + 1, label);
  138. }
  139. qRegisterMetaType<BenchmarkResult>();
  140. benchmarker.moveToThread(&benchThread);
  141. connect(&benchThread, &QThread::started, &benchmarker, &Benchmarker::start);
  142. connect(&benchmarker, SIGNAL (finished()), &benchThread, SLOT (quit()));
  143. connect(&benchmarker, SIGNAL (update(BenchmarkResult)), this, SLOT (update(BenchmarkResult)));
  144. }
  145. void BenchmarkDialog::update(BenchmarkResult br)
  146. {
  147. std::vector<double> cpu = br.values[0];
  148. for (int j = 0; j < int(br.values.size()); j++) {
  149. for (int i = 0; i < int(br.values[j].size()); i++) {
  150. ui.tableWidget->setItem(j, i, new QTableWidgetItem(QString::number(br.values[j][i])));
  151. }
  152. }
  153. ui.progressBar->setValue(int(br.percentage));
  154. }
  155. void BenchmarkDialog::on_run_clicked()
  156. {
  157. if (!benchThread.isRunning()) {
  158. /*for (int i = 0; i < ui.tableWidget->columnCount(); i++) {
  159. for (int j = 0; j < ui.tableWidget->rowCount(); j++) {
  160. ui.tableWidget->setItem(j, i, new QTableWidgetItem(""));
  161. }
  162. }*/
  163. benchThread.start();
  164. }
  165. // ui.tableWidget->setItem(0, 1, new QTableWidgetItem(benchmarkResult(clg, 4000, 10000)));
  166. }