resolve_par_reg_conflicts: generate unique names, region merging: generate common blocks with _c suffix
This commit is contained in:
@@ -280,7 +280,7 @@ static pair<string, string> createNewArray(DIST::Array *example_array, const str
|
||||
const map<string, vector<FuncInfo *>> &allFuncInfo,
|
||||
unordered_map<FuncInfo *, unordered_map<string, SgSymbol *>> &inserted_arrays)
|
||||
{
|
||||
auto common_block_name = base_name + "_merge_cb";
|
||||
auto common_block_name = base_name + "_merge_r";
|
||||
auto array_name = base_name;
|
||||
|
||||
for (const auto &by_file : allFuncInfo)
|
||||
|
||||
@@ -4,7 +4,9 @@
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <set>
|
||||
#include <unordered_set>
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
#include <stack>
|
||||
#include <string.h>
|
||||
|
||||
@@ -23,6 +25,8 @@
|
||||
using std::map;
|
||||
using std::pair;
|
||||
using std::set;
|
||||
using std::unordered_set;
|
||||
using std::unordered_map;
|
||||
using std::vector;
|
||||
using std::stack;
|
||||
using std::string;
|
||||
@@ -30,6 +34,9 @@ using std::wstring;
|
||||
using std::to_string;
|
||||
using std::make_pair;
|
||||
|
||||
static const string COMMON_ARRAY_SUFFIX = "_c";
|
||||
static const string COMMON_BLOCK_SUFFIX = "_r";
|
||||
|
||||
static inline int getRegionExplicitLine(SgStatement *startR)
|
||||
{
|
||||
checkNull(startR, convertFileName(__FILE__).c_str(), __LINE__);
|
||||
@@ -106,6 +113,103 @@ static const vector<const Variable*> getArraySynonyms(DIST::Array *array)
|
||||
return arrayBlock->second->getVariables(pos);
|
||||
}
|
||||
|
||||
class UniqueNameCreator
|
||||
{
|
||||
map<string, vector<FuncInfo*>> func_info;
|
||||
unordered_set<string> all_declarations;
|
||||
bool declarations_analyzed = false;
|
||||
unordered_map<const DIST::Array*, pair<string, string>> generated_names;
|
||||
|
||||
static void GetSymbolsRec(SgExpression* exp, unordered_set<string>& add_to)
|
||||
{
|
||||
if (!exp)
|
||||
return;
|
||||
|
||||
if (isArrayRef(exp) && exp->symbol() && exp->symbol()->identifier())
|
||||
add_to.emplace(exp->symbol()->identifier());
|
||||
|
||||
GetSymbolsRec(exp->lhs(), add_to);
|
||||
GetSymbolsRec(exp->rhs(), add_to);
|
||||
}
|
||||
|
||||
void FillDeclarations()
|
||||
{
|
||||
all_declarations.clear();
|
||||
|
||||
auto* file_before = current_file;
|
||||
|
||||
for (const auto& by_file : func_info)
|
||||
{
|
||||
if (SgFile::switchToFile(by_file.first) != -1)
|
||||
{
|
||||
for (const auto* by_func : by_file.second)
|
||||
{
|
||||
SgStatement* st = by_func->funcPointer;
|
||||
|
||||
if (st)
|
||||
st = st->lexNext();
|
||||
|
||||
while (st)
|
||||
{
|
||||
for (int i = 0; i < 3; i++)
|
||||
GetSymbolsRec(st->expr(i), all_declarations);
|
||||
|
||||
st = st->lexNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SgFile::switchToFile(file_before->filename());
|
||||
declarations_analyzed = true;
|
||||
}
|
||||
public:
|
||||
UniqueNameCreator(const map<string, vector<FuncInfo*>> &allFuncInfo)
|
||||
{
|
||||
declarations_analyzed = false;
|
||||
func_info = allFuncInfo;
|
||||
}
|
||||
|
||||
void GetUniqueName(DIST::Array* array, string& array_name, string& common_block_name)
|
||||
{
|
||||
auto varsOnPos = getArraySynonyms(array);
|
||||
if (!varsOnPos.size())
|
||||
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
|
||||
|
||||
auto array_plain_name = varsOnPos[0]->getName();
|
||||
|
||||
auto it = generated_names.find(array);
|
||||
if (it != generated_names.end())
|
||||
{
|
||||
array_name.assign(it->second.first);
|
||||
common_block_name.assign(it->second.second);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!declarations_analyzed)
|
||||
FillDeclarations();
|
||||
|
||||
int v = 1;
|
||||
auto created_array_name = array_plain_name + COMMON_ARRAY_SUFFIX;
|
||||
auto created_common_name = array_plain_name + COMMON_BLOCK_SUFFIX;
|
||||
|
||||
while (all_declarations.find(created_array_name) != all_declarations.end() ||
|
||||
all_declarations.find(created_common_name) != all_declarations.end())
|
||||
{
|
||||
created_array_name = array_plain_name + "_v" + std::to_string(v) + COMMON_ARRAY_SUFFIX;
|
||||
created_common_name = array_plain_name + "_v" + std::to_string(v) + COMMON_BLOCK_SUFFIX;
|
||||
v++;
|
||||
}
|
||||
|
||||
all_declarations.insert(created_array_name);
|
||||
all_declarations.insert(created_common_name);
|
||||
generated_names.emplace(array, std::make_pair(created_array_name, created_common_name));
|
||||
|
||||
array_name.assign(created_array_name);
|
||||
common_block_name.assign(created_common_name);
|
||||
}
|
||||
};
|
||||
|
||||
static string getStringDeclaration(SgSymbol *symb)
|
||||
{
|
||||
string decl;
|
||||
@@ -127,15 +231,16 @@ static string getStringDeclaration(SgSymbol *symb)
|
||||
return decl;
|
||||
}
|
||||
|
||||
static void insertStringDeclarations(SgStatement *insertPlace, DIST::Array *array)
|
||||
static void insertStringDeclarations(SgStatement *insertPlace, DIST::Array *array, UniqueNameCreator& unique_name_creator)
|
||||
{
|
||||
auto varsOnPos = getArraySynonyms(array);
|
||||
if (varsOnPos.size() && varsOnPos[0]->getName() == array->GetShortName())
|
||||
{
|
||||
string newArrName, commName;
|
||||
unique_name_creator.GetUniqueName(array, newArrName, commName);
|
||||
SgSymbol *varSymb = varsOnPos[0]->getSymbol();
|
||||
string varName = varSymb->identifier();
|
||||
string newName = varName + "_c";
|
||||
varSymb->changeName(newName.c_str());
|
||||
varSymb->changeName(newArrName.c_str());
|
||||
string decl = getStringDeclaration(varsOnPos[0]->getSymbol());
|
||||
varSymb->changeName(varName.c_str());
|
||||
insertPlace->addComment(decl.c_str());
|
||||
@@ -662,12 +767,11 @@ static void replaceFuncCalls(const ParallelRegionLines &lines, const map<string,
|
||||
static map<string, map<DIST::Array*, SgStatement*>> createdCommonBlocks; // file -> array -> new common statement
|
||||
static map<string, map<DIST::Array*, pair<SgSymbol*, SgSymbol*>>> createdCommonArrays; // file -> array -> (orig, copy)
|
||||
|
||||
static SgStatement* createCommonBlock(SgFile *file, DIST::Array *array)
|
||||
static SgStatement* createCommonBlock(SgFile *file, DIST::Array *array, UniqueNameCreator& unique_name_creator)
|
||||
{
|
||||
auto varsOnPos = getArraySynonyms(array);
|
||||
if (!varsOnPos.size())
|
||||
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
|
||||
string newArrName, commBlockName;
|
||||
|
||||
unique_name_creator.GetUniqueName(array, newArrName, commBlockName);
|
||||
string fileName = file->filename();
|
||||
|
||||
if (SgFile::switchToFile(fileName) != -1)
|
||||
@@ -683,7 +787,6 @@ static SgStatement* createCommonBlock(SgFile *file, DIST::Array *array)
|
||||
// creating new common-block statement
|
||||
//TODO: consistence with declaration
|
||||
//string commBlockName = checkSymbNameAndCorrect(array->GetShortName() + "_r");
|
||||
string commBlockName = array->GetShortName() + "_r";
|
||||
SgStatement *commDecl = new SgStatement(COMM_STAT);
|
||||
|
||||
SgSymbol *commSymb = new SgSymbol(VARIABLE_NAME, commBlockName.c_str());
|
||||
@@ -705,7 +808,6 @@ static SgStatement* createCommonBlock(SgFile *file, DIST::Array *array)
|
||||
{
|
||||
//TODO: consistence with declaration
|
||||
//string newArrName = checkSymbNameAndCorrect(varsOnPos[0]->getName() + "_c");
|
||||
string newArrName = varsOnPos[0]->getName() + "_c";
|
||||
|
||||
newArrSymb = new SgSymbol(VARIABLE_NAME, newArrName.c_str(), file->firstStatement());
|
||||
SgType *type = new SgType(T_ARRAY);
|
||||
@@ -751,7 +853,7 @@ static SgStatement* createCommonBlock(SgFile *file, DIST::Array *array)
|
||||
// func -> arrays; funcs where new common statement inserted
|
||||
static map<FuncInfo*, set<DIST::Array*>> insertedCommonBlocks;
|
||||
|
||||
static void insertCommonBlock(FuncInfo *func, DIST::Array *array)
|
||||
static void insertCommonBlock(FuncInfo *func, DIST::Array *array, UniqueNameCreator& unique_name_creator)
|
||||
{
|
||||
SgFile *file = func->funcPointer->GetOriginal()->getFile();
|
||||
SgStatement *insertPlace = NULL;
|
||||
@@ -767,7 +869,7 @@ static void insertCommonBlock(FuncInfo *func, DIST::Array *array)
|
||||
if (!insertPlace)
|
||||
insertPlace = func->funcPointer->GetOriginal();
|
||||
|
||||
SgStatement *commDecl = createCommonBlock(file, array);
|
||||
SgStatement *commDecl = createCommonBlock(file, array, unique_name_creator);
|
||||
SgStatement *copyDecl = commDecl->copyPtr();
|
||||
|
||||
auto st = insertPlace->controlParent();
|
||||
@@ -779,7 +881,7 @@ static void insertCommonBlock(FuncInfo *func, DIST::Array *array)
|
||||
insertPlace->lexNext()->setlineNumber(nextLine);
|
||||
|
||||
// create declaration via comment
|
||||
insertStringDeclarations(insertPlace->lexNext(), array);
|
||||
insertStringDeclarations(insertPlace->lexNext(), array, unique_name_creator);
|
||||
}
|
||||
|
||||
// file -> lines -> arrays; lines where arrays copying is inserted
|
||||
@@ -1097,6 +1199,7 @@ pair<SgSymbol*, SgSymbol*> copyArray(const pair<string, int> &place,
|
||||
static void copyFunction(ParallelRegion *region,
|
||||
FuncInfo *func,
|
||||
const map<string, FuncInfo*> &funcMap,
|
||||
UniqueNameCreator& unique_name_creator,
|
||||
const string &suffix = "")
|
||||
{
|
||||
if (SgFile::switchToFile(func->fileName) != -1)
|
||||
@@ -1146,14 +1249,14 @@ static void copyFunction(ParallelRegion *region,
|
||||
if (origStat->variant() == COMM_STAT)
|
||||
{
|
||||
for (auto &arrayBlock : allUsedCommonArrays)
|
||||
createCommonBlock(file, arrayBlock.first);
|
||||
createCommonBlock(file, arrayBlock.first, unique_name_creator);
|
||||
|
||||
auto usedCommonArrays = region->GetUsedCommonArrays().find(func);
|
||||
if (usedCommonArrays != region->GetUsedCommonArrays().end())
|
||||
{
|
||||
for (auto &arrayLines : usedCommonArrays->second)
|
||||
{
|
||||
SgStatement *commDecl = createCommonBlock(file, arrayLines.first);
|
||||
SgStatement *commDecl = createCommonBlock(file, arrayLines.first, unique_name_creator);
|
||||
SgStatement *copyDecl = commDecl->copyPtr();
|
||||
|
||||
while (!isSgExecutableStatement(copyStat) || isSPF_stat(copyStat))
|
||||
@@ -1163,7 +1266,7 @@ static void copyFunction(ParallelRegion *region,
|
||||
copyStat->insertStmtAfter(*copyDecl, *copyStat->controlParent());
|
||||
|
||||
// making declaration of new common array symbol via comment through files
|
||||
insertStringDeclarations(copyStat->lexNext(), arrayLines.first);
|
||||
insertStringDeclarations(copyStat->lexNext(), arrayLines.first, unique_name_creator);
|
||||
}
|
||||
|
||||
auto it = createdCommonArrays.find(file->filename());
|
||||
@@ -1619,6 +1722,8 @@ int resolveParRegions(vector<ParallelRegion*> ®ions, const map<string, vector
|
||||
map<string, FuncInfo*> funcMap;
|
||||
createMapOfFunc(allFuncInfo, funcMap);
|
||||
|
||||
UniqueNameCreator unique_name_creator(allFuncInfo);
|
||||
|
||||
if (sharedMemoryParallelization == 0)
|
||||
{
|
||||
map<string, map<int, set<string>>> copied;
|
||||
@@ -1688,7 +1793,7 @@ int resolveParRegions(vector<ParallelRegion*> ®ions, const map<string, vector
|
||||
if (itt == it->second.end() && arrayBlock.first->GetShortName() == varsOnPos[0]->getName())
|
||||
{
|
||||
// need to insert common-block
|
||||
insertCommonBlock(func, arrayBlock.first);
|
||||
insertCommonBlock(func, arrayBlock.first, unique_name_creator);
|
||||
it->second.insert(arrayBlock.first);
|
||||
}
|
||||
}
|
||||
@@ -1781,7 +1886,7 @@ int resolveParRegions(vector<ParallelRegion*> ®ions, const map<string, vector
|
||||
if (itt == it->second.end() && arrayBlock.first->GetShortName() == varsOnPos[0]->getName())
|
||||
{
|
||||
// need to insert common-block
|
||||
insertCommonBlock(func, arrayBlock.first);
|
||||
insertCommonBlock(func, arrayBlock.first, unique_name_creator);
|
||||
it->second.insert(arrayBlock.first);
|
||||
}
|
||||
}
|
||||
@@ -1813,7 +1918,7 @@ int resolveParRegions(vector<ParallelRegion*> ®ions, const map<string, vector
|
||||
if (usedCommonArrays != region->GetUsedCommonArrays().end())
|
||||
{
|
||||
for (auto &arrayBlock : allUsedCommonArrays)
|
||||
createCommonBlock(file, arrayBlock.first);
|
||||
createCommonBlock(file, arrayBlock.first, unique_name_creator);
|
||||
|
||||
auto it = insertedCommonBlocks.find(func);
|
||||
if (it == insertedCommonBlocks.end())
|
||||
@@ -1825,7 +1930,7 @@ int resolveParRegions(vector<ParallelRegion*> ®ions, const map<string, vector
|
||||
if (itt == it->second.end())
|
||||
{
|
||||
// need to insert common-block
|
||||
insertCommonBlock(func, arrayLines.first);
|
||||
insertCommonBlock(func, arrayLines.first, unique_name_creator);
|
||||
it->second.insert(arrayLines.first);
|
||||
|
||||
// replace common arrays to new common arrays in executable code section
|
||||
@@ -1858,7 +1963,7 @@ int resolveParRegions(vector<ParallelRegion*> ®ions, const map<string, vector
|
||||
// need copy function for every region, the exeption is defalut region
|
||||
for (auto ®ionId : func->callRegions)
|
||||
if (regionId)
|
||||
copyFunction(getRegionById(regions, regionId), func, funcMap, string("_r") + to_string(regionId));
|
||||
copyFunction(getRegionById(regions, regionId), func, funcMap, unique_name_creator, string("_r") + to_string(regionId));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user