merge code of directives creation for general and shared memory cases

This commit is contained in:
2024-07-05 18:03:12 +03:00
parent 7daf1b4038
commit 444f7e36bc
4 changed files with 207 additions and 209 deletions

View File

@@ -1843,10 +1843,10 @@ void selectParallelDirectiveForVariant(File* file, ParallelRegion* currParReg,
const bool isMyRegion = loop->region == currParReg; const bool isMyRegion = loop->region == currParReg;
const bool noUserDir = loop->userDvmDirective == NULL; const bool noUserDir = loop->userDvmDirective == NULL;
DIST::Array* sameAlignTemplate = NULL; DIST::Array* sameAlignTemplate = NULL;
const bool sameAligns = loop->isArrayTemplatesTheSame(sameAlignTemplate, regionId, arrayLinksByFuncCalls); const bool sameAligns = sharedMemoryParallelization ? true : loop->isArrayTemplatesTheSame(sameAlignTemplate, regionId, arrayLinksByFuncCalls);
bool freeLoopDistr = true; bool freeLoopDistr = true;
if (hasDirective && loop->directive->arrayRef2->IsLoopArray()) if (!sharedMemoryParallelization && hasDirective && loop->directive->arrayRef2->IsLoopArray())
{ {
bool ok = false; bool ok = false;
for (auto& elem : distribution) for (auto& elem : distribution)
@@ -1879,6 +1879,9 @@ void selectParallelDirectiveForVariant(File* file, ParallelRegion* currParReg,
//try to unite loops //try to unite loops
if (createNestedLoops(loop, depInfoForLoopGraph, mapFuncInfo, messages)) if (createNestedLoops(loop, depInfoForLoopGraph, mapFuncInfo, messages))
parDirective = loop->recalculateParallelDirective(); parDirective = loop->recalculateParallelDirective();
if(!sharedMemoryParallelization)
{
bool topCheck = isOnlyTopPerfect(loop, distribution); bool topCheck = isOnlyTopPerfect(loop, distribution);
bool needToContinue = false; bool needToContinue = false;
@@ -1897,15 +1900,17 @@ void selectParallelDirectiveForVariant(File* file, ParallelRegion* currParReg,
if (needToContinue) if (needToContinue)
continue; continue;
}
vector<pair<DIST::Array*, const DistrVariant*>> newRules; vector<pair<DIST::Array*, const DistrVariant*>> newRules;
if(!sharedMemoryParallelization)
constructRules(newRules, distribution, loop); constructRules(newRules, distribution, loop);
Directive* dirImpl = parDirective->genDirective(file, newRules, loop, reducedG, allArrays, regionId, arrayLinksByFuncCalls); Directive* dirImpl = parDirective->genDirective(file, newRules, loop, reducedG, allArrays, regionId, arrayLinksByFuncCalls);
#if __SPF #if __SPF
//move label before loop //move label before loop
if (loop->hasRedistribute()) if (!sharedMemoryParallelization && loop->hasRedistribute())
{ {
auto prev = loop->loop->lexPrev(); auto prev = loop->loop->lexPrev();
if (!prev) if (!prev)

View File

@@ -281,6 +281,9 @@ static vector<SgExpression*>
for (int z = 0; z < loops.size(); ++z) for (int z = 0; z < loops.size(); ++z)
{ {
currLoop = loops[z]; currLoop = loops[z];
if(!sharedMemoryParallelization)
{
const uint64_t regId = sharedMemoryParallelization ? (uint64_t)currLoop : currLoop->region->GetId(); const uint64_t regId = sharedMemoryParallelization ? (uint64_t)currLoop : currLoop->region->GetId();
auto dirForLoop = currLoop->directiveForLoop; auto dirForLoop = currLoop->directiveForLoop;
@@ -352,6 +355,30 @@ static vector<SgExpression*>
} }
} }
} }
else
{
for (const auto& source : { currLoop->readOpsForLoop, currLoop->writeOpsForLoop }) {
auto array_it = source.find(pairs.second);
if (array_it != source.end()) {
bool dim_found = false;
for (int i = 0; i < array_it->second.size(); i++) {
if (array_it->second[i].coefficients.size() != 0)
{
needToAdd = true;
dim_found = true;
subs[i] = new SgVarRefExp(findSymbolOrCreate(file, currLoop->loopSymbol));
break;
}
}
if (dim_found)
break;
}
}
}
}
if (needToAdd) if (needToAdd)
{ {
@@ -477,7 +504,7 @@ ParallelDirective::genDirective(File* file, const vector<pair<DIST::Array*, cons
{ {
const set<DIST::Array*>& acrossOutAttribute = currLoop->acrossOutAttribute; const set<DIST::Array*>& acrossOutAttribute = currLoop->acrossOutAttribute;
const map<DIST::Array*, pair<vector<ArrayOp>, vector<bool>>>& readOps = currLoop->readOps; const map<DIST::Array*, pair<vector<ArrayOp>, vector<bool>>>& readOps = currLoop->readOps;
map< DIST::Array*, vector<ArrayOp>>& remoteReads = currLoop->remoteRegularReads; map<DIST::Array*, vector<ArrayOp>>& remoteReads = currLoop->remoteRegularReads;
Statement* loop = currLoop->loop; Statement* loop = currLoop->loop;
string directive = ""; string directive = "";
@@ -501,6 +528,10 @@ ParallelDirective::genDirective(File* file, const vector<pair<DIST::Array*, cons
LoopGraph* pLoop = currLoop; LoopGraph* pLoop = currLoop;
const set<string> allFiles = getAllFilesInProject(); const set<string> allFiles = getAllFilesInProject();
map<string, DIST::Array*> arrayByName;
for (DIST::Array* arr : currLoop->getAllArraysInLoop())
arrayByName[arr->GetName()] = arr;
for (int z = 0; z < nested; ++z) for (int z = 0; z < nested; ++z)
{ {
loopSymbs.push_back(loopG->symbol()); loopSymbs.push_back(loopG->symbol());
@@ -552,21 +583,25 @@ ParallelDirective::genDirective(File* file, const vector<pair<DIST::Array*, cons
p->setRhs(NULL); p->setRhs(NULL);
} }
DIST::Array* mapTo = arrayRef2->IsLoopArray() ? arrayRef : arrayRef2; DIST::Array* mapTo;
auto onTo = arrayRef2->IsLoopArray() ? on : on2;
dirStatement[2] = new Expression(expr); dirStatement[2] = new Expression(expr);
if (sharedMemoryParallelization) if (sharedMemoryParallelization)
{
directive += ")"; directive += ")";
}
else else
{
mapTo = arrayRef2->IsLoopArray() ? arrayRef : arrayRef2;
directive += ") ON " + mapTo->GetShortName() + "("; directive += ") ON " + mapTo->GetShortName() + "(";
}
SgArrayRefExp* arrayExpr = NULL; SgArrayRefExp* arrayExpr = NULL;
string arrayExprS = ""; string arrayExprS = "";
if (!sharedMemoryParallelization) if (!sharedMemoryParallelization)
{ {
auto onTo = arrayRef2->IsLoopArray() ? on : on2;
SgSymbol* symbForPar = NULL; SgSymbol* symbForPar = NULL;
if (arrayRef->IsTemplate()) if (arrayRef->IsTemplate())
{ {
@@ -729,10 +764,23 @@ ParallelDirective::genDirective(File* file, const vector<pair<DIST::Array*, cons
{ {
const int i1 = ordered[k]; const int i1 = ordered[k];
vector<map<pair<int, int>, int>> shiftsByAccess; vector<map<pair<int, int>, int>> shiftsByAccess;
DIST::Array* currArray = NULL;
DIST::Array* currArray = allArrays.GetArrayByName(across[i1].first.second); if (!sharedMemoryParallelization)
{
currArray = allArrays.GetArrayByName(across[i1].first.second);
if (currArray == NULL) if (currArray == NULL)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__); printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
}
else
{
auto currArray_it = arrayByName.find(across[i1].first.second);
if (currArray_it == arrayByName.end())
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
currArray = currArray_it->second;
}
bool isOut = acrossOutAttribute.find(currArray) != acrossOutAttribute.end(); bool isOut = acrossOutAttribute.find(currArray) != acrossOutAttribute.end();
string bounds = genBounds(across[i1], acrossShifts[i1], reducedG, allArrays, remoteReads, readOps, true, regionId, distribution, arraysInAcross, shiftsByAccess, arrayLinksByFuncCalls); string bounds = genBounds(across[i1], acrossShifts[i1], reducedG, allArrays, remoteReads, readOps, true, regionId, distribution, arraysInAcross, shiftsByAccess, arrayLinksByFuncCalls);

View File

@@ -268,70 +268,29 @@ static inline string calculateShifts(DIST::GraphCSR<int, double, attrType> &redu
const uint64_t regionId, const uint64_t regionId,
const map<DIST::Array*, set<DIST::Array*>> &arrayLinksByFuncCalls) const map<DIST::Array*, set<DIST::Array*>> &arrayLinksByFuncCalls)
{ {
vector<tuple<DIST::Array*, int, pair<int, int>>> ruleForOn = vector<tuple<DIST::Array*, int, pair<int, int>>> ruleForOn, ruleForShadow;
getAlignRuleWithTemplate(arrayRef, arrayLinksByFuncCalls, reducedG, allArrays, regionId);
vector<tuple<DIST::Array*, int, pair<int, int>>> ruleForShadow = if (!sharedMemoryParallelization)
getAlignRuleWithTemplate(calcForArray, arrayLinksByFuncCalls, reducedG, allArrays, regionId); {
ruleForOn = getAlignRuleWithTemplate(arrayRef, arrayLinksByFuncCalls, reducedG, allArrays, regionId);
ruleForShadow = getAlignRuleWithTemplate(calcForArray, arrayLinksByFuncCalls, reducedG, allArrays, regionId);
}
string out = ""; string out = "";
// check for distributed and not mapped dims -> zero them out ('coeffs.second') // check for distributed and not mapped dims -> zero them out ('coeffs.second')
set<DIST::Array*> refs; set<DIST::Array*> refs;
getRealArrayRefs(calcForArray, calcForArray, refs, arrayLinksByFuncCalls); getRealArrayRefs(calcForArray, calcForArray, refs, arrayLinksByFuncCalls);
if (sharedMemoryParallelization == 0)
{//TODO: need to correct errors
/*for (auto& array : refs)
{
DIST::Array* tmpl = array->GetTemplateArray(regionId);
checkNull(tmpl, convertFileName(__FILE__).c_str(), __LINE__);
auto align = array->GetLinksWithTemplate(regionId);
bool found = false;
for (auto& t : distribution)
{
if (t.first == tmpl)
{
found = true;
for (int aDim = 0; aDim < align.size(); ++aDim)
{
int link = align[aDim];
if (link != -1)
{
int tLink = link;
if (!arrayRef->IsTemplate())
{
auto alignMain = arrayRef->GetLinksWithTemplate(regionId);
for (int z = 0; z < alignMain.size(); ++z)
if (alignMain[z] == tLink)
tLink = z;
}
if (t.second->distRule[link] == dist::BLOCK && baseOnRule[aDim].first == "*")
{
for (int z = 0; z < coeffs.second.size(); ++z)
coeffs.second[z].first = coeffs.second[z].second = 0;
return out;
}
}
}
}
}
if (!found)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
} */
}
const pair<vector<ArrayOp>, vector<bool>> *currReadOp = NULL; const pair<vector<ArrayOp>, vector<bool>> *currReadOp = NULL;
auto readIt = readOps.find(calcForArray); auto readIt = readOps.find(calcForArray);
if (readIt != readOps.end()) if (readIt != readOps.end())
currReadOp = &(readIt->second); currReadOp = &(readIt->second);
if(!sharedMemoryParallelization)
{
findAndReplaceDimentions(ruleForOn, allArrays); findAndReplaceDimentions(ruleForOn, allArrays);
findAndReplaceDimentions(ruleForShadow, allArrays); findAndReplaceDimentions(ruleForShadow, allArrays);
}
const int len = (int)coeffs.second.size(); const int len = (int)coeffs.second.size();
vector<pair<int, int>> shift(len); vector<pair<int, int>> shift(len);
@@ -352,7 +311,20 @@ static inline string calculateShifts(DIST::GraphCSR<int, double, attrType> &redu
// no unrecognized read operations // no unrecognized read operations
if (currReadOp->second[k] == false) if (currReadOp->second[k] == false)
{ {
if (get<0>(ruleForShadow[k]) != NULL) if (sharedMemoryParallelization)
{
for (auto& coefs : currReadOp->first[k].coefficients)
{
auto currAccess = coefs.first;
const int currShift = coefs.first.second;
auto itFound = shiftsByAccess[k].find(currAccess);
if (itFound == shiftsByAccess[k].end())
itFound = shiftsByAccess[k].insert(itFound, make_pair(currAccess, currShift));
}
}
else if (get<0>(ruleForShadow[k]) != NULL)
{ {
const pair<int, int> currRuleShadow = get<2>(ruleForShadow[k]); const pair<int, int> currRuleShadow = get<2>(ruleForShadow[k]);
@@ -451,6 +423,8 @@ static inline string calculateShifts(DIST::GraphCSR<int, double, attrType> &redu
} }
} }
if(!sharedMemoryParallelization)
{
if (coeffs.second[k].first + shift[k].first < 0) if (coeffs.second[k].first + shift[k].first < 0)
shift[k].first = -coeffs.second[k].first; shift[k].first = -coeffs.second[k].first;
@@ -468,6 +442,7 @@ static inline string calculateShifts(DIST::GraphCSR<int, double, attrType> &redu
shift[k].second = -coeffs.second[k].second; shift[k].second = -coeffs.second[k].second;
shiftsByAccess[k].clear(); shiftsByAccess[k].clear();
} }
}
sprintf(buf, "%d:%d", coeffs.second[k].first + shift[k].first, coeffs.second[k].second + shift[k].second); sprintf(buf, "%d:%d", coeffs.second[k].first + shift[k].first, coeffs.second[k].second + shift[k].second);
shifts[k] = shift[k]; shifts[k] = shift[k];
@@ -500,8 +475,11 @@ string ParallelDirective::genBounds(pair<pair<string, string>, vector<pair<int,
checkNull(shadowArray, convertFileName(__FILE__).c_str(), __LINE__); checkNull(shadowArray, convertFileName(__FILE__).c_str(), __LINE__);
auto on_ext = on; auto on_ext = on;
if(!sharedMemoryParallelization)
{
//replace to template align ::on //replace to template align ::on
if (arrayRef->IsTemplate() == false && sharedMemoryParallelization == 0) if (arrayRef->IsTemplate() == false)
{ {
vector<tuple<DIST::Array*, int, pair<int, int>>> ruleForRef = vector<tuple<DIST::Array*, int, pair<int, int>>> ruleForRef =
getAlignRuleWithTemplate(arrayRef, arrayLinksByFuncCalls, reducedG, allArrays, regionId); getAlignRuleWithTemplate(arrayRef, arrayLinksByFuncCalls, reducedG, allArrays, regionId);
@@ -535,6 +513,7 @@ string ParallelDirective::genBounds(pair<pair<string, string>, vector<pair<int,
on_ext[i].first = "SINGLE"; on_ext[i].first = "SINGLE";
} }
} }
}
string ret = ""; string ret = "";
if (isAcross) if (isAcross)

View File

@@ -541,7 +541,7 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne
} }
else if (curr_regime == CORRECT_FORMAT_PLACE) else if (curr_regime == CORRECT_FORMAT_PLACE)
checkAndMoveFormatOperators(file, getObjectForFileFromMap(file_name, SPF_messages), false); checkAndMoveFormatOperators(file, getObjectForFileFromMap(file_name, SPF_messages), false);
else if (curr_regime == CREATE_PARALLEL_DIRS) else if (curr_regime == CREATE_PARALLEL_DIRS || curr_regime == INSERT_PARALLEL_DIRS_NODIST)
{ {
auto &loopsInFile = getObjectForFileFromMap(file_name, loopGraph); auto &loopsInFile = getObjectForFileFromMap(file_name, loopGraph);
@@ -568,40 +568,6 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne
UniteNestedDirectives(loopsInFile); UniteNestedDirectives(loopsInFile);
} }
else if (curr_regime == INSERT_PARALLEL_DIRS_NODIST)
{
auto& loopsInFile = getObjectForFileFromMap(file_name, loopGraph);
map<int, LoopGraph*> mapLoopsInFile;
createMapLoopGraph(loopsInFile, mapLoopsInFile);
map<string, FuncInfo*> mapFuncInfo;
createMapOfFunc(allFuncInfo, mapFuncInfo);
for (int z = 0; z < parallelRegions.size(); ++z)
{
vector<Directive*> toInsert;
DIST::Arrays<int>& allArrays = parallelRegions[z]->GetAllArraysToModify();
map<LoopGraph*, void*> depInfoForLoopGraphV;
for (auto& elem : depInfoForLoopGraph)
depInfoForLoopGraphV[elem.first] = elem.second;
selectParallelDirectiveForVariantNoDist(new File(file), parallelRegions[z], allArrays, loopsInFile, mapLoopsInFile, mapFuncInfo,
toInsert, arrayLinksByFuncCalls, depInfoForLoopGraphV, getObjectForFileFromMap(file_name, SPF_messages));
if (toInsert.size() > 0)
{
auto it = createdDirectives.find(file_name);
if (it == createdDirectives.end())
createdDirectives.insert(it, make_pair(file_name, toInsert));
else
for (int m = 0; m < toInsert.size(); ++m)
it->second.push_back(toInsert[m]);
}
}
}
else if (curr_regime == INSERT_SHADOW_DIRS || curr_regime == EXTRACT_SHADOW_DIRS) else if (curr_regime == INSERT_SHADOW_DIRS || curr_regime == EXTRACT_SHADOW_DIRS)
{ {
const bool extract = (curr_regime == EXTRACT_SHADOW_DIRS); const bool extract = (curr_regime == EXTRACT_SHADOW_DIRS);
@@ -2139,7 +2105,7 @@ void runPass(const int curr_regime, const char *proj_name, const char *folderNam
string additionalName = (consoleMode && folderName == NULL) ? "__shared" : ""; string additionalName = (consoleMode && folderName == NULL) ? "__shared" : "";
runAnalysis(*project, INSERT_PARALLEL_DIRS_NODIST, false); runAnalysis(*project, CREATE_PARALLEL_DIRS, false);
runPass(REVERT_SUBST_EXPR_RD, proj_name, folderName); runPass(REVERT_SUBST_EXPR_RD, proj_name, folderName);