Optimized Belief Propagation (CPU and GPU)
RunResultsSpeedups.cpp
Go to the documentation of this file.
1 /*
2 Copyright (C) 2024 Scott Grauer-Gray
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
27 #include <iostream>
28 #include <fstream>
29 #include "RunEvalConstsEnums.h"
30 #include "RunResultsSpeedups.h"
31 
32 //constructor that takes in implementation file path and run name and retrieves
33 //run results and speedup evaluation for the run if available
35  const std::filesystem::path& imp_results_file_path,
36  const std::string& run_name) : run_name_{run_name}
37 {
38  std::pair<std::map<std::string, std::vector<std::string>>,
39  std::vector<std::string>>
40  run_results_header_to_data_ordered_headers;
41  //get run results data from file if available
42  if (const auto run_results_fp =
43  imp_results_file_path / run_eval::kImpResultsRunDataFolderName /
44  (std::string(run_name_) + '_' +
46  std::string(run_eval::kCsvFileExtension));
47  ((std::filesystem::exists(run_results_fp))) &&
48  (std::filesystem::is_regular_file(run_results_fp)))
49  {
50  run_results_header_to_data_ordered_headers =
51  HeaderToDataWOrderedHeadersCsv(run_results_fp);
52  }
53 
54  //get speedup evaluation data from file if available
55  if (const auto run_speedup_fp =
56  imp_results_file_path / run_eval::kImpResultsSpeedupsFolderName /
57  (std::string(run_name_) + '_' +
58  std::string(run_eval::kSpeedupsDescFileName) +
59  std::string(run_eval::kCsvFileExtension));
60  ((std::filesystem::exists(run_speedup_fp)) &&
61  (std::filesystem::is_regular_file(run_speedup_fp))))
62  {
63  speedup_header_to_result_speedup_order_ =
64  HeaderToDataWOrderedHeadersCsv(run_speedup_fp);
65  }
66 
67  //generate input signature to data mappings
68  GenInputSignatureToDataMapping(
69  run_results_header_to_data_ordered_headers.first);
70 }
71 
72 //constructor that takes in run results path and processes run results
73 //speedups not available when using this constructor
75  const std::filesystem::path& run_results_file_path)
76 {
77  //get run results data from file if available
78  std::pair<std::map<std::string, std::vector<std::string>>,
79  std::vector<std::string>>
80  run_results_header_to_data_ordered_headers;
81  if ((std::filesystem::exists(run_results_file_path)) &&
82  (std::filesystem::is_regular_file(run_results_file_path)))
83  {
84  run_results_header_to_data_ordered_headers =
85  HeaderToDataWOrderedHeadersCsv(run_results_file_path);
86  }
87 
88  //generate input signature to data mappings
89  GenInputSignatureToDataMapping(
90  run_results_header_to_data_ordered_headers.first);
91 }
92 
93 //generate input sig to run data mappings from run results as read from file
94 void RunResultsSpeedups::GenInputSignatureToDataMapping(
95  const std::optional<std::map<std::string,
96  std::vector<std::string>>>& run_results_header_to_data)
97 {
98  if (run_results_header_to_data)
99  {
100  //initialize input signature to run data mapping
101  input_sig_to_run_data_ = decltype(input_sig_to_run_data_)::value_type();
102 
103  //get input "signature" mapped to run data for each run in run results
104  const unsigned int tot_num_runs =
105  run_results_header_to_data->at(
106  std::string(run_eval::kOptimizedRuntimeHeader)).size();
107  for (unsigned int num_run = 0; num_run < tot_num_runs; num_run++)
108  {
109  //get unique input signature for evaluation run (evaluation data number,
110  //data type, setting of whether to not to have loops with templated
111  //iteration counts)
112  const InputSignature run_input({
113  run_results_header_to_data->at(
114  std::string(run_eval::kRunInputSigHeaders[0])).at(num_run),
115  run_results_header_to_data->at(
116  std::string(run_eval::kRunInputSigHeaders[1])).at(num_run),
117  run_results_header_to_data->at(
118  std::string(run_eval::kRunInputSigHeaders[2])).at(num_run)});
119 
120  //retrieve all data for run corresponding to current input signature
121  //and generate mapping between input signature and run data
122  std::map<std::string, std::string> run_headers_to_data;
123  for (const auto& header_data : *run_results_header_to_data) {
124  run_headers_to_data.insert(
125  {header_data.first, header_data.second.at(num_run)});
126  }
127  input_sig_to_run_data_->insert({run_input, run_headers_to_data});
128  }
129  }
130 }
131 
132 //get mapping of run input signature to value corresponding to input key
133 //for each run result
134 std::map<InputSignature, std::string> RunResultsSpeedups::InputsToKeyVal(
135  std::string_view key) const
136 {
137  std::map<InputSignature, std::string> input_sig_to_key_val;
138  if (input_sig_to_run_data_) {
139  //get input "signature" for run mapped to corresponding key value for each
140  //run on input
141  for (const auto& [input_sig, run_data] : *input_sig_to_run_data_)
142  {
143  //add mapping of key value to corresponding input signature
144  input_sig_to_key_val.insert(
145  {input_sig, run_data.at(std::string(key))});
146  }
147  }
148 
149  return input_sig_to_key_val;
150 }
151 
152 //get mapping of headers to data in csv file for run results and speedups
153 //assumed that there are no commas in data since it is used as delimiter
154 //between data
155 //first output is mapping of headers to results, second output is headers
156 //in order
157 std::pair<std::map<std::string, std::vector<std::string>>,
158  std::vector<std::string>>
159 RunResultsSpeedups::HeaderToDataWOrderedHeadersCsv(
160  const std::filesystem::path& csv_file_path) const
161 {
162  //open data file with comma-separate headers and data
163  std::ifstream csv_file_str(csv_file_path);
164  if (!(csv_file_str.is_open())) {
165  std::cout << "ERROR CREATING STREAM: " << csv_file_path << std::endl;
166  }
167 
168  //initialize containers for header to data mapping and data headers in order
169  std::map<std::string, std::vector<std::string>> header_to_data;
170  std::vector<std::string> data_headers;
171 
172  //get comma-separated data headers in top row and initialize header to data
173  //mapping for each header
174  std::string headers_line;
175  std::getline(csv_file_str, headers_line);
176  std::stringstream headers_str(headers_line);
177  for (std::string header; std::getline(headers_str, header, ',');)
178  {
179  data_headers.push_back(header);
180  header_to_data.insert({header, std::vector<std::string>()});
181  }
182 
183  //go through each data line and add data mapping to each header
184  for (std::string data_line; std::getline(csv_file_str, data_line);)
185  {
186  //get each comma-separated data element and add to mapping to corresponding
187  //header in top row of column
188  for (auto [data_line_str, data, num_data] =
189  std::make_tuple(std::stringstream(data_line), std::string(), 0u);
190  std::getline(data_line_str, data, ',');)
191  {
192  header_to_data.at(data_headers[num_data++]).push_back(data);
193  }
194  }
195 
196  //return mapping of headers to data and data headers in order
197  return {header_to_data, data_headers};
198 }
Contains namespace with enums and constants for implementation run evaluation.
Declares class to load and store run results data including speedups from evaluation.
Class defines input signature for evaluation run that contains evaluation set number,...
RunResultsSpeedups(const std::filesystem::path &imp_results_file_path, const std::string &run_name)
Constructor that takes in implementation file path and run name and retrieves run results and speedup...
std::map< InputSignature, std::string > InputsToKeyVal(std::string_view key) const
Get mapping of run input signature to value corresponding to input key for each run result.
constexpr std::string_view kImpResultsRunDataFolderName
constexpr std::string_view kCsvFileExtension
constexpr std::string_view kSpeedupsDescFileName
constexpr std::string_view kOptimizedRuntimeHeader
constexpr std::array< std::string_view, 3 > kRunInputSigHeaders
constexpr std::string_view kImpResultsSpeedupsFolderName