Skip to content

Commit 8f1ce41

Browse files
committed
add type error for incorrect memory/exec space
1 parent c39b146 commit 8f1ce41

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

pykokkos/interface/parallel_dispatch.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from pykokkos.core.cppast import BuiltinType
99

1010
from .execution_policy import ExecutionPolicy, RangePolicy
11-
from .execution_space import ExecutionSpace
11+
from .execution_space import ExecutionSpace, is_host_execution_space
1212
from .views import ViewType, array
1313

1414
from .interface_util import generic_error, get_filename, get_lineno
@@ -228,8 +228,23 @@ def convert_arrays(kwargs: Dict[str, Any], workunit: Optional[Callable] = None)
228228
# Convert Python list to numpy array, then to View
229229
kwargs[k] = array(np.array(v, dtype=dtype))
230230
elif isinstance(v, np.ndarray):
231+
default_space: ExecutionSpace = km.get_default_space()
232+
_gpu_spaces = {ExecutionSpace.Cuda, ExecutionSpace.HIP}
233+
if default_space in _gpu_spaces:
234+
raise TypeError(
235+
f"Argument '{k}' is a numpy array, which cannot be accessed "
236+
f"from the {default_space.value} execution space. "
237+
f"Use a pk.View (e.g. pk.View([...], dtype)) or a CuPy array instead."
238+
)
231239
kwargs[k] = array(v)
232240
elif cp_available and isinstance(v, cp.ndarray):
241+
default_space = km.get_default_space()
242+
if is_host_execution_space(default_space):
243+
raise TypeError(
244+
f"Argument '{k}' is a CuPy array, which cannot be accessed "
245+
f"from the {default_space.value} (host) execution space. "
246+
f"Convert it to a numpy array or pk.View in host memory first."
247+
)
233248
kwargs[k] = array(v)
234249
elif torch_available and torch.is_tensor(v):
235250
kwargs[k] = array(v)

0 commit comments

Comments
 (0)