From 214fc5db0b74c39935f22981c745c50ec410419e Mon Sep 17 00:00:00 2001 From: Isuru Fernando Date: Mon, 7 Oct 2019 19:27:38 -0500 Subject: [PATCH 01/11] Fix get_source_args for DirectionalTargetDerivative --- sumpy/kernel.py | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/sumpy/kernel.py b/sumpy/kernel.py index aebaa9fe..832ea849 100644 --- a/sumpy/kernel.py +++ b/sumpy/kernel.py @@ -853,17 +853,6 @@ class DirectionalDerivative(DerivativeBase): self.inner_kernel, self.dir_vec_name) - def get_source_args(self): - return [ - KernelArgument( - loopy_arg=lp.GlobalArg( - self.dir_vec_name, - None, - shape=(self.dim, "nsources"), - dim_tags="sep,C"), - ) - ] + self.inner_kernel.get_source_args() - class DirectionalTargetDerivative(DirectionalDerivative): directional_kind = "tgt" @@ -892,6 +881,17 @@ class DirectionalTargetDerivative(DirectionalDerivative): return sum(dir_vec[axis]*expr.diff(bvec[axis]) for axis in range(dim)) + def get_source_args(self): + return [ + KernelArgument( + loopy_arg=lp.GlobalArg( + self.dir_vec_name, + None, + shape=(self.dim, "ntargets"), + dim_tags="sep,C"), + ) + ] + self.inner_kernel.get_source_args() + mapper_method = "map_directional_target_derivative" @@ -923,6 +923,17 @@ class DirectionalSourceDerivative(DirectionalDerivative): return sum(-dir_vec[axis]*expr.diff(avec[axis]) for axis in range(dimensions)) + def get_source_args(self): + return [ + KernelArgument( + loopy_arg=lp.GlobalArg( + self.dir_vec_name, + None, + shape=(self.dim, "nsources"), + dim_tags="sep,C"), + ) + ] + self.inner_kernel.get_source_args() + mapper_method = "map_directional_source_derivative" # }}} -- GitLab From 9ec3aabcb3c15c8d4a3aa49157c8bb6c9ef4c1c5 Mon Sep 17 00:00:00 2001 From: Isuru Fernando Date: Mon, 7 Oct 2019 19:29:30 -0500 Subject: [PATCH 02/11] Check for same argument name when getting kernel arguments --- sumpy/tools.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/sumpy/tools.py b/sumpy/tools.py index 540208f3..78dd954b 100644 --- a/sumpy/tools.py +++ b/sumpy/tools.py @@ -209,9 +209,10 @@ def gather_arguments(kernel_likes): result = {} for knl in kernel_likes: for arg in knl.get_args(): - result[arg.name] = arg - # FIXME: possibly check that arguments match before overwriting - + # Check for strict equality until there's a usecase + if result.setdefault(arg.name, arg) != arg: + raise ValueError("Merging two different kernel arguments with" + "the same name {}".format(arg.name)) return sorted(six.itervalues(result), key=lambda arg: arg.name) @@ -219,8 +220,10 @@ def gather_source_arguments(kernel_likes): result = {} for knl in kernel_likes: for arg in knl.get_args() + knl.get_source_args(): - result[arg.name] = arg - # FIXME: possibly check that arguments match before overwriting + # Check for strict equality until there's a usecase + if result.setdefault(arg.name, arg) != arg: + raise ValueError("Merging two different kernel arguments with" + "the same name {}".format(arg.name)) return sorted(six.itervalues(result), key=lambda arg: arg.name) -- GitLab From 38637705cc3f679ae0967326d03ebdd3a5a5737d Mon Sep 17 00:00:00 2001 From: Isuru Fernando Date: Mon, 7 Oct 2019 21:25:28 -0500 Subject: [PATCH 03/11] Add __eq__ and __hash__ to sumpy.KernelArgument --- sumpy/kernel.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/sumpy/kernel.py b/sumpy/kernel.py index 832ea849..4606cff7 100644 --- a/sumpy/kernel.py +++ b/sumpy/kernel.py @@ -93,6 +93,13 @@ class KernelArgument(object): def name(self): return self.loopy_arg.name + def __eq__(self, other): + return isinstance(other, KernelArgument) and \ + self.loopy_arg == other.loopy_arg + + def __hash__(self): + return (self.loopy_arg,) + # {{{ basic kernel interface -- GitLab From 6ea3698eec18090f8984b18afbd7489e903c2fdd Mon Sep 17 00:00:00 2001 From: Isuru Fernando Date: Mon, 7 Oct 2019 21:54:47 -0500 Subject: [PATCH 04/11] Fix KernelArgument.__eq__ --- sumpy/kernel.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sumpy/kernel.py b/sumpy/kernel.py index 4606cff7..3df9a605 100644 --- a/sumpy/kernel.py +++ b/sumpy/kernel.py @@ -94,11 +94,12 @@ class KernelArgument(object): return self.loopy_arg.name def __eq__(self, other): - return isinstance(other, KernelArgument) and \ - self.loopy_arg == other.loopy_arg + if not isinstance(other, KernelArgument): + return NotImplemented + return self.loopy_arg == other.loopy_arg def __hash__(self): - return (self.loopy_arg,) + return (type(self), self.loopy_arg) # {{{ basic kernel interface -- GitLab From a8554e7ecee5ab89ba8b6a516ad4cd1ba7e0973c Mon Sep 17 00:00:00 2001 From: Isuru Fernando Date: Mon, 7 Oct 2019 21:55:07 -0500 Subject: [PATCH 05/11] Refactor gather_arguments --- sumpy/tools.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/sumpy/tools.py b/sumpy/tools.py index 78dd954b..36d4a794 100644 --- a/sumpy/tools.py +++ b/sumpy/tools.py @@ -205,14 +205,19 @@ def vector_from_device(queue, vec): return with_object_array_or_scalar(from_dev, vec) +def _merge_kernel_arguments(dictionary, arg): + # Check for strict equality until there's a usecase + if dictionary.setdefault(arg.name, arg) != arg: + raise ValueError("Merging two different kernel arguments with" + "the same name {}".format(arg.name)) + + def gather_arguments(kernel_likes): result = {} for knl in kernel_likes: for arg in knl.get_args(): - # Check for strict equality until there's a usecase - if result.setdefault(arg.name, arg) != arg: - raise ValueError("Merging two different kernel arguments with" - "the same name {}".format(arg.name)) + _merge_kernel_arguments(result, arg) + return sorted(six.itervalues(result), key=lambda arg: arg.name) @@ -220,10 +225,7 @@ def gather_source_arguments(kernel_likes): result = {} for knl in kernel_likes: for arg in knl.get_args() + knl.get_source_args(): - # Check for strict equality until there's a usecase - if result.setdefault(arg.name, arg) != arg: - raise ValueError("Merging two different kernel arguments with" - "the same name {}".format(arg.name)) + _merge_kernel_arguments(result, arg) return sorted(six.itervalues(result), key=lambda arg: arg.name) -- GitLab From 0e4947cfed27b826b7dd3dd3018e39ed0030cf7f Mon Sep 17 00:00:00 2001 From: Isuru Fernando Date: Mon, 7 Oct 2019 22:50:43 -0500 Subject: [PATCH 06/11] More informative error message --- sumpy/tools.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sumpy/tools.py b/sumpy/tools.py index 36d4a794..c0b5b7b9 100644 --- a/sumpy/tools.py +++ b/sumpy/tools.py @@ -208,7 +208,9 @@ def vector_from_device(queue, vec): def _merge_kernel_arguments(dictionary, arg): # Check for strict equality until there's a usecase if dictionary.setdefault(arg.name, arg) != arg: - raise ValueError("Merging two different kernel arguments with" + raise ValueError( + "Merging two different kernel arguments {} {} with".format( + arg.loopy_arg, dictionary[arg].loopy_arg) + \ "the same name {}".format(arg.name)) -- GitLab From 3fa7a8446d1ee29748faa424c5cedd79214d3036 Mon Sep 17 00:00:00 2001 From: Isuru Fernando Date: Mon, 7 Oct 2019 23:16:52 -0500 Subject: [PATCH 07/11] Fix error message --- sumpy/tools.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/sumpy/tools.py b/sumpy/tools.py index c0b5b7b9..4d109842 100644 --- a/sumpy/tools.py +++ b/sumpy/tools.py @@ -208,10 +208,8 @@ def vector_from_device(queue, vec): def _merge_kernel_arguments(dictionary, arg): # Check for strict equality until there's a usecase if dictionary.setdefault(arg.name, arg) != arg: - raise ValueError( - "Merging two different kernel arguments {} {} with".format( - arg.loopy_arg, dictionary[arg].loopy_arg) + \ - "the same name {}".format(arg.name)) + msg = "Merging two different kernel arguments {} and {} with the same name" + raise ValueError(msg.format(arg.loopy_arg, dictionary[arg].loopy_arg)) def gather_arguments(kernel_likes): -- GitLab From e4ff1647cc6eaf20a3746d686536a5bc2b3a2200 Mon Sep 17 00:00:00 2001 From: Isuru Fernando Date: Mon, 7 Oct 2019 23:32:00 -0500 Subject: [PATCH 08/11] Check for type instead of isinstance --- sumpy/kernel.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sumpy/kernel.py b/sumpy/kernel.py index 3df9a605..48a75481 100644 --- a/sumpy/kernel.py +++ b/sumpy/kernel.py @@ -94,7 +94,9 @@ class KernelArgument(object): return self.loopy_arg.name def __eq__(self, other): - if not isinstance(other, KernelArgument): + if not type(self) == KernelArgument: + return NotImplemented + if not type(other) == KernelArgument: return NotImplemented return self.loopy_arg == other.loopy_arg -- GitLab From 60fb61d64977b516099c309ce6f3af6fbf638bb2 Mon Sep 17 00:00:00 2001 From: Isuru Fernando Date: Mon, 7 Oct 2019 23:47:25 -0500 Subject: [PATCH 09/11] Python 2 fixes --- sumpy/kernel.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sumpy/kernel.py b/sumpy/kernel.py index 48a75481..6f553519 100644 --- a/sumpy/kernel.py +++ b/sumpy/kernel.py @@ -100,6 +100,10 @@ class KernelArgument(object): return NotImplemented return self.loopy_arg == other.loopy_arg + def __neq__(self, other): + # Needed for python2 + return not self == other + def __hash__(self): return (type(self), self.loopy_arg) -- GitLab From e18ec666c09e60b2378a3f5b1ce6df9b060e8c1b Mon Sep 17 00:00:00 2001 From: Isuru Fernando Date: Mon, 7 Oct 2019 23:57:23 -0500 Subject: [PATCH 10/11] Fix typo --- sumpy/kernel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sumpy/kernel.py b/sumpy/kernel.py index 6f553519..e33fb8fb 100644 --- a/sumpy/kernel.py +++ b/sumpy/kernel.py @@ -100,7 +100,7 @@ class KernelArgument(object): return NotImplemented return self.loopy_arg == other.loopy_arg - def __neq__(self, other): + def __ne__(self, other): # Needed for python2 return not self == other -- GitLab From 8b759635ae176e127641d6889b0dd487897515a8 Mon Sep 17 00:00:00 2001 From: Isuru Fernando Date: Tue, 8 Oct 2019 00:44:08 -0500 Subject: [PATCH 11/11] Use id for subclasses --- sumpy/kernel.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sumpy/kernel.py b/sumpy/kernel.py index e33fb8fb..3936c518 100644 --- a/sumpy/kernel.py +++ b/sumpy/kernel.py @@ -94,6 +94,8 @@ class KernelArgument(object): return self.loopy_arg.name def __eq__(self, other): + if id(self) == id(other): + return True if not type(self) == KernelArgument: return NotImplemented if not type(other) == KernelArgument: -- GitLab