benchmarkdialog.cpp 8.5 KB

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