From 0ee77457c8294c0706f6fda3de0a1a64097493b4 Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Thu, 11 Aug 2022 23:54:39 -0500 Subject: [PATCH 1/2] use importlib to get the pycuda location pkg_resources is returning incorrect location for pip==22.2 --- pycuda/compiler.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pycuda/compiler.py b/pycuda/compiler.py index b84a0716..369b6375 100644 --- a/pycuda/compiler.py +++ b/pycuda/compiler.py @@ -206,9 +206,12 @@ def _get_per_user_string(): def _find_pycuda_include_path(): - from pkg_resources import Requirement, resource_filename + import importlib.util + import os - return resource_filename(Requirement.parse("pycuda"), "pycuda/cuda") + return os.path.abspath( + os.path.join(importlib.util.find_spec("pycuda").origin, + os.path.join(os.path.pardir, "cuda"))) DEFAULT_NVCC_FLAGS = [ -- GitLab From c26badf712edd4697bca7816040c3d9fd7d582a7 Mon Sep 17 00:00:00 2001 From: Mit Kotak Date: Tue, 16 Aug 2022 13:13:27 -0500 Subject: [PATCH 2/2] added bool + pure_scalars to min | max --- pycuda/compiler.py | 7 +++++-- pycuda/elementwise.py | 2 +- pycuda/gpuarray.py | 12 +++++++++++- test/test_gpuarray.py | 9 +++++++++ 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/pycuda/compiler.py b/pycuda/compiler.py index b84a0716..369b6375 100644 --- a/pycuda/compiler.py +++ b/pycuda/compiler.py @@ -206,9 +206,12 @@ def _get_per_user_string(): def _find_pycuda_include_path(): - from pkg_resources import Requirement, resource_filename + import importlib.util + import os - return resource_filename(Requirement.parse("pycuda"), "pycuda/cuda") + return os.path.abspath( + os.path.join(importlib.util.find_spec("pycuda").origin, + os.path.join(os.path.pardir, "cuda"))) DEFAULT_NVCC_FLAGS = [ diff --git a/pycuda/elementwise.py b/pycuda/elementwise.py index 7d633011..83fd779a 100644 --- a/pycuda/elementwise.py +++ b/pycuda/elementwise.py @@ -565,7 +565,7 @@ def get_binary_func_scalar_kernel(func, dtype_x, dtype_y, dtype_z): def get_binary_minmax_kernel(func, dtype_x, dtype_y, dtype_z, use_scalar): - if np.float64 not in [dtype_x, dtype_y]: + if (np.float64 not in [dtype_x, dtype_y]) and (bool not in [dtype_x, dtype_y]): func = func + "f" if any(dt.kind == "f" for dt in [dtype_x, dtype_y, dtype_z]): diff --git a/pycuda/gpuarray.py b/pycuda/gpuarray.py index 100d21cc..893a802e 100644 --- a/pycuda/gpuarray.py +++ b/pycuda/gpuarray.py @@ -2019,7 +2019,17 @@ def where(criterion, then_, else_, out=None, stream=None): def _make_binary_minmax_func(which): def f(a, b, out=None, stream=None): - if isinstance(a, GPUArray) and isinstance(b, GPUArray): + allocator = ( + getattr(a, "allocator", None) + or getattr(b, "allocator", None) + or drv.mem_alloc) + + if np.isscalar(a) and np.isscalar(b): + if out is None: + out = GPUArray(shape=(), dtype=np.bool_, allocator=allocator) + import pycuda.gpuarray as gpuarray + out[...] = gpuarray.to_gpu(np.array(getattr(np, "f"+which)(a, b))) + elif isinstance(a, GPUArray) and isinstance(b, GPUArray): if out is None: out = empty_like(a) func = elementwise.get_binary_minmax_kernel( diff --git a/test/test_gpuarray.py b/test/test_gpuarray.py index 7091fee9..62791e7d 100644 --- a/test/test_gpuarray.py +++ b/test/test_gpuarray.py @@ -11,6 +11,7 @@ import pycuda.gpuarray as gpuarray import pycuda.driver as drv from pycuda.compiler import SourceModule import pytest +import itertools @pytest.fixture(autouse=True) @@ -809,6 +810,14 @@ class TestGPUArray: assert la.norm(max_a_b_gpu.get() - np.maximum(a, b)) == 0 assert la.norm(min_a_b_gpu.get() - np.minimum(a, b)) == 0 + @pytest.mark.parametrize("func", ["minimum", "maximum"]) + def test_min_max_elemwise_on_scalars(self, func): + + for a, b in itertools.product([False, True], [False, True]): + result_ref = getattr(np, func)(a, b) + result = getattr(gpuarray, func)(a, b) + np.testing.assert_array_equal(result.get(), result_ref) + def test_take_put(self): for n in [5, 17, 333]: one_field_size = 8 -- GitLab