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 6.2 kB

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