Skip to content

Commit 40136ed

Browse files
dccoteJLBegin
authored andcommitted
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.
1 parent de410d4 commit 40136ed

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

pytissueoptics/rayscattering/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
)
1616
from .energyLogging import EnergyLogger
1717
from .materials import ScatteringMaterial
18-
from .opencl import CONFIG, hardwareAccelerationIsAvailable
18+
from .opencl import CONFIG, forceCalculationOnCPU, hardwareAccelerationIsAvailable
1919
from .photon import Photon
2020
from .scatteringScene import ScatteringScene
2121
from .source import DirectionalSource, DivergentSource, IsotropicPointSource, PencilPointSource

pytissueoptics/rayscattering/opencl/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from pytissueoptics.rayscattering.opencl.config.CLConfig import OPENCL_AVAILABLE, WEIGHT_THRESHOLD, CLConfig, warnings
22
from pytissueoptics.rayscattering.opencl.config.IPPTable import IPPTable
3+
import os
34

45
OPENCL_OK = True
6+
PYTISSUE_FORCE_CPU = os.environ.get('PYTISSUE_FORCE_CPU', '0')
57

68
if OPENCL_AVAILABLE:
79
try:
@@ -13,9 +15,16 @@
1315
else:
1416
CONFIG = None
1517

18+
def forceCalculationOnCPU():
19+
os.environ["PYTISSUE_FORCE_CPU"] = "1"
20+
print("You can define PYTISSUE_FORCE_CPU=1 in your profile to avoid this call.")
1621

1722
def validateOpenCL() -> bool:
1823
notAvailableMessage = "Error: Hardware acceleration not available. Falling back to CPU. "
24+
25+
if os.environ.get("PYTISSUE_FORCE_CPU", '0') != '0':
26+
warnings.warn("User requested not using OpenCL with environment variable 'PYTISSUE_FORCE_CPU'=1.")
27+
return False
1928
if not OPENCL_AVAILABLE:
2029
warnings.warn(notAvailableMessage + "Please install pyopencl.")
2130
return False
@@ -28,7 +37,7 @@ def validateOpenCL() -> bool:
2837

2938

3039
def hardwareAccelerationIsAvailable() -> bool:
31-
return OPENCL_AVAILABLE and OPENCL_OK
40+
return OPENCL_AVAILABLE and OPENCL_OK and not PYTISSUE_FORCE_CPU
3241

3342

3443
__all__ = ["IPPTable", "WEIGHT_THRESHOLD"]

0 commit comments

Comments
 (0)