diff --git a/sapfor/experts/Sapfor_2017/_src/LoopAnalyzer/loop_analyzer.cpp b/sapfor/experts/Sapfor_2017/_src/LoopAnalyzer/loop_analyzer.cpp index f618651..71da168 100644 --- a/sapfor/experts/Sapfor_2017/_src/LoopAnalyzer/loop_analyzer.cpp +++ b/sapfor/experts/Sapfor_2017/_src/LoopAnalyzer/loop_analyzer.cpp @@ -125,9 +125,8 @@ static void addInfoToMap(map> &loopInfo, S __spf_print(DEB, "RemoteAccess[%d]: true for dim %d and array %s, loop line %d\n", __LINE__, dimNum, symb->identifier(), position->lineNumber()); } -enum { READ_OP, WRITE_OP, UNREC_OP }; -static void addInfoToVectors(map> &loopInfo, SgForStmt *position, SgSymbol *symb, - const int dimNum, const pair newCoef, int type, const int maxDimSize, const double currentW) +void addInfoToVectors(map> &loopInfo, SgForStmt *position, SgSymbol *symb, + const int dimNum, const pair newCoef, int type, const int maxDimSize, const double currentW) { auto itLoop = loopInfo.find(position); if (itLoop == loopInfo.end()) @@ -159,10 +158,10 @@ static void addInfoToVectors(map> &loopInf } -static vector matchSubscriptToLoopSymbols(const vector &parentLoops, SgExpression *subscr, - SgArrayRefExp *arrayRefIn, const int side, const int dimNum, - map> &loopInfo, - const int currLine, const int numOfSubscriptions, const double currentW) +vector matchSubscriptToLoopSymbols(const vector &parentLoops, SgExpression *subscr, + SgArrayRefExp *arrayRefIn, const int side, const int dimNum, + map> &loopInfo, + const int currLine, const int numOfSubscriptions, const double currentW) { SgExpression *origSubscr = subscr; ArrayRefExp *arrayRef = new ArrayRefExp(arrayRefIn); @@ -557,7 +556,7 @@ static void mapArrayRef(SgStatement* currentSt, SgExpression* currExp, __spf_print(PRINT_ARRAY_ARCS, "\n"); } -void findArrayRef(const vector &parentLoops, SgExpression *currExp, const int lineNum, const int side, +static void findArrayRef(const vector &parentLoops, SgExpression *currExp, const int lineNum, const int side, map> &loopInfo, const set &privatesVars, vector>& privatesVarsForLoop, map &sortedLoopGraph, const map> &commonBlocks, diff --git a/sapfor/experts/Sapfor_2017/_src/LoopAnalyzer/loop_analyzer_internal.h b/sapfor/experts/Sapfor_2017/_src/LoopAnalyzer/loop_analyzer_internal.h index a01cb76..9ab28a7 100644 --- a/sapfor/experts/Sapfor_2017/_src/LoopAnalyzer/loop_analyzer_internal.h +++ b/sapfor/experts/Sapfor_2017/_src/LoopAnalyzer/loop_analyzer_internal.h @@ -58,13 +58,13 @@ void changeLoopWeight(double& currentWeight, const std::map& so SgStatement* takeOutConditions(std::stack& conditions, std::stack& ifBlocks, SgStatement* st); -void findArrayRef(const std::vector& parentLoops, SgExpression* currExp, const int lineNum, const int side, - std::map>& loopInfo, const std::set& privatesVars, - std::vector>& privatesVarsForLoop, std::map& sortedLoopGraph, - const std::map>& commonBlocks, - const std::map, std::pair>& declaredArrays, - bool wasDistributedArrayRef, std::map>& notMappedDistributedArrays, - std::set& mappedDistrbutedArrays, SgStatement* currentSt, const ParallelRegion* reg, const double currentW, - const std::map>& arrayLinksByFuncCalls); +enum { READ_OP, WRITE_OP, UNREC_OP }; +void addInfoToVectors(std::map>& loopInfo, SgForStmt* position, SgSymbol* symb, + const int dimNum, const std::pair newCoef, int type, const int maxDimSize, const double currentW); + +std::vector matchSubscriptToLoopSymbols(const std::vector& parentLoops, SgExpression* subscr, + SgArrayRefExp* arrayRefIn, const int side, const int dimNum, + std::map>& loopInfo, + const int currLine, const int numOfSubscriptions, const double currentW); bool hasNonPureFunctions(SgExpression* ex, LoopGraph* loopRef, std::vector& messagesForFile, const int line, const std::map& funcByName); \ No newline at end of file diff --git a/sapfor/experts/Sapfor_2017/_src/LoopAnalyzer/loop_analyzer_nodist.cpp b/sapfor/experts/Sapfor_2017/_src/LoopAnalyzer/loop_analyzer_nodist.cpp index 777b7a8..a91df00 100644 --- a/sapfor/experts/Sapfor_2017/_src/LoopAnalyzer/loop_analyzer_nodist.cpp +++ b/sapfor/experts/Sapfor_2017/_src/LoopAnalyzer/loop_analyzer_nodist.cpp @@ -36,7 +36,6 @@ extern void createMapLoopGraph(map& sortedLoopGraph, const vect extern map> tableOfUniqNamesByArray; static void convertOneLoopNoDist(LoopGraph* currLoop, map>& outInfo, const map& toConvert, - const set& privateArrays, const map>& commonBlocks, const map, pair>& declaredArrays, const map>& arrayLinksByFuncCalls, @@ -109,7 +108,6 @@ static void convertOneLoopNoDist(LoopGraph* currLoop, map> convertLoopInfoNoDist(const map>& loopInfo, const map& sortedLoopGraph, - const set& privateArrays, const map>& commonBlocks, const map, pair>& declaredArrays, const map>& arrayLinksByFuncCalls, @@ -123,12 +121,188 @@ convertLoopInfoNoDist(const map>& loopInfo if (itGraph == sortedLoopGraph.end()) printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - convertOneLoopNoDist(itGraph->second, outInfo, it->second, privateArrays, commonBlocks, declaredArrays, arrayLinksByFuncCalls, createdArrays); + convertOneLoopNoDist(itGraph->second, outInfo, it->second, commonBlocks, declaredArrays, arrayLinksByFuncCalls, createdArrays); } return outInfo; } +static vector matchArrayToLoopSymbols(const vector &parentLoops, vector>& privatesVarsForLoop, + SgExpression *currExp, const int side, + map> &loopInfo, const int currLine, + map &sortedLoopGraph, const ParallelRegion *reg, const double currentW, + const map> &arrayLinksByFuncCalls) +{ + SgArrayRefExp *arrayRef = (SgArrayRefExp*)currExp; + int numOfSubs = arrayRef->numberOfSubscripts(); + + currExp = currExp->lhs(); + vector wasFoundForLoop(parentLoops.size()); + vector matched(numOfSubs); + vector matchedToDim(parentLoops.size()); + std::fill(wasFoundForLoop.begin(), wasFoundForLoop.end(), 0); + std::fill(matched.begin(), matched.end(), -1); + std::fill(matchedToDim.begin(), matchedToDim.end(), -1); + int maxMatched = 0; + int sumMatched = 0; + + for (int i = 0; i < numOfSubs; ++i) + { + vector matchToLoops = matchSubscriptToLoopSymbols(parentLoops, currExp->lhs(), arrayRef, side, i, loopInfo, currLine, numOfSubs, currentW); + for (int k = 0; k < matchToLoops.size(); ++k) + { + wasFoundForLoop[matchToLoops[k]]++; + matchedToDim[matchToLoops[k]] = i; + } + + matched[i] = matchToLoops.size(); + sumMatched += matchToLoops.size(); + maxMatched = std::max(maxMatched, (int)matchToLoops.size()); + currExp = currExp->rhs(); + } + + //full array is used, add unknown operations to all loops + if (numOfSubs == 0) + { + SgSymbol *currOrigArrayS = OriginalSymbol(arrayRef->symbol()); + auto arrType = isSgArrayType(currOrigArrayS->type()); + if (arrType != NULL) + { + for (int d = 0; d < arrType->dimension(); ++d) + for (int i = 0; i < parentLoops.size(); ++i) + addInfoToVectors(loopInfo, parentLoops[i], currOrigArrayS, d, make_pair(0, 0), UNREC_OP, arrType->dimension(), currentW); + } + } + + bool ifUnknownArrayAssignFound = false; + vector canNotMapToLoop; + for (int i = 0; i < wasFoundForLoop.size(); ++i) + { + if (wasFoundForLoop[i] != 1 && + privatesVarsForLoop[i].find(string(arrayRef->symbol()->identifier())) == privatesVarsForLoop[i].end()) + { + auto itLoop = sortedLoopGraph.find(parentLoops[i]->lineNumber()); + if (itLoop == sortedLoopGraph.end()) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + ifUnknownArrayAssignFound = true; + if (side == LEFT) + itLoop->second->hasUnknownArrayAssigns = true; + + itLoop->second->hasUnknownDistributedMap = true; + canNotMapToLoop.push_back(parentLoops[i]->lineNumber()); + } + } + + if (side == LEFT) + { + if (ifUnknownArrayAssignFound) + { + const string arrayRefS = arrayRef->unparse(); + for (auto &line : canNotMapToLoop) + { + __spf_print(1, "WARN: can not map write to array '%s' to loop on line %d\n", arrayRefS.c_str(), line); + wstring messageE, messageR; + __spf_printToLongBuf(messageE, L"can not map write to array '%s' to this loop", to_wstring(arrayRefS).c_str()); + + __spf_printToLongBuf(messageR, R59, to_wstring(arrayRefS).c_str()); + + if (line > 0) + currMessages->push_back(Messages(WARR, line, messageR, messageE, 1025)); + } + } + } + + return wasFoundForLoop; +} + +static void mapArrayRef(SgStatement* currentSt, SgExpression* currExp, + const vector& parentLoops, const int side, const int lineNum, + map>& loopInfo, + vector>& privatesVarsForLoop, + map& sortedLoopGraph, map>& notMappedDistributedArrays, + set& mappedDistrbutedArrays, + const ParallelRegion* reg, const double currentW, const map>& arrayLinksByFuncCalls) +{ + const char* printSide = NULL; + if (PRINT_ARRAY_ARCS) + printBlanks(2, (int)parentLoops.size()); + if (side == LEFT) + printSide = "W_OP"; + else + printSide = "R_OP"; + + __spf_print(PRINT_ARRAY_ARCS, "%s to array <%s> on line %d: ", printSide, OriginalSymbol(currExp->symbol())->identifier(), lineNum); + bool wasMapped = false; + vector matched = matchArrayToLoopSymbols(parentLoops, privatesVarsForLoop, currExp, side, loopInfo, lineNum, sortedLoopGraph, reg, currentW, arrayLinksByFuncCalls); + for (int z = 0; z < matched.size(); ++z) + wasMapped |= (matched[z] != 0); + + if (parentLoops.size() == 0) + { + SgSymbol* symb = currExp->symbol(); + if (symb->type()->variant() == T_ARRAY) + notMappedDistributedArrays[symb->identifier()] = make_pair(symb, currentSt); + } + else + { + if (wasMapped) + mappedDistrbutedArrays.insert(currExp->symbol()->identifier()); + else + { + SgSymbol* symb = currExp->symbol(); + if (symb->type()->variant() == T_ARRAY) + notMappedDistributedArrays[symb->identifier()] = make_pair(symb, currentSt); + } + } + __spf_print(PRINT_ARRAY_ARCS, "\n"); +} + +static void findArrayRef(const vector& parentLoops, SgExpression* currExp, const int lineNum, const int side, + map>& loopInfo, + vector>& privatesVarsForLoop, map& sortedLoopGraph, + map>& notMappedDistributedArrays, + set& mappedDistrbutedArrays, SgStatement* currentSt, const ParallelRegion* reg, const double currentW, + const map>& arrayLinksByFuncCalls) +{ + int nextSide = side; + if (isArrayRef(currExp)) + { + mapArrayRef(currentSt, currExp, parentLoops, side, lineNum, loopInfo, privatesVarsForLoop, sortedLoopGraph, + notMappedDistributedArrays, mappedDistrbutedArrays, reg, currentW, arrayLinksByFuncCalls); + nextSide = (side == LEFT) ? RIGHT : side; + } + + bool needToContinue = true; + if (currExp->variant() == FUNC_CALL) + { + SgFunctionCallExp* funcExp = (SgFunctionCallExp*)currExp; + auto currFunc = isUserFunctionInProject(funcExp->funName()->identifier()); + if (currFunc) + { + for (int z = 0; z < funcExp->numberOfArgs(); ++z) + { + if ((currFunc->funcParams.inout_types[z] & OUT_BIT) != 0) + nextSide = LEFT; + else + nextSide = RIGHT; + findArrayRef(parentLoops, funcExp->arg(z), lineNum, nextSide, loopInfo, privatesVarsForLoop, sortedLoopGraph, + notMappedDistributedArrays, mappedDistrbutedArrays, currentSt, reg, currentW, arrayLinksByFuncCalls); + } + needToContinue = false; + } + } + + if (needToContinue) + { + if (currExp->lhs()) + findArrayRef(parentLoops, currExp->lhs(), lineNum, nextSide, loopInfo, privatesVarsForLoop, sortedLoopGraph, + notMappedDistributedArrays, mappedDistrbutedArrays, currentSt, reg, currentW, arrayLinksByFuncCalls); + if (currExp->rhs()) + findArrayRef(parentLoops, currExp->rhs(), lineNum, nextSide, loopInfo, privatesVarsForLoop, sortedLoopGraph, + notMappedDistributedArrays, mappedDistrbutedArrays, currentSt, reg, currentW, arrayLinksByFuncCalls); + } +} + void loopAnalyzerNoDist(SgFile* file, vector& regions, map, DIST::Array*>& createdArrays, vector& messagesForFile, const map>& AllfuncInfo, const map, pair>& declaredArrays, @@ -187,8 +361,6 @@ void loopAnalyzerNoDist(SgFile* file, vector& regions, map> loopInfo; set loopWithOutArrays; - set privatesVars; - SgStatement* st = file->functions(i); string funcName = ""; if (st->variant() == PROG_HEDR) @@ -210,6 +382,9 @@ void loopAnalyzerNoDist(SgFile* file, vector& regions, mapsymbol()->identifier(); } + if (funcName == string("interp")) + int a = 0; + vector loopsForFunction; for (auto& loop : *loopGraph) { @@ -269,7 +444,7 @@ void loopAnalyzerNoDist(SgFile* file, vector& regions, mapvariant(); if (currV == FOR_NODE) { - tryToFindPrivateInAttributes(st, privatesVars); + //tryToFindPrivateInAttributes(st, privatesVars); set toAdd; tryToFindPrivateInAttributes(st, toAdd); @@ -312,12 +487,12 @@ void loopAnalyzerNoDist(SgFile* file, vector& regions, mapexpr(0)) - findArrayRef(parentLoops, st->expr(0), st->lineNumber(), LEFT, loopInfo, privatesVars, privatesVarsForLoop, - sortedLoopGraph, commonBlocks, declaredArrays, false, notMappedDistributedArrays, + findArrayRef(parentLoops, st->expr(0), st->lineNumber(), LEFT, loopInfo, privatesVarsForLoop, + sortedLoopGraph, notMappedDistributedArrays, mappedDistrbutedArrays, st, currReg, currentWeight, arrayLinksByFuncCalls); if (st->expr(1)) - findArrayRef(parentLoops, st->expr(1), st->lineNumber(), RIGHT, loopInfo, privatesVars, privatesVarsForLoop, - sortedLoopGraph, commonBlocks, declaredArrays, false, notMappedDistributedArrays, + findArrayRef(parentLoops, st->expr(1), st->lineNumber(), RIGHT, loopInfo, privatesVarsForLoop, + sortedLoopGraph, notMappedDistributedArrays, mappedDistrbutedArrays, st, currReg, currentWeight, arrayLinksByFuncCalls); } else if (currV == IF_NODE || currV == ELSEIF_NODE || currV == LOGIF_NODE || currV == SWITCH_NODE) @@ -325,8 +500,8 @@ void loopAnalyzerNoDist(SgFile* file, vector& regions, mapexpr(0)) { - findArrayRef(parentLoops, st->expr(0), st->lineNumber(), RIGHT, loopInfo, privatesVars, privatesVarsForLoop, - sortedLoopGraph, commonBlocks, declaredArrays, false, notMappedDistributedArrays, + findArrayRef(parentLoops, st->expr(0), st->lineNumber(), RIGHT, loopInfo, privatesVarsForLoop, + sortedLoopGraph, notMappedDistributedArrays, mappedDistrbutedArrays, st, currReg, currentWeight, arrayLinksByFuncCalls); } } @@ -344,12 +519,12 @@ void loopAnalyzerNoDist(SgFile* file, vector& regions, mapelem(z); if ((func->funcParams.inout_types[z] & OUT_BIT) != 0) - findArrayRef(parentLoops, par, st->lineNumber(), LEFT, loopInfo, privatesVars, privatesVarsForLoop, - sortedLoopGraph, commonBlocks, declaredArrays, false, notMappedDistributedArrays, + findArrayRef(parentLoops, par, st->lineNumber(), LEFT, loopInfo, privatesVarsForLoop, + sortedLoopGraph, notMappedDistributedArrays, mappedDistrbutedArrays, st, currReg, currentWeight, arrayLinksByFuncCalls); else - findArrayRef(parentLoops, par, st->lineNumber(), RIGHT, loopInfo, privatesVars, privatesVarsForLoop, - sortedLoopGraph, commonBlocks, declaredArrays, false, notMappedDistributedArrays, + findArrayRef(parentLoops, par, st->lineNumber(), RIGHT, loopInfo, privatesVarsForLoop, + sortedLoopGraph, notMappedDistributedArrays, mappedDistrbutedArrays, st, currReg, currentWeight, arrayLinksByFuncCalls); } @@ -385,8 +560,8 @@ void loopAnalyzerNoDist(SgFile* file, vector& regions, mapexpr(z)) - findArrayRef(parentLoops, st->expr(z), st->lineNumber(), side, loopInfo, privatesVars, privatesVarsForLoop, - sortedLoopGraph, commonBlocks, declaredArrays, false, notMappedDistributedArrays, + findArrayRef(parentLoops, st->expr(z), st->lineNumber(), side, loopInfo, privatesVarsForLoop, + sortedLoopGraph, notMappedDistributedArrays, mappedDistrbutedArrays, st, currReg, currentWeight, arrayLinksByFuncCalls); } @@ -395,7 +570,7 @@ void loopAnalyzerNoDist(SgFile* file, vector& regions, maplexNext(); } - auto convertedLoopInfo = convertLoopInfoNoDist(loopInfo, sortedLoopGraph, privatesVars, commonBlocks, declaredArrays, arrayLinksByFuncCalls, createdArrays); + auto convertedLoopInfo = convertLoopInfoNoDist(loopInfo, sortedLoopGraph, commonBlocks, declaredArrays, arrayLinksByFuncCalls, createdArrays); processLoopInformationForFunction(convertedLoopInfo); @@ -469,8 +644,26 @@ void loopAnalyzerNoDist(SgFile* file, vector& regions, mapwithoutDistributedArrays && loopRef->region && !loopRef->hasLimitsToParallel() && loopRef->lineNum > 0) + { + int nesting = 0; + LoopGraph* it = loopRef; + for (int z = 0; z < loopRef->perfectLoop; ++z, it->children.size() ? it = it->children[0] : it) + if (it->withoutDistributedArrays && it->region && !it->hasLimitsToParallel() && it->lineNum > 0) + ++nesting; + + map> convertedLoopInfo; + + it = loopRef; + for (int z = 0; z < nesting; ++z, it->children.size() ? it = it->children[0] : it) + convertedLoopInfo.insert(make_pair(it, map())); + + createParallelDirectivesNoDist(convertedLoopInfo, regions, map>(), messagesForFile); + } + } __spf_print(PRINT_PROF_INFO, "Function ended\n"); }