ENH: codeStream: added codeLibs

This commit is contained in:
mattijs
2011-03-21 17:54:20 +00:00
parent c81dd4ee08
commit cf99e5c800
11 changed files with 122 additions and 48 deletions

View File

@ -198,7 +198,7 @@
{ {
type codedFixedValue; type codedFixedValue;
value uniform 0; value uniform 0;
redirectType fixedValue10; redirectType ramped;
code code
#{ #{

View File

@ -26,6 +26,10 @@
Example: Look up dictionary entries and do some calculation Example: Look up dictionary entries and do some calculation
#+BEGIN_SRC c++ #+BEGIN_SRC c++
// For paraFoam's sake re-load in OpenFOAM library with exported symbols
libs ("libOpenFOAM.so");
startTime 0; startTime 0;
endTime 100; endTime 100;
.. ..
@ -46,7 +50,7 @@
=code=, =codeInclude=, =codeOptions= sections (these are just strings) and =code=, =codeInclude=, =codeOptions= sections (these are just strings) and
calculates the SHA1 checksum of the contents. calculates the SHA1 checksum of the contents.
- it copies a template file - it copies a template file
=(~OpenFOAM/codeTemplates/dynamicCode/codeStreamTemplate.C)= or =(etc/codeTemplates/dynamicCode/codeStreamTemplate.C)= or
=($FOAM_CODE_TEMPLATES/codeStreamTemplate.C)=, substituting all =($FOAM_CODE_TEMPLATES/codeStreamTemplate.C)=, substituting all
occurences of =code=, =codeInclude=, =codeOptions=. occurences of =code=, =codeInclude=, =codeOptions=.
- it writes library source files to =dynamicCode/<SHA1>= and compiles - it writes library source files to =dynamicCode/<SHA1>= and compiles
@ -60,7 +64,7 @@
* Boundary condition: =codedFixedValue= * Boundary condition: =codedFixedValue=
This uses the code from codeStream to have an in-line specialised This uses the code from codeStream to have an in-line specialised
=fixedValueFvPatchScalarField=. For now only for scalars: =fixedValueFvPatchField=.
#+BEGIN_SRC c++ #+BEGIN_SRC c++
outlet outlet
{ {
@ -77,6 +81,11 @@
It by default always includes =fvCFD.H= and adds the =finiteVolume= library to It by default always includes =fvCFD.H= and adds the =finiteVolume= library to
the include search path. the include search path.
When postprocessing using paraFoam it requires one to add the used libraries
to the libs entry so in the system/controlDict:
libs ("libOpenFOAM.so" "libfiniteVolume.so");
A special form is where the code is not supplied in-line but instead comes A special form is where the code is not supplied in-line but instead comes
from the =codeDict= dictionary in the =system= directory. It should contain from the =codeDict= dictionary in the =system= directory. It should contain
a =fixedValue10= entry: a =fixedValue10= entry:
@ -159,6 +168,19 @@
just preserves the entries as strings (including all formatting). just preserves the entries as strings (including all formatting).
* Other * Other
- paraFoam: paraview does not export symbols on loaded libraries
(more specific : it does not add 'RTLD_GLOBAL' to the dlopen flags) so
one will have to add the used libraries (libOpenFOAM, libfiniteVolume,
lib..) to the 'libs' entry in system/controlDict to prevent getting
an error of the form
--> FOAM FATAL IO ERROR:
Failed loading library "libcodeStream_3cd388ceb070a2f8b0ae61782adbc21c5687ce6f.so"
This will force re-loading
these libraries, this time exporting the symbols so the generated library
can be loaded.
- parallel running not tested a lot. What about distributed data - parallel running not tested a lot. What about distributed data
(i.e. non-=NFS=) parallel? (i.e. non-=NFS=) parallel?
- paraview has been patched so it will pass in RTLD_GLOBAL when loading - paraview has been patched so it will pass in RTLD_GLOBAL when loading

View File

@ -37,6 +37,7 @@ Description
#include "fileStat.H" #include "fileStat.H"
#include "timer.H" #include "timer.H"
#include "IFstream.H" #include "IFstream.H"
#include "DynamicList.H"
#include <fstream> #include <fstream>
#include <cstdlib> #include <cstdlib>
@ -52,6 +53,7 @@ Description
#include <sys/socket.h> #include <sys/socket.h>
#include <netdb.h> #include <netdb.h>
#include <dlfcn.h> #include <dlfcn.h>
#include <link.h>
#include <netinet/in.h> #include <netinet/in.h>
@ -1107,10 +1109,20 @@ void* Foam::dlOpen(const fileName& lib)
{ {
if (POSIX::debug) if (POSIX::debug)
{ {
Info<< "dlOpen(const fileName&)" std::cout<< "dlOpen(const fileName&)"
<< " : dlopen of " << lib << endl; << " : dlopen of " << lib << std::endl;
} }
return ::dlopen(lib.c_str(), RTLD_LAZY|RTLD_GLOBAL); void* handle = ::dlopen(lib.c_str(), RTLD_LAZY|RTLD_GLOBAL);
if (POSIX::debug)
{
std::cout
<< "dlOpen(const fileName&)"
<< " : dlopen of " << lib
<< " handle " << handle << std::endl;
}
return handle;
} }
@ -1118,8 +1130,9 @@ bool Foam::dlClose(void* handle)
{ {
if (POSIX::debug) if (POSIX::debug)
{ {
Info<< "dlClose(void*)" std::cout
<< " : dlclose" << endl; << "dlClose(void*)"
<< " : dlclose of handle " << handle << std::endl;
} }
return ::dlclose(handle) == 0; return ::dlclose(handle) == 0;
} }
@ -1129,8 +1142,9 @@ void* Foam::dlSym(void* handle, const std::string& symbol)
{ {
if (POSIX::debug) if (POSIX::debug)
{ {
Info<< "dlSym(void*, const std::string&)" std::cout
<< " : dlsym of " << symbol << endl; << "dlSym(void*, const std::string&)"
<< " : dlsym of " << symbol << std::endl;
} }
// clear any old errors - see manpage dlopen // clear any old errors - see manpage dlopen
(void) ::dlerror(); (void) ::dlerror();
@ -1158,8 +1172,9 @@ bool Foam::dlSymFound(void* handle, const std::string& symbol)
{ {
if (POSIX::debug) if (POSIX::debug)
{ {
Info<< "dlSymFound(void*, const std::string&)" std::cout
<< " : dlsym of " << symbol << endl; << "dlSymFound(void*, const std::string&)"
<< " : dlsym of " << symbol << std::endl;
} }
// clear any old errors - see manpage dlopen // clear any old errors - see manpage dlopen
@ -1178,4 +1193,32 @@ bool Foam::dlSymFound(void* handle, const std::string& symbol)
} }
static int collectLibsCallback
(
struct dl_phdr_info *info,
size_t size,
void *data
)
{
Foam::DynamicList<Foam::fileName>* ptr =
reinterpret_cast<Foam::DynamicList<Foam::fileName>*>(data);
ptr->append(info->dlpi_name);
return 0;
}
Foam::fileNameList Foam::dlLoaded()
{
DynamicList<fileName> libs;
dl_iterate_phdr(collectLibsCallback, &libs);
if (POSIX::debug)
{
std::cout
<< "dlLoaded()"
<< " : determined loaded libraries :" << libs.size() << endl;
}
return libs;
}
// ************************************************************************* // // ************************************************************************* //

View File

@ -136,7 +136,9 @@ bool Foam::functionEntries::codeStream::execute
( (
"EXE_INC = -g \\\n" "EXE_INC = -g \\\n"
+ context.options() + context.options()
+ "\n\nLIB_LIBS =" + "\n\nLIB_LIBS = \\\n"
+ " -lOpenFOAM \\\n"
+ context.libs()
); );
if (!dynCode.copyOrCreateFiles(true)) if (!dynCode.copyOrCreateFiles(true))
@ -172,6 +174,8 @@ bool Foam::functionEntries::codeStream::execute
"functionEntries::codeStream::execute(..)", "functionEntries::codeStream::execute(..)",
parentDict parentDict
) << "Failed loading library " << libPath << nl ) << "Failed loading library " << libPath << nl
<< "Did you add all libraries to the 'libs' entry"
<< " in system/controlDict?"
<< exit(FatalIOError); << exit(FatalIOError);
} }

View File

@ -31,8 +31,6 @@ License
#include "OFstream.H" #include "OFstream.H"
#include "OSspecific.H" #include "OSspecific.H"
#include "dictionary.H" #include "dictionary.H"
#include "dlLibraryTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -557,22 +555,4 @@ bool Foam::dynamicCode::upToDate(const dynamicCodeContext& context) const
} }
// bool Foam::dynamicCode::openLibrary() const
// {
// return dlLibraryTable::openLibrary(this->libPath(), false);
// }
//
//
// bool Foam::dynamicCode::closeLibrary() const
// {
// return dlLibraryTable::closeLibrary(this->libPath(), false);
// }
//
//
// void* Foam::dynamicCode::findLibrary() const
// {
// return dlLibraryTable::findLibrary(this->libPath());
// }
// ************************************************************************* // // ************************************************************************* //

