Skip to content

Commit

Permalink
Merge pull request #985 from Iximiel/actionLOAD_GLOBAL
Browse files Browse the repository at this point in the history
Adding GLOBAL keyword to LOAD action
  • Loading branch information
GiovanniBussi authored Nov 15, 2023
2 parents ae54bc5 + 05ac10b commit ee93ec2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 26 deletions.
30 changes: 17 additions & 13 deletions src/core/PlumedMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1058,38 +1058,42 @@ void PlumedMain::update() {
}
}

void PlumedMain::load(const std::string& ss) {
void PlumedMain::load(const std::string& fileName, const bool loadGlobal) {
if(DLLoader::installed()) {
std::string s=ss;
size_t n=s.find_last_of(".");
std::string libName=fileName;
size_t n=libName.find_last_of(".");
std::string extension="";
std::string base=s;
if(n!=std::string::npos && n<s.length()-1) extension=s.substr(n+1);
if(n!=std::string::npos && n<s.length()) base=s.substr(0,n);
std::string base=libName;
if(n!=std::string::npos && n<libName.length()-1)
extension=libName.substr(n+1);
if(n!=std::string::npos && n<libName.length())
base=libName.substr(0,n);
if(extension=="cpp") {
// full path command, including environment setup
// this will work even if plumed is not in the execution path or if it has been
// installed with a name different from "plumed"
std::string cmd=config::getEnvCommand()+" \""+config::getPlumedRoot()+"\"/scripts/mklib.sh "+s;
std::string cmd=config::getEnvCommand()+" \""+config::getPlumedRoot()+"\"/scripts/mklib.sh "+libName;
log<<"Executing: "<<cmd;
if(comm.Get_size()>0) log<<" (only on master node)";
log<<"\n";
if(comm.Get_rank()==0) {
int ret=std::system(cmd.c_str());
if(ret!=0) plumed_error() <<"An error happened while executing command "<<cmd<<"\n";
if(ret!=0)
plumed_error() <<"An error happened while executing command "<<cmd<<"\n";
}
comm.Barrier();
base="./"+base;
}
s=base+"."+config::getSoExt();
void *p=dlloader.load(s);
libName=base+"."+config::getSoExt();
void *p=dlloader.load(libName,loadGlobal);
if(!p) {
plumed_error()<<"I cannot load library " << ss << " " << dlloader.error();
plumed_error()<<"I cannot load library " << fileName << " " << dlloader.error();
}
log<<"Loading shared library "<<s.c_str()<<"\n";
log<<"Loading shared library "<<libName.c_str()<<"\n";
log<<"Here is the new list of available actions\n";
log<<actionRegister();
} else plumed_error()<<"While loading library "<< ss << " loading was not enabled, please check if dlopen was found at configure time";
} else
plumed_error()<<"While loading library "<< fileName << " loading was not enabled, please check if dlopen was found at configure time";
}

double PlumedMain::getBias() const {
Expand Down
2 changes: 1 addition & 1 deletion src/core/PlumedMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ class PlumedMain:
/// Stop the run
void exit(int c=0);
/// Load a shared library
void load(const std::string&);
void load(const std::string&, bool=false);
/// Get the suffix string
const std::string & getSuffix()const;
/// Set the suffix string
Expand Down
5 changes: 4 additions & 1 deletion src/setup/Load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ PLUMED_REGISTER_ACTION(Load,"LOAD")
void Load::registerKeywords( Keywords& keys ) {
ActionSetup::registerKeywords(keys);
keys.add("compulsory","FILE","file to be loaded");
keys.addFlag("GLOBAL",false,"when selected the shared object is LOADed with RTLD_GLOBAL instead of RTLD_LOCAL");
}

Load::Load(const ActionOptions&ao):
Expand All @@ -114,8 +115,10 @@ Load::Load(const ActionOptions&ao):
{
std::string f;
parse("FILE",f);
bool loadWithGlobal=false;
parseFlag("GLOBAL",loadWithGlobal);
checkRead();
plumed.load(f);
plumed.load(f,loadWithGlobal);
}

}
Expand Down
8 changes: 6 additions & 2 deletions src/tools/DLLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ bool DLLoader::installed() {
}


void* DLLoader::load(const std::string&s) {
void* DLLoader::load(const std::string&s, const bool useGlobal) {
#ifdef __PLUMED_HAS_DLOPEN
void* p=dlopen(s.c_str(),RTLD_NOW|RTLD_LOCAL);
void* p=nullptr;
if (useGlobal)
p=dlopen(s.c_str(),RTLD_NOW|RTLD_GLOBAL);
else
p=dlopen(s.c_str(),RTLD_NOW|RTLD_LOCAL);
if(!p) {
lastError=dlerror();
} else {
Expand Down
18 changes: 9 additions & 9 deletions src/tools/DLLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ namespace PLMD {
class DLLoader {
std::stack<void*> handles;
std::string lastError;
/// Deleted copy constructor
/// Deleted copy constructor
DLLoader(const DLLoader&) = delete;
/// Deleted assignment
/// Deleted assignment
DLLoader&operator=(const DLLoader&) = delete;
public:
/// Default constructor
/// Default constructor
DLLoader();
/// Cleanup
/// Cleanup
~DLLoader();
/// Load a library, returning its handle
void* load(const std::string&);
/// Returns the last error in dynamic loader
/// Load a library, returning its handle
void* load(const std::string&, bool=false);
/// Returns the last error in dynamic loader
const std::string & error();
/// Returns true if the dynamic loader is available (on some systems it may not).
/// Returns true if the dynamic loader is available (on some systems it may not).
static bool installed();
};

}
} // namespace PLMD

#endif

1 comment on commit ee93ec2

@PlumedBot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found broken examples in automatic/a-masterclass-22-09.txt
Found broken examples in automatic/a-masterclass-22-11.txt
Found broken examples in automatic/a-masterclass-22-12.txt
Found broken examples in automatic/performance-optimization.txt
Found broken examples in automatic/a-trieste-6.txt
Found broken examples in automatic/munster.txt
Found broken examples in automatic/ANN.tmp
Found broken examples in automatic/EDS.tmp
Found broken examples in automatic/EMMI.tmp
Found broken examples in automatic/ENVIRONMENTSIMILARITY.tmp
Found broken examples in automatic/FOURIER_TRANSFORM.tmp
Found broken examples in automatic/FUNCPATHGENERAL.tmp
Found broken examples in automatic/FUNCPATHMSD.tmp
Found broken examples in automatic/FUNNEL.tmp
Found broken examples in automatic/FUNNEL_PS.tmp
Found broken examples in automatic/GHBFIX.tmp
Found broken examples in automatic/INCLUDE.tmp
Found broken examples in automatic/MAZE_MEMETIC_SAMPLING.tmp
Found broken examples in automatic/MAZE_OPTIMIZER_BIAS.tmp
Found broken examples in automatic/MAZE_RANDOM_ACCELERATION_MD.tmp
Found broken examples in automatic/MAZE_RANDOM_WALK.tmp
Found broken examples in automatic/MAZE_SIMULATED_ANNEALING.tmp
Found broken examples in automatic/MAZE_STEERED_MD.tmp
Found broken examples in automatic/PIV.tmp
Found broken examples in automatic/PLUMED.tmp
Found broken examples in MiscelaneousPP.md

Please sign in to comment.