diff --git a/llvm/lib/Transforms/InsertApproxInfo/InsertApproxInfo.cpp b/llvm/lib/Transforms/InsertApproxInfo/InsertApproxInfo.cpp index cbe9f32ff70f96c3cf0bdca2018c2e02a5bba5b4..6ad8539567dc16ff8f300347f7b897a6b0e75ee0 100644 --- a/llvm/lib/Transforms/InsertApproxInfo/InsertApproxInfo.cpp +++ b/llvm/lib/Transforms/InsertApproxInfo/InsertApproxInfo.cpp @@ -24,6 +24,8 @@ #include <unordered_map> #include <dirent.h> #include <stdio.h> +#include <sstream> +#include <fstream> using namespace llvm; @@ -71,6 +73,8 @@ private: void codeGen(DFInternalNode* N); void codeGen(DFLeafNode* N); void loadTrainedApproxMetrics(std::string dir_path); + void loadMetricsFromFile(std::string dir_path, std::string file_path); + void readApproxValues(const std::string line, ApproxMetrics* approx_metrics); // private data std::unordered_map<std::string, std::vector<ApproxMetrics*>> operation_metrics; @@ -108,11 +112,72 @@ bool InsertApproxInfoWrapperPass::runOnModule(Module &M) { } +void InsertApproxInfo::readApproxValues(const std::string line, ApproxMetrics* approx_metrics){ + + std::istringstream in(line); + std::string op_name; + + float approx_level; + + float mean_l1; + float mean_l2; + float mean_linf; + + float relative_l1; + float relative_l2; + float relative_linf; + + in >> op_name; + in >> approx_level; + + in >> mean_l1; + in >> mean_l2; + in >> mean_linf; + + in >> relative_l1; + in >> relative_l2; + in >> relative_linf; + + + printf("\n *** op_name = %s \n", op_name.c_str()); + printf("approx_level = %f \n", approx_level); + printf("relative_l1 = %f \n", relative_l1); + printf("relative_l2 = %f \n", relative_l2); + printf("relative_linf = %f \n", relative_linf); + printf("mean_l1 = %f \n", mean_l1); + printf("mean_l2 = %f \n", mean_l2); + printf("mean_linf = %f \n", mean_linf); +} + + +void InsertApproxInfo::loadMetricsFromFile(std::string dir_path, std::string file_path){ + + std::string full_path = dir_path + "/" + file_path; + printf("full_path = %s \n", full_path.c_str()); + std::ifstream infile(full_path.c_str()); + std::string line; + + unsigned int it_count = 0; + while(std::getline(infile, line)){ + + // Skip first line with confidence information + if(it_count > 0){ + std::vector<float> approx_values; + ApproxMetrics* approx_metrics = new ApproxMetrics; + readApproxValues(line, approx_metrics); + } + + it_count++; + } + +} + void InsertApproxInfo::loadTrainedApproxMetrics(std::string dir_path){ struct dirent* entry; + dir_path += "/high_confidence"; DIR* dir = opendir(dir_path.c_str()); if(dir == NULL){ printf("Directory %s not found . Aborting ... \n\n ", dir_path.c_str()); @@ -120,7 +185,9 @@ void InsertApproxInfo::loadTrainedApproxMetrics(std::string dir_path){ } while((entry = readdir(dir)) != NULL){ - printf("%s \n", entry->d_name); + printf("f_name = %s \n", entry->d_name); + std::string f_name = entry->d_name; + loadMetricsFromFile(dir_path, f_name); }