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/ diff --git a/pytissueoptics/rayscattering/__init__.py b/pytissueoptics/rayscattering/__init__.py index fc012536..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, 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 703e0ee8..71a92eb3 100644 --- a/pytissueoptics/rayscattering/opencl/__init__.py +++ b/pytissueoptics/rayscattering/opencl/__init__.py @@ -1,5 +1,6 @@ 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 @@ -14,8 +15,17 @@ CONFIG = None +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("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.") return False @@ -28,7 +38,8 @@ def validateOpenCL() -> bool: def hardwareAccelerationIsAvailable() -> bool: - return OPENCL_AVAILABLE and OPENCL_OK + OPENCL_DISABLED = os.environ.get("PTO_DISABLE_OPENCL", "0") == "1" + return OPENCL_AVAILABLE and OPENCL_OK and not OPENCL_DISABLED __all__ = ["IPPTable", "WEIGHT_THRESHOLD"]