You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.cpp 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright 2017 Tencent
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. #include <iostream>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <vector>
  7. #include <string>
  8. #include <cfloat>
  9. #include "Rcards.h"
  10. //---------------------------------------------------------------------------
  11. using namespace std;
  12. //---------------------------------------------------------------------------
  13. #define REF_BOARD "Raspberry Pi 5 Broadcom BCM2712, Cortex-A76 (ARMv8)"
  14. //---------------------------------------------------------------------------
  15. // Define a custom comparator function for sorting based on Ratio
  16. bool compareByRatio(const TBoard& a, const TBoard& b)
  17. {
  18. return a.Ratio < b.Ratio;
  19. }
  20. //---------------------------------------------------------------------------
  21. int main(int argc, char** argv)
  22. {
  23. size_t i, t, n, r;
  24. int RefBoard;
  25. float f, x;
  26. string Line;
  27. TModel Model;
  28. vector<string> Lines; // Vector to store strings
  29. vector<TBoard> Boards; // Vector to store boards
  30. ifstream inputFile;
  31. // Check existence of the ../README.md file
  32. inputFile.open("../README.md");
  33. if (!inputFile.is_open())
  34. {
  35. if (argc != 2)
  36. {
  37. fprintf(stderr, "Usage: ./RankCards <your README.md> \n");
  38. return -1;
  39. }
  40. const char* imagepath = argv[1];
  41. // Open the file given as argument
  42. inputFile.open(imagepath);
  43. // Check if the file is open
  44. if (!inputFile.is_open())
  45. {
  46. cerr << "Error opening file" << endl;
  47. return 1; // Return an error code
  48. }
  49. }
  50. // Read each Line from the file and add it to the vector
  51. while (std::getline(inputFile, Line))
  52. {
  53. Lines.push_back(Line);
  54. }
  55. // Close the file
  56. inputFile.close();
  57. // Get the boards.
  58. for (i = 0; i < Lines.size(); i++)
  59. {
  60. TBoard Brd;
  61. if (Lines[i].find("###") != string::npos)
  62. {
  63. Brd.Name = Lines[i].substr(4, Lines[i].length() - 4);
  64. Brd.StartLine = i + 1;
  65. Boards.push_back(Brd);
  66. }
  67. }
  68. // Get the boards end Line.
  69. for (t = 0; t < Boards.size() - 1; t++)
  70. {
  71. Boards[t].EndLine = Boards[t + 1].StartLine;
  72. }
  73. Boards[t].EndLine = Lines.size();
  74. // Get the bench sets (must always start with squeezenet)
  75. for (t = 0; t < Boards.size(); t++)
  76. {
  77. TModelSet MdSet;
  78. bool FirstSet = true;
  79. for (n = Boards[t].StartLine; n < Boards[t].EndLine; n++)
  80. {
  81. GetNameAver(Lines[n], Model);
  82. MdSet.Store(Model);
  83. if (Model.Name == "squeezenet")
  84. {
  85. //start of new set, check if it is the first set
  86. if (FirstSet)
  87. FirstSet = false;
  88. else
  89. Boards[t].BenchSet.push_back(MdSet);
  90. }
  91. }
  92. Boards[t].BenchSet.push_back(MdSet);
  93. }
  94. // Get the total AvrTime of the bench sets and set the lowest as best set
  95. for (t = 0; t < Boards.size(); t++)
  96. {
  97. x = FLT_MAX;
  98. for (n = 0; n < Boards[t].BenchSet.size(); n++)
  99. {
  100. f = Boards[t].BenchSet[n].Sum();
  101. if (f < x)
  102. {
  103. x = f;
  104. Boards[t].BestSet = n;
  105. }
  106. }
  107. }
  108. // Get the reference set
  109. RefBoard = -1;
  110. for (t = 0; t < Boards.size(); t++)
  111. {
  112. if (Boards[t].Name.find(REF_BOARD) != string::npos)
  113. {
  114. RefBoard = static_cast<int>(t);
  115. }
  116. }
  117. if (RefBoard == -1)
  118. {
  119. cerr << "Error finding reference board :" << endl;
  120. cerr << REF_BOARD << endl;
  121. return 1; // Return an error code
  122. }
  123. // Get the ratios between the best bench sets and reference
  124. r = Boards[RefBoard].BestSet;
  125. for (t = 0; t < Boards.size(); t++)
  126. {
  127. n = Boards[t].BestSet;
  128. Boards[t].Ratio = Boards[t].BenchSet[n].Ratio(Boards[RefBoard].BenchSet[r]);
  129. }
  130. // Sort the vector using the custom comparator
  131. std::sort(Boards.begin(), Boards.end(), compareByRatio);
  132. // Open an output README.md file
  133. std::ofstream outputFile("README.md");
  134. // Check if the file is successfully opened
  135. if (outputFile.is_open())
  136. {
  137. outputFile << "### Rank the boards." << endl;
  138. outputFile << "The table below is generated by RankCards, using the timings found in the /ncnn/benchmark/README.md file.<br>" << endl;
  139. outputFile << "First, the best set of timings is selected from each board.<br>" << endl;
  140. outputFile << "The set is then compared to a reference set by calculating the ratio of each model one by one and averaging all results.<br>" << endl;
  141. outputFile << "Finally, the boards are ranked from fast to slow.<br>" << endl;
  142. outputFile << "| | Board | Ratio | " << endl;
  143. outputFile << "| :--: | :---- | :--- | " << endl;
  144. // Write the sorted vector to the file
  145. for (t = 0; t < Boards.size(); t++)
  146. {
  147. outputFile << "| " << t + 1 << " | " << Boards[t].Name << " | " << setprecision(3) << Boards[t].Ratio << " | " << endl;
  148. }
  149. // Close the file stream
  150. outputFile.close();
  151. cout << "Sorted data has been written to README.md" << endl;
  152. }
  153. else
  154. {
  155. cerr << "Error opening the file." << endl;
  156. return 1; // Return an error code
  157. }
  158. return 0; // Return success
  159. }
  160. //---------------------------------------------------------------------------