table_helper.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. // Copyright John Maddock 2015.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifdef _MSC_VER
  6. # pragma warning (disable : 4224)
  7. #endif
  8. #include <boost/regex.hpp>
  9. #include <boost/lexical_cast.hpp>
  10. #include <boost/filesystem.hpp>
  11. #include <boost/filesystem/fstream.hpp>
  12. #include <boost/container_hash/hash.hpp>
  13. #include <vector>
  14. #include <set>
  15. #include <iostream>
  16. #include <sstream>
  17. #include <iomanip>
  18. std::vector<std::vector<double> > data;
  19. inline std::string sanitize_string(const std::string& s)
  20. {
  21. static const boost::regex e("[^a-zA-Z0-9]+");
  22. std::string result = boost::regex_replace(s, e, "_");
  23. while(result[0] == '_')
  24. result.erase(0);
  25. return result;
  26. }
  27. inline std::string sanitize_short_string(const std::string& s)
  28. {
  29. unsigned id = boost::hash<std::string>()(s);
  30. return sanitize_string("id" + boost::lexical_cast<std::string>(id));
  31. }
  32. std::string format_precision(double val, int digits)
  33. {
  34. std::stringstream ss;
  35. ss << std::setprecision(digits);
  36. ss << std::fixed;
  37. ss << val;
  38. return ss.str();
  39. }
  40. static std::string content;
  41. boost::filesystem::path path_to_content;
  42. struct content_loader
  43. {
  44. content_loader()
  45. {
  46. boost::filesystem::path p(__FILE__);
  47. p = p.parent_path();
  48. p /= "doc";
  49. p /= "performance_tables.qbk";
  50. path_to_content = p;
  51. if(boost::filesystem::exists(p))
  52. {
  53. boost::filesystem::ifstream is(p);
  54. if(is.good())
  55. {
  56. do
  57. {
  58. char c = static_cast<char>(is.get());
  59. if(c != EOF)
  60. content.append(1, c);
  61. } while(is.good());
  62. }
  63. }
  64. }
  65. ~content_loader()
  66. {
  67. boost::filesystem::ofstream os(path_to_content);
  68. os << content;
  69. }
  70. void instantiate()const
  71. {
  72. }
  73. };
  74. static const content_loader loader;
  75. void load_table(std::vector<std::vector<std::string> >& table, std::string::const_iterator begin, std::string::const_iterator end)
  76. {
  77. static const boost::regex item_e(
  78. "\\["
  79. "([^\\[\\]]*(?0)?)*"
  80. "\\]"
  81. );
  82. boost::regex_token_iterator<std::string::const_iterator> i(begin, end, item_e), j;
  83. while(i != j)
  84. {
  85. // Add a row:
  86. table.push_back(std::vector<std::string>());
  87. boost::regex_token_iterator<std::string::const_iterator> k(i->first + 1, i->second - 1, item_e);
  88. while(k != j)
  89. {
  90. // Add a cell:
  91. table.back().push_back(std::string(k->first + 1, k->second - 1));
  92. ++k;
  93. }
  94. ++i;
  95. }
  96. }
  97. std::string save_table(std::vector<std::vector<std::string> >& table)
  98. {
  99. std::string result;
  100. for(std::vector<std::vector<std::string> >::const_iterator i = table.begin(), j = table.end(); i != j; ++i)
  101. {
  102. result += "[";
  103. for(std::vector<std::string>::const_iterator k = i->begin(), l = i->end(); k != l; ++k)
  104. {
  105. result += "[";
  106. result += *k;
  107. result += "]";
  108. }
  109. result += "]\n";
  110. }
  111. return result;
  112. }
  113. void add_to_all_sections(const std::string& id, std::string list_name = "performance_all_sections")
  114. {
  115. std::string::size_type pos = content.find("[template " + list_name + "[]"), end_pos;
  116. if(pos == std::string::npos)
  117. {
  118. //
  119. // Just append to the end:
  120. //
  121. content.append("\n[template ").append(list_name).append("[]\n[").append(id).append("]\n]\n");
  122. }
  123. else
  124. {
  125. //
  126. // Read in the all list of sections, add our new one (in alphabetical order),
  127. // and then rewrite the whole thing:
  128. //
  129. static const boost::regex item_e(
  130. "\\["
  131. "((?=[^\\]])[^\\[\\]]*+(?0)?+)*+"
  132. "\\]|\\]"
  133. );
  134. boost::regex_token_iterator<std::string::const_iterator> i(content.begin() + pos + 12 + list_name.size(), content.end(), item_e), j;
  135. std::set<std::string> sections;
  136. while(i != j)
  137. {
  138. if(i->length() == 1)
  139. {
  140. end_pos = i->first - content.begin();
  141. break;
  142. }
  143. sections.insert(std::string(i->first + 1, i->second - 1));
  144. ++i;
  145. }
  146. sections.insert(id);
  147. std::string new_list = "\n";
  148. for(std::set<std::string>::const_iterator sec = sections.begin(); sec != sections.end(); ++sec)
  149. {
  150. new_list += "[" + *sec + "]\n";
  151. }
  152. content.replace(pos + 12 + list_name.size(), end_pos - pos - 12 - list_name.size(), new_list);
  153. }
  154. }
  155. std::string get_colour(boost::uintmax_t val, boost::uintmax_t best)
  156. {
  157. if(val <= best * 1.2)
  158. return "green";
  159. if(val > best * 4)
  160. return "red";
  161. return "blue";
  162. }
  163. boost::intmax_t get_value_from_cell(const std::string& cell)
  164. {
  165. static const boost::regex time_e("(\\d+)ns");
  166. boost::smatch what;
  167. if(regex_search(cell, what, time_e))
  168. {
  169. return boost::lexical_cast<boost::uintmax_t>(what.str(1));
  170. }
  171. return -1;
  172. }
  173. void add_cell(boost::intmax_t val, const std::string& table_name, const std::string& row_name, const std::string& column_heading)
  174. {
  175. //
  176. // Load the table, add our data, and re-write:
  177. //
  178. std::string table_id = "table_" + sanitize_string(table_name);
  179. boost::regex table_e("\\[table:" + table_id
  180. + "\\s(?:[^\\[]|\\\\.)++"
  181. "((\\["
  182. "((?:[^\\[\\]]|\\\\.)*+(?2)?+)*+"
  183. "\\]\\s*+)*+\\s*+)"
  184. "\\]"
  185. );
  186. boost::smatch table_location;
  187. if(regex_search(content, table_location, table_e))
  188. {
  189. std::vector<std::vector<std::string> > table_data;
  190. load_table(table_data, table_location[1].first, table_location[1].second);
  191. //
  192. // Figure out which column we're on:
  193. //
  194. unsigned column_id = 1001u;
  195. for(unsigned i = 0; i < table_data[0].size(); ++i)
  196. {
  197. if(table_data[0][i] == column_heading)
  198. {
  199. column_id = i;
  200. break;
  201. }
  202. }
  203. if(column_id > 1000)
  204. {
  205. //
  206. // Need a new column, must be adding a new compiler to the table!
  207. //
  208. table_data[0].push_back(column_heading);
  209. for(unsigned i = 1; i < table_data.size(); ++i)
  210. table_data[i].push_back(std::string());
  211. column_id = table_data[0].size() - 1;
  212. }
  213. //
  214. // Figure out the row:
  215. //
  216. unsigned row_id = 1001;
  217. for(unsigned i = 1; i < table_data.size(); ++i)
  218. {
  219. if(table_data[i][0] == row_name)
  220. {
  221. row_id = i;
  222. break;
  223. }
  224. }
  225. if(row_id > 1000)
  226. {
  227. //
  228. // Need a new row, add it now:
  229. //
  230. table_data.push_back(std::vector<std::string>());
  231. table_data.back().push_back(row_name);
  232. for(unsigned i = 1; i < table_data[0].size(); ++i)
  233. table_data.back().push_back(std::string());
  234. row_id = table_data.size() - 1;
  235. }
  236. //
  237. // Find the best result in this row:
  238. //
  239. boost::uintmax_t best = (std::numeric_limits<boost::uintmax_t>::max)();
  240. std::vector<boost::intmax_t> values;
  241. for(unsigned i = 1; i < table_data[row_id].size(); ++i)
  242. {
  243. if(i == column_id)
  244. {
  245. if(val < best)
  246. best = val;
  247. values.push_back(val);
  248. }
  249. else
  250. {
  251. std::cout << "Existing cell value was " << table_data[row_id][i] << std::endl;
  252. boost::uintmax_t cell_val = get_value_from_cell(table_data[row_id][i]);
  253. std::cout << "Extracted value: " << cell_val << std::endl;
  254. if(cell_val < best)
  255. best = cell_val;
  256. values.push_back(cell_val);
  257. }
  258. }
  259. //
  260. // Update the row:
  261. //
  262. for(unsigned i = 1; i < table_data[row_id].size(); ++i)
  263. {
  264. std::string& s = table_data[row_id][i];
  265. s = "[role ";
  266. if(values[i - 1] < 0)
  267. {
  268. s += "grey -]";
  269. }
  270. else
  271. {
  272. s += get_colour(values[i - 1], best);
  273. s += " ";
  274. s += format_precision(static_cast<double>(values[i - 1]) / best, 2);
  275. s += "[br](";
  276. s += boost::lexical_cast<std::string>(values[i - 1]) + "ns)]";
  277. }
  278. }
  279. //
  280. // Convert back to a string and insert into content:
  281. std::sort(table_data.begin() + 1, table_data.end(), [](std::vector<std::string> const& a, std::vector<std::string> const& b) { return a[0] < b[0]; } );
  282. std::string c = save_table(table_data);
  283. content.replace(table_location.position(1), table_location.length(1), c);
  284. }
  285. else
  286. {
  287. //
  288. // Create a new table and try again:
  289. //
  290. std::string new_table = "\n[template " + table_id;
  291. new_table += "[]\n[table:" + table_id;
  292. new_table += " ";
  293. new_table += table_name;
  294. new_table += "\n[[Expression[br]Text][";
  295. new_table += column_heading;
  296. new_table += "]]\n";
  297. new_table += "[[";
  298. new_table += row_name;
  299. new_table += "][[role blue 1.00[br](";
  300. new_table += boost::lexical_cast<std::string>(val);
  301. new_table += "ns)]]]\n]\n]\n";
  302. std::string::size_type pos = content.find("[/tables:]");
  303. if(pos != std::string::npos)
  304. content.insert(pos + 10, new_table);
  305. else
  306. content += "\n\n[/tables:]\n" + new_table;
  307. //
  308. // Add a section for this table as well:
  309. //
  310. std::string section_id = "section_" + sanitize_short_string(table_name);
  311. if(content.find(section_id + "[]") == std::string::npos)
  312. {
  313. std::string new_section = "\n[template " + section_id + "[]\n[section:" + section_id + " " + table_name + "]\n[" + table_id + "]\n[endsect]\n]\n";
  314. pos = content.find("[/sections:]");
  315. if(pos != std::string::npos)
  316. content.insert(pos + 12, new_section);
  317. else
  318. content += "\n\n[/sections:]\n" + new_section;
  319. add_to_all_sections(section_id);
  320. }
  321. //
  322. // Add to list of all tables (not in sections):
  323. //
  324. add_to_all_sections(table_id, "performance_all_tables");
  325. }
  326. }
  327. void report_execution_time(double t, std::string table, std::string row, std::string heading)
  328. {
  329. try {
  330. add_cell(static_cast<boost::uintmax_t>(t / 1e-9), table, row, heading);
  331. }
  332. catch(const std::exception& e)
  333. {
  334. std::cout << "Error in adding cell: " << e.what() << std::endl;
  335. throw;
  336. }
  337. }
  338. std::string boost_name()
  339. {
  340. return "boost " + boost::lexical_cast<std::string>(BOOST_VERSION / 100000) + "." + boost::lexical_cast<std::string>((BOOST_VERSION / 100) % 1000);
  341. }
  342. std::string compiler_name()
  343. {
  344. #ifdef COMPILER_NAME
  345. return COMPILER_NAME;
  346. #else
  347. return BOOST_COMPILER;
  348. #endif
  349. }
  350. std::string platform_name()
  351. {
  352. #ifdef _WIN32
  353. return "Windows x64";
  354. #else
  355. return BOOST_PLATFORM;
  356. #endif
  357. }
  358. std::string get_compiler_options_name()
  359. {
  360. #if defined(BOOST_MSVC) || defined(__ICL)
  361. std::string result;
  362. #ifdef BOOST_MSVC
  363. result = "cl ";
  364. #else
  365. result = "icl ";
  366. #endif
  367. #ifdef _M_AMD64
  368. #ifdef __AVX__
  369. result += "/arch:AVX /Ox";
  370. #else
  371. result += "/Ox";
  372. #endif
  373. result += " (x64 build)";
  374. #else
  375. #ifdef _DEBUG
  376. result += "/Od";
  377. #elif defined(__AVX2__)
  378. result += "/arch:AVX2 /Ox";
  379. #elif defined(__AVX__)
  380. result += "/arch:AVX /Ox";
  381. #elif _M_IX86_FP == 2
  382. result += "/arch:sse2 /Ox";
  383. #else
  384. result += "/arch:ia32 /Ox";
  385. #endif
  386. result += " (x86 build)";
  387. #endif
  388. std::cout << "Compiler options are found as: " << result << std::endl;
  389. return result;
  390. #else
  391. return "Unknown";
  392. #endif
  393. }