diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d43a9a..5d2f60c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -82,6 +82,21 @@ endif() currentdate(CN24_DATE) add_definitions("-DBUILD_DATE=\"${CN24_DATE}\"") +# Tell CN24 the operating system +if(WIN32) + add_definitions("-DBUILD_WIN32") +else() + if(APPLE) + add_definitions("-DBUILD_OSX") + else() + if(UNIX) + add_definitions("-DBUILD_LINUX") + else() + message(STATUS "Unsupported OS, good luck!") + endif() + endif() +endif() + # The library comes first # Recurse over files file(GLOB_RECURSE CN24_SOURCES ${CN24_SRC}/*.cpp) diff --git a/include/cn24/util/Init.h b/include/cn24/util/Init.h index 87a1be2..cfcac37 100644 --- a/include/cn24/util/Init.h +++ b/include/cn24/util/Init.h @@ -14,11 +14,14 @@ #ifndef CONV_INIT_H #define CONV_INIT_H +#include + namespace Conv { class TensorViewer; class System { public: static void Init(); + static void GetExecutablePath(std::string& binary_path); static TensorViewer* viewer; }; } diff --git a/src/util/Init.cpp b/src/util/Init.cpp index 230dc72..7bfd2be 100644 --- a/src/util/Init.cpp +++ b/src/util/Init.cpp @@ -22,6 +22,18 @@ #include #endif +#ifdef BUILD_WIN32 +#include +#else +#ifdef BUILD_OSX +#include +#else +#ifdef BUILD_LINUX +#include +#endif +#endif +#endif + #include "TensorViewer.h" namespace Conv { @@ -56,6 +68,10 @@ void System::Init() { LOGINFO << "Copyright (C) 2015 Clemens-Alexander Brust"; LOGINFO << "For licensing information, see the LICENSE" << " file included with this project."; + + std::string binary_path; + GetExecutablePath(binary_path); + LOGDEBUG << "Executable path: " << binary_path; CLHelper::Init(); #ifdef BUILD_GUI @@ -66,6 +82,59 @@ void System::Init() { viewer = new TensorViewer(); } +void System::GetExecutablePath(std::string& binary_path) { +#ifdef BUILD_WIN32 + binary_path = ""; + TCHAR path[16384]; + DWORD return_value = GetModuleFileName(NULL, path, 16384); + if (return_value > 0 && return_value < 16384) { + DWORD last_error = GetLastError(); + if (last_error != ERROR_SUCCESS) { + LOGWARN << "Could not get executable path, may be unable to locate kernels!"; + } + binary_path = std::string(path); + std::size_t last_slash = binary_path.rfind("\\"); + // last_slash should never be npos because this is supposed to be a path + binary_path = binary_path.substr(0, last_slash + 1); + } + else { + LOGWARN << "Could not get executable path, may be unable to locate kernels!"; + } +#else +#ifdef BUILD_OSX + binary_path = ""; + char path[16384]; + uint32_t size = sizeof(path); + if (_NSGetExecutablePath(path, &size) == 0) { + binary_path = std::string(path); + std::size_t last_slash = binary_path.rfind("/"); + // last_slash should never be npos because this is supposed to be a path + binary_path = binary_path.substr(0, last_slash+1); + } + else { + LOGWARN << "Could not get executable path, may be unable to locate kernels!"; + } +#else +#ifdef BUILD_LINUX + char buffer[16384]; + ssize_t path_length = ::readlink("/proc/self/exe", buffer, sizeof(buffer)-1); + binary_path = ""; + if(path_length != -1) { + buffer[path_length] = '\0'; + binary_path = std::string(buffer); + std::size_t last_slash = binary_path.rfind("/"); + // last_slash should never be npos because this is supposed to be a path + binary_path = binary_path.substr(0, last_slash+1); + } else { + LOGWARN << "Could not get executable path, may be unable to locate kernels!"; + } +#else + binary_path = ""; +#endif +#endif +#endif +} + void CLHelper::Init() { #ifdef BUILD_OPENCL // TODO make this configurable @@ -257,14 +326,19 @@ cl_program CLHelper::CreateProgram ( const char* file_name ) { cl_program program = 0; LOGDEBUG << "Compiling " << file_name; + + std::string binary_path; + System::GetExecutablePath(binary_path); #ifdef _MSC_VER - std::ifstream kernel_file ( "../" + std::string ( file_name ), std::ios::in ); + std::string full_path = binary_path + "..\\" + std::string(file_name); + std::ifstream kernel_file ( full_path, std::ios::in ); #else - std::ifstream kernel_file ( file_name, std::ios::in ); + std::string full_path = binary_path + std::string(file_name); + std::ifstream kernel_file ( full_path, std::ios::in ); #endif if ( !kernel_file.good() ) { - FATAL ( "Cannot open kernel: " << file_name ); + FATAL ( "Cannot open kernel: " << full_path ); } std::ostringstream oss; diff --git a/tools/testOpenCL.cpp b/tools/testOpenCL.cpp index 55cc062..831be86 100644 --- a/tools/testOpenCL.cpp +++ b/tools/testOpenCL.cpp @@ -58,6 +58,7 @@ int main() { #else int main() { + Conv::System::Init(); LOGERROR << "OpenCL not built in!"; LOGEND; return -1;