Skip to content

Commit

Permalink
allow device none
Browse files Browse the repository at this point in the history
  • Loading branch information
StRigaud committed Nov 21, 2024
1 parent 41c61f7 commit daea8e4
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions pyclesperanto/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ def select_device(device_id: Union[str, int] = "", device_type: str = "all") ->
-------
device : Device
"""
print("\t\tselecting device ...")
if isinstance(device_id, str):
device = BackendManager.get_backend().getDeviceFromName(device_id, device_type)
elif isinstance(device_id, int):
Expand All @@ -52,10 +51,17 @@ def select_device(device_id: Union[str, int] = "", device_type: str = "all") ->
raise ValueError(
f"'{device_id}' is not a supported device_id. Please use either a string or an integer."
)

if device is None:
warnings.warn(
"No device found in the system. Please check your system installation.",
RuntimeWarning,
)

if _current_device._instance and device == _current_device._instance:
return _current_device._instance

_current_device._instance = device
print("\t\tdone!")
return device


Expand Down Expand Up @@ -94,14 +100,12 @@ def list_available_backends() -> list:
-------
name list : list[str]
"""
print("\tlist available backends ...")
back_list = list(BackendManager.get_backends_list())
if not back_list:
warnings.warn(
"No backend available. Please install either OpenCL or CUDA on your system.",
RuntimeWarning,
)
print("\tdone!")
return back_list


Expand All @@ -116,7 +120,6 @@ def select_backend(backend: str = "opencl") -> str:
type : str, default = "opencl"
determine the backend to use between opencl and cuda
"""
print("\tset backend ...")
# enforce lowercase for backend_type
backend = backend.lower()
# is backend_type is different than "cuda" or "opencl", raise an error
Expand All @@ -125,11 +128,8 @@ def select_backend(backend: str = "opencl") -> str:
f"'{backend}' is not a supported Backend. Please use either 'opencl' or 'cuda'."
)
BackendManager.set_backend(backend=backend)
print("\tdone!")
# reset current device to default one
print("\tset default device ...")
select_device()
print("\tdone!")
return f"{BackendManager.get_backend()} selected."


Expand Down Expand Up @@ -157,13 +157,11 @@ def default_initialisation():
"""Set default backend and device"""

try:
print("Initialising pyclesperanto...")
backends = list_available_backends()
if backends:
_ = select_backend(backends[-1])
else:
raise RuntimeError("No backend available.")
print("done!")
except Exception as e:
warnings.warn(
f"Error while initialising pyclesperanto: {e}\n\n"
Expand Down

0 comments on commit daea8e4

Please sign in to comment.