From b2ef03b0c2f7a953584ded73b0df114a57c6f3c2 Mon Sep 17 00:00:00 2001 From: Giovanni Bussi <giovanni.bussi@gmail.com> Date: Wed, 21 Sep 2011 10:33:13 +0200 Subject: [PATCH] Automatic compilation I modified the "LOAD" command in such a way that if one writes "LOAD pippo.cpp" in the plumed.dat file, pippo.cpp is automatically compiled to a pippo.so which is then loaded. --- src/PlumedMain.cpp | 57 +++++++++++++++++++++++++++------------------- src/PlumedMain.h | 2 ++ 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/src/PlumedMain.cpp b/src/PlumedMain.cpp index 6059ea2a6..d807fa2c8 100644 --- a/src/PlumedMain.cpp +++ b/src/PlumedMain.cpp @@ -15,6 +15,7 @@ #include "ActionRegister.h" using namespace PLMD; +using namespace std; // !!!!!!!!!!!!!!!!!!!!!! DANGER !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!11 // THE FOLLOWING ARE UTILITIES WHICH ARE NECESSARY FOR DYNAMIC LOADING OF THE PLUMED KERNEL: @@ -307,29 +308,8 @@ void PlumedMain::readInputFile(std::string str){ while(Tools::getParsedLine(fp,words)){ if(words.size()==0)continue; else if(words[0]=="ENDPLUMED") break; - else if(words[0]=="LOAD"){ - std::string s=words[1]; - assert(words.size()==2); - void *p=plumed_dlopen(s.c_str()); - if(!p){ -// try with different extension - size_t n=s.find_last_of("."); - if(n==std::string::npos) s+="."; - else s=s.substr(0,n+1); - s+=soext; - p=plumed_dlopen(s.c_str()); - } - if(!p){ - log<<"ERROR\n"; - log<<"I cannot load library "<<words[1].c_str()<<"\n"; - log<<plumed_dlerror(); - log<<"\n"; - this->exit(1); - } - log<<"Loading shared library "<<s.c_str()<<"\n"; - log<<"Here is the new list of available actions\n"; - log<<actionRegister(); - } else if(words[0]=="INCLUDE"){ + else if(words[0]=="LOAD") load(words); + else if(words[0]=="INCLUDE"){ assert(words.size()==2); readInputFile(words[1]); continue; @@ -456,6 +436,37 @@ void PlumedMain::performCalc(){ for(ActionSet::iterator p=actionSet.begin();p!=actionSet.end();p++) (*p)->deactivate(); } + +void PlumedMain::load(std::vector<std::string> & words){ + string s=words[1]; + assert(words.size()==2); + size_t n=s.find_last_of("."); + string extension=""; + 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); + if(extension=="cpp"){ + string cmd="plumed mklib "+s; + log<<"Executing: "<<cmd; + if(comm.Get_size()>0) log<<" (only on master node)"; + log<<"\n"; + if(comm.Get_rank()==0) system(cmd.c_str()); + comm.Barrier(); + base="./"+base; + } + s=base+"."+soext; + void *p=plumed_dlopen(s.c_str()); + if(!p){ + log<<"ERROR\n"; + log<<"I cannot load library "<<words[1].c_str()<<"\n"; + log<<plumed_dlerror(); + log<<"\n"; + this->exit(1); + } + log<<"Loading shared library "<<s.c_str()<<"\n"; + log<<"Here is the new list of available actions\n"; + log<<actionRegister(); +} ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/PlumedMain.h b/src/PlumedMain.h index 11016419b..1f32fa785 100644 --- a/src/PlumedMain.h +++ b/src/PlumedMain.h @@ -117,6 +117,8 @@ public: const int & getStep()const{return step;}; /// Stop the run void exit(int c=0); +/// Load a shared library + void load(std::vector<std::string> & words); }; ///// -- GitLab