STYLE: minor code cleanup for codeStream/codedFixedValueFvPatchScalarField

This commit is contained in:
Mark Olesen
2011-03-03 14:16:00 +01:00
parent 0f8b423a68
commit 7ca8b24a0d
4 changed files with 90 additions and 148 deletions

View File

@ -98,21 +98,20 @@ bool Foam::functionEntries::codeStream::execute
// see if library is loaded // see if library is loaded
void* lib = dlLibraryTable::findLibrary(libPath); void* lib = dlLibraryTable::findLibrary(libPath);
bool reuseLib = false;
// nothing loaded // nothing loaded
// avoid compilation if possible by loading an existing library // avoid compilation if possible by loading an existing library
if (!lib && dlLibraryTable::open(libPath, false)) if (!lib && dlLibraryTable::open(libPath, false))
{ {
lib = dlLibraryTable::findLibrary(libPath); lib = dlLibraryTable::findLibrary(libPath);
reuseLib = true;
} }
// create library if required // create library if required
if (!lib) if (!lib)
{ {
if (Pstream::master()) bool create = Pstream::master();
if (create)
{ {
if (!dynCode.upToDate(context)) if (!dynCode.upToDate(context))
{ {
@ -137,7 +136,7 @@ bool Foam::functionEntries::codeStream::execute
"functionEntries::codeStream::execute(..)", "functionEntries::codeStream::execute(..)",
parentDict parentDict
) << "Failed writing files for" << nl ) << "Failed writing files for" << nl
<< dynCode.libPath() << nl << dynCode.libRelPath() << nl
<< exit(FatalIOError); << exit(FatalIOError);
} }
} }
@ -148,14 +147,13 @@ bool Foam::functionEntries::codeStream::execute
( (
"functionEntries::codeStream::execute(..)", "functionEntries::codeStream::execute(..)",
parentDict parentDict
) << "Failed wmake " << libPath ) << "Failed wmake " << dynCode.libRelPath() << nl
<< exit(FatalIOError); << exit(FatalIOError);
} }
} }
// all processes must wait for compile // all processes must wait for compile to finish
bool waiting = true; reduce(create, orOp<bool>());
reduce(waiting, orOp<bool>());
if (!dlLibraryTable::open(libPath, false)) if (!dlLibraryTable::open(libPath, false))
{ {
@ -163,24 +161,20 @@ bool Foam::functionEntries::codeStream::execute
( (
"functionEntries::codeStream::execute(..)", "functionEntries::codeStream::execute(..)",
parentDict parentDict
) << "Failed loading library " << libPath ) << "Failed loading library " << libPath << nl
<< exit(FatalIOError); << exit(FatalIOError);
} }
lib = dlLibraryTable::findLibrary(libPath); lib = dlLibraryTable::findLibrary(libPath);
} }
else if (reuseLib)
{
Info<< "Reusing library in " << libPath << endl;
}
// Find the function handle in the library // Find the function handle in the library
void (*function)(Ostream&, const dictionary&); streamingFunctionType function =
function = reinterpret_cast<void(*)(Ostream&, const dictionary&)> reinterpret_cast<streamingFunctionType>
( (
dlSym(lib, dynCode.codeName()) dlSym(lib, dynCode.codeName())
); );
if (!function) if (!function)

View File