View File

@ -37,10 +37,8 @@ SourceFiles
#define dynamicCode_H #define dynamicCode_H
#include "Tuple2.H" #include "Tuple2.H"
#include "SHA1Digest.H"
#include "HashTable.H" #include "HashTable.H"
#include "DynamicList.H" #include "DynamicList.H"
#include "dlLibraryTable.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -51,6 +49,7 @@ namespace Foam
class dynamicCodeContext; class dynamicCodeContext;
class ISstream; class ISstream;
class OSstream; class OSstream;
class SHA1Digest;
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class dynamicCode Declaration Class dynamicCode Declaration
@ -283,16 +282,6 @@ public:
//- Compile a libso //- Compile a libso
bool wmakeLibso() const; bool wmakeLibso() const;
// //- Open the libPath() library
// bool openLibrary() const;
//
// //- Close the libPath() library
// bool closeLibrary() const;
//
// //- Find the handle of the libPath() library
// void* findLibrary() const;
}; };

View File

@ -37,7 +37,8 @@ Foam::dynamicCodeContext::dynamicCodeContext(const dictionary& dict)
code_(stringOps::trim(dict["code"])), code_(stringOps::trim(dict["code"])),
localCode_(), localCode_(),
include_(), include_(),
options_() options_(),
libs_()
{ {
// expand dictionary entries // expand dictionary entries
stringOps::inplaceExpand(code_, dict); stringOps::inplaceExpand(code_, dict);
@ -67,9 +68,16 @@ Foam::dynamicCodeContext::dynamicCodeContext(const dictionary& dict)
stringOps::inplaceExpand(options_, dict); stringOps::inplaceExpand(options_, dict);
} }
// optional
if (dict.found("codeLibs"))
{
libs_ = stringOps::trim(dict["codeLibs"]);
stringOps::inplaceExpand(libs_, dict);
}
// calculate SHA1 digest from include, options, localCode, code // calculate SHA1 digest from include, options, localCode, code
OSHA1stream os; OSHA1stream os;
os << include_ << options_ << localCode_ << code_; os << include_ << options_ << libs_ << localCode_ << code_;
sha1_ = os.digest(); sha1_ = os.digest();
} }

