void setupMPIInit(SgSourceFile* cur_file) { //#include "mpi.h" SageInterface::insertHeader (cur_file, "mpi.h", false,true); SageInterface::insertHeader (cur_file, "libxomp_mpi.h", false, true); SgFunctionDeclaration* main_decl = findMain(cur_file); // TODO: handle multiple files, some of them don't have main() ROSE_ASSERT (main_decl != NULL); SgFunctionDefinition* main_def = main_decl->get_definition(); ROSE_ASSERT (main_def != NULL); SgBasicBlock* func_body = main_def->get_body(); ROSE_ASSERT (func_body != NULL); // Setup MPI SgStatement* decl_rank = buildStatementFromString("int _xomp_rank;", func_body); prependStatement(decl_rank, func_body); SgStatement* decl_nprocs= buildStatementFromString("int _xomp_nprocs;", func_body); prependStatement(decl_nprocs, func_body); // xomp_init_mpi (&argc, &argv, &_xomp_rank, &_xomp_nprocs); SgExprListExp * para_list = buildExprListExp (buildAddressOfOp (buildVarRefExp("argc", func_body)), buildAddressOfOp (buildVarRefExp("argv", func_body)), buildAddressOfOp (buildVarRefExp("_xomp_rank", func_body)), buildAddressOfOp (buildVarRefExp("_xomp_nprocs", func_body)) ); SgExprStatement* mpi_init_stmt = buildFunctionCallStmt ("xomp_init_mpi", buildIntType(), para_list, func_body); // SgStatement* last_decl = findLastDeclarationStatement (func_body); insertStatementAfter (decl_rank, mpi_init_stmt); }
//! Setup MPI finalize void setupMPIFinalize(SgSourceFile* sfile) { SgFunctionDeclaration* main_decl = findMain(sfile); // TODO: handle multiple files, some of them don't have main() ROSE_ASSERT (main_decl != NULL); SgFunctionDefinition* main_def = main_decl->get_definition(); ROSE_ASSERT (main_def != NULL); SgBasicBlock* func_body = main_def->get_body(); ROSE_ASSERT (func_body != NULL); SgStatement* mf = buildFunctionCallStmt ("MPI_Finalize", buildVoidType(), NULL, func_body ); instrumentEndOfFunction (main_decl, mf); }
VOID *dlopen(const char *path, int mode) { register ModulePtr mp; static void *mainModule; /* * Upon the first call register a terminate handler that will * close all libraries. Also get a reference to the main module * for use with loadbind. */ if (!mainModule) { if ((mainModule = findMain()) == NULL) return NULL; atexit(terminate); } /* * Scan the list of modules if we have the module already loaded. */ for (mp = modList; mp; mp = mp->next) if (strcmp(mp->name, path) == 0) { mp->refCnt++; return (VOID *) mp; } if ((mp = (ModulePtr)calloc(1, sizeof(*mp))) == NULL) { errvalid++; strcpy(errbuf, "calloc: "); strcat(errbuf, strerror(errno)); return (VOID *) NULL; } mp->name = malloc((unsigned) (strlen(path) + 1)); strcpy(mp->name, path); /* * load should be declared load(const char *...). Thus we * cast the path to a normal char *. Ugly. */ if ((mp->entry = (void *)load((char *)path, L_NOAUTODEFER, NULL)) == NULL) { free(mp->name); free(mp); errvalid++; strcpy(errbuf, "dlopen: "); strcat(errbuf, path); strcat(errbuf, ": "); /* * If AIX says the file is not executable, the error * can be further described by querying the loader about * the last error. */ if (errno == ENOEXEC) { char *tmp[BUFSIZ/sizeof(char *)]; if (loadquery(L_GETMESSAGES, tmp, sizeof(tmp)) == -1) strcpy(errbuf, strerror(errno)); else { char **p; for (p = tmp; *p; p++) caterr(*p); } } else strcat(errbuf, strerror(errno)); return (VOID *) NULL; } mp->refCnt = 1; mp->next = modList; modList = mp; if (loadbind(0, mainModule, mp->entry) == -1) { dlclose(mp); errvalid++; strcpy(errbuf, "loadbind: "); strcat(errbuf, strerror(errno)); return (VOID *) NULL; } /* * If the user wants global binding, loadbind against all other * loaded modules. */ if (mode & RTLD_GLOBAL) { register ModulePtr mp1; for (mp1 = mp->next; mp1; mp1 = mp1->next) if (loadbind(0, mp1->entry, mp->entry) == -1) { dlclose(mp); errvalid++; strcpy(errbuf, "loadbind: "); strcat(errbuf, strerror(errno)); return (VOID *) NULL; } } if (readExports(mp) == -1) { dlclose(mp); return (VOID *) NULL; } /* * If there is a dl_info structure, call the init function. */ if (mp->info = (struct dl_info *)dlsym(mp, "dl_info")) { if (mp->info->init) (*mp->info->init)(); } else errvalid = 0; /* * If the shared object was compiled using xlC we will need * to call static constructors (and later on dlclose destructors). */ if (mp->cdtors = (CdtorPtr)dlsym(mp, "__cdtors")) { while (mp->cdtors->init) { (*mp->cdtors->init)(); mp->cdtors++; } } else errvalid = 0; return (VOID *) mp; }
int main(int argc, char** argv) { //Parse command-line options ProgramOptions po(argc, argv); //Initialize the AST SgProject* project = new SgProject(argc, argv); ROSE_ASSERT(project); AstTests::runAllTests(project); //TODO switch on/off with command-line args //Insert MM-wrapper header file string mmHeader = po.getMMWrapperHeaderLocation(); insertHeader(mmHeader, PreprocessingInfo::after, false, getGlobalScope(findMain(project))); //Initialize set of system headers & compiler generated vars RegisterPointers::initialize(); //Add calls to registers sizes of static variables RegisterVars rv(project, mmHeader); rv.registerStaticVars(); //Used to accumulate all global variables set<SgInitializedName*> globalVars; //Add wrapper calls to each function/sub-function so that all pointers are registered Rose_STL_Container<SgNode*> pragmas = querySubTree(project, V_SgPragmaDeclaration); Rose_STL_Container<SgNode*>::const_iterator pragmaIt; Rose_STL_Container<SgNode*> funcCalls = querySubTree(project, V_SgFunctionCallExp); SgPragmaDeclaration* pragma; SgStatement* stmt; SgFunctionDeclaration* funcDecl; for(pragmaIt = pragmas.begin(); pragmaIt != pragmas.end(); pragmaIt++) { pragma = isSgPragmaDeclaration(*pragmaIt); ROSE_ASSERT(pragma); PragmaParser pp(pragma); if(pp.isPopcornPragma() && pp.getPragmaType() == PARTITIONED) { //Get function declaration stmt = getNextStatement(pragma); while(!isSgFunctionDeclaration(stmt)) stmt = getNextStatement(stmt); funcDecl = isSgFunctionDeclaration(stmt); ROSE_ASSERT(funcDecl); Pragmas pragmas(funcDecl); //Save global variables saveGlobalVariables(pragmas.getGlobalInputs(), globalVars, pragmas.getFunction()->get_scope()); saveGlobalVariables(pragmas.getGlobalOutputs(), globalVars, pragmas.getFunction()->get_scope()); //Update call sites //TODO I don't think we need this anymore, since all sizes are //handled through the mm_wrapper interface //FunctionCallUpdater fcu(funcDecl, funcCalls); //fcu.updateDeclaration(); //fcu.updateSites(); } } //Register/unregister global variables RegisterVars::registerGlobalVars(globalVars); //Add call to initialize wrapper SgFunctionDeclaration* main = findMain(project); ROSE_ASSERT(main); FunctionCallUpdater::insertInitWrapperCall(main); return backend(project); }
/* * Add calls to register and unregister expressions/arrays with the memory * management wrapper. */ bool RegisterPointers::addRegUnregCalls() { string msg; if(definedInSystemHeader) { msg = "\t\t\tVariable " + NAME(varSymbol) + " is in system headers"; WARNING(TOOL, msg); return false; } if(compilerGenerated) { msg = "\t\t\tVariable " + NAME(varSymbol) + " is compiler-generated"; WARNING(TOOL, msg); return false; } if(addrUsedInIO) { msg = "\t\t\tVariable " + NAME(varSymbol) + " has its address taken in " + "a function known to not require registering/unregistering"; WARNING(TOOL, msg); return false; } //Add register/unregister calls SgStatement* prevStmt = NULL; SgExprStatement* funcCall = NULL; SgType* type = NULL; SgExpression* expr = NULL; if(isGlobal) { type = varName->get_type(); SgName name = varName->get_name(); int numVals = 1; if(isSgPointerType(type)) return false; //If its a pointer, it points to a static array, dynamic array, or scalar //which has its address taken (all of which have already been registered) SgFunctionDeclaration* main = findMain(getScope(varName)); ROSE_ASSERT(main); expr = buildVarRefExp(name, varName->get_scope()); if(isSgArrayType(type)) numVals = getArrayElementCount(isSgArrayType(type)); else expr = buildAddressOfOp(expr); funcCall = MMCallBuilder::buildRegisterPointerCall(expr, buildUnsignedIntVal(numVals), main->get_definition()); insertStatement(getFirstStatement(main->get_definition()), funcCall);//, false, true); funcCall = MMCallBuilder::buildUnregisterPointerCall(expr, main->get_definition()); instrumentEndOfFunction(main, funcCall); } else if(expression) //Address-of expressions { prevStmt = getEnclosingStatement(expression); funcCall = MMCallBuilder::buildRegisterPointerCall(expression, buildUnsignedIntVal(1), getScope(expression)); insertStatement(prevStmt, funcCall); funcCall = MMCallBuilder::buildUnregisterPointerCall(expression, getScope(expression)); instrumentEndOfFunction(getEnclosingFunctionDeclaration(prevStmt), funcCall); } else //Array types { SgVarRefExp* varRef = buildVarRefExp(varName, getScope(varName)); int numVals = getArrayElementCount(isSgArrayType(varName->get_type())); funcCall = MMCallBuilder::buildRegisterPointerCall(varRef, buildUnsignedIntVal(numVals), varName->get_scope()); insertStatement(varName->get_declaration(), funcCall, false, true); varRef = buildVarRefExp(varName, getScope(varName)); funcCall = MMCallBuilder::buildUnregisterPointerCall(varRef, varName->get_scope()); instrumentEndOfFunction(getEnclosingFunctionDeclaration(varName), funcCall); } return true; }
/* * Default Constructor */ MigrationOutliner::MigrationOutliner(SgProject* p){ project = p; main_body = findMain(p); counter = 0; }
/* ARGSUSED */ void *dlopen(const char *path, int mode) { register ModulePtr mp; static void *mainModule; /* * Upon the first call register a terminate handler that will * close all libraries. Also get a reference to the main module * for use with loadbind. */ if (!mainModule) { if ((mainModule = findMain()) == NULL) return NULL; atexit(terminate); } /* * Scan the list of modules if have the module already loaded. */ for (mp = modList; mp; mp = mp->next) if (strcmp(mp->name, path) == 0) { mp->refCnt++; return mp; } if ((mp = (ModulePtr)calloc(1, sizeof(*mp))) == NULL) { errvalid++; strcpy(errbuf, "calloc: "); strcat(errbuf, strerror(errno)); return NULL; } if ((mp->name = strdup(path)) == NULL) { errvalid++; strcpy(errbuf, "strdup: "); strcat(errbuf, strerror(errno)); free(mp); return NULL; } /* * load should be declared load(const char *...). Thus we * cast the path to a normal char *. Ugly. */ if ((mp->entry = (void *)load((char *)path, L_NOAUTODEFER, NULL)) == NULL) { free(mp->name); free(mp); errvalid++; strcpy(errbuf, "dlopen: "); strcat(errbuf, path); strcat(errbuf, ": "); /* * If AIX says the file is not executable, the error * can be further described by querying the loader about * the last error. */ if (errno == ENOEXEC) { char *tmp[BUFSIZ/sizeof(char *)]; if (loadquery(L_GETMESSAGES, tmp, sizeof(tmp)) == -1) strcpy(errbuf, strerror(errno)); else { char **p; for (p = tmp; *p; p++) caterr(*p); } } else strcat(errbuf, strerror(errno)); return NULL; } mp->refCnt = 1; mp->next = modList; modList = mp; if (loadbind(0, mainModule, mp->entry) == -1) { dlclose(mp); errvalid++; strcpy(errbuf, "loadbind: "); strcat(errbuf, strerror(errno)); return NULL; } if (readExports(mp) == -1) { dlclose(mp); return NULL; } /* * If there is a dl_info structure, call the init function. */ if (mp->info = (struct dl_info *)dlsym(mp, "dl_info")) { if (mp->info->init) (*mp->info->init)(); } else errvalid = 0; return mp; }