From c40fd26c20e5627285f8abb10f1435bea401fa70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20Br=C3=BCckner?= Date: Mon, 27 Sep 2021 23:13:30 +0200 Subject: [PATCH] Fix CPU count parameter for cmft --- blender/arm/utils.py | 38 ++++++++++++++++++++++++++++++++++++- blender/arm/write_probes.py | 5 ++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/blender/arm/utils.py b/blender/arm/utils.py index 20c9c2e8..6980d710 100755 --- a/blender/arm/utils.py +++ b/blender/arm/utils.py @@ -130,7 +130,7 @@ def get_fp(): def get_fp_build(): return os.path.join(get_fp(), build_dir()) -def get_os(): +def get_os() -> str: s = platform.system() if s == 'Windows': return 'win' @@ -1111,6 +1111,42 @@ def get_list_installed_vs_path() -> []: err = 'File "'+ path_file +'" not found.' return items, err + +def cpu_count(*, physical_only=False) -> Optional[int]: + """Returns the number of logical (default) or physical CPUs. + The result can be `None` if `os.cpu_count()` was not able to get the + correct count of logical CPUs. + """ + if not physical_only: + return os.cpu_count() + + _os = get_os() + try: + if _os == 'win': + result = subprocess.check_output(['wmic', 'cpu', 'get', 'NumberOfCores']) + result = result.decode('utf-8').splitlines() + result = int(result[2]) + if result > 0: + return result + + elif _os == 'linux': + result = subprocess.check_output("grep -P '^core id' /proc/cpuinfo | sort -u | wc -l", shell=True) + result = result.decode('utf-8').splitlines() + result = int(result[0]) + if result > 0: + return result + + # macOS + else: + return int(subprocess.check_output(['sysctl', '-n', 'hw.physicalcpu'])) + except subprocess.CalledProcessError: + pass + + # Last resort even though it can be wrong + log.warn("Could not retrieve count of physical CPUs, using logical CPU count instead.") + return os.cpu_count() + + def register(local_sdk=False): global use_local_sdk use_local_sdk = local_sdk diff --git a/blender/arm/write_probes.py b/blender/arm/write_probes.py index a0d1f67b..80c30a80 100644 --- a/blender/arm/write_probes.py +++ b/blender/arm/write_probes.py @@ -187,7 +187,10 @@ def write_probes(image_filepath: str, disable_hdr: bool, cached_num_mips: int, a wrd = bpy.data.worlds['Arm'] use_opencl = 'true' - cpu_count = multiprocessing.cpu_count() + + # cmft doesn't work correctly when passing the number of logical + # CPUs if there are more logical than physical CPUs on a machine + cpu_count = arm.utils.cpu_count(physical_only=True) if arm.utils.get_os() == 'win': cmd = [