From 22c281398a47b3039204d3473ece319c1926ef11 Mon Sep 17 00:00:00 2001 From: AntonMilienkov Date: Fri, 19 Apr 2024 14:35:58 +0300 Subject: [PATCH] added load/asve blocks fixed creation module --- .../_src/Transformations/checkpoints.cpp | 224 +++++++++++++----- 1 file changed, 164 insertions(+), 60 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/checkpoints.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/checkpoints.cpp index 4cd4c7a..44b7f11 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/checkpoints.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/checkpoints.cpp @@ -76,7 +76,7 @@ static void findDecls(SgExpression* ex, vector& local, const map< static FuncInfo* findFileInfoByName(SgStatement* func, const vector& allFuncInfo) { FuncInfo* funcI = NULL; - for (auto& funcs : allFuncInfo) + for (const auto& funcs : allFuncInfo) if (funcs->funcName == func->symbol()->identifier()) funcI = funcs; checkNull(funcI, convertFileName(__FILE__).c_str(), __LINE__); @@ -462,7 +462,7 @@ static int insertInitNamesOfFiles(const int numOfFiles, const string& additional if (before) insert->insertStmtBefore(*new SgAssignStmt(*new SgArrayRefExp(*files, *new SgValueExp(z)), *new SgValueExp(tmp.c_str())), *insert->controlParent()); else - insert->insertStmtAfter(*new SgAssignStmt(*new SgArrayRefExp(*files, *new SgValueExp(z)), *new SgValueExp(tmp.c_str())), *insert); + insert->insertStmtAfter(*new SgAssignStmt(*new SgArrayRefExp(*files, *new SgValueExp(z)), *new SgValueExp(tmp.c_str())), *insert->controlParent()); } } @@ -472,7 +472,7 @@ static int insertInitNamesOfFiles(const int numOfFiles, const string& additional if (before) insert->insertStmtBefore(*new SgAssignStmt(*journal, *new SgValueExp(tmp.c_str())), *insert->controlParent()); else - insert->insertStmtAfter(*new SgAssignStmt(*journal, *new SgValueExp(tmp.c_str())), *insert); + insert->insertStmtAfter(*new SgAssignStmt(*journal, *new SgValueExp(tmp.c_str())), *insert->controlParent()); } return maxFileLen; @@ -744,7 +744,15 @@ static void processModules(SgFile* file, const vector& allFuncInfo) findModulesInFile(file, modules); for (auto& mod : modules) { - if (!strcmp(mod->symbol()->identifier(), "spf_module_checkpoint")) + /*if (!strcmp(mod->symbol()->identifier(), "spf_module_checkpoint")) { + std::cout << "was here" << std::endl; + continue; + }*/ + + char pref[5]; + strncpy(pref, mod->symbol()->identifier(), 4); + pref[4] = '\0'; + if (!strcmp(pref, "spf_")) continue; bool hasContains = false; @@ -782,14 +790,21 @@ static void processModules(SgFile* file, const vector& allFuncInfo) } } -static SgSymbol* renamedNewSymb(SgSymbol* oldSymb, const char* prefix) { - int lenPrefix = strlen(prefix); - int lenSymbol = strlen(oldSymb->identifier()); - char* result = new char[lenPrefix + lenSymbol + 2]; +static char* mergedChar(const char* l, const char* r) +{ + int lenL = strlen(l); + int lenR = strlen(r); + char* result = new char[lenL + lenR + 2]; - strcpy(result, prefix); + strcpy(result, l); strcat(result, "_"); - strcat(result, oldSymb->identifier()); + strcat(result, r); + + return result; +} + +static SgSymbol* renamedNewSymb(SgSymbol* oldSymb, const char* prefix) { + char* result = mergedChar(prefix, oldSymb->identifier()); SgSymbol* newSymb = new SgSymbol(oldSymb->variant(), result, oldSymb->type(), oldSymb->scope()); @@ -813,14 +828,16 @@ static void renameEx(SgExpression* sizeEx, const char* funcName) { static void findSizeEx(SgExpression* sizeEx, const map& moduleStmts, const map& moduleParamStmts, SgStatement* proc_moduleF, set& addedModuleParams) { if (sizeEx->variant() == CONST_REF) { - if (moduleParamStmts.count(sizeEx->symbol()->identifier()) == 1) { + if (moduleParamStmts.count(sizeEx->symbol()->identifier()) == 1 && addedModuleParams.count(sizeEx->symbol()->identifier()) == 0) { auto decl = (SgParameterStmt*)moduleParamStmts.at(sizeEx->symbol()->identifier()); SgStatement* copyParamStmt = moduleParamStmts.at(sizeEx->symbol()->identifier())->copyPtr(); proc_moduleF->insertStmtAfter(*copyParamStmt, *proc_moduleF); - SgStatement* copyStmt = moduleStmts.at(sizeEx->symbol()->identifier())->copyPtr(); - proc_moduleF->insertStmtAfter(*copyStmt, *proc_moduleF); + if (moduleStmts.count(sizeEx->symbol()->identifier())) { + SgStatement* copyStmt = moduleStmts.at(sizeEx->symbol()->identifier())->copyPtr(); + proc_moduleF->insertStmtAfter(*copyStmt, *proc_moduleF); + } addedModuleParams.insert(sizeEx->symbol()->identifier()); @@ -867,12 +884,7 @@ static void processVarStmt(SgStatement* st, map& moduleStm { lhs = ex->lhs(); - int lenSymbol = strlen(lhs->symbol()->identifier()); - char* result = new char[lenFuncName + lenSymbol + 2]; - - strcpy(result, funcName); - strcat(result, "_"); - strcat(result, lhs->symbol()->identifier()); + char* result = mergedChar(funcName, lhs->symbol()->identifier()); SgSymbol* symb = new SgSymbol(lhs->symbol()->variant(), result, lhs->symbol()->type(), lhs->symbol()->scope()); SgExpression* newExpr = new SgExpression(lhs->variant()); @@ -932,36 +944,58 @@ static void processParamStmt(SgStatement* st, map& moduleP } } -static void processVarBlock(SgStatement* func, SgStatement* firstExec, map& moduleStmts, map& moduleParamStmts, set& commonVariables) +static void processExternStmt(SgStatement* st, set& externVars) +{ + if (st->expr(0)) + { + SgExpression* ex = st->expr(0); + + SgExpression* lhs; + while (ex && ex->lhs()) + { + lhs = ex->lhs(); + externVars.insert(lhs->sunparse()); + + ex = ex->rhs(); + } + } +} + +static void processVarBlock(SgStatement* func, SgStatement* firstExec, map& moduleStmts, + map& moduleParamStmts, set& commonVariables, + set& externVars) { const char* funcName = func->symbol()->identifier(); for (SgStatement* st = func->lexNext(); st != firstExec; st = st->lexNext()) { - if (st->variant() == COMM_STAT) + if (st->variant() == COMM_STAT) processCommonStmt(st, commonVariables); else if (st->variant() == VAR_DECL || st->variant() == VAR_DECL_90) processVarStmt(st, moduleStmts, funcName); else if (st->variant() == PARAM_DECL) processParamStmt(st, moduleParamStmts, funcName); + else if (st->variant() == EXTERN_STAT) + processExternStmt(st, externVars); } } -static void insertStmtToModule(map& moduleStmts, map& moduleParamStmts, set& addedModuleParams, set& commonVariables, SgStatement* proc_moduleF) +static void insertStmtToModule(const map& moduleStmts, const map& moduleParamStmts, + set& addedModuleParams, const set& commonVariables, SgStatement* proc_moduleF, + set& localVarNoParams, const set& externVars) { SgStatement* endProcModuleF = proc_moduleF->lastNodeOfStmt(); - endProcModuleF->unparsestdout(); - for (const auto& [varName, varStmt] : moduleStmts) { string varNameNoPref = varName; - string::size_type pos{}; pos = varName.find_first_of("_", pos); varNameNoPref.erase(0, pos + 1); - if ((commonVariables.count(varNameNoPref) == 0) && (moduleParamStmts.count(varName) == 0)) { + if (commonVariables.count(varNameNoPref) == 0 && moduleParamStmts.count(varName) == 0 + && externVars.count(varNameNoPref) == 0) { + localVarNoParams.insert(varNameNoPref); endProcModuleF->insertStmtBefore(*varStmt, *proc_moduleF); if (varStmt->expr(0)->variant() == ARRAY_REF) { @@ -1052,40 +1086,36 @@ void createCheckpoints(SgFile* file, const map& commonBloc set addedToList; findLocalData(func, firstExec, local, localParams, addedToList, allFuncInfo); - // external perems? - const char* funcName = func->symbol()->identifier(); int lenFuncName = strlen(funcName); SgStatement* proc_moduleF = NULL; - const string proc_cpModule = "spf_module_" + string(funcName); - SgSymbol* proc_mainModuleCpS = new SgSymbol(MODULE_NAME, proc_cpModule.c_str()); - proc_moduleF = new SgStatement(MODULE_STMT, NULL, proc_mainModuleCpS); - moduleF->insertStmtAfter(*proc_moduleF, *global); - - SgStatement* endProcModuleF = new SgStatement(CONTROL_END); - proc_moduleF->insertStmtAfter(*endProcModuleF, *proc_moduleF); - + const string proc_cpModule = "spf_module_" + string(funcName);; + createModule(proc_moduleF, proc_cpModule); + SgSymbol* proc_mainModuleCpS = proc_moduleF->symbol(); + //SgStatement* endProcModuleF = proc_moduleF->lastNodeOfStmt(); + map moduleStmts; map moduleParamStmts; set commonVariables; - - processVarBlock(func, firstExec, moduleStmts, moduleParamStmts, commonVariables); + set externVars; + processVarBlock(func, firstExec, moduleStmts, moduleParamStmts, commonVariables, externVars); set addedModuleParams; - insertStmtToModule(moduleStmts, moduleParamStmts, addedModuleParams, commonVariables, proc_moduleF); + set localVarNoParams; + insertStmtToModule(moduleStmts, moduleParamStmts, addedModuleParams, commonVariables, proc_moduleF, localVarNoParams, externVars); const vector useOfMods = findUseOfModules(func->lexNext(), firstExec); SgStatement* loadBlock = new SgStatement(IF_NODE); SgStatement* storeBlock = new SgStatement(IF_NODE); - point->insertStmtBefore(*loadBlock, *point->controlParent()); + firstExec->insertStmtBefore(*loadBlock, *firstExec->controlParent()); point->insertStmtBefore(*storeBlock, *point->controlParent()); loadBlock->addComment("! LOAD CHECKPOINT\n"); - auto loadblockLab = new SgLabel(labNum); - loadBlock->setLabel(*loadblockLab); + //auto loadblockLab = new SgLabel(labNum); + //loadBlock->setLabel(*loadblockLab); string filesS = "spf_cp_files" + additional; SgSymbol* files = new SgSymbol(VARIABLE_NAME, filesS.c_str(), createArrayCharType(32, numOfFiles + 1), func); @@ -1103,7 +1133,7 @@ void createCheckpoints(SgFile* file, const map& commonBloc profS.push_back(new SgSymbol(VARIABLE_NAME, profSs.c_str(), SgTypeFloat(), func)); profS.push_back(new SgSymbol(VARIABLE_NAME, profEs.c_str(), SgTypeFloat(), func)); - SgSymbol* timeF = new SgSymbol(FUNCTION_NAME, "omp_get_wtime", SgTypeDouble(), func); // OR dvtime + SgSymbol* timeF = new SgSymbol(FUNCTION_NAME, "omp_get_wtime", SgTypeDouble(), func); // OR dvtime SgStatement* profCallS = new SgAssignStmt(*new SgVarRefExp(profS[0]), *new SgFunctionCallExp(*timeF)); SgStatement* profCallE = new SgAssignStmt(*new SgVarRefExp(profS[1]), *new SgFunctionCallExp(*timeF)); @@ -1171,12 +1201,16 @@ void createCheckpoints(SgFile* file, const map& commonBloc loadS.push_back(new SgSymbol(VARIABLE_NAME, loadLabelS.c_str(), SgTypeInt(), func)); initLoadS.push_back(new SgValueExp(0)); + string loadFS = "spf_cp_load_flag" + additional; + loadS.push_back(new SgSymbol(VARIABLE_NAME, loadFS.c_str(), SgTypeInt(), func)); + initLoadS.push_back(new SgValueExp(1)); + string saveFS = "spf_cp_save_flag" + additional; loadS.push_back(new SgSymbol(VARIABLE_NAME, saveFS.c_str(), SgTypeInt(), func)); initLoadS.push_back(new SgValueExp(0)); - loadBlock->setExpression(0, *new SgVarRefExp(loadS[0]) == *new SgValueExp(0)); - SgAssignStmt* init = new SgAssignStmt(*new SgVarRefExp(loadS[0]), *new SgValueExp(1)); + loadBlock->setExpression(0, *new SgVarRefExp(loadS[4]) == *new SgValueExp(1)); //*new SgVarRefExp(loadS[0]) + SgAssignStmt* init = new SgAssignStmt(*new SgVarRefExp(loadS[4]), *new SgValueExp(0)); insertToLoadS.push_back(init); @@ -1186,6 +1220,7 @@ void createCheckpoints(SgFile* file, const map& commonBloc SgExpression& frmt = SgAssignOp(*new SgKeywordValExp("fmt"), *new SgKeywordValExp("*")); SgExpression& frmtProf = SgAssignOp(*new SgKeywordValExp("fmt"), *new SgValueExp(("(A,A" + to_string(maxFileLen) + ",F4.2,A)").c_str())); + SgExpression* labelIdx = new SgVarRefExp(loadS[3]); SgExpression* iostat = new SgVarRefExp(loadS[2]); SgExpression* fileIdx = new SgVarRefExp(loadS[1]); @@ -1195,15 +1230,21 @@ void createCheckpoints(SgFile* file, const map& commonBloc SgIfStmt* ifLoadOk = new SgIfStmt(*iostat == *new SgValueExp(0)); insertToLoadS.push_back(ifLoadOk); - SgInputOutputStmt* read = new SgInputOutputStmt(READ_STAT, *makeExprList({ &frmt, &unit }, false), *fileIdx); + SgInputOutputStmt* read = new SgInputOutputStmt(READ_STAT, *makeExprList({ &frmt, &unit }, false), *fileIdx); //*makeExprList({ fileIdx, labelIdx }) insertToifLoadOk.push_back(read); insertToifLoadOk.push_back(new SgIOControlStmt(CLOSE_STAT, unit)); insertToifLoadOk.push_back(createOpen(iostat, files, fileIdx, unit)); - SgIfStmt* ifLoadOk1 = new SgIfStmt(*iostat == *new SgValueExp(0), *new SgIOControlStmt(CLOSE_STAT, unit)); - + SgIfStmt* ifLoadOk1 = new SgIfStmt(*iostat == *new SgValueExp(0)); //, *new SgIOControlStmt(CLOSE_STAT, unit) insertToifLoadOk.push_back(ifLoadOk1); + + auto nextStLab = new SgLabel(labNum); + ifLoadOk1->insertStmtAfter(*new SgGotoStmt(*nextStLab), *ifLoadOk1); + point->setLabel(*nextStLab); + + ifLoadOk1->insertStmtAfter(*new SgIOControlStmt(CLOSE_STAT, unit), *ifLoadOk1); + ifLoadOk1->insertStmtAfter(*new SgIfStmt(*fileIdx == *new SgValueExp(numOfFiles + 1), *new SgAssignStmt(*fileIdx, *new SgValueExp(1))), *ifLoadOk1); ifLoadOk1->insertStmtAfter(*new SgAssignStmt(*fileIdx, *fileIdx + *new SgValueExp(1)), *ifLoadOk1); ifLoadOk1->addComment("! LOAD DATA FROM CHECKPOINT\n"); // program sleep (frozed) here @@ -1216,6 +1257,16 @@ void createCheckpoints(SgFile* file, const map& commonBloc ifLoadOk1->insertStmtAfter(*new SgInputOutputStmt(WRITE_STAT, *makeExprList({ &frmtProf, &unitNull }, false), *makeExprList(commentArgs, false)), *ifLoadOk1); ifLoadOk1->insertStmtAfter(profCallE->copy(), *ifLoadOk1); + + // insert copy_block + for (auto localVar : localVarNoParams) + { + SgSymbol* leftSymb = new SgSymbol(VARIABLE_NAME, localVar.c_str()); + SgExpression* leftEx = new SgVarRefExp(leftSymb); + SgSymbol* rightSymb = renamedNewSymb(leftSymb, funcName); + SgExpression* rightEx = new SgVarRefExp(rightSymb); + ifLoadOk1->insertStmtAfter(*new SgAssignStmt(*leftEx, *rightEx), *ifLoadOk1); + } //open all files if (createdModuleForIO) @@ -1224,6 +1275,22 @@ void createCheckpoints(SgFile* file, const map& commonBloc ifLoadOk1->insertStmtAfter(*call, *ifLoadOk1); } + //READ LOCAL DATA + vector variablesVec; + if (moduleStmts.size()) + { + for (auto localVar : localVarNoParams) + { + const char* varName = localVar.c_str(); + char* varNameWithPref = mergedChar(funcName, varName); + SgSymbol* symbVar = new SgSymbol(VARIABLE_NAME, varNameWithPref); + SgExpression* exVar = new SgVarRefExp(symbVar); + variablesVec.push_back(exVar); + } + auto dataRead = new SgInputOutputStmt(READ_STAT, unit, *makeExprList(variablesVec, false)); + ifLoadOk1->insertStmtAfter(*dataRead, *ifLoadOk1); + } + //READ from modules for (auto& mod : moduleNames) { @@ -1233,12 +1300,26 @@ void createCheckpoints(SgFile* file, const map& commonBloc ifLoadOk1->insertStmtAfter(*call, *ifLoadOk1); } - //READ DATA + //READ COMMON DATA + vector commanVariablesVec; + if (commonVariables.size()) + { + for (auto commanVar : commonVariables) + { + SgSymbol* symbCommon = new SgSymbol(VARIABLE_NAME, commanVar.c_str()); + SgExpression* exCommon = new SgVarRefExp(symbCommon); + commanVariablesVec.push_back(exCommon); + } + auto commonDataRead = new SgInputOutputStmt(READ_STAT, unit, *makeExprList(commanVariablesVec, false)); + ifLoadOk1->insertStmtAfter(*commonDataRead, *ifLoadOk1); + } + + /* if (local.size()) { auto dataRead = new SgInputOutputStmt(READ_STAT, unit, *makeExprList(local, false)); ifLoadOk1->insertStmtAfter(*dataRead, *ifLoadOk1); - } + } */ ifLoadOk1->insertStmtAfter(profCallS->copy(), *ifLoadOk1); @@ -1293,12 +1374,35 @@ void createCheckpoints(SgFile* file, const map& commonBloc } //WRITE DATA + if (variablesVec.size()) + { + auto dataWrite = new SgInputOutputStmt(WRITE_STAT, unit, *makeExprList(variablesVec, false)); + ifStoreOk->insertStmtAfter(*dataWrite, *ifStoreOk); + } + + if (commanVariablesVec.size()) + { + auto commonDataWrite = new SgInputOutputStmt(WRITE_STAT, unit, *makeExprList(commanVariablesVec, false)); + ifStoreOk->insertStmtAfter(*commonDataWrite, *ifStoreOk); + } + + + // insert copy_block + for (auto localVar : localVarNoParams) + { + SgSymbol* rightSymb = new SgSymbol(VARIABLE_NAME, localVar.c_str()); + SgExpression* rightEx = new SgVarRefExp(rightSymb); + SgSymbol* leftSymb = renamedNewSymb(rightSymb, funcName); + SgExpression* leftEx = new SgVarRefExp(leftSymb); + ifStoreOk->insertStmtAfter(*new SgAssignStmt(*leftEx, *rightEx), *ifStoreOk); + } + /* if (local.size()) { SgStatement* dataWrite = new SgInputOutputStmt(WRITE_STAT, unit, *makeExprList(local, false)); ifStoreOk->insertStmtAfter(*dataWrite, *ifStoreOk); } - + */ listSpec.clear(); listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("iostat"), *iostat)); listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("file"), *journal)); @@ -1313,18 +1417,18 @@ void createCheckpoints(SgFile* file, const map& commonBloc ifStoreOk->insertStmtAfter(profCallS->copy(), *ifStoreOk); - SgStatement* copyForGoto = loadBlock->copyPtr(); - copyForGoto->deleteLabel(); - firstExec->insertStmtBefore(*copyForGoto, *func); - insertInitNamesOfFiles(numOfFiles, additional, files, journal, copyForGoto, true); - copyForGoto->insertStmtAfter(insertToLoadS[insertToLoadS.size() - 1]->copy(), *copyForGoto); - copyForGoto->insertStmtAfter(insertToLoadS[insertToLoadS.size() - 2]->copy(), *copyForGoto); + insertInitNamesOfFiles(numOfFiles, additional, files, journal, lastDecl, false); + /* + SgStatement* timeFStmt = new SgStatement(VAR_DECL); + timeFStmt->setSymbol(*timeF); - copyForGoto = copyForGoto->lexNext()->lexNext(); + SgExpression* timeFExpr = new SgExpression(VAR_REF); + timeFStmt->setExpression(0, timeFExpr); - copyForGoto->insertStmtAfter(*new SgGotoStmt(*loadblockLab), *copyForGoto); - copyForGoto->insertStmtAfter(*new SgIOControlStmt(CLOSE_STAT, unit), *copyForGoto); + SgExpression* typeExpr = new SgExpression(DOUBLE_VAL); + timeFStmt->setExpression(1, typeExpr); + lastDecl->insertStmtBefore(*timeFStmt, *lastDecl->controlParent()); */ for (int z = insertToLoadS.size() - 1; z >= 0; --z) loadBlock->insertStmtAfter(*insertToLoadS[z], *loadBlock);