Skip to content

Commit 478754e

Browse files
committed
add CI env vars for CL config
1 parent 0b480e6 commit 478754e

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

pytissueoptics/rayscattering/opencl/config/CLConfig.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,26 @@ def _load(self):
174174
with open(OPENCL_CONFIG_PATH, "r") as f:
175175
self._config = json.load(f)
176176

177+
if os.getenv("PTO_DEVICE_INDEX") is not None:
178+
try:
179+
self.DEVICE_INDEX = int(os.getenv("PTO_DEVICE_INDEX"))
180+
except ValueError:
181+
raise ValueError(f"Invalid value for PTO_DEVICE_INDEX: {os.getenv('PTO_DEVICE_INDEX')}. Must be an "
182+
f"integer.")
183+
if os.getenv("PTO_N_WORK_UNITS") is not None:
184+
try:
185+
self.N_WORK_UNITS = int(os.getenv("PTO_N_WORK_UNITS"))
186+
except ValueError:
187+
raise ValueError(f"Invalid value for PTO_N_WORK_UNITS: {os.getenv('PTO_N_WORK_UNITS')}. Must be an "
188+
f"integer.")
189+
190+
if os.getenv("PTO_MAX_MEMORY_MB") is not None:
191+
try:
192+
self.MAX_MEMORY_MB = int(os.getenv("PTO_MAX_MEMORY_MB"))
193+
except ValueError:
194+
raise ValueError(f"Invalid value for PTO_MAX_MEMORY_MB: {os.getenv('PTO_MAX_MEMORY_MB')}. Must be an "
195+
f"integer.")
196+
177197
def _assertExists(self):
178198
if not os.path.exists(OPENCL_CONFIG_PATH):
179199
warnings.warn("No OpenCL config file found. Creating a new one.")
@@ -183,6 +203,8 @@ def _assertExists(self):
183203
def save(self):
184204
if not self.AUTO_SAVE:
185205
return
206+
207+
os.makedirs(os.path.dirname(OPENCL_CONFIG_PATH), exist_ok=True)
186208
with open(OPENCL_CONFIG_PATH, "w") as f:
187209
json.dump(self._config, f, indent=4)
188210

pytissueoptics/rayscattering/tests/opencl/config/testCLConfig.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import tempfile
33
import unittest
4+
from unittest.mock import patch
45

56
from pytissueoptics.rayscattering.opencl.config import CLConfig as clc
67

@@ -23,11 +24,11 @@ def testGivenNoConfigFile_shouldWarnAndCreateANewOne(self):
2324
self.assertTrue(os.path.exists(clc.OPENCL_CONFIG_PATH))
2425

2526
@tempConfigPath
26-
def testGivenNewConfigFile_shouldHaveDefaultValues(self):
27+
def testGivenNewConfigFile_shouldHaveDefaultsFromEnvironment(self):
2728
with self.assertWarns(UserWarning):
2829
config = clc.CLConfig()
29-
self.assertEqual(None, config.N_WORK_UNITS)
30-
self.assertEqual(None, config.MAX_MEMORY_MB)
30+
self.assertEqual(str(os.getenv("PTO_N_WORK_UNITS")), str(config.N_WORK_UNITS))
31+
self.assertEqual(str(os.getenv("PTO_MAX_MEMORY_MB")), str(config.MAX_MEMORY_MB))
3132
self.assertEqual(1000, config.IPP_TEST_N_PHOTONS)
3233
self.assertEqual(0.20, config.BATCH_LOAD_FACTOR)
3334

@@ -44,7 +45,8 @@ def testGivenMaxMemoryNotSet_whenValidate_shouldWarnAndSetMaxMemory(self):
4445
with open(clc.OPENCL_CONFIG_PATH, "w") as f:
4546
f.write('{"DEVICE_INDEX": 0, "N_WORK_UNITS": 100, "MAX_MEMORY_MB": null, '
4647
'"IPP_TEST_N_PHOTONS": 1000, "BATCH_LOAD_FACTOR": 0.2}')
47-
config = clc.CLConfig()
48+
with patch("os.getenv", return_value=None):
49+
config = clc.CLConfig()
4850
with self.assertWarns(UserWarning):
4951
config.validate()
5052
self.assertIsNotNone(config.MAX_MEMORY_MB)
@@ -66,8 +68,10 @@ def testGivenFileWithAParameterBelowOrEqualToZero_whenValidate_shouldResetDefaul
6668
with open(clc.OPENCL_CONFIG_PATH, "w") as f:
6769
f.write('{"DEVICE_INDEX": 0, "N_WORK_UNITS": 100, "MAX_MEMORY_MB": 0, '
6870
'"IPP_TEST_N_PHOTONS": 1000, "BATCH_LOAD_FACTOR": 0.2}')
69-
config = clc.CLConfig()
70-
with self.assertRaises(ValueError):
71-
config.validate()
72-
config = clc.CLConfig()
73-
self.assertIsNone(config.MAX_MEMORY_MB)
71+
72+
with patch("os.getenv", return_value=None):
73+
config = clc.CLConfig()
74+
with self.assertRaises(ValueError):
75+
config.validate()
76+
config = clc.CLConfig()
77+
self.assertIsNone(config.MAX_MEMORY_MB)

tox.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ envlist = py39, py310, py311, py312
44
[testenv]
55
setenv =
66
QT_QPA_PLATFORM = offscreen
7+
PTO_DEVICE_INDEX = 0
8+
PTO_N_WORK_UNITS = 128
9+
PTO_MAX_MEMORY_MB = 1024
710
deps =
811
git+https://github.com/enthought/mayavi.git@main
912
commands =

0 commit comments

Comments
 (0)