View File

@ -66,6 +66,9 @@ class dynamicCodeContext
//- Optional "codeOptions" entry //- Optional "codeOptions" entry
string options_; string options_;
//- Optional "codeLib" entry
string libs_;
//- Calculated SHA1Digest //- Calculated SHA1Digest
SHA1Digest sha1_; SHA1Digest sha1_;
@ -96,6 +99,12 @@ public:
return options_; return options_;
} }
//- Return the code-libs
const string& libs() const
{
return libs_;
}
//- Return the code //- Return the code
const string& code() const const string& code() const
{ {

View File

@ -196,6 +196,9 @@ void* dlSym(void* handle, const std::string& symbol);
//- Report if symbol in a dlopened library could be found //- Report if symbol in a dlopened library could be found
bool dlSymFound(void* handle, const std::string& symbol); bool dlSymFound(void* handle, const std::string& symbol);
//- Return all loaded libraries
fileNameList dlLoaded();
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -37,6 +37,9 @@ License
#include "stringOps.H" #include "stringOps.H"
#include "IOdictionary.H" #include "IOdictionary.H"
#include <dlfcn.h>
#include <link.h>
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
template<class Type> template<class Type>
@ -270,7 +273,10 @@ void Foam::codedFixedValueFvPatchField<Type>::createLibrary
"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 = \\\n"
+ " -lOpenFOAM \\\n"
+ " -lfiniteVolume \\\n"
+ context.libs()
); );
if (!dynCode.copyOrCreateFiles(true)) if (!dynCode.copyOrCreateFiles(true))

View File

@ -80,6 +80,9 @@ SourceFiles
#include "fixedValueFvPatchFields.H" #include "fixedValueFvPatchFields.H"
#include <dlfcn.h>
#include <link.h>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam namespace Foam
@ -119,6 +122,13 @@ class codedFixedValueFvPatchField
//- Global loader/unloader function type //- Global loader/unloader function type
typedef void (*loaderFunctionType)(bool); typedef void (*loaderFunctionType)(bool);
static int collectLibsCallback
(
struct dl_phdr_info *info,
size_t size,
void *data
);
//- Load specified library and execute globalFuncName(true) //- Load specified library and execute globalFuncName(true)
static void* loadLibrary static void* loadLibrary
( (