Skip to content

Commit

Permalink
Merge pull request #18 from cvjena/master
Browse files Browse the repository at this point in the history
Fix issue #17
  • Loading branch information
Clemens-Alexander Brust committed Feb 23, 2015
2 parents 864c07b + da4e106 commit 4bd3cd1
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 3 deletions.
15 changes: 15 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions include/cn24/util/Init.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
#ifndef CONV_INIT_H
#define CONV_INIT_H

#include <string>

namespace Conv {
class TensorViewer;
class System {
public:
static void Init();
static void GetExecutablePath(std::string& binary_path);
static TensorViewer* viewer;
};
}
Expand Down
80 changes: 77 additions & 3 deletions src/util/Init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@
#include <gtk/gtk.h>
#endif

#ifdef BUILD_WIN32
#include <Windows.h>
#else
#ifdef BUILD_OSX
#include <mach-o/dyld.h>
#else
#ifdef BUILD_LINUX
#include <unistd.h>
#endif
#endif
#endif

#include "TensorViewer.h"

namespace Conv {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions tools/testOpenCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ int main() {
#else

int main() {
Conv::System::Init();
LOGERROR << "OpenCL not built in!";
LOGEND;
return -1;
Expand Down

0 comments on commit 4bd3cd1

Please sign in to comment.