@ -109,6 +109,11 @@ class codeStream
: :
public functionEntry public functionEntry
{ {
//- Interpreter function type
typedef void (*streamingFunctionType)(Ostream&, const dictionary&);
// Private Member Functions // Private Member Functions
//- Disallow default bitwise copy construct //- Disallow default bitwise copy construct

View File

@ -47,32 +47,6 @@ const Foam::word Foam::codedFixedValueFvPatchScalarField::codeTemplateH
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
Foam::string Foam::codedFixedValueFvPatchScalarField::libraryGlobalName
(
const fileName& libPath
)
{
// global function name (SHA1-encoded)
// that can be used for version control and/or explicit loading/unloading
string globalFuncName = libPath.name();
// remove ".so" extension
string::size_type dot = globalFuncName.find('.');
if (dot != string::npos)
{
globalFuncName.resize(dot);
}
// remove leading 'lib' from name
globalFuncName.erase(0, 3);
return globalFuncName;
}
void* Foam::codedFixedValueFvPatchScalarField::loadLibrary void* Foam::codedFixedValueFvPatchScalarField::loadLibrary
( (
const fileName& libPath, const fileName& libPath,
@ -82,20 +56,17 @@ void* Foam::codedFixedValueFvPatchScalarField::loadLibrary
{ {
void* lib = 0; void* lib = 0;
// global function name (SHA1-encoded)
// that can be used for version control and/or explicit loading/unloading
// avoid compilation by loading an existing library // avoid compilation by loading an existing library
if (dlLibraryTable::open(libPath, false)) if (!libPath.empty() && dlLibraryTable::open(libPath, false))
{ {
lib = dlLibraryTable::findLibrary(libPath); lib = dlLibraryTable::findLibrary(libPath);
// verify the loaded version and unload if needed // verify the loaded version and unload if needed
if (lib) if (lib)
{ {
// provision for manual execution of code after loading
if (dlSymFound(lib, globalFuncName)) if (dlSymFound(lib, globalFuncName))
{ {
// Find the function handle in the library
loaderFunctionType function = loaderFunctionType function =
reinterpret_cast<loaderFunctionType> reinterpret_cast<loaderFunctionType>
( (
@ -104,8 +75,7 @@ void* Foam::codedFixedValueFvPatchScalarField::loadLibrary
if (function) if (function)
{ {
// force load (*function)(true); // force load
(*function)(true);
} }
else else
{ {
@ -152,7 +122,12 @@ void Foam::codedFixedValueFvPatchScalarField::unloadLibrary
const dictionary& contextDict const dictionary& contextDict
) )
{ {
void* lib = dlLibraryTable::findLibrary(libPath); void* lib = 0;
if (!libPath.empty())
{
lib = dlLibraryTable::findLibrary(libPath);
}
if (!lib) if (!lib)
{ {
@ -162,7 +137,6 @@ void Foam::codedFixedValueFvPatchScalarField::unloadLibrary
// provision for manual execution of code before unloading // provision for manual execution of code before unloading
if (dlSymFound(lib, globalFuncName)) if (dlSymFound(lib, globalFuncName))
{ {
// Find the function handle in the library
loaderFunctionType function = loaderFunctionType function =
reinterpret_cast<loaderFunctionType> reinterpret_cast<loaderFunctionType>
( (
@ -171,8 +145,7 @@ void Foam::codedFixedValueFvPatchScalarField::unloadLibrary
if (function) if (function)
{ {
// force unload (*function)(false); // force unload
(*function)(false);
} }
else else
{ {
@ -181,8 +154,7 @@ void Foam::codedFixedValueFvPatchScalarField::unloadLibrary
"codedFixedValueFvPatchScalarField::unloadLibrary()", "codedFixedValueFvPatchScalarField::unloadLibrary()",
contextDict contextDict
) << "Failed looking up symbol " << globalFuncName << nl ) << "Failed looking up symbol " << globalFuncName << nl
<< "from " << libPath << "from " << libPath << exit(FatalIOError);
<< exit(FatalIOError);
} }
} }
@ -233,46 +205,65 @@ void Foam::codedFixedValueFvPatchScalarField::createLibrary
const dynamicCodeContext& context const dynamicCodeContext& context
) const ) const
{ {
// Write files for new library bool create = Pstream::master();
if (Pstream::master() && !dynCode.upToDate(context))
if (create)
{ {
// filter with this context // Write files for new library
dynCode.reset(context); if (!dynCode.upToDate(context))
{
// filter with this context
dynCode.reset(context);
// compile filtered C template // compile filtered C template
dynCode.addCompileFile(codeTemplateC); dynCode.addCompileFile(codeTemplateC);
// copy filtered H template // copy filtered H template
dynCode.addCopyFile(codeTemplateH); dynCode.addCopyFile(codeTemplateH);
// take no chances - typeName must be identical to redirectType_ // take no chances - typeName must be identical to redirectType_
dynCode.setFilterVariable("typeName", redirectType_); dynCode.setFilterVariable("typeName", redirectType_);
// debugging: make BC verbose // debugging: make BC verbose
// dynCode.setFilterVariable("verbose", "true"); // dynCode.setFilterVariable("verbose", "true");
// Info<<"compile " << redirectType_ << " sha1: " // Info<<"compile " << redirectType_ << " sha1: "
// << context.sha1() << endl; // << context.sha1() << endl;
// define Make/options // define Make/options
dynCode.setMakeOptions dynCode.setMakeOptions
( (
"EXE_INC = -g \\\n" "EXE_INC = -g \\\n"
"-I$(LIB_SRC)/finiteVolume/lnInclude\\\n" "-I$(LIB_SRC)/finiteVolume/lnInclude\\\n"
+ context.options() + context.options()
+ "\n\nLIB_LIBS = " + "\n\nLIB_LIBS = "
); );
if (!dynCode.copyOrCreateFiles(true)) if (!dynCode.copyOrCreateFiles(true))
{
FatalIOErrorIn
(
"codedFixedValueFvPatchScalarField::createLibrary(..)",
context.dict()
) << "Failed writing files for" << nl
<< dynCode.libRelPath() << nl
<< exit(FatalIOError);
}
}
if (!dynCode.wmakeLibso())
{ {
FatalIOErrorIn FatalIOErrorIn
( (
"codedFixedValueFvPatchScalarField::writeLibrary(..)", "codedFixedValueFvPatchScalarField::createLibrary(..)",
context.dict() context.dict()
) << "Failed writing files for" << nl ) << "Failed wmake " << dynCode.libRelPath() << nl
<< dynCode.libPath() << nl
<< exit(FatalIOError); << exit(FatalIOError);
} }
} }
// all processes must wait for compile to finish
reduce(create, orOp<bool>());
} }
@ -294,7 +285,6 @@ void Foam::codedFixedValueFvPatchScalarField::updateLibrary() const
dynamicCodeContext context(codeDict); dynamicCodeContext context(codeDict);
// codeName: redirectType + _<sha1> // codeName: redirectType + _<sha1>
// codeDir : redirectType // codeDir : redirectType
dynamicCode dynCode dynamicCode dynCode
@ -305,77 +295,33 @@ void Foam::codedFixedValueFvPatchScalarField::updateLibrary() const
const fileName libPath = dynCode.libPath(); const fileName libPath = dynCode.libPath();
// see if library is loaded // the correct library was already loaded => we are done
void* lib = dlLibraryTable::findLibrary(libPath); if (dlLibraryTable::findLibrary(libPath))
// library not loaded, and also need to unload old version
if (!lib && !oldLibPath_.empty())
{
// unload code
// Remove instantiation of fvPatchField provided by library
redirectPatchFieldPtr_.clear();
unloadLibrary
(
oldLibPath_,
libraryGlobalName(oldLibPath_),
context.dict()
);
}
// retain for future reference
oldLibPath_ = libPath;
bool waiting = false;
if (lib)
{ {
return; return;
} }
// Remove instantiation of fvPatchField provided by library // remove instantiation of fvPatchField provided by library
redirectPatchFieldPtr_.clear(); redirectPatchFieldPtr_.clear();
// avoid compilation by loading an existing library // may need to unload old library
lib = loadLibrary unloadLibrary
( (
libPath, oldLibPath_,
dynCode.codeName(), dynamicCode::libraryBaseName(oldLibPath_),
context.dict() context.dict()
); );
// try loading an existing library (avoid compilation when possible)
// really do need to create library if (!loadLibrary(libPath, dynCode.codeName(), context.dict()))
if (!lib)
{ {
if (Pstream::master()) createLibrary(dynCode, context);
{
createLibrary(dynCode, context);
if (!dynCode.wmakeLibso()) loadLibrary(libPath, dynCode.codeName(), context.dict());
{
FatalIOErrorIn
(
"codedFixedValueFvPatchScalarField::updateLibrary()",
context.dict()
) << "Failed wmake " << libPath
<< exit(FatalIOError);
}
}
// all processes must wait for compile
waiting = true;
reduce(waiting, orOp<bool>());
// load newly compiled libray
lib = loadLibrary
(
libPath,
dynCode.codeName(),
context.dict()
);
} }
// retain for future reference
oldLibPath_ = libPath;
} }

View File

@ -114,13 +114,11 @@ class codedFixedValueFvPatchScalarField
// Private Member Functions // Private Member Functions
// Generic library management pieces const IOdictionary& dict() const;
//- Global loader/unloader function type //- Global loader/unloader function type
typedef void (*loaderFunctionType)(bool); typedef void (*loaderFunctionType)(bool);
static string libraryGlobalName(const fileName& libPath);
static void* loadLibrary static void* loadLibrary
( (
const fileName& libPath, const fileName& libPath,
@ -136,13 +134,12 @@ class codedFixedValueFvPatchScalarField
); );
const IOdictionary& dict() const;
void createLibrary(dynamicCode&, const dynamicCodeContext&) const; void createLibrary(dynamicCode&, const dynamicCodeContext&) const;
//- Update library as required //- Update library as required
void updateLibrary() const; void updateLibrary() const;
public: public:
// Static data members // Static data members