PhoenixHPCProxy  0.8.0
Lightweight HPC proxy
Loading...
Searching...
No Matches
backend_proxy_loader.cpp File Reference
#include "backend.h"
#include "backend_proxy_loader.h"
+ Include dependency graph for backend_proxy_loader.cpp:

Go to the source code of this file.

Functions

bool cpp_backend_proxy (const PVecSource &vecSource, const PString &libraryName, const PVecArchLib &vecArchLib, const PPath &libDir, const PPath &outputDir)
 Save ProxyLoader with the corresponding a vector of PSource in the output directory.
 
PString cpp_backendProxyLoaderCloseSource (const PString &className)
 Create the Source of the ProxyLoader close.
 
PString cpp_backendProxyLoaderConstructorSource (const PString &className, const PString &defaultLib)
 Create the Source of the ProxyLoader class constructor.
 
PString cpp_backendProxyLoaderDesonstructorSource (const PString &className)
 Create the Source of the ProxyLoader class desctructor.
 
PString cpp_backendProxyLoaderHeader (const PString &libraryName, const PString &className)
 Create the Header definition of the ProxyLoader class.
 
PString cpp_backendProxyLoaderInitialisationSource (const PString &className)
 Create the Source of the ProxyLoader initialisation.
 
PString cpp_backendProxyLoaderIsOpenSource (const PString &className)
 Create the Source of the ProxyLoader isOpen.
 
PString cpp_backendProxyLoaderLoadSource (const PString &className)
 Create the Source of the ProxyLoader load.
 
PString cpp_backendProxyLoaderParseArchFileSource (const PVecArchLib &vecArchLib, const PString &className, const PPath &libDir)
 Create the Source of the ProxyLoader load.
 
PString cpp_backendProxyLoaderSource (const PVecSource &vecSource, const PVecArchLib &vecArchLib, const PString &className, const PString &libDir)
 Create the Source of the ProxyLoader class.
 
PString cpp_backendProxyLoaderUpdateSource (const PString &className, const PVecSource &vecSource)
 Create the Source of the ProxyLoader update.
 

Function Documentation

◆ cpp_backend_proxy()

bool cpp_backend_proxy ( const PVecSource & vecSource,
const PString & libraryName,
const PVecArchLib & vecArchLib,
const PPath & libDir,
const PPath & outputDir )

Save ProxyLoader with the corresponding a vector of PSource in the output directory.

Parameters
vecSource: vector of source
libraryName: name of the library to be created
vecArchLib: vector of library to be loaded on the fly by respect to the host architecture
libDir: directory where the sub libraries are installed
outputDir: output directory where to save ProxyLoader
Returns
true on success, false otherwise

Definition at line 273 of file backend_proxy_loader.cpp.

273 {
274 PString className(PString(libraryName + "ProxyLoader").firstToUpper());
275 PString headerSrc(cpp_backendProxyLoaderHeader(libraryName, className));
276 PString sourceSrc(cpp_backendProxyLoaderSource(vecSource, vecArchLib, className, libDir));
277 PPath outputHeaderFile(outputDir + "/" + className + ".h");
278 PPath outputSourceFile(outputDir + "/" + className + ".cpp");
279 if(!outputHeaderFile.saveFileContent(headerSrc)){
280 std::cerr << "cpp_backend : cannot save header file '"<<outputHeaderFile<<"'" << std::endl;
281 return false;
282 }
283 if(!outputSourceFile.saveFileContent(sourceSrc)){
284 std::cerr << "cpp_backend : cannot save source file '"<<outputSourceFile<<"'" << std::endl;
285 return false;
286 }
287 return true;
288}
PString cpp_backendProxyLoaderSource(const PVecSource &vecSource, const PVecArchLib &vecArchLib, const PString &className, const PString &libDir)
Create the Source of the ProxyLoader class.
PString cpp_backendProxyLoaderHeader(const PString &libraryName, const PString &className)
Create the Header definition of the ProxyLoader class.

