benchmarkdialog.cpp 8.7 KB

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