From 29ece9072eb6cf9a1250d96e8e5f5daa5dcd7bc0 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Sat, 5 Jul 2025 20:49:23 +0300 Subject: [PATCH] fixed shared memory parallelization --- CMakeLists.txt | 1 + src/DirectiveProcessing/directive_creator.cpp | 2 +- .../directive_creator_base.cpp | 2 +- src/DvmhRegions/DvmhRegionInserter.cpp | 152 +----------------- src/GraphLoop/graph_loops.cpp | 2 +- src/LoopAnalyzer/loop_analyzer.cpp | 2 +- .../resolve_par_reg_conflicts.cpp | 2 +- src/Sapfor.cpp | 7 +- .../FunctionPurifying/function_purifying.cpp | 127 +++++++++++---- .../FunctionPurifying/function_purifying.h | 4 +- src/Utils/version.h | 2 +- 11 files changed, 117 insertions(+), 186 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 03afe1f..95c47c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,7 @@ include_directories(src/Distribution) include_directories(src/GraphCall) include_directories(src/GraphLoop) include_directories(src/Transformations/ExpressionSubstitution) +include_directories(src/Transformations) #Sage lib includes include_directories(${fdvm_include}) diff --git a/src/DirectiveProcessing/directive_creator.cpp b/src/DirectiveProcessing/directive_creator.cpp index 1601d27..fe5ae3b 100644 --- a/src/DirectiveProcessing/directive_creator.cpp +++ b/src/DirectiveProcessing/directive_creator.cpp @@ -22,7 +22,7 @@ #include "SgUtils.h" #include "../Sapfor.h" #include "graph_loops_func.h" -#include "../Transformations/LoopNesting/loop_transform.h" +#include "LoopNesting/loop_transform.h" #include "expr_transform.h" #include "graph_calls_func.h" diff --git a/src/DirectiveProcessing/directive_creator_base.cpp b/src/DirectiveProcessing/directive_creator_base.cpp index c05e281..5138de2 100644 --- a/src/DirectiveProcessing/directive_creator_base.cpp +++ b/src/DirectiveProcessing/directive_creator_base.cpp @@ -10,7 +10,7 @@ #include "../ParallelizationRegions/ParRegions.h" #include "../Distribution/Arrays.h" -#include "../Transformations/LoopNesting/loop_transform.h" +#include "LoopNesting/loop_transform.h" #include "errors.h" #include "directive_parser.h" diff --git a/src/DvmhRegions/DvmhRegionInserter.cpp b/src/DvmhRegions/DvmhRegionInserter.cpp index f2398ac..71c872d 100644 --- a/src/DvmhRegions/DvmhRegionInserter.cpp +++ b/src/DvmhRegions/DvmhRegionInserter.cpp @@ -11,9 +11,9 @@ #include "DvmhRegionInserter.h" #include "DvmhRegions/RegionsMerger.h" #include "../VerificationCode/verifications.h" -#include "../Transformations/FunctionPurifying/function_purifying.h" #include "../LoopAnalyzer/loop_analyzer.h" #include "../DirectiveProcessing/directive_parser.h" +#include "FunctionPurifying/function_purifying.h" using namespace std; @@ -794,154 +794,6 @@ ArraySet DvmhRegionInserter::get_used_arrs_for_block(SgStatement* st, int usage_ return usages; } -static bool filterFromList(SgStatement* st, const set& idents, bool exclude = false) -{ - bool empty = false; - SgExpression* list = st->expr(0); - vector newList; - - int total = 0; - while (list) - { - if (exclude) - { - if (idents.find(list->lhs()->symbol()->identifier()) == idents.end()) - newList.push_back(list->lhs()); - } - else - { - if (idents.find(list->lhs()->symbol()->identifier()) != idents.end()) - newList.push_back(list->lhs()); - } - total++; - list = list->rhs(); - } - - if (newList.size() == 0) - empty = true; - else if (total != newList.size()) - st->setExpression(0, makeExprList(newList)); - - return empty; -} - -static string getInterfaceBlock(SgStatement* func, const FuncParam& pars) -{ - string oldFile = current_file->filename(); - if (!func->switchToFile()) - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - - auto copy = duplicateProcedure(func, NULL, false, false, false, true); - const set idents(pars.identificators.begin(), pars.identificators.end()); - - bool need = (func->symbol()->identifier() == string("bl182")); - - //remove all exec - SgStatement* st = copy->lexNext(); - SgStatement* last = copy->lastNodeOfStmt(); - vector toExtract; - - while (st != last) - { - if (isDVM_stat(st) || isSPF_stat(st)) - { - if (st->variant() != ACC_ROUTINE_DIR) - { - toExtract.push_back(st); - st = st->lexNext(); - } - else - st = st->lexNext(); - } - else if (isSgExecutableStatement(st)) - { - SgStatement* next = st->lastNodeOfStmt(); - if (next != last) - next = next->lexNext(); - toExtract.push_back(st); - st = next; - } - else - st = st->lexNext(); - } - - //remove unused declarations - st = copy->lexNext(); - while (st != last) - { - const int var = st->variant(); - - if (var == VAR_DECL - || var == VAR_DECL_90 - || var == DIM_STAT - || var == INTENT_STMT - || var == EXTERN_STAT) - { - bool empty = filterFromList(st, idents); - if (empty) - { - toExtract.push_back(st); - st = st->lexNext(); - continue; - } - } - else if (!isDVM_stat(st) && !isSPF_stat(st)) - toExtract.push_back(st); - - if (st->variant() == CONTAINS_STMT) - break; - st = st->lexNext(); - } - - for (auto& elem : toExtract) - elem->extractStmt(); - - string retVal = copy->unparse(); - - if (SgFile::switchToFile(oldFile) == -1) - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - - return retVal; -} - -static void insertInterface(SgStatement* func, const string& iface, const string& fName) -{ - string oldFile = current_file->filename(); - if (!func->switchToFile()) - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - - SgStatement* st = func->lexNext(); - SgStatement* last = func->lastNodeOfStmt(); - while (st != last) - { - if (st->variant() == VAR_DECL || st->variant() == VAR_DECL_90) - { - bool empty = filterFromList(st, { fName }, true); - if (empty) - { - SgStatement* next = st->lexNext(); - st->extractStmt(); - st = next; - continue; - } - } - - if (isSgExecutableStatement(st)) - break; - st = st->lexNext(); - } - SgStatement* ifaceBlock = new SgStatement(INTERFACE_STMT); - addControlEndToStmt(ifaceBlock->thebif); - - ifaceBlock->setlineNumber(getNextNegativeLineNumber()); // st->lineNumber() - ifaceBlock->setFileName(st->fileName()); - st->insertStmtBefore(*ifaceBlock, *st->controlParent()); - ifaceBlock->lastNodeOfStmt()->addComment(iface.c_str()); - - if (SgFile::switchToFile(oldFile) == -1) - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); -} - static LoopGraph* getParallelLoop(LoopGraph* loop) { auto prev_st = loop->loop->lexPrev(); @@ -1172,7 +1024,7 @@ static bool isPure(SgStatement* func) void DvmhRegionInserter::createInterfaceBlockForOutCall(FuncInfo* func, FuncInfo* callFrom) { - insertInterface(func->funcPointer, getInterfaceBlock(callFrom->funcPointer->GetOriginal(), callFrom->funcParams), callFrom->funcName); + insertInterface(func->funcPointer, callFrom); } void DvmhRegionInserter::createInterfaceBlockForParallelFunctions(bool onlyRoutine) diff --git a/src/GraphLoop/graph_loops.cpp b/src/GraphLoop/graph_loops.cpp index a749f9f..0a6c2b6 100644 --- a/src/GraphLoop/graph_loops.cpp +++ b/src/GraphLoop/graph_loops.cpp @@ -31,7 +31,7 @@ #include "../DirectiveProcessing/directive_parser.h" #include "../DynamicAnalysis/gCov_parser_func.h" -#include "../Transformations/VectorAssignToLoop/array_assign_to_loop.h" +#include "VectorAssignToLoop/array_assign_to_loop.h" using std::vector; using std::map; diff --git a/src/LoopAnalyzer/loop_analyzer.cpp b/src/LoopAnalyzer/loop_analyzer.cpp index 0fdcb67..3844fed 100644 --- a/src/LoopAnalyzer/loop_analyzer.cpp +++ b/src/LoopAnalyzer/loop_analyzer.cpp @@ -48,7 +48,7 @@ extern int passDone; #include "../VisualizerCalls/get_information.h" #include "../VisualizerCalls/SendMessage.h" -#include "../Transformations/LoopEndDoConverter/enddo_loop_converter.h" +#include "LoopEndDoConverter/enddo_loop_converter.h" #include "../DirectiveProcessing/remote_access.h" #include "../DirectiveProcessing/directive_omp_parser.h" diff --git a/src/ParallelizationRegions/resolve_par_reg_conflicts.cpp b/src/ParallelizationRegions/resolve_par_reg_conflicts.cpp index dc77d6a..0f2e922 100644 --- a/src/ParallelizationRegions/resolve_par_reg_conflicts.cpp +++ b/src/ParallelizationRegions/resolve_par_reg_conflicts.cpp @@ -18,7 +18,7 @@ #include "../DirectiveProcessing/insert_directive.h" #include "SgUtils.h" #include "expr_transform.h" -#include "../Transformations/FunctionPurifying/function_purifying.h" +#include "FunctionPurifying/function_purifying.h" using std::map; using std::pair; diff --git a/src/Sapfor.cpp b/src/Sapfor.cpp index 24f4a77..26796f4 100644 --- a/src/Sapfor.cpp +++ b/src/Sapfor.cpp @@ -1281,7 +1281,9 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne detectCopies(allFuncInfo); fillInterfaceBlock(allFuncInfo); intentInsertToInterfaces(allFuncInfo); - createInterfacesForAssumedSize(allFuncInfo); + + if (sharedMemoryParallelization != 1) + createInterfacesForAssumedSize(allFuncInfo); //this call is only for testing //setPureStatus(allFuncInfo); @@ -2632,7 +2634,10 @@ int main(int argc, char **argv) } if (curr_regime == INSERT_PARALLEL_DIRS_NODIST) + { ignoreArrayDistributeState = true; + sharedMemoryParallelization = 1; + } if (runAsClient) { diff --git a/src/Transformations/FunctionPurifying/function_purifying.cpp b/src/Transformations/FunctionPurifying/function_purifying.cpp index 2eb2470..6e4ce8e 100644 --- a/src/Transformations/FunctionPurifying/function_purifying.cpp +++ b/src/Transformations/FunctionPurifying/function_purifying.cpp @@ -1388,6 +1388,37 @@ static string makeName(SgSymbol* var, map>& modVarsTo return name; } +static bool filterFromList(SgStatement* st, const set& idents, bool exclude = false) +{ + bool empty = false; + SgExpression* list = st->expr(0); + vector newList; + + int total = 0; + while (list) + { + if (exclude) + { + if (idents.find(list->lhs()->symbol()->identifier()) == idents.end()) + newList.push_back(list->lhs()); + } + else + { + if (idents.find(list->lhs()->symbol()->identifier()) != idents.end()) + newList.push_back(list->lhs()); + } + total++; + list = list->rhs(); + } + + if (newList.size() == 0) + empty = true; + else if (total != newList.size()) + st->setExpression(0, makeExprList(newList)); + + return empty; +} + static string getInterfaceBlock(SgStatement* func, const FuncParam& pars) { string oldFile = current_file->filename(); @@ -1395,22 +1426,21 @@ static string getInterfaceBlock(SgStatement* func, const FuncParam& pars) printInternalError(convertFileName(__FILE__).c_str(), __LINE__); auto copy = duplicateProcedure(func, NULL, false, false, false, true); - - const set ident(pars.identificators.begin(), pars.identificators.end()); + const set idents(pars.identificators.begin(), pars.identificators.end()); //remove all exec SgStatement* st = copy->lexNext(); SgStatement* last = copy->lastNodeOfStmt(); vector toExtract; + while (st != last) { if (isDVM_stat(st) || isSPF_stat(st)) { if (st->variant() != ACC_ROUTINE_DIR) { - SgStatement* next = st->lexNext(); - st->extractStmt(); - st = next; + toExtract.push_back(st); + st = st->lexNext(); } else st = st->lexNext(); @@ -1431,31 +1461,20 @@ static string getInterfaceBlock(SgStatement* func, const FuncParam& pars) st = copy->lexNext(); while (st != last) { - if (st->variant() == VAR_DECL - || st->variant() == VAR_DECL_90 - || st->variant() == DIM_STAT - || st->variant() == INTENT_STMT) - { - SgExpression* list = st->expr(0); - vector newList; - while (list) - { - if (ident.find(list->lhs()->symbol()->identifier()) != ident.end()) - newList.push_back(list->lhs()); - list = list->rhs(); - } + const int var = st->variant(); - if (newList.size() == 0) + if (var == VAR_DECL || var == VAR_DECL_90 || var == DIM_STAT || + var == INTENT_STMT || var == EXTERN_STAT) + { + bool empty = filterFromList(st, idents); + if (empty) { - SgStatement* next = st->lexNext(); toExtract.push_back(st); - st = next; + st = st->lexNext(); continue; } - else - st->setExpression(0, makeExprList(newList)); } - else + else if (!isDVM_stat(st) && !isSPF_stat(st)) toExtract.push_back(st); if (st->variant() == CONTAINS_STMT) @@ -1466,16 +1485,56 @@ static string getInterfaceBlock(SgStatement* func, const FuncParam& pars) for (auto& elem : toExtract) elem->extractStmt(); - string retVal = copy->unparse(); + string codeString = copy->unparse(); if (SgFile::switchToFile(oldFile) == -1) printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + //insert tabs + const string tab = " "; + const int countEnds = std::count(codeString.begin(), codeString.end(), '\n'); + + string retVal = " "; + retVal.reserve(retVal.size() + codeString.size() + countEnds * tab.size()); + + for (int z = 0, ends = 0; z < codeString.size(); ++z) + { + retVal += codeString[z]; + if (codeString[z] == '\n') + { + ends++; + if (ends == countEnds) + continue; + + int p = z + 1; + while (codeString[p] == ' ' && p < codeString.size()) + ++p; + + auto start = p; + auto end = string::npos; + auto sub = codeString.find("subroutine", p); + auto func = codeString.find("function", p); + auto end_sub = codeString.find("end subroutine", p); + auto end_func = codeString.find("end function", p); + + if (sub != end && sub == start || end_sub != end && end_sub == start || + func != end && func == start || end_func != end && end_func == start) + { + retVal += " "; + } + else + retVal += tab; + } + } + return retVal; } -static void insertInterface(SgStatement* func, const string& iface) +void insertInterface(SgStatement* func, const FuncInfo *callFrom) { + const string& iface = getInterfaceBlock(callFrom->funcPointer->GetOriginal(), callFrom->funcParams); + const string& fName = callFrom->funcName; + string oldFile = current_file->filename(); if (!func->switchToFile()) printInternalError(convertFileName(__FILE__).c_str(), __LINE__); @@ -1484,6 +1543,18 @@ static void insertInterface(SgStatement* func, const string& iface) SgStatement* last = func->lastNodeOfStmt(); while (st != last) { + if (st->variant() == VAR_DECL || st->variant() == VAR_DECL_90) + { + bool empty = filterFromList(st, { fName }, true); + if (empty) + { + SgStatement* next = st->lexNext(); + st->extractStmt(); + st = next; + continue; + } + } + if (isSgExecutableStatement(st)) break; st = st->lexNext(); @@ -1491,7 +1562,7 @@ static void insertInterface(SgStatement* func, const string& iface) SgStatement* ifaceBlock = new SgStatement(INTERFACE_STMT); addControlEndToStmt(ifaceBlock->thebif); - ifaceBlock->setlineNumber(st->lineNumber()); + ifaceBlock->setlineNumber(getNextNegativeLineNumber()); ifaceBlock->setFileName(st->fileName()); st->insertStmtBefore(*ifaceBlock, *st->controlParent()); ifaceBlock->lastNodeOfStmt()->addComment(iface.c_str()); @@ -1507,7 +1578,7 @@ static void createInterfaceBlockForToCalls(FuncInfo* func) if (callTo->interfaceBlocks.find(func->funcName) == callTo->interfaceBlocks.end()) { callTo->interfaceBlocks[func->funcName] = func; - insertInterface(callTo->funcPointer, getInterfaceBlock(func->funcPointer->GetOriginal(), func->funcParams)); + insertInterface(callTo->funcPointer, func); } } } diff --git a/src/Transformations/FunctionPurifying/function_purifying.h b/src/Transformations/FunctionPurifying/function_purifying.h index 4226cde..01d6d56 100644 --- a/src/Transformations/FunctionPurifying/function_purifying.h +++ b/src/Transformations/FunctionPurifying/function_purifying.h @@ -13,4 +13,6 @@ void setPureStatus(const std::map>& allFuncI void commonTransfer(const std::map>& allFuncInfo, const std::map& commonBlocks); void saveTransfer(const std::map>& allFuncInfo); -void moduleTransfer(const std::map>& allFuncInfo); \ No newline at end of file +void moduleTransfer(const std::map>& allFuncInfo); + +void insertInterface(SgStatement* func, const FuncInfo* callFrom); \ No newline at end of file diff --git a/src/Utils/version.h b/src/Utils/version.h index 5f1114c..705d7b2 100644 --- a/src/Utils/version.h +++ b/src/Utils/version.h @@ -1,3 +1,3 @@ #pragma once -#define VERSION_SPF "2433" +#define VERSION_SPF "2434"