Skip to content

Commit

Permalink
Merge pull request #23 from cvjena/master
Browse files Browse the repository at this point in the history
Enhanced OpenCL kernel search locations, Made OpenCL platform and device configurable
  • Loading branch information
Clemens-Alexander Brust committed Jun 13, 2015
2 parents 1fd36aa + 59b0359 commit 90873bc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
3 changes: 2 additions & 1 deletion include/private/CLHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
namespace Conv {
class CLHelper {
public:
static void Init();
static void Init(unsigned int platform_number = 0,
unsigned int device_number = 0);
#ifdef BUILD_OPENCL
static cl_program CreateProgram(const char* file_name);
static cl_device_id device;
Expand Down
48 changes: 37 additions & 11 deletions src/util/Init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "Init.h"
#include "CLHelper.h"
#include "Config.h"
#include "ConfigParsing.h"
#include "Log.h"

#include <locale.h>
Expand Down Expand Up @@ -74,8 +75,33 @@ void System::Init() {
std::string binary_path;
GetExecutablePath(binary_path);
LOGDEBUG << "Executable path: " << binary_path;

unsigned int platform_number = 0;
unsigned int device_number = 0;

// Look for configuration file
std::string config_path = binary_path + "config";
if(!std::ifstream(config_path, std::ios::in).good()) {
config_path = binary_path + "../config";
}

// Load and parse config file
std::ifstream config_file(config_path, std::ios::in);
if(config_file.good()) {
LOGINFO << "Loading config file: " << config_path;

while (!config_file.eof()) {
std::string line;
std::getline (config_file, line);

ParseUIntIfPossible(line, "opencl_platform", platform_number);
ParseUIntIfPossible(line, "opencl_device", device_number);
}
} else {
LOGINFO << "Could not find a config file.";
}

CLHelper::Init();
CLHelper::Init(platform_number, device_number);
#ifdef BUILD_GUI
if(!gtk_init_check ( nullptr, nullptr )) {
LOGWARN << "Could not initialize GTK!";
Expand Down Expand Up @@ -137,12 +163,8 @@ void System::GetExecutablePath(std::string& binary_path) {
#endif
}

void CLHelper::Init() {
void CLHelper::Init(unsigned int platform_number, unsigned int device_number) {
#ifdef BUILD_OPENCL
// TODO make this configurable
unsigned int platform_number = 0;
unsigned int device_number = 0;

cl_uint platform_count = 0;
clGetPlatformIDs ( 0, 0, &platform_count );

Expand Down Expand Up @@ -331,13 +353,17 @@ cl_program CLHelper::CreateProgram ( const char* file_name ) {

std::string binary_path;
System::GetExecutablePath(binary_path);
#ifdef _MSC_VER
std::string full_path = binary_path + "..\\" + std::string(file_name);
std::ifstream kernel_file ( full_path, std::ios::in );
#else

// Search in binary path first
std::string full_path = binary_path + std::string(file_name);

// If kernel cannot be found, go up one folder (Xcode, Visual Studio and
// other multi-target build setups)
if ( !std::ifstream(full_path, std::ios::in).good()) {
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: " << full_path );
Expand Down
6 changes: 3 additions & 3 deletions tools/testOpenCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ int main() {
for(int i = 0; i < platform_count; i++) {
cl_uint device_count = 0;
clGetDeviceIDs(platform_ids[i], CL_DEVICE_TYPE_ALL, 0, 0, &device_count);
LOGINFO << "Platform " << i + 1 << ": " << device_count << " devices";
LOGINFO << "Platform " << i << ": " << device_count << " devices";

cl_device_id* device_ids = new cl_device_id[device_count];
clGetDeviceIDs(platform_ids[i], CL_DEVICE_TYPE_ALL, device_count,
Expand All @@ -38,11 +38,11 @@ int main() {
for(int j = 0; j < device_count; j++) {
char buf[256];
clGetDeviceInfo(device_ids[j], CL_DEVICE_NAME, 256, buf, 0);
LOGINFO << "Platform " << i + 1 << ", device " << j + 1 << ": "
LOGINFO << "Platform " << i << ", device " << j << ": "
<< buf;

clGetDeviceInfo(device_ids[j], CL_DEVICE_VERSION, 256, buf, 0);
LOGINFO << "Platform " << i + 1 << ", device " << j + 1 << ": supports "
LOGINFO << "Platform " << i << ", device " << j << ": supports "
<< buf;
}

Expand Down

0 comments on commit 90873bc

Please sign in to comment.