55 :
cmd(
"missing"), flags(
"missing"), lang(
"missing"), out(
"-o "), temp(
detectTemp())
58 env = getenv (
"JIT_COMPILER_CMD");
59 if(env!=NULL)
cmd = env;
61 env = getenv (
"JIT_COMPILER_FLAGS");
62 if(env!=NULL) flags = env;
64 env = getenv (
"JIT_COMPILER_LANG");
65 if(env!=NULL) lang = env;
67 env = getenv (
"JIT_COMPILER_TEMP");
68 if(env!=NULL) temp = env;
75 const std::string& flags,
76 const std::string& lang,
77 const std::string& out,
79 :
cmd(
cmd), flags(flags), lang(lang), out(out), temp(temp)
84 std::swap(
cmd , other.
cmd );
85 std::swap(flags, other.flags);
86 std::swap(lang , other.lang );
87 std::swap(out , other.out );
88 std::swap(temp , other.temp );
91# if __cplusplus >= 201103L || _MSC_VER >= 1600
110 :
cmd(std::move(other.
cmd)), flags(std::move(other.flags)),
111 lang(std::move(other.lang)), out(std::move(other.out)),
112 temp(std::move(other.temp))
118 cmd = std::move(other.cmd);
119 flags = std::move(other.flags);
120 lang = std::move(other.lang);
121 out = std::move(other.out);
122 temp = std::move(other.temp);
135 virtual const std::string&
getCmd()
const {
return cmd; }
138 virtual const std::string&
getFlags()
const {
return flags; }
141 virtual const std::string&
getLang()
const {
return lang; }
144 virtual const std::string&
getOut()
const {
return out; }
147 virtual const std::string&
getTemp()
const {
return temp; }
151 { this->cmd = _cmd; }
155 { this->flags = _flags; }
159 { this->lang = _lang; }
163 { this->out = _out; }
167 { this->temp = _temp; }
170 std::ostream&
print(std::ostream &os)
const
172 os <<
"JIT Compiler.\n"
173 <<
" cmd: " <<
cmd <<
"\n"
174 <<
" flags: " << flags <<
"\n"
175 <<
" language: " << lang <<
"\n"
176 <<
" output flag: " << out <<
"\n"
177 <<
" temporal directory: " << temp <<
"\n";
183 void load(
const std::string filename,
187 "Error: Invalid compiler language.");
192 std::swap(*cc, *
this);
193 if (this->temp.empty()) this->temp=
detectTemp();
204 std::swap(*cc, *
this);
205 if (this->temp.empty()) this->temp=
detectTemp();
227 GISMO_ERROR(
"Error : Clang does not provide any Fortran compiler.");
408# if defined(__INTEL_COMPILER)
415# elif defined(_MSC_VER)
418# elif defined(__clang__)
421# elif defined(__GNUC__)
422# if defined(__cplusplus)
424# elif defined(__GFortran__)
430# elif defined(__PGIC__)
433# elif defined(__SUNPRO_C)
435# elif defined(__SUNPRO_CC)
437# elif defined(__SUNPRO_F90) || defined(__SUNPRO_F95)
463inline std::ostream &operator<<(std::ostream &os,
465{
return c.
print(os); }
474class gsXml< gsJITCompilerConfig >
480 GSXML_COMMON_FUNCTIONS(gsJITCompilerConfig)
481 GSXML_GET_POINTER(gsJITCompilerConfig)
482 static std::string tag () {
return "JITCompilerConfig"; }
483 static std::string type() {
return ""; }
485 static void get_into(gsXmlNode * node, gsJITCompilerConfig & result)
487 gsXmlAttribute * tmp = node->first_attribute(
"cmd");
489 result.setCmd(tmp->value());
491 tmp = node->first_attribute(
"flags");
493 result.setFlags(tmp->value());
495 tmp = node->first_attribute(
"lang");
497 result.setLang(tmp->value());
499 tmp = node->first_attribute(
"out");
501 result.setOut(tmp->value());
503 tmp = node->first_attribute(
"temp");
505 result.setTemp(tmp->value());
508 static gsXmlNode * put (
const gsJITCompilerConfig & obj, gsXmlTree & data)
514 tmp->append_attribute(
makeAttribute(
"cmd" , obj.getCmd() , data) );
515 tmp->append_attribute(
makeAttribute(
"flags", obj.getFlags(), data) );
516 tmp->append_attribute(
makeAttribute(
"lang" , obj.getLang() , data) );
517 tmp->append_attribute(
makeAttribute(
"out" , obj.getOut() , data) );
518 tmp->append_attribute(
makeAttribute(
"temp" , obj.getTemp() , data) );
541 gsDebug <<
"Loading dynamic library: " << filename <<
"\n";
545 HMODULE dl = LoadLibrary(filename);
548 std::ostringstream err;
549 err <<
"LoadLibrary - error: " << GetLastError();
550 throw std::runtime_error( err.str() );
552 handle.reset(dl, FreeLibrary);
553#elif defined(__APPLE__) || defined(__linux__) || defined(__unix)
554 void * dl = ::dlopen(filename, flag);
556 throw std::runtime_error( ::dlerror() );
557 handle.reset(dl, ::dlclose);
559#error("Unsupported operating system")
568 throw std::runtime_error(
"An error occured while accessing the dynamic library");
572 *(
void **)(&symbol) = (
void*)GetProcAddress(
handle.get(), name );
573#elif defined(__APPLE__) || defined(__linux__) || defined(__unix)
574 *(
void **)(&symbol) = ::dlsym(
handle.get(), name );
577 throw std::runtime_error(
"An error occured while getting symbol from the dynamic library");
583 operator bool()
const {
return (
bool)
handle; }
589 memory::shared_ptr< util::remove_pointer<HMODULE>::type >
handle;
631# if __cplusplus >= 201103L || _MSC_VER >= 1600
637 kernel << other.kernel.rdbuf();
644 config = std::move(other.config);
667# if __cplusplus >= 201103L || _MSC_VER >= 1600
668 size_t h = std::hash<std::string>()(
getKernel().str() +
671 return build(std::to_string(h), force);
673 return build(
"JIT",
true);
682 std::stringstream libName;
688# elif defined(__APPLE__)
690# elif defined(unix) || defined(__unix__) || defined(__unix)
693# error("Unsupported operating system")
697 std::ifstream libfile(libName.str().c_str());
698 if(!libfile || force)
701 std::stringstream srcName;
704 std::ofstream file(srcName.str().c_str());
705 file <<
"#ifdef __cplusplus\n";
706 file <<
"#define EXPORT extern \"C\" __declspec(dllexport)\n";
710 std::ofstream file(srcName.str().c_str());
711 file <<
"#ifdef __cplusplus\n";
712 file <<
"#define EXPORT extern \"C\"\n";
719 std::stringstream systemcall;
725 << srcName.str() <<
"\" "
730 << srcName.str() <<
"\" "
734 gsDebug <<
"Compiling dynamic library: " << systemcall.str() <<
"\n";
735 if(std::system(systemcall.str().c_str()) != 0)
736 throw std::runtime_error(
"An error occured while compiling the kernel source code");
750 kernel.str(std::string());
754 std::ostream&
print(std::ostream &os)
const
782{
return c.
print(os); }
791class gsXml< gsJITCompiler >
797 GSXML_COMMON_FUNCTIONS(gsJITCompiler)
798 GSXML_GET_POINTER(gsJITCompiler)
799 static std::string tag () {
return "JITCompiler"; }
800 static std::string type() {
return ""; }
802 static void get_into(gsXmlNode * node, gsJITCompiler & result)
804 result.getKernel() << node->value();
807 static gsXmlNode * put (
const gsJITCompiler & obj, gsXmlTree & data)
This class represents an XML data tree which can be read from or written to a (file) stream.
Definition gsFileData.h:34
static std::string getTempPath()
Auto-detect temp directory.
Definition gsFileManager.cpp:355
Class defining a just-in-time compiler.
Definition gsJITCompiler.h:604
gsDynamicLibrary build(const std::string &name, bool force=false)
Definition gsJITCompiler.h:679
const std::ostringstream & getKernel() const
Return kernel source code (as output stringstream)
Definition gsJITCompiler.h:761
gsJITCompiler & operator<<(std::istream &is)
Input kernel source code from input stream.
Definition gsJITCompiler.h:657
gsJITCompilerConfig config
Compiler configuration.
Definition gsJITCompiler.h:777
gsJITCompiler & operator<<(const std::string &s)
Input kernel source code from string.
Definition gsJITCompiler.h:650
gsJITCompiler(const gsJITCompilerConfig &config)
Constructor (using compiler configuration)
Definition gsJITCompiler.h:619
gsJITCompiler()
Constructor (default)
Definition gsJITCompiler.h:607
gsJITCompiler & operator=(gsJITCompiler const &other)
Assignment operator (copy)
Definition gsJITCompiler.h:624
void clear()
Clear kernel source code.
Definition gsJITCompiler.h:747
std::ostringstream & getKernel()
Return pointer to kernel source code.
Definition gsJITCompiler.h:767
gsJITCompiler(gsJITCompiler const &other)
Constructor (copy)
Definition gsJITCompiler.h:612
std::ostream & print(std::ostream &os) const
Prints the object as a string.
Definition gsJITCompiler.h:754
std::ostringstream kernel
Kernel source code.
Definition gsJITCompiler.h:774
gsDynamicLibrary build(bool force=false)
Definition gsJITCompiler.h:665
#define gsDebug
Definition gsDebug.h:61
#define GISMO_ERROR(message)
Definition gsDebug.h:118
#define GISMO_UNUSED(x)
Definition gsDebug.h:112
#define GISMO_ENSURE(cond, message)
Definition gsDebug.h:102
Utility class for finding files and handling paths.
Provides utility function related to memory management.
Provides declaration of input/output XML utilities struct.
gsXmlNode * makeNode(const std::string &name, gsXmlTree &data)
Helper to allocate XML node.
Definition gsXml.cpp:54
gsXmlAttribute * makeAttribute(const std::string &name, const std::string &value, gsXmlTree &data)
Helper to allocate XML attribute.
Definition gsXml.cpp:37
The G+Smo namespace, containing all definitions for the library.
Class defining a dynamic library.
Definition gsJITCompiler.h:533
T * getSymbol(const char *name) const
Get symbol from dynamic library.
Definition gsJITCompiler.h:565
memory::shared_ptr< void > handle
Handle to dynamic library object.
Definition gsJITCompiler.h:591
gsDynamicLibrary()
Default Constructor.
Definition gsJITCompiler.h:536
gsDynamicLibrary(const char *filename, int flag)
Constructor (using file name)
Definition gsJITCompiler.h:539
Struct definig a compiler configuration.
Definition gsJITCompiler.h:52
static gsJITCompilerConfig clang(const int lang=gsJITLang::CXX)
Initialize to default Clang compiler.
Definition gsJITCompiler.h:210
gsJITCompilerConfig()
Constructor (default)
Definition gsJITCompiler.h:54
virtual const std::string & getTemp() const
Return compiler temporal directory.
Definition gsJITCompiler.h:147
static gsJITCompilerConfig msvc(const int lang=gsJITLang::CXX)
Initialize to default Microsoft Visual Studio compiler.
Definition gsJITCompiler.h:312
void setCmd(const std::string &_cmd)
Set compiler command.
Definition gsJITCompiler.h:150
gsJITCompilerConfig & operator=(gsJITCompilerConfig other)
Assignment operator.
Definition gsJITCompiler.h:127
virtual const std::string & getFlags() const
Return compiler flags.
Definition gsJITCompiler.h:138
virtual const std::string & getCmd() const
Return compiler command.
Definition gsJITCompiler.h:135
static gsJITCompilerConfig sunstudio(const int lang=gsJITLang::CXX)
Initialize to default Oracle/SunStudio compiler.
Definition gsJITCompiler.h:378
void load(const std::string filename, const int _lang=gsJITLang::CXX)
Reads compiler configuration from XML file by language.
Definition gsJITCompiler.h:183
static gsJITCompilerConfig gcc(const int lang=gsJITLang::CXX)
Initialize to default GCC compiler.
Definition gsJITCompiler.h:235
void setFlags(const std::string &_flags)
Set compiler flags.
Definition gsJITCompiler.h:154
static std::string detectTemp()
Auto-detect temp directory.
Definition gsJITCompiler.h:456
gsJITCompilerConfig(const std::string &cmd, const std::string &flags, const std::string &lang, const std::string &out, const std::string &temp=detectTemp())
Constructor (passing arguments as strings)
Definition gsJITCompiler.h:74
virtual const std::string & getLang() const
Return compiler language.
Definition gsJITCompiler.h:141
std::string cmd
Members variables.
Definition gsJITCompiler.h:447
static gsJITCompilerConfig nvcc(const int lang=gsJITLang::CUDA)
Initialize to default NVIDIA nvcc compiler.
Definition gsJITCompiler.h:334
static gsJITCompilerConfig pgi(const int lang=gsJITLang::CXX)
Initialize to default PGI compiler.
Definition gsJITCompiler.h:350
static gsJITCompilerConfig intel(const int lang=gsJITLang::CXX)
Initialize to default Intel compiler.
Definition gsJITCompiler.h:263
void setTemp(const std::string &_temp)
Set compiler temporal directory.
Definition gsJITCompiler.h:166
void load_id(const std::string filename, const int id)
Reads compiler configuration from XML file by ID.
Definition gsJITCompiler.h:198
static gsJITCompilerConfig guess()
Try to initialize compiler automatically based on the context.
Definition gsJITCompiler.h:406
std::ostream & print(std::ostream &os) const
Prints the object as a string.
Definition gsJITCompiler.h:170
void setLang(const std::string &_lang)
Set compiler language.
Definition gsJITCompiler.h:158
void setOut(const std::string &_out)
Set compiler output flag.
Definition gsJITCompiler.h:162
virtual const std::string & getOut() const
Return compiler output flag.
Definition gsJITCompiler.h:144
Supported languages.
Definition gsJITCompiler.h:36
@ CUDA
Cuda.
Definition gsJITCompiler.h:40
@ C
C.
Definition gsJITCompiler.h:38
@ Fortran
Fortran.
Definition gsJITCompiler.h:41
@ CXX
C++.
Definition gsJITCompiler.h:39