Commit cf99e5c8 authored by mattijs's avatar mattijs
Browse files

ENH: codeStream: added codeLibs

parent c81dd4ee
......@@ -198,7 +198,7 @@
{
type codedFixedValue;
value uniform 0;
redirectType fixedValue10;
redirectType ramped;
code
#{
......
......@@ -26,6 +26,10 @@
Example: Look up dictionary entries and do some calculation
#+BEGIN_SRC c++
// For paraFoam's sake re-load in OpenFOAM library with exported symbols
libs ("libOpenFOAM.so");
startTime 0;
endTime 100;
..
......@@ -46,7 +50,7 @@
=code=, =codeInclude=, =codeOptions= sections (these are just strings) and
calculates the SHA1 checksum of the contents.
- it copies a template file
=(~OpenFOAM/codeTemplates/dynamicCode/codeStreamTemplate.C)= or
=(etc/codeTemplates/dynamicCode/codeStreamTemplate.C)= or
=($FOAM_CODE_TEMPLATES/codeStreamTemplate.C)=, substituting all
occurences of =code=, =codeInclude=, =codeOptions=.
- it writes library source files to =dynamicCode/<SHA1>= and compiles
......@@ -60,7 +64,7 @@
* Boundary condition: =codedFixedValue=
This uses the code from codeStream to have an in-line specialised
=fixedValueFvPatchScalarField=. For now only for scalars:
=fixedValueFvPatchField=.
#+BEGIN_SRC c++
outlet
{
......@@ -77,6 +81,11 @@
It by default always includes =fvCFD.H= and adds the =finiteVolume= library to
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
from the =codeDict= dictionary in the =system= directory. It should contain
a =fixedValue10= entry:
......@@ -159,6 +168,19 @@
just preserves the entries as strings (including all formatting).
* 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
(i.e. non-=NFS=) parallel?
- paraview has been patched so it will pass in RTLD_GLOBAL when loading
......
......@@ -37,6 +37,7 @@ Description
#include "fileStat.H"
#include "timer.H"
#include "IFstream.H"
#include "DynamicList.H"
#include <fstream>
#include <cstdlib>
......@@ -52,6 +53,7 @@ Description
#include <sys/socket.h>
#include <netdb.h>
#include <dlfcn.h>
#include <link.h>
#include <netinet/in.h>
......@@ -1107,10 +1109,20 @@ void* Foam::dlOpen(const fileName& lib)
{
if (POSIX::debug)
{
Info<< "dlOpen(const fileName&)"
<< " : dlopen of " << lib << endl;
std::cout<< "dlOpen(const fileName&)"
<< " : 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)
{
Info<< "dlClose(void*)"
<< " : dlclose" << endl;
std::cout
<< "dlClose(void*)"
<< " : dlclose of handle " << handle << std::endl;
}
return ::dlclose(handle) == 0;
}
......@@ -1129,8 +1142,9 @@ void* Foam::dlSym(void* handle, const std::string& symbol)
{
if (POSIX::debug)
{
Info<< "dlSym(void*, const std::string&)"
<< " : dlsym of " << symbol << endl;
std::cout
<< "dlSym(void*, const std::string&)"
<< " : dlsym of " << symbol << std::endl;
}
// clear any old errors - see manpage dlopen
(void) ::dlerror();
......@@ -1158,8 +1172,9 @@ bool Foam::dlSymFound(void* handle, const std::string& symbol)
{
if (POSIX::debug)
{
Info<< "dlSymFound(void*, const std::string&)"
<< " : dlsym of " << symbol << endl;
std::cout
<< "dlSymFound(void*, const std::string&)"
<< " : dlsym of " << symbol << std::endl;
}
// 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;
}
// ************************************************************************* //
......@@ -136,7 +136,9 @@ bool Foam::functionEntries::codeStream::execute
(
"EXE_INC = -g \\\n"
+ context.options()
+ "\n\nLIB_LIBS ="
+ "\n\nLIB_LIBS = \\\n"
+ " -lOpenFOAM \\\n"
+ context.libs()
);
if (!dynCode.copyOrCreateFiles(true))
......@@ -172,6 +174,8 @@ bool Foam::functionEntries::codeStream::execute
"functionEntries::codeStream::execute(..)",
parentDict
) << "Failed loading library " << libPath << nl
<< "Did you add all libraries to the 'libs' entry"
<< " in system/controlDict?"
<< exit(FatalIOError);
}
......
......@@ -31,8 +31,6 @@ License
#include "OFstream.H"
#include "OSspecific.H"
#include "dictionary.H"
#include "dlLibraryTable.H"
// * * * * * * * * * * * * * * 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());
// }
// ************************************************************************* //
......@@ -37,10 +37,8 @@ SourceFiles
#define dynamicCode_H
#include "Tuple2.H"
#include "SHA1Digest.H"
#include "HashTable.H"
#include "DynamicList.H"
#include "dlLibraryTable.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......@@ -51,6 +49,7 @@ namespace Foam
class dynamicCodeContext;
class ISstream;
class OSstream;
class SHA1Digest;
/*---------------------------------------------------------------------------*\
Class dynamicCode Declaration
......@@ -283,16 +282,6 @@ public:
//- Compile a libso
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;
};
......
......@@ -37,7 +37,8 @@ Foam::dynamicCodeContext::dynamicCodeContext(const dictionary& dict)
code_(stringOps::trim(dict["code"])),
localCode_(),
include_(),
options_()
options_(),
libs_()
{
// expand dictionary entries
stringOps::inplaceExpand(code_, dict);
......@@ -67,9 +68,16 @@ Foam::dynamicCodeContext::dynamicCodeContext(const dictionary& 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
OSHA1stream os;
os << include_ << options_ << localCode_ << code_;
os << include_ << options_ << libs_ << localCode_ << code_;
sha1_ = os.digest();
}
......
......@@ -66,6 +66,9 @@ class dynamicCodeContext
//- Optional "codeOptions" entry
string options_;
//- Optional "codeLib" entry
string libs_;
//- Calculated SHA1Digest
SHA1Digest sha1_;
......@@ -96,6 +99,12 @@ public:
return options_;
}
//- Return the code-libs
const string& libs() const
{
return libs_;
}
//- Return the code
const string& code() const
{
......
......@@ -196,6 +196,9 @@ void* dlSym(void* handle, const std::string& symbol);
//- Report if symbol in a dlopened library could be found
bool dlSymFound(void* handle, const std::string& symbol);
//- Return all loaded libraries
fileNameList dlLoaded();
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -37,6 +37,9 @@ License
#include "stringOps.H"
#include "IOdictionary.H"
#include <dlfcn.h>
#include <link.h>
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
template<class Type>
......@@ -270,7 +273,10 @@ void Foam::codedFixedValueFvPatchField<Type>::createLibrary
"EXE_INC = -g \\\n"
"-I$(LIB_SRC)/finiteVolume/lnInclude\\\n"
+ context.options()
+ "\n\nLIB_LIBS = "
+ "\n\nLIB_LIBS = \\\n"
+ " -lOpenFOAM \\\n"
+ " -lfiniteVolume \\\n"
+ context.libs()
);
if (!dynCode.copyOrCreateFiles(true))
......
......@@ -80,6 +80,9 @@ SourceFiles
#include "fixedValueFvPatchFields.H"
#include <dlfcn.h>
#include <link.h>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
......@@ -119,6 +122,13 @@ class codedFixedValueFvPatchField
//- Global loader/unloader function type
typedef void (*loaderFunctionType)(bool);
static int collectLibsCallback
(
struct dl_phdr_info *info,
size_t size,
void *data
);
//- Load specified library and execute globalFuncName(true)
static void* loadLibrary
(
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment