From 42342b7712d0678acae07193413b5bda3e1e1840 Mon Sep 17 00:00:00 2001 From: liu___yi <11356862+liu___yi@user.noreply.gitee.com> Date: Sun, 6 Nov 2022 14:37:30 +0800 Subject: [PATCH] modify testeError --- include/tools.hpp | 10 ++- src/main.cpp | 146 +++++++++++++++++++++---------------- src/tools.cpp | 180 +++++++++++++++++++++++++++------------------- 3 files changed, 196 insertions(+), 140 deletions(-) diff --git a/include/tools.hpp b/include/tools.hpp index 8085c85..a6f5b8d 100644 --- a/include/tools.hpp +++ b/include/tools.hpp @@ -23,7 +23,11 @@ public: string getUniqueLabel(); -exprInfo testError(string uniqueLabel, string suffix, vector intervals, vector scales); +vector getIntervals(string interval, const char *split); + +vector getScales(string scale, const char *split); + +exprInfo testError(string uniqueLabel, string suffix, vector &intervals, vector &scales); exprInfo testError(string uniqueLabel, string suffix, double x0Start, double x0End, int scale); @@ -31,9 +35,9 @@ exprInfo testError(string uniqueLabel, string suffix, double x0Start, double x0E exprInfo testError(string uniqueLabel, string suffix, double x0Start, double x0End, double x1Start, double x1End, double x2Start, double x2End, int x0Size, int x1Size, int x2Size); -void geneBoundaryData(string exprStr, string uniqueLabel); +void geneBoundaryData(string uniqueLabel, string suffix); -void geneIntervalData(string exprStr, string unqiueLabel); +void geneIntervalData(string uniqeuLabel, vector &intervals, vector &threholds); vector getIntervalData(); diff --git a/src/main.cpp b/src/main.cpp index daebe47..a3358aa 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,16 +7,15 @@ #include "geneCode.hpp" #include "tools.hpp" - #include #include #include -using std::string; -using std::vector; using std::cin; using std::cout; using std::endl; +using std::string; +using std::vector; #define MAIN_PRECISION 6 #ifndef DOUBLE_PRECISION #define DOUBLE_PRECISION 17 @@ -32,19 +31,19 @@ int main() // tmp files for input and output. ifstream infile; - infile.open("tmpInput.txt", ios::in); - if (!infile.is_open()) - { - cout << "open tmpInput.txt failed"; - exit(EXIT_FAILURE); - } + infile.open("tmpInput.txt", ios::in); + if (!infile.is_open()) + { + cout << "open tmpInput.txt failed"; + exit(EXIT_FAILURE); + } std::ofstream fout; fout.open("./tmpResult.txt"); if (!fout.is_open()) - { - cout << "open tmpResult.txt failed"; - exit(EXIT_FAILURE); - } + { + cout << "open tmpResult.txt failed"; + exit(EXIT_FAILURE); + } int getlineCount = 0; fprintf(stderr, GREEN "ready> " RESET); @@ -52,76 +51,97 @@ int main() // while (getline(infile, inputStr)) // read line from file's input while (getline(cin, inputStr)) // read line from keyboard input { + // only rewrite getlineCount++; - if(getlineCount == 35 || getlineCount == 36 || getlineCount < 0) continue; + if (getlineCount == 35 || getlineCount == 36 || getlineCount < 0) + continue; auto start = std::chrono::high_resolution_clock::now(); - if(inputStr == "exit;" || inputStr == "quit;" || inputStr == "exit" || inputStr == "quit") { + if (inputStr == "exit;" || inputStr == "quit;" || inputStr == "exit" || inputStr == "quit") + { fprintf(stderr, "main: the application is end after inputing '%s'\n", inputStr.c_str()); break; } - if(inputStr.empty()) { + if (inputStr.empty()) + { fprintf(stderr, "WARNIGN: main: please input a exprssion before pressing Enter\n"); fprintf(stderr, GREEN "ready> " RESET); continue; - } else if(inputStr.back() == ';') { + } + else if (inputStr.back() == ';') + { fprintf(stderr, "you do not need to add a ';' after the expression\n"); inputStr.pop_back(); // remove the last char ';' } + + const char *split = " "; + + fprintf(stderr, GREEN "interval> " RESET); + string intervalStr; + getline(cin, intervalStr); + vector intervals = getIntervals(intervalStr, split); + + fprintf(stderr, GREEN "scale> " RESET); + string scaleStr; + getline(cin, scaleStr); + vector scales = getScales(scaleStr, split); + + bool runAllFlag = true; std::chrono::_V2::system_clock::time_point tmp1, tmp2, tmp3; - if(runAllFlag) + if (runAllFlag) { // the whole process - auto uniqueLabel = getUniqueLabel(); - cout << "uniqueLabel: " << uniqueLabel << endl; - - // get the information about the input expr - auto originExpr = ParseExpressionFromString(inputStr); - vector vars; - getVariablesFromExpr(originExpr, vars); - - auto funcNameOrigin = geneOriginCodeKernel(inputStr, vars, uniqueLabel, "origin"); - // auto funcNameOrigin = geneOriginCode(inputStr, uniqueLabel, "origin"); - // auto funcNameHerbie = geneHerbieCode(inputStr, uniqueLabel, "herbie"); - // auto funcNameDaisy = geneDaisyCode(inputStr, uniqueLabel, "daisy"); - auto funcNameMpfr = geneMpfrCode(inputStr, uniqueLabel, vars); - - // TODO: pick the best from origin, herbie, daisy - // pickTheBest(uniqueLabel, 0, 1, 100); - int scale = 256; - vector intervals{3.8, 7.8, -4.5, -0.3, 0.4, 0.9}; - vector scales{scale, scale, scale}; - tmp1 = std::chrono::high_resolution_clock::now(); - testError(uniqueLabel, "origin", intervals, scales); - tmp2 = std::chrono::high_resolution_clock::now(); - // geneBoundaryData(inputStr, uniqueLabel); // matlab - - // geneIntervalData(inputStr, uniqueLabel); - - cout << "=-=-=-=-=-=-=-=-=-=-=-=-= rewrite start =-=-=-=-=-=-=-=-=-=-=-=-=" << endl; - auto exprInfoVector = rewrite(inputStr, uniqueLabel); - tmp3 = std::chrono::high_resolution_clock::now(); - cout << "=-=-=-=-=-=-=-=-=-=-=-=-= rewrite end =-=-=-=-=-=-=-=-=-=-=-=-=" << endl; - auto funcNameFinal = geneFinalCodeKernel(inputStr, uniqueLabel, exprInfoVector, vars); + auto uniqueLabel = getUniqueLabel(); + cout << "uniqueLabel: " << uniqueLabel << endl; + + // get the information about the input expr + auto originExpr = ParseExpressionFromString(inputStr); + vector vars; + getVariablesFromExpr(originExpr, vars); + + auto funcNameOrigin = geneOriginCodeKernel(inputStr, vars, uniqueLabel, "origin"); + // auto funcNameOrigin = geneOriginCode(inputStr, uniqueLabel, "origin"); + // auto funcNameHerbie = geneHerbieCode(inputStr, uniqueLabel, "herbie"); + // auto funcNameDaisy = geneDaisyCode(inputStr, uniqueLabel, "daisy"); + auto funcNameMpfr = geneMpfrCode(inputStr, uniqueLabel, vars); + + // TODO: pick the best from origin, herbie, daisy + // pickTheBest(uniqueLabel, 0, 1, 100); + + tmp1 = std::chrono::high_resolution_clock::now(); + auto infoTmp = testError(uniqueLabel, "origin", intervals, scales); + tmp2 = std::chrono::high_resolution_clock::now(); + + geneBoundaryData(uniqueLabel, "origin"); // matlab + + vector intervalsTemp; + vector threholdsTemp; + geneIntervalData(uniqueLabel, intervalsTemp, threholdsTemp); + + cout << "=-=-=-=-=-=-=-=-=-=-=-=-= rewrite start =-=-=-=-=-=-=-=-=-=-=-=-=" << endl; + auto exprInfoVector = rewrite(inputStr, uniqueLabel); + tmp3 = std::chrono::high_resolution_clock::now(); + cout << "=-=-=-=-=-=-=-=-=-=-=-=-= rewrite end =-=-=-=-=-=-=-=-=-=-=-=-=" << endl; + auto funcNameFinal = geneFinalCodeKernel(inputStr, uniqueLabel, exprInfoVector, vars); } // the whole process end else { // only rewrite - auto results = exprAutoWrapper(inputStr); - - // write the results to file - fout << "-------------------------------------NO." << getlineCount <<": " << inputStr << endl; - for(size_t i = 0; i < results.size(); i++) - { - auto &result = results.at(i); - // string treeStr; - // printAST(result, treeStr, MAIN_PRECISION); - fout << PrintExpression(result, MAIN_PRECISION) << endl; - // fout << "*resultPtr = " << PrintExpression(result, DOUBLE_PRECISION) << ";" << endl; - // fout << treeStr << endl; - } - fout << endl; + auto results = exprAutoWrapper(inputStr); + + // write the results to file + fout << "-------------------------------------NO." << getlineCount << ": " << inputStr << endl; + for (size_t i = 0; i < results.size(); i++) + { + auto &result = results.at(i); + // string treeStr; + // printAST(result, treeStr, MAIN_PRECISION); + fout << PrintExpression(result, MAIN_PRECISION) << endl; + // fout << "*resultPtr = " << PrintExpression(result, DOUBLE_PRECISION) << ";" << endl; + // fout << treeStr << endl; + } + fout << endl; } // only rewrite end auto end = std::chrono::high_resolution_clock::now(); diff --git a/src/tools.cpp b/src/tools.cpp index 27d2960..cf453f8 100644 --- a/src/tools.cpp +++ b/src/tools.cpp @@ -20,7 +20,7 @@ using std::flush; using std::string; using std::to_string; -//获取唯一标识符(时间戳) +//get unique label(timestamp) string getUniqueLabel() { time_t currentTime = time(NULL); @@ -30,6 +30,48 @@ string getUniqueLabel() return uniqueLabel; } +//get init interval info +vector getIntervals(string interval, const char *split) +{ + char *p; + char a[interval.length() + 1]; + int i; + for (i = 0; i < interval.length(); i++) + { + a[i] = interval[i]; + } + a[i] = '\0'; + vector intervalVector; + p = strtok(a, split); + while (p != NULL) + { + intervalVector.push_back(atof(p)); + p = strtok(NULL, split); + } + return intervalVector; +} + +//get init scale info +vector getScales(string scale, const char *split) +{ + char *q; + char b[scale.length() + 1]; + int i; + for (i = 0; i < scale.length(); i++) + { + b[i] = scale[i]; + } + b[i] = '\0'; + vector scaleVector; + q = strtok(b, split); + while (q != NULL) + { + scaleVector.push_back(stoi(q)); + q = strtok(NULL, split); + } + return scaleVector; +} + // TODO: set the return value as a exprInfo object // test error exprInfo testError(string uniqueLabel, string suffix, double x0Start, double x0End, int x0Size) @@ -38,7 +80,7 @@ exprInfo testError(string uniqueLabel, string suffix, double x0Start, double x0E os1 << x0Start; std::ostringstream os2; os2 << x0End; - + std::ostringstream os7; os7 << x0Size; @@ -46,7 +88,7 @@ exprInfo testError(string uniqueLabel, string suffix, double x0Start, double x0E string middle = os1.str() + "_" + os2.str() + "_" + os7.str(); string fileNameKernel = prefix + "__" + middle + "_" + suffix; string testName = "./outputs/" + fileNameKernel + "_error.txt"; - string scriptName = "./detectErrorOne.sh"; + string scriptName = "./detectErrorOneParallel.sh"; string commandStr = scriptName + " " + uniqueLabel + " " + os1.str() + " " + os2.str() + " " + os7.str() + " " + prefix + " " + middle + " " + suffix; cout << "fileNameKernel: " << fileNameKernel << "\ncommand: " << commandStr << "\ntestName: " << testName << endl; char command[200] = {0}; @@ -57,7 +99,7 @@ exprInfo testError(string uniqueLabel, string suffix, double x0Start, double x0E double aveError = 0; double maxError = 0; exprInfo tempError; - + char ch; ifs >> ch; if (ifs.eof()) @@ -77,11 +119,11 @@ exprInfo testError(string uniqueLabel, string suffix, double x0Start, double x0E std::ifstream ifs(testName, std::ios::in); std::string lineStr; - std::getline(ifs, lineStr); // discard first line + std::getline(ifs, lineStr); // discard first line std::getline(ifs, lineStr); // get the second line - char *line = (char *)calloc(lineStr.length(), sizeof(char)); + char *line = (char *)calloc(lineStr.length(), sizeof(char)); strcpy(line, lineStr.c_str()); - const char *delim = " ,\t"; // Sets the delimiter for the string to be decomposed + const char *delim = " ,\t"; // Sets the delimiter for the string to be decomposed string aveErrorTemp = strtok(line, delim); string maxErrorTemp = strtok(NULL, delim); // cout << "aveError: " << aveErrorTemp << "\tmaxError: " << maxErrorTemp << endl; @@ -108,13 +150,12 @@ exprInfo testError(string uniqueLabel, string suffix, double x0Start, double x0E os3 << x1Start; std::ostringstream os4; os4 << x1End; - + std::ostringstream os7; os7 << x0Size; std::ostringstream os8; os8 << x1Size; - string prefix = "expr_" + uniqueLabel; string middle = os1.str() + "_" + os2.str() + "_" + os3.str() + "_" + os4.str() + "_" + os7.str() + "_" + os8.str(); string fileNameKernel = prefix + "__" + middle + "_" + suffix; @@ -130,7 +171,7 @@ exprInfo testError(string uniqueLabel, string suffix, double x0Start, double x0E double aveError = 0; double maxError = 0; exprInfo tempError; - + char ch; ifs >> ch; if (ifs.eof()) @@ -152,11 +193,11 @@ exprInfo testError(string uniqueLabel, string suffix, double x0Start, double x0E std::ifstream ifs(testName, std::ios::in); std::string lineStr; - std::getline(ifs, lineStr); // discard first line + std::getline(ifs, lineStr); // discard first line std::getline(ifs, lineStr); // get the second line - char *line = (char *)calloc(lineStr.length(), sizeof(char)); + char *line = (char *)calloc(lineStr.length(), sizeof(char)); strcpy(line, lineStr.c_str()); - const char *delim = " ,\t"; // Sets the delimiter for the string to be decomposed + const char *delim = " ,\t"; // Sets the delimiter for the string to be decomposed string aveErrorTemp = strtok(line, delim); string maxErrorTemp = strtok(NULL, delim); // cout << "aveError: " << aveErrorTemp << "\tmaxError: " << maxErrorTemp << endl; @@ -211,7 +252,7 @@ exprInfo testError(string uniqueLabel, string suffix, double x0Start, double x0E double aveError = 0; double maxError = 0; exprInfo tempError; - + char ch; ifs >> ch; if (ifs.eof()) @@ -235,11 +276,11 @@ exprInfo testError(string uniqueLabel, string suffix, double x0Start, double x0E std::ifstream ifs(testName, std::ios::in); std::string lineStr; - std::getline(ifs, lineStr); // discard first line + std::getline(ifs, lineStr); // discard first line std::getline(ifs, lineStr); // get the second line - char *line = (char *)calloc(lineStr.length(), sizeof(char)); + char *line = (char *)calloc(lineStr.length(), sizeof(char)); strcpy(line, lineStr.c_str()); - const char *delim = " ,\t"; // Sets the delimiter for the string to be decomposed + const char *delim = " ,\t"; // Sets the delimiter for the string to be decomposed string aveErrorTemp = strtok(line, delim); string maxErrorTemp = strtok(NULL, delim); // cout << "aveError: " << aveErrorTemp << "\tmaxError: " << maxErrorTemp << endl; @@ -260,37 +301,30 @@ exprInfo testError(string uniqueLabel, string suffix, double x0Start, double x0E return tempError; } -exprInfo testError(string uniqueLabel, string suffix, vector intervals, vector scales) +exprInfo testError(string uniqueLabel, string suffix, vector &intervals, vector &scales) { exprInfo tempError; size_t size = scales.size(); size = 1; - switch(size) // choose the test error version according to the input parameters number + + switch (size) { - case 1: - { - int scale = 500000; - tempError = testError(uniqueLabel, suffix, -1.57079632679, 1.57079632679, scale); // for 1 param - break; - } - case 2: - { - int scale = 1024; - tempError = testError(uniqueLabel, suffix, 0, 1, 0, 1, scale, scale); // for 2 params - break; - } - case 3: - { - int scale = 256; - tempError = testError(uniqueLabel, suffix, 3.8, 7.8, -4.5, -0.3, 0.4, 0.9, scale, scale, scale); // for 3 params - break; - } - default: - { - fprintf(stderr, "WRONG: main: the variables number is %ld, which we don't support now.\n", size); - exit(EXIT_FAILURE); - break; - } + case 1: + tempError = testError(uniqueLabel, suffix, intervals.at(0), intervals.at(1), scales.at(0)); + break; + + case 2: + tempError = testError(uniqueLabel, suffix, intervals.at(0), intervals.at(1), intervals.at(2), intervals.at(3), scales.at(0), scales.at(1)); + break; + + case 3: + tempError = testError(uniqueLabel, suffix, intervals.at(0), intervals.at(1), intervals.at(2), intervals.at(3), intervals.at(4), intervals.at(5), scales.at(0), scales.at(1), scales.at(2)); + break; + + default: + fprintf(stderr, "WRONG: rewrite: the intervalTmp's dimension is %ld, which we don't support now.\n", size); + exit(EXIT_FAILURE); + break; } return tempError; @@ -298,9 +332,10 @@ exprInfo testError(string uniqueLabel, string suffix, vector intervals, // TODO: implement // call matlab to generate the boundaryData to file -void geneBoundaryData(string exprStr, string uniqueLabel) +void geneBoundaryData(string uniqueLabel, string suffix) { - string filename = "expr_" + uniqueLabel + "_boudary.txt"; + suffix = "origin"; + string filename = "expr_" + uniqueLabel + "_" + suffix + "_boudary.txt"; std::ofstream ofs; ofs.open(filename, std::ios::out); ofs << "123" << std::endl; @@ -310,14 +345,28 @@ void geneBoundaryData(string exprStr, string uniqueLabel) // TODO: improve // generate the interval info to file -void geneIntervalData(string exprStr, string uniqueLabel) +void geneIntervalData(string uniqueLabel, vector &intervals, vector &threholds) { - string filename = "expr_" + uniqueLabel + "_interval.txt"; - std::ofstream ofs; - ofs.open(filename, std::ios::out); - ofs << "123" << std::endl; - ofs.close(); - std::cout << "generate boundaryData file:" << filename << std::endl; + // filename = "expr_" + uniqueLabel + "_interval.txt"; + for (int j = 1; j < threholds.size(); j++) + { + intervals.push_back(to_string(j * 2)); + intervals.push_back(to_string(j * 2 + 1)); + threholds.push_back(j + 0.0002); + } + for (int i = 0; i < threholds.size(); i++) + { + string fileName; + string prefix = "./outputs/expr_" + uniqueLabel; + string middle = "_" + intervals.at(i * 2) + "_" + intervals.at(i * 2 + 1) + "_" + to_string(threholds.at(i)); + string suffix = "_interval.txt"; + fileName = prefix + middle + suffix; + std::ofstream ofs; + ofs.open(fileName, std::ios::out); + ofs << "123" << std::endl; + ofs.close(); + std::cout << "generate boudaryDate file:" << fileName << std::endl; + } } //生成区间数据 @@ -398,7 +447,7 @@ vector rewrite(string exprStr, string uniqueLabel) scale = 256; else scale = 10; - + vector scales(dimension, scale); string suffix = "temp_" + std::to_string(count) + "_"; // pick the best rewrite expression @@ -416,25 +465,7 @@ vector rewrite(string exprStr, string uniqueLabel) string suffixTmp = suffix + std::to_string(j); geneOriginCode(newExpr, uniqueLabel, suffixTmp); exprInfo tempError; - switch (dimension) - { - case 1: - tempError = testError(uniqueLabel, suffixTmp, intervalTmp.at(0), intervalTmp.at(1), scale); - break; - - case 2: - tempError = testError(uniqueLabel, suffixTmp, intervalTmp.at(0), intervalTmp.at(1), intervalTmp.at(2), intervalTmp.at(3), scale, scale); - break; - - case 3: - tempError = testError(uniqueLabel, suffixTmp, intervalTmp.at(0), intervalTmp.at(1), intervalTmp.at(2), intervalTmp.at(3), intervalTmp.at(4), intervalTmp.at(5), scale, scale, scale); - break; - - default: - fprintf(stderr, "WRONG: rewrite: the intervalTmp's dimension is %ld, which we don't support now.\n", dimension); - exit(EXIT_FAILURE); - break; - } + tempError = testError(uniqueLabel, suffixTmp, intervalTmp, scales); if (j == 0) { maxError = tempError.maxError; @@ -453,7 +484,8 @@ vector rewrite(string exprStr, string uniqueLabel) } } - cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-\n" << endl; + cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-\n" + << endl; } exprInfo tempInfo; @@ -466,7 +498,7 @@ vector rewrite(string exprStr, string uniqueLabel) tempInfo.maxError = maxError; tempInfo.performance = 0.2; tempInfo.rewriteID = maxIdx; - + exprInfoVector.push_back(tempInfo); count++; -- Gitee