References cpp_backendProxyLoaderHeader(), and cpp_backendProxyLoaderSource().

Referenced by createSourceFromConfig().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ cpp_backendProxyLoaderCloseSource()

PString cpp_backendProxyLoaderCloseSource ( const PString & className)

Create the Source of the ProxyLoader close.

Parameters
className: name of the ProxyLoader class
Returns
corresponding code

Definition at line 189 of file backend_proxy_loader.cpp.

189 {
190 PString body("");
191 body += "///Close the current library\n";
192 body += "void "+className+"::close(){\n";
193 body += "\tif(isOpen()){\n";
194 body += "\t\tdlclose(p_handle);\n";
195 body += "\t}\n";
196 body += "}\n\n";
197 return body;
198}

Referenced by cpp_backendProxyLoaderSource().

+ Here is the caller graph for this function:

◆ cpp_backendProxyLoaderConstructorSource()

PString cpp_backendProxyLoaderConstructorSource ( const PString & className,
const PString & defaultLib )

Create the Source of the ProxyLoader class constructor.

Parameters
className: name of the ProxyLoader class
defaultLib: default library to be use if no extention was found
Returns
corresponding code

Definition at line 48 of file backend_proxy_loader.cpp.

48 {
49 PString body("");
50 body += "///Default constructor of "+className+"\n";
51 body += className+"::"+className+"(){\n";
52 body += "\tinitialisation"+className+"();\n";
53 body += "\tstd::cout << \""+className+"::"+className+"() : check current architecture\" << std::endl;\n";
54 body += "\t//First get the /proc/cpuinfo file\n";
55 body += "\tif(!parseArchFile(\"/proc/cpuinfo\")){\n";
56 body += "\t\t//No library found, let's load the default library\n";
57 body += "\t\tload(\""+defaultLib+"\");\n";
58 body += "\t}\n";
59 body += "\tupdateFunction();\n";
60 body += "}\n\n";
61 return body;
62}

Referenced by cpp_backendProxyLoaderSource().

+ Here is the caller graph for this function:

◆ cpp_backendProxyLoaderDesonstructorSource()

PString cpp_backendProxyLoaderDesonstructorSource ( const PString & className)

Create the Source of the ProxyLoader class desctructor.

Parameters
className: name of the ProxyLoader class
Returns
corresponding code

Definition at line 68 of file backend_proxy_loader.cpp.

68 {
69 PString body("");
70 body += "///Destructor of "+className+"\n";
71 body += ""+className+"::~"+className+"(){\n";
72 body += "\tclose();\n";
73 body += "}\n\n";
74 return body;
75}

Referenced by cpp_backendProxyLoaderSource().

+ Here is the caller graph for this function:

◆ cpp_backendProxyLoaderHeader()

PString cpp_backendProxyLoaderHeader ( const PString & libraryName,
const PString & className )

Create the Header definition of the ProxyLoader class.

Parameters
libraryName: name of the library to be created (to avoid multiple definition)
className: name of the ProxyLoader class
Returns
corresponding code

Definition at line 15 of file backend_proxy_loader.cpp.

15 {
16 PString body(cpp_licenceSaveStr());
17 PString baseMacro("__" + className.toUpper() + "_H__");
18 body += "#ifndef " + baseMacro + "\n";
19 body += "#define " + baseMacro + "\n\n";
20 body += "#include <string>\n\n\n";
21 body += "///@brief Class which loads automatically the right sub-library of "+libraryName+"\n";
22 body += "class "+className+"{\n";
23 body += "\tpublic:\n";
24 body += "\t\t"+className+"();\n";
25 body += "\t\tvirtual ~"+className+"();\n";
26 body += "\t\t\n";
27 body += "\t\tbool load(const std::string & libName);\n";
28 body += "\t\tbool parseArchFile(const std::string & archfileName);\n";
29 body += "\t\tbool isOpen() const;\n";
30 body += "\t\tvoid close();\n";
31 body += "\t\t\n";
32 body += "\t\tvoid updateFunction();\n";
33 body += "\tprivate:\n";
34 body += "\t\tvoid initialisation"+className+"();\n";
35 body += "\t\t\n";
36 body += "\t\t///Handle of the library we load\n";
37 body += "\t\tvoid* p_handle;\n";
38 body += "};\n\n";
39 body += "#endif\n\n";
40 return body;
41}
PString cpp_licenceSaveStr()
Get the licence in string.
Definition backend.cpp:12

