From de410d49867585b15b4a31f3f37c3e2426c98b3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20C=C3=B4t=C3=A9?= Date: Wed, 31 Jul 2024 13:41:33 -0400 Subject: [PATCH 1/4] Update .gitignore Removed macOS specific files and allowed for mulitple venvs with different endings (venv-3.x) --- .gitignore | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index b2c18337..837d4992 100644 --- a/.gitignore +++ b/.gitignore @@ -100,8 +100,8 @@ celerybeat-schedule # Environments .env .venv -env/ -venv/ +env*/ +venv*/ ENV/ env.bak/ venv.bak/ From 40136ed9b93132d2e7eb4c0ed945ff7a622f7e5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20C=C3=B4t=C3=A9?= Date: Wed, 31 Jul 2024 14:30:17 -0400 Subject: [PATCH 2/4] Added forceCalculationOnCPU Even when available, bugs in the OpenCL code can render the whole module useless if we cannot bypass OpenCL. The user can disable it: 1. with the environement variable PYTISSUE_FORCE_CPU = 1 2. with the function `forceCalculationOnCPU` It is not perfect, but at least it works. --- pytissueoptics/rayscattering/__init__.py | 2 +- pytissueoptics/rayscattering/opencl/__init__.py | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/pytissueoptics/rayscattering/__init__.py b/pytissueoptics/rayscattering/__init__.py index fc012536..c6473efc 100644 --- a/pytissueoptics/rayscattering/__init__.py +++ b/pytissueoptics/rayscattering/__init__.py @@ -15,7 +15,7 @@ ) from .energyLogging import EnergyLogger from .materials import ScatteringMaterial -from .opencl import CONFIG, hardwareAccelerationIsAvailable +from .opencl import CONFIG, forceCalculationOnCPU, hardwareAccelerationIsAvailable from .photon import Photon from .scatteringScene import ScatteringScene from .source import DirectionalSource, DivergentSource, IsotropicPointSource, PencilPointSource diff --git a/pytissueoptics/rayscattering/opencl/__init__.py b/pytissueoptics/rayscattering/opencl/__init__.py index 703e0ee8..d4e99a64 100644 --- a/pytissueoptics/rayscattering/opencl/__init__.py +++ b/pytissueoptics/rayscattering/opencl/__init__.py @@ -1,7 +1,9 @@ from pytissueoptics.rayscattering.opencl.config.CLConfig import OPENCL_AVAILABLE, WEIGHT_THRESHOLD, CLConfig, warnings from pytissueoptics.rayscattering.opencl.config.IPPTable import IPPTable +import os OPENCL_OK = True +PYTISSUE_FORCE_CPU = os.environ.get('PYTISSUE_FORCE_CPU', '0') if OPENCL_AVAILABLE: try: @@ -13,9 +15,16 @@ else: CONFIG = None +def forceCalculationOnCPU(): + os.environ["PYTISSUE_FORCE_CPU"] = "1" + print("You can define PYTISSUE_FORCE_CPU=1 in your profile to avoid this call.") def validateOpenCL() -> bool: notAvailableMessage = "Error: Hardware acceleration not available. Falling back to CPU. " + + if os.environ.get("PYTISSUE_FORCE_CPU", '0') != '0': + warnings.warn("User requested not using OpenCL with environment variable 'PYTISSUE_FORCE_CPU'=1.") + return False if not OPENCL_AVAILABLE: warnings.warn(notAvailableMessage + "Please install pyopencl.") return False @@ -28,7 +37,7 @@ def validateOpenCL() -> bool: def hardwareAccelerationIsAvailable() -> bool: - return OPENCL_AVAILABLE and OPENCL_OK + return OPENCL_AVAILABLE and OPENCL_OK and not PYTISSUE_FORCE_CPU __all__ = ["IPPTable", "WEIGHT_THRESHOLD"] From 22c11ca062e563a671b79da8d3212f6f669f212f Mon Sep 17 00:00:00 2001 From: JLBegin Date: Mon, 21 Apr 2025 21:01:29 -0400 Subject: [PATCH 3/4] rename flag to DISABLE_OPENCL --- pytissueoptics/rayscattering/__init__.py | 3 ++- .../rayscattering/opencl/__init__.py | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/pytissueoptics/rayscattering/__init__.py b/pytissueoptics/rayscattering/__init__.py index c6473efc..8ab8c01e 100644 --- a/pytissueoptics/rayscattering/__init__.py +++ b/pytissueoptics/rayscattering/__init__.py @@ -15,7 +15,7 @@ ) from .energyLogging import EnergyLogger from .materials import ScatteringMaterial -from .opencl import CONFIG, forceCalculationOnCPU, hardwareAccelerationIsAvailable +from .opencl import CONFIG, disableOpenCL, hardwareAccelerationIsAvailable from .photon import Photon from .scatteringScene import ScatteringScene from .source import DirectionalSource, DivergentSource, IsotropicPointSource, PencilPointSource @@ -49,6 +49,7 @@ "View2DSliceZ", "samples", "Stats", + "disableOpenCL", "hardwareAccelerationIsAvailable", "CONFIG", ] diff --git a/pytissueoptics/rayscattering/opencl/__init__.py b/pytissueoptics/rayscattering/opencl/__init__.py index d4e99a64..e923c2fa 100644 --- a/pytissueoptics/rayscattering/opencl/__init__.py +++ b/pytissueoptics/rayscattering/opencl/__init__.py @@ -3,7 +3,7 @@ import os OPENCL_OK = True -PYTISSUE_FORCE_CPU = os.environ.get('PYTISSUE_FORCE_CPU', '0') +OPENCL_DISABLED = os.environ.get("PTO_DISABLE_OPENCL", "0") == "1" if OPENCL_AVAILABLE: try: @@ -15,15 +15,17 @@ else: CONFIG = None -def forceCalculationOnCPU(): - os.environ["PYTISSUE_FORCE_CPU"] = "1" - print("You can define PYTISSUE_FORCE_CPU=1 in your profile to avoid this call.") + +def disableOpenCL(): + os.environ["PTO_DISABLE_OPENCL"] = "1" + print("You can define PTO_DISABLE_OPENCL=1 in your profile to avoid this call.") + def validateOpenCL() -> bool: notAvailableMessage = "Error: Hardware acceleration not available. Falling back to CPU. " - - if os.environ.get("PYTISSUE_FORCE_CPU", '0') != '0': - warnings.warn("User requested not using OpenCL with environment variable 'PYTISSUE_FORCE_CPU'=1.") + + if os.environ.get("PTO_DISABLE_OPENCL", "0") == "1": + warnings.warn("User requested not to use OpenCL with environment variable 'PTO_DISABLE_OPENCL'=1.") return False if not OPENCL_AVAILABLE: warnings.warn(notAvailableMessage + "Please install pyopencl.") @@ -37,7 +39,7 @@ def validateOpenCL() -> bool: def hardwareAccelerationIsAvailable() -> bool: - return OPENCL_AVAILABLE and OPENCL_OK and not PYTISSUE_FORCE_CPU + return OPENCL_AVAILABLE and OPENCL_OK and not OPENCL_DISABLED __all__ = ["IPPTable", "WEIGHT_THRESHOLD"] From a2b822cf86e0e9b9fe43b91487c3d61c705a0037 Mon Sep 17 00:00:00 2001 From: JLBegin Date: Wed, 23 Apr 2025 20:14:52 -0400 Subject: [PATCH 4/4] fix hardwareAccelerationIsAvailable --- pytissueoptics/rayscattering/opencl/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytissueoptics/rayscattering/opencl/__init__.py b/pytissueoptics/rayscattering/opencl/__init__.py index e923c2fa..71a92eb3 100644 --- a/pytissueoptics/rayscattering/opencl/__init__.py +++ b/pytissueoptics/rayscattering/opencl/__init__.py @@ -3,7 +3,6 @@ import os OPENCL_OK = True -OPENCL_DISABLED = os.environ.get("PTO_DISABLE_OPENCL", "0") == "1" if OPENCL_AVAILABLE: try: @@ -39,6 +38,7 @@ def validateOpenCL() -> bool: def hardwareAccelerationIsAvailable() -> bool: + OPENCL_DISABLED = os.environ.get("PTO_DISABLE_OPENCL", "0") == "1" return OPENCL_AVAILABLE and OPENCL_OK and not OPENCL_DISABLED