From 4849f7e564abf1deb898b292020d278453c50b3f Mon Sep 17 00:00:00 2001 From: hjw Date: Sat, 5 Nov 2022 10:52:32 +0800 Subject: [PATCH 1/3] Solve the problem of insufficient significant digits in mpfrCodeGenerator --- src/basic.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/basic.cpp b/src/basic.cpp index 36c71f9..316f2f1 100644 --- a/src/basic.cpp +++ b/src/basic.cpp @@ -579,7 +579,9 @@ string mpfrCodeGenerator(const ast_ptr &expr, size_t &mpfr_variables, const std: ++mpfr_variables; NumberExprAST *numberExpr = dynamic_cast(expr.get()); double number = (numberExpr->getNumber()); - number_str = "mpfr_set_d(mp" + to_string(mpfr_variables) + ", " + to_string(number) + ", MPFR_RNDN);"; + std::stringstream ss; + ss << std::setprecision(DOUBLE_PRECISION) << number; + number_str = "mpfr_set_d(mp" + to_string(mpfr_variables) + ", " + ss.str() + ", MPFR_RNDN);"; // cout << number_str << endl; ofs << "\t" << number_str << endl; return "mp" + to_string(mpfr_variables); -- Gitee From 48eb66c22e40396f503233f2ae3b77016588d76a Mon Sep 17 00:00:00 2001 From: hjw Date: Sat, 5 Nov 2022 10:54:57 +0800 Subject: [PATCH 2/3] Fix the problem of duplicate counting variables in getVariablesFromExpr --- src/geneCode.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/geneCode.cpp b/src/geneCode.cpp index e9d4567..9e01aed 100644 --- a/src/geneCode.cpp +++ b/src/geneCode.cpp @@ -96,6 +96,8 @@ bool getVariablesFromExpr(const ast_ptr &expr, vector &vars) exit(EXIT_FAILURE); } } + std::sort(vars.begin(), vars.end()); // sort to unique the parameters queue + vars.erase(std::unique(vars.begin(), vars.end()), vars.end()); return true; } if (type != "Binary") -- Gitee From 2d5747b65b4608965b1176d0c53d440b30781da2 Mon Sep 17 00:00:00 2001 From: hjw Date: Sat, 5 Nov 2022 10:56:51 +0800 Subject: [PATCH 3/3] Add timing function in main function and improve code details --- src/main.cpp | 39 +++++++++++---------------------------- 1 file changed, 11 insertions(+), 28 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 3f42a75..daebe47 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -70,7 +70,8 @@ int main() fprintf(stderr, "you do not need to add a ';' after the expression\n"); inputStr.pop_back(); // remove the last char ';' } - bool runAllFlag = false; + bool runAllFlag = true; + std::chrono::_V2::system_clock::time_point tmp1, tmp2, tmp3; if(runAllFlag) { // the whole process auto uniqueLabel = getUniqueLabel(); @@ -92,38 +93,16 @@ int main() 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); - - // switch (vars.size()) // choose the test error version according to the input parameters number - // { - // case 1: { - // int scale = 50000; - // testError(uniqueLabel, "origin", 0, 1, scale); // for 1 param - // break; - // } - // case 2: { - // int scale = 1024; - // testError(uniqueLabel, "origin", 0, 1, 0, 1, scale, scale); // for 2 params - // break; - // } - // case 3: { - // int scale = 256; - // testError(uniqueLabel, "origin", 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", vars.size()); - // exit(EXIT_FAILURE); - // break; - // } - // } - + 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); } // the whole process end @@ -138,15 +117,19 @@ int main() auto &result = results.at(i); // string treeStr; // printAST(result, treeStr, MAIN_PRECISION); - // fout << "No." << i << ": " << PrintExpression(result, MAIN_PRECISION) << endl; - fout << "*resultPtr = " << PrintExpression(result, DOUBLE_PRECISION) << ";" << endl; + 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(); + std::chrono::duration testError_seconds = tmp2 - tmp1; + std::chrono::duration rewrite_seconds = tmp3 - tmp2; std::chrono::duration elapsed_seconds = end - start; + cout << BLUE << "testError time: " << testError_seconds.count() << "s" << RESET << endl; + cout << BLUE << "rewrite time: " << rewrite_seconds.count() << "s" << RESET << endl; cout << BLUE << "elapsed time: " << elapsed_seconds.count() << "s" << RESET << endl; fprintf(stderr, GREEN "ready> " RESET); } -- Gitee