References cpp_licenceSaveStr().

Referenced by cpp_backend_proxy().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ cpp_backendProxyLoaderInitialisationSource()

PString cpp_backendProxyLoaderInitialisationSource ( const PString & className)

Create the Source of the ProxyLoader initialisation.

Parameters
className: name of the ProxyLoader class
Returns
corresponding code

Definition at line 222 of file backend_proxy_loader.cpp.

222 {
223 PString body("");
224 body += "///Initialisation function of the class "+className+"\n";
225 body += "void "+className+"::initialisation"+className+"(){\n";
226 body += "\tp_handle = NULL;\n";
227 body += "}\n\n";
228 return body;
229}

Referenced by cpp_backendProxyLoaderSource().

+ Here is the caller graph for this function:

◆ cpp_backendProxyLoaderIsOpenSource()

PString cpp_backendProxyLoaderIsOpenSource ( const PString & className)

Create the Source of the ProxyLoader isOpen.

Parameters
className: name of the ProxyLoader class
Returns
corresponding code

Definition at line 174 of file backend_proxy_loader.cpp.

174 {
175 PString body("");
176 body += "///Say if the current library is opened or not\n";
177 body += "/**\t@return true if the current library is opened, false otherwise\n";
178 body += "*/\n";
179 body += "bool "+className+"::isOpen() const{\n";
180 body += "\treturn p_handle != NULL;\n";
181 body += "}\n\n";
182 return body;
183}

Referenced by cpp_backendProxyLoaderSource().

+ Here is the caller graph for this function:

◆ cpp_backendProxyLoaderLoadSource()

PString cpp_backendProxyLoaderLoadSource ( const PString & className)

Create the Source of the ProxyLoader load.

Parameters
className: name of the ProxyLoader class
Returns
corresponding code

Definition at line 81 of file backend_proxy_loader.cpp.

81 {
82 PString body("");
83 body += "///Load the given library\n";
84 body += "/**\t@param libName : name of the library to be loaded\n";
85 body += "* \t@return true on success, false otherwise\n";
86 body += "*/\n";
87 body += "bool "+className+"::load(const std::string & libName){\n";
88 body += "\tif(libName == \"\"){return false;}\n";
89 body += "\tp_handle = dlopen(libName.c_str(), RTLD_LAZY);\n";
90 body += "\tstd::cout << \""+className+"::load : loading library '\"<<libName<<\"'\" << std::endl;\n";
91 body += "\tif(p_handle == NULL){\n";
92 body += "\t\tfprintf(stderr, \""+className+"::load : %s\\n\", dlerror());\n";
93 body += "\t\treturn false;\n";
94 body += "\t}\n";
95 body += "\tdlerror(); //Erase an exsiting error\n";
96 body += "\treturn true;\n";
97 body += "}\n\n";
98 return body;
99}

Referenced by cpp_backendProxyLoaderSource().

+ Here is the caller graph for this function:

◆ cpp_backendProxyLoaderParseArchFileSource()

PString cpp_backendProxyLoaderParseArchFileSource ( const PVecArchLib & vecArchLib,
const PString & className,
const PPath & libDir )

Create the Source of the ProxyLoader load.

Parameters
vecArchLib: vector of library to be loaded on the fly by respect to the host architecture
className: name of the ProxyLoader class
libDir: directory where the sub libraries are installed
Returns
corresponding code

Definition at line 108 of file backend_proxy_loader.cpp.

