diff options
Diffstat (limited to 'intern/cycles/device/opencl/opencl_util.cpp')
-rw-r--r-- | intern/cycles/device/opencl/opencl_util.cpp | 485 |
1 files changed, 403 insertions, 82 deletions
diff --git a/intern/cycles/device/opencl/opencl_util.cpp b/intern/cycles/device/opencl/opencl_util.cpp index 82e1640e508..7d5173a5f1d 100644 --- a/intern/cycles/device/opencl/opencl_util.cpp +++ b/intern/cycles/device/opencl/opencl_util.cpp @@ -16,11 +16,12 @@ #ifdef WITH_OPENCL -#include "opencl.h" +#include "device/opencl/opencl.h" -#include "util_logging.h" -#include "util_path.h" -#include "util_time.h" +#include "util/util_logging.h" +#include "util/util_md5.h" +#include "util/util_path.h" +#include "util/util_time.h" using std::cerr; using std::endl; @@ -234,15 +235,15 @@ string OpenCLCache::get_kernel_md5() thread_scoped_lock lock(self.kernel_md5_lock); if(self.kernel_md5.empty()) { - self.kernel_md5 = path_files_md5_hash(path_get("kernel")); + self.kernel_md5 = path_files_md5_hash(path_get("source")); } return self.kernel_md5; } OpenCLDeviceBase::OpenCLProgram::OpenCLProgram(OpenCLDeviceBase *device, - string program_name, - string kernel_file, - string kernel_build_options, + const string& program_name, + const string& kernel_file, + const string& kernel_build_options, bool use_stdout) : device(device), program_name(program_name), @@ -273,20 +274,21 @@ void OpenCLDeviceBase::OpenCLProgram::release() } } -void OpenCLDeviceBase::OpenCLProgram::add_log(string msg, bool debug) +void OpenCLDeviceBase::OpenCLProgram::add_log(const string& msg, bool debug) { if(!use_stdout) { log += msg + "\n"; } else if(!debug) { printf("%s\n", msg.c_str()); + fflush(stdout); } else { VLOG(2) << msg; } } -void OpenCLDeviceBase::OpenCLProgram::add_error(string msg) +void OpenCLDeviceBase::OpenCLProgram::add_error(const string& msg) { if(use_stdout) { fprintf(stderr, "%s\n", msg.c_str()); @@ -309,6 +311,8 @@ bool OpenCLDeviceBase::OpenCLProgram::build_kernel(const string *debug_src) string build_options; build_options = device->kernel_build_options(debug_src) + kernel_build_options; + VLOG(1) << "Build options passed to clBuildProgram: '" + << build_options << "'."; cl_int ciErr = clBuildProgram(program, 0, NULL, build_options.c_str(), NULL, NULL); /* show warnings even if build is successful */ @@ -336,12 +340,13 @@ bool OpenCLDeviceBase::OpenCLProgram::build_kernel(const string *debug_src) bool OpenCLDeviceBase::OpenCLProgram::compile_kernel(const string *debug_src) { - string source = "#include \"kernels/opencl/" + kernel_file + "\" // " + OpenCLCache::get_kernel_md5() + "\n"; + string source = "#include \"kernel/kernels/opencl/" + kernel_file + "\"\n"; /* We compile kernels consisting of many files. unfortunately OpenCL * kernel caches do not seem to recognize changes in included files. * so we force recompile on changes by adding the md5 hash of all files. */ - source = path_source_replace_includes(source, path_get("kernel")); + source = path_source_replace_includes(source, path_get("source")); + source += "\n// " + util_md5_string(source) + "\n"; if(debug_src) { path_write_text(*debug_src, source); @@ -352,10 +357,10 @@ bool OpenCLDeviceBase::OpenCLProgram::compile_kernel(const string *debug_src) cl_int ciErr; program = clCreateProgramWithSource(device->cxContext, - 1, - &source_str, - &source_len, - &ciErr); + 1, + &source_str, + &source_len, + &ciErr); if(ciErr != CL_SUCCESS) { add_error(string("OpenCL program creation failed: ") + clewErrorString(ciErr)); @@ -438,7 +443,11 @@ void OpenCLDeviceBase::OpenCLProgram::load() if(!program) { add_log(string("OpenCL program ") + program_name + " not found in cache.", true); - string basename = "cycles_kernel_" + program_name + "_" + device_md5 + "_" + OpenCLCache::get_kernel_md5(); + /* need to create source to get md5 */ + string source = "#include \"kernel/kernels/opencl/" + kernel_file + "\"\n"; + source = path_source_replace_includes(source, path_get("source")); + + string basename = "cycles_kernel_" + program_name + "_" + device_md5 + "_" + util_md5_string(source); basename = path_cache_get(path_join("kernels", basename)); string clbin = basename + ".clbin"; @@ -544,6 +553,11 @@ bool OpenCLInfo::use_debug() return DebugFlags().opencl.debug; } +bool OpenCLInfo::use_single_program() +{ + return DebugFlags().opencl.single_program; +} + bool OpenCLInfo::kernel_use_advanced_shading(const string& platform) { /* keep this in sync with kernel_types.h! */ @@ -587,14 +601,46 @@ bool OpenCLInfo::device_supported(const string& platform_name, const cl_device_id device_id) { cl_device_type device_type; - clGetDeviceInfo(device_id, - CL_DEVICE_TYPE, - sizeof(cl_device_type), - &device_type, - NULL); + if(!get_device_type(device_id, &device_type)) { + return false; + } + string device_name; + if(!get_device_name(device_id, &device_name)) { + return false; + } + + int driver_major = 0; + int driver_minor = 0; + if(!get_driver_version(device_id, &driver_major, &driver_minor)) { + return false; + } + VLOG(3) << "OpenCL driver version " << driver_major << "." << driver_minor; + + /* It is possible tyo have Iris GPU on AMD/Apple OpenCL framework + * (aka, it will not be on Intel framework). This isn't supported + * and needs an explicit blacklist. + */ + if(strstr(device_name.c_str(), "Iris")) { + return false; + } if(platform_name == "AMD Accelerated Parallel Processing" && device_type == CL_DEVICE_TYPE_GPU) { + if(driver_major < 2236) { + VLOG(1) << "AMD driver version " << driver_major << "." << driver_minor << " not supported."; + return false; + } + const char *blacklist[] = { + /* GCN 1 */ + "Tahiti", "Pitcairn", "Capeverde", "Oland", + NULL + }; + for(int i = 0; blacklist[i] != NULL; i++) { + if(device_name == blacklist[i]) { + VLOG(1) << "AMD device " << device_name << " not supported"; + return false; + } + } return true; } if(platform_name == "Apple" && device_type == CL_DEVICE_TYPE_GPU) { @@ -661,7 +707,7 @@ bool OpenCLInfo::device_version_check(cl_device_id device, return true; } -string OpenCLInfo::get_hardware_id(string platform_name, cl_device_id device_id) +string OpenCLInfo::get_hardware_id(const string& platform_name, cl_device_id device_id) { if(platform_name == "AMD Accelerated Parallel Processing" || platform_name == "Apple") { /* Use cl_amd_device_topology extension. */ @@ -705,39 +751,30 @@ void OpenCLInfo::get_usable_devices(vector<OpenCLPlatformDevice> *usable_devices return; } + cl_int error; vector<cl_device_id> device_ids; - cl_uint num_devices = 0; vector<cl_platform_id> platform_ids; - cl_uint num_platforms = 0; - /* Get devices. */ - if(clGetPlatformIDs(0, NULL, &num_platforms) != CL_SUCCESS || - num_platforms == 0) - { - FIRST_VLOG(2) << "No OpenCL platforms were found."; + /* Get platforms. */ + if(!get_platforms(&platform_ids, &error)) { + FIRST_VLOG(2) << "Error fetching platforms:" + << string(clewErrorString(error)); first_time = false; return; } - platform_ids.resize(num_platforms); - if(clGetPlatformIDs(num_platforms, &platform_ids[0], NULL) != CL_SUCCESS) { - FIRST_VLOG(2) << "Failed to fetch platform IDs from the driver.."; + if(platform_ids.size() == 0) { + FIRST_VLOG(2) << "No OpenCL platforms were found."; first_time = false; return; } /* Devices are numbered consecutively across platforms. */ - for(int platform = 0; platform < num_platforms; platform++) { + for(int platform = 0; platform < platform_ids.size(); platform++) { cl_platform_id platform_id = platform_ids[platform]; - char pname[256]; - if(clGetPlatformInfo(platform_id, - CL_PLATFORM_NAME, - sizeof(pname), - &pname, - NULL) != CL_SUCCESS) - { + string platform_name; + if(!get_platform_name(platform_id, &platform_name)) { FIRST_VLOG(2) << "Failed to get platform name, ignoring."; continue; } - string platform_name = pname; FIRST_VLOG(2) << "Enumerating devices for platform " << platform_name << "."; if(!platform_version_check(platform_id)) { @@ -745,39 +782,28 @@ void OpenCLInfo::get_usable_devices(vector<OpenCLPlatformDevice> *usable_devices << " due to too old compiler version."; continue; } - num_devices = 0; - cl_int ciErr; - if((ciErr = clGetDeviceIDs(platform_id, - device_type, - 0, - NULL, - &num_devices)) != CL_SUCCESS || num_devices == 0) + if(!get_platform_devices(platform_id, + device_type, + &device_ids, + &error)) { FIRST_VLOG(2) << "Ignoring platform " << platform_name - << ", failed to fetch number of devices: " << string(clewErrorString(ciErr)); + << ", failed to fetch of devices: " + << string(clewErrorString(error)); continue; } - device_ids.resize(num_devices); - if(clGetDeviceIDs(platform_id, - device_type, - num_devices, - &device_ids[0], - NULL) != CL_SUCCESS) - { + if(device_ids.size() == 0) { FIRST_VLOG(2) << "Ignoring platform " << platform_name - << ", failed to fetch devices list."; + << ", it has no devices."; continue; } - for(int num = 0; num < num_devices; num++) { - cl_device_id device_id = device_ids[num]; - char device_name[1024] = "\0"; - if(clGetDeviceInfo(device_id, - CL_DEVICE_NAME, - sizeof(device_name), - &device_name, - NULL) != CL_SUCCESS) - { - FIRST_VLOG(2) << "Failed to fetch device name, ignoring."; + for(int num = 0; num < device_ids.size(); num++) { + const cl_device_id device_id = device_ids[num]; + string device_name; + if(!get_device_name(device_id, &device_name, &error)) { + FIRST_VLOG(2) << "Failed to fetch device name: " + << string(clewErrorString(error)) + << ", ignoring."; continue; } if(!device_version_check(device_id)) { @@ -789,24 +815,28 @@ void OpenCLInfo::get_usable_devices(vector<OpenCLPlatformDevice> *usable_devices device_supported(platform_name, device_id)) { cl_device_type device_type; - if(clGetDeviceInfo(device_id, - CL_DEVICE_TYPE, - sizeof(cl_device_type), - &device_type, - NULL) != CL_SUCCESS) - { + if(!get_device_type(device_id, &device_type, &error)) { FIRST_VLOG(2) << "Ignoring device " << device_name - << ", failed to fetch device type."; + << ", failed to fetch device type:" + << string(clewErrorString(error)); continue; } - FIRST_VLOG(2) << "Adding new device " << device_name << "."; + string readable_device_name = + get_readable_device_name(device_id); + if(readable_device_name != device_name) { + FIRST_VLOG(2) << "Using more readable device name: " + << readable_device_name; + } + FIRST_VLOG(2) << "Adding new device " + << readable_device_name << "."; string hardware_id = get_hardware_id(platform_name, device_id); - usable_devices->push_back(OpenCLPlatformDevice(platform_id, - platform_name, - device_id, - device_type, - device_name, - hardware_id)); + usable_devices->push_back(OpenCLPlatformDevice( + platform_id, + platform_name, + device_id, + device_type, + readable_device_name, + hardware_id)); } else { FIRST_VLOG(2) << "Ignoring device " << device_name @@ -817,6 +847,297 @@ void OpenCLInfo::get_usable_devices(vector<OpenCLPlatformDevice> *usable_devices first_time = false; } +bool OpenCLInfo::get_platforms(vector<cl_platform_id> *platform_ids, + cl_int *error) +{ + /* Reset from possible previous state. */ + platform_ids->resize(0); + cl_uint num_platforms; + if(!get_num_platforms(&num_platforms, error)) { + return false; + } + /* Get actual platforms. */ + cl_int err; + platform_ids->resize(num_platforms); + if((err = clGetPlatformIDs(num_platforms, + &platform_ids->at(0), + NULL)) != CL_SUCCESS) { + if(error != NULL) { + *error = err; + } + return false; + } + if(error != NULL) { + *error = CL_SUCCESS; + } + return true; +} + +vector<cl_platform_id> OpenCLInfo::get_platforms() +{ + vector<cl_platform_id> platform_ids; + get_platforms(&platform_ids); + return platform_ids; +} + +bool OpenCLInfo::get_num_platforms(cl_uint *num_platforms, cl_int *error) +{ + cl_int err; + if((err = clGetPlatformIDs(0, NULL, num_platforms)) != CL_SUCCESS) { + if(error != NULL) { + *error = err; + } + *num_platforms = 0; + return false; + } + if(error != NULL) { + *error = CL_SUCCESS; + } + return true; +} + +cl_uint OpenCLInfo::get_num_platforms() +{ + cl_uint num_platforms; + if(!get_num_platforms(&num_platforms)) { + return 0; + } + return num_platforms; +} + +bool OpenCLInfo::get_platform_name(cl_platform_id platform_id, + string *platform_name) +{ + char buffer[256]; + if(clGetPlatformInfo(platform_id, + CL_PLATFORM_NAME, + sizeof(buffer), + &buffer, + NULL) != CL_SUCCESS) + { + *platform_name = ""; + return false; + } + *platform_name = buffer; + return true; +} + +string OpenCLInfo::get_platform_name(cl_platform_id platform_id) +{ + string platform_name; + if(!get_platform_name(platform_id, &platform_name)) { + return ""; + } + return platform_name; +} + +bool OpenCLInfo::get_num_platform_devices(cl_platform_id platform_id, + cl_device_type device_type, + cl_uint *num_devices, + cl_int *error) +{ + cl_int err; + if((err = clGetDeviceIDs(platform_id, + device_type, + 0, + NULL, + num_devices)) != CL_SUCCESS) + { + if(error != NULL) { + *error = err; + } + *num_devices = 0; + return false; + } + if(error != NULL) { + *error = CL_SUCCESS; + } + return true; +} + +cl_uint OpenCLInfo::get_num_platform_devices(cl_platform_id platform_id, + cl_device_type device_type) +{ + cl_uint num_devices; + if(!get_num_platform_devices(platform_id, + device_type, + &num_devices)) + { + return 0; + } + return num_devices; +} + +bool OpenCLInfo::get_platform_devices(cl_platform_id platform_id, + cl_device_type device_type, + vector<cl_device_id> *device_ids, + cl_int* error) +{ + /* Reset from possible previous state. */ + device_ids->resize(0); + /* Get number of devices to pre-allocate memory. */ + cl_uint num_devices; + if(!get_num_platform_devices(platform_id, + device_type, + &num_devices, + error)) + { + return false; + } + /* Get actual device list. */ + device_ids->resize(num_devices); + cl_int err; + if((err = clGetDeviceIDs(platform_id, + device_type, + num_devices, + &device_ids->at(0), + NULL)) != CL_SUCCESS) + { + if(error != NULL) { + *error = err; + } + return false; + } + if(error != NULL) { + *error = CL_SUCCESS; + } + return true; +} + +vector<cl_device_id> OpenCLInfo::get_platform_devices(cl_platform_id platform_id, + cl_device_type device_type) +{ + vector<cl_device_id> devices; + get_platform_devices(platform_id, device_type, &devices); + return devices; +} + +bool OpenCLInfo::get_device_name(cl_device_id device_id, + string *device_name, + cl_int* error) +{ + char buffer[1024]; + cl_int err; + if((err = clGetDeviceInfo(device_id, + CL_DEVICE_NAME, + sizeof(buffer), + &buffer, + NULL)) != CL_SUCCESS) + { + if(error != NULL) { + *error = err; + } + *device_name = ""; + return false; + } + if(error != NULL) { + *error = CL_SUCCESS; + } + *device_name = buffer; + return true; +} + +string OpenCLInfo::get_device_name(cl_device_id device_id) +{ + string device_name; + if(!get_device_name(device_id, &device_name)) { + return ""; + } + return device_name; +} + +bool OpenCLInfo::get_device_type(cl_device_id device_id, + cl_device_type *device_type, + cl_int* error) +{ + cl_int err; + if((err = clGetDeviceInfo(device_id, + CL_DEVICE_TYPE, + sizeof(cl_device_type), + device_type, + NULL)) != CL_SUCCESS) + { + if(error != NULL) { + *error = err; + } + *device_type = 0; + return false; + } + if(error != NULL) { + *error = CL_SUCCESS; + } + return true; +} + +cl_device_type OpenCLInfo::get_device_type(cl_device_id device_id) +{ + cl_device_type device_type; + if(!get_device_type(device_id, &device_type)) { + return 0; + } + return device_type; +} + +string OpenCLInfo::get_readable_device_name(cl_device_id device_id) +{ + char board_name[1024]; + size_t length = 0; + if(clGetDeviceInfo(device_id, + CL_DEVICE_BOARD_NAME_AMD, + sizeof(board_name), + &board_name, + &length) == CL_SUCCESS) + { + if(length != 0 && board_name[0] != '\0') { + return board_name; + } + } + /* Fallback to standard device name API. */ + return get_device_name(device_id); +} + +bool OpenCLInfo::get_driver_version(cl_device_id device_id, + int *major, + int *minor, + cl_int* error) +{ + char buffer[1024]; + cl_int err; + if((err = clGetDeviceInfo(device_id, + CL_DRIVER_VERSION, + sizeof(buffer), + &buffer, + NULL)) != CL_SUCCESS) + { + if(error != NULL) { + *error = err; + } + return false; + } + if(error != NULL) { + *error = CL_SUCCESS; + } + if(sscanf(buffer, "%d.%d", major, minor) < 2) { + VLOG(1) << string_printf("OpenCL: failed to parse driver version string (%s).", buffer); + return false; + } + return true; +} + +int OpenCLInfo::mem_address_alignment(cl_device_id device_id) +{ + int base_align_bits; + if(clGetDeviceInfo(device_id, + CL_DEVICE_MEM_BASE_ADDR_ALIGN, + sizeof(int), + &base_align_bits, + NULL) == CL_SUCCESS) + { + return base_align_bits/8; + } + return 1; +} + CCL_NAMESPACE_END #endif |