diff options
author | Brecht Van Lommel <brecht@blender.org> | 2020-03-25 15:11:09 +0300 |
---|---|---|
committer | Brecht Van Lommel <brecht@blender.org> | 2020-03-25 18:39:37 +0300 |
commit | f48d15a86189756ae7dd9b7ba45fb3af24e0846b (patch) | |
tree | 0d64974359a8118960578dee64a31b3d20dc4224 /intern/cycles/device | |
parent | e5f7b31dd427fa72ae6b7e3e085c6b92621084a7 (diff) |
Cycles: limit number of processes compiling OpenCL kernel based on memory
The numbers here can probably be tweaked to be better, but it's hard to
predict and this should at least avoid excessive memory swapping.
Fixes T57064.
Diffstat (limited to 'intern/cycles/device')
-rw-r--r-- | intern/cycles/device/opencl/device_opencl_impl.cpp | 10 | ||||
-rw-r--r-- | intern/cycles/device/opencl/opencl_util.cpp | 35 |
2 files changed, 37 insertions, 8 deletions
diff --git a/intern/cycles/device/opencl/device_opencl_impl.cpp b/intern/cycles/device/opencl/device_opencl_impl.cpp index b7a2be79804..2766f85d17c 100644 --- a/intern/cycles/device/opencl/device_opencl_impl.cpp +++ b/intern/cycles/device/opencl/device_opencl_impl.cpp @@ -257,16 +257,16 @@ void OpenCLDevice::OpenCLSplitPrograms::load_kernels( /* Ordered with most complex kernels first, to reduce overall compile time. */ ADD_SPLIT_KERNEL_PROGRAM(subsurface_scatter); + ADD_SPLIT_KERNEL_PROGRAM(direct_lighting); + ADD_SPLIT_KERNEL_PROGRAM(indirect_background); if (requested_features.use_volume || is_preview) { ADD_SPLIT_KERNEL_PROGRAM(do_volume); } + ADD_SPLIT_KERNEL_PROGRAM(shader_eval); + ADD_SPLIT_KERNEL_PROGRAM(lamp_emission); + ADD_SPLIT_KERNEL_PROGRAM(holdout_emission_blurring_pathtermination_ao); ADD_SPLIT_KERNEL_PROGRAM(shadow_blocked_dl); ADD_SPLIT_KERNEL_PROGRAM(shadow_blocked_ao); - ADD_SPLIT_KERNEL_PROGRAM(holdout_emission_blurring_pathtermination_ao); - ADD_SPLIT_KERNEL_PROGRAM(lamp_emission); - ADD_SPLIT_KERNEL_PROGRAM(direct_lighting); - ADD_SPLIT_KERNEL_PROGRAM(indirect_background); - ADD_SPLIT_KERNEL_PROGRAM(shader_eval); /* Quick kernels bundled in a single program to reduce overhead of starting * Blender processes. */ diff --git a/intern/cycles/device/opencl/opencl_util.cpp b/intern/cycles/device/opencl/opencl_util.cpp index 978c75d2e2c..b8b07cf2947 100644 --- a/intern/cycles/device/opencl/opencl_util.cpp +++ b/intern/cycles/device/opencl/opencl_util.cpp @@ -23,6 +23,7 @@ # include "util/util_logging.h" # include "util/util_md5.h" # include "util/util_path.h" +# include "util/util_semaphore.h" # include "util/util_system.h" # include "util/util_time.h" @@ -390,8 +391,27 @@ static void escape_python_string(string &str) string_replace(str, "'", "\'"); } +static int opencl_compile_process_limit() +{ + /* Limit number of concurrent processes compiling, with a heuristic based + * on total physical RAM and estimate of memory usage needed when compiling + * with all Cycles features enabled. + * + * This is somewhat arbitrary as we don't know the actual available RAM or + * how much the kernel compilation will needed depending on the features, but + * better than not limiting at all. */ + static const int64_t GB = 1024LL * 1024LL * 1024LL; + static const int64_t process_memory = 2 * GB; + static const int64_t base_memory = 2 * GB; + static const int64_t system_memory = system_physical_ram(); + static const int64_t process_limit = (system_memory - base_memory) / process_memory; + + return max((int)process_limit, 1); +} + bool OpenCLDevice::OpenCLProgram::compile_separate(const string &clbin) { + /* Construct arguments. */ vector<string> args; args.push_back("--background"); args.push_back("--factory-startup"); @@ -419,14 +439,23 @@ bool OpenCLDevice::OpenCLProgram::compile_separate(const string &clbin) kernel_file_escaped.c_str(), clbin_escaped.c_str())); - double starttime = time_dt(); + /* Limit number of concurrent processes compiling. */ + static thread_counting_semaphore semaphore(opencl_compile_process_limit()); + semaphore.acquire(); + + /* Compile. */ + const double starttime = time_dt(); add_log(string("Cycles: compiling OpenCL program ") + program_name + "...", false); add_log(string("Build flags: ") + kernel_build_options, true); - if (!system_call_self(args) || !path_exists(clbin)) { + const bool success = system_call_self(args); + const double elapsed = time_dt() - starttime; + + semaphore.release(); + + if (!success || !path_exists(clbin)) { return false; } - double elapsed = time_dt() - starttime; add_log( string_printf("Kernel compilation of %s finished in %.2lfs.", program_name.c_str(), elapsed), false); |