108 {
109 PString body("");
110 body += "///Parse the given architecture file\n";
111 body += "/**\t@param archfileName : name of the architecture file to be loaded\n";
112 body += "* \t@return true on success, false otherwise\n";
113 body += "*/\n";
114 body += "bool "+className+"::parseArchFile(const std::string & archfileName){\n";
115 body += "\tFILE * fp = fopen(archfileName.c_str(), \"r\");\n";
116 body += "\tif(fp == NULL){\n";
117 body += "\t\tstd::cerr << \"Cannot open file '\" << archfileName << \"'\" << std::endl;\n";
118 body += "\t\treturn false;\n";
119 body += "\t}\n";
120
121 body += "\tstd::string bufferAllFile(\"\");\n";
122 body += "\tint buffer;\n";
123 body += "\twhile(!feof(fp)){\n";
124 body += "\t\tbuffer = fgetc(fp);\n";
125 body += "\t\tif(buffer != EOF) bufferAllFile += (char)buffer;\n";
126 body += "\t\telse break;\n";
127 body += "\t}\n";
128 body += "\tif(bufferAllFile == \"\"){\n";
129 body += "\t\tstd::cerr << \"Empty file '\" << archfileName << \"'\" << std::endl;\n";
130 body += "\t\treturn false;\n\t}\n";
131 body += "\tconst char * procCpuInfo = bufferAllFile.c_str();\n";
132 //The following method does not work with text file for some reason
133// body += "\t//Let's get the file size\n";
134// body += "\tfseek(fp, 0, SEEK_END);\n";
135// body += "\tsize_t fileSize(ftell(fp));\n";
136// body += "\tfseek(fp, 0, SEEK_SET);\n";
137// body += "\tif(fileSize == 0lu){\n";
138// body += "\t\tstd::cerr << \"Empty file '\" << archfileName << \"'\" << std::endl;\n";
139// body += "\t\treturn false;\n";
140// body += "\t}\n";
141// body += "\t//Now we can alloate the table to get the file content\n";
142// body += "\tchar * procCpuInfo = new char[fileSize];\n";
143// body += "\t//And load the file\n";
144// body += "\tif(fread(procCpuInfo, sizeof(char), fileSize, fp) != fileSize){\n";
145// body += "\t\tstd::cerr << \"Cannot read file '\" << archfileName << \"'\" << std::endl;\n";
146// body += "\t\treturn false;\n\t}\n";
147
148 body += "\t//Let's check the avalaible architectures\n";
149 body += "\tbool isLibFound(true);\n";
150 PString strElse("\t"), libraryFullDirectory("");
151 if(libDir != ""){
152 libraryFullDirectory = libDir + "/";
153 }
154 for(PVecArchLib::const_reverse_iterator it(vecArchLib.rbegin()); it != vecArchLib.rend(); ++it){
155 body += strElse + "if(strstr(procCpuInfo, \""+it->getArchitecture()+"\") != NULL){\n";
156 PPath libName(libraryFullDirectory + it->getName().getFileName());
157 body += "\t\tload(\""+libName+"\");\n";
158 body += "\t}";
159 strElse = "else ";
160 }
161 body += "else{\n\t\tisLibFound = false;\n\t}\n";
162 body += "\n";
163 body += "\tfclose(fp);\n";
164// body += "\tdelete [] procCpuInfo;\n";
165 body += "\treturn isLibFound;\n";
166 body += "}\n\n";
167 return body;
168}

Referenced by cpp_backendProxyLoaderSource().

+ Here is the caller graph for this function:

◆ cpp_backendProxyLoaderSource()

PString cpp_backendProxyLoaderSource ( const PVecSource & vecSource,
const PVecArchLib & vecArchLib,
const PString & className,
const PString & libDir )

Create the Source of the ProxyLoader class.

Parameters
vecSource: vector of source
vecArchLib: vector of library to be loaded on the fly by respect to the host architecture
className: name of the ProxyLoader class
libDir: directory where the sub libraries are installed
Returns
corresponding code

Definition at line 238 of file backend_proxy_loader.cpp.

240{
241 PString body(cpp_licenceSaveStr());
242 body += "#include <stdio.h>\n";
243 body += "#include <stdlib.h>\n";
244 body += "#include <dlfcn.h>\n";
245 body += "#include <string.h>\n\n";
246
247 for(PVecSource::const_iterator it(vecSource.begin()); it != vecSource.end(); ++it){
248 body += "#include \""+it->getName()+".h\"\n";
249 }
250 body += "\n#include \""+className+".h\"\n\n";
251 body += "///Variable which will load automatically the right library by respect to the given architecture\n";
252 body += className + " PROXY_LOADER;\n\n";
253 PString defaultLib(getLibraryFile(vecArchLib).getFileName());
254 body += cpp_backendProxyLoaderConstructorSource(className, defaultLib);
256 body += cpp_backendProxyLoaderLoadSource(className);
257 body += cpp_backendProxyLoaderParseArchFileSource(vecArchLib, className, libDir);
258 body += cpp_backendProxyLoaderIsOpenSource(className);
259 body += cpp_backendProxyLoaderCloseSource(className);
260 body += cpp_backendProxyLoaderUpdateSource(className, vecSource);
262 return body;
263}
PString cpp_backendProxyLoaderDesonstructorSource(const PString &className)
Create the Source of the ProxyLoader class desctructor.
PString cpp_backendProxyLoaderLoadSource(const PString &className)
Create the Source of the ProxyLoader load.
PString cpp_backendProxyLoaderParseArchFileSource(const PVecArchLib &vecArchLib, const PString &className, const PPath &libDir)
Create the Source of the ProxyLoader load.
PString cpp_backendProxyLoaderCloseSource(const PString &className)
Create the Source of the ProxyLoader close.
PString cpp_backendProxyLoaderUpdateSource(const PString &className, const PVecSource &vecSource)
Create the Source of the ProxyLoader update.
PString cpp_backendProxyLoaderConstructorSource(const PString &className, const PString &defaultLib)
Create the Source of the ProxyLoader class constructor.
PString cpp_backendProxyLoaderInitialisationSource(const PString &className)
Create the Source of the ProxyLoader initialisation.
PString cpp_backendProxyLoaderIsOpenSource(const PString &className)
Create the Source of the ProxyLoader isOpen.
PPath getLibraryFile(const PVecArchLib &vecArch)
Get the first non empty library file name.

References cpp_backendProxyLoaderCloseSource(), cpp_backendProxyLoaderConstructorSource(), cpp_backendProxyLoaderDesonstructorSource(), cpp_backendProxyLoaderInitialisationSource(), cpp_backendProxyLoaderIsOpenSource(), cpp_backendProxyLoaderLoadSource(), cpp_backendProxyLoaderParseArchFileSource(), cpp_backendProxyLoaderUpdateSource(), cpp_licenceSaveStr(), and getLibraryFile().

Referenced by cpp_backend_proxy().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ cpp_backendProxyLoaderUpdateSource()

PString cpp_backendProxyLoaderUpdateSource ( const PString & className,
const PVecSource & vecSource )

Create the Source of the ProxyLoader update.

Parameters
className: name of the ProxyLoader class
vecSource: vector of source
Returns
corresponding code

Definition at line 205 of file backend_proxy_loader.cpp.

205 {
206 PString body("");
207 body += "///Update the functions to be used\n";
208 body += "void "+className+"::updateFunction(){\n";
209 body += "\tif(!isOpen()){exit(-1);return;}\n";
210 body += "\t//Then set all the function pointers\n";
211 for(PVecSource::const_iterator it(vecSource.begin()); it != vecSource.end(); ++it){
212 body += "\t"+getUpdateFunction(it->getName())+"(p_handle);\n";
213 }
214 body += "}\n\n";
215 return body;
216}
PString getUpdateFunction(const PPath &fileName)
Get the update type name of the given file.
Definition backend.cpp:46

References getUpdateFunction().

Referenced by cpp_backendProxyLoaderSource().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: