Elffuss BraidHub

kikocis/ncnn public

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