Skip to content

Commit

Permalink
Store BCH/RS decoders for each base/extension field pair
Browse files Browse the repository at this point in the history
Fixes #483
  • Loading branch information
mhostetter committed May 8, 2023
1 parent 1518169 commit 2019c05
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src/galois/_codes/_bch.py
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,18 @@ def __init__(self, field: Type[FieldArray], extension_field: Type[FieldArray]):
super().__init__(field)
self.extension_field = extension_field

@property
def key_1(self):
# Make the key in the cache lookup table specific to both the base field and extension field
return (
self.field.characteristic,
self.field.degree,
int(self.field.irreducible_poly),
self.extension_field.characteristic,
self.extension_field.degree,
int(self.extension_field.irreducible_poly),
)

def __call__(self, codeword, design_n, alpha, c, roots):
if self.extension_field.ufunc_mode != "python-calculate":
output = self.jit(codeword.astype(np.int64), design_n, alpha, c, roots.astype(np.int64))
Expand Down
26 changes: 16 additions & 10 deletions src/galois/_domains/_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ def set_globals(self):
# Various ufuncs based on implementation and compilation
###############################################################################

@property
def key_1(self):
return (self.field.characteristic, self.field.degree, int(self.field.irreducible_poly))

@property
def key_2(self):
if self.field.ufunc_mode == "jit-lookup":
key = (str(self.__class__), self.field.ufunc_mode, int(self.field.primitive_element))
else:
key = (str(self.__class__), self.field.ufunc_mode)
return key

@property
def function(self):
"""
Expand All @@ -72,19 +84,13 @@ def jit(self) -> numba.types.FunctionType:
"""
assert self.field.ufunc_mode in ["jit-lookup", "jit-calculate"]

key_1 = (self.field.characteristic, self.field.degree, int(self.field.irreducible_poly))
if self.field.ufunc_mode == "jit-lookup":
key_2 = (str(self.__class__), self.field.ufunc_mode, int(self.field.primitive_element))
else:
key_2 = (str(self.__class__), self.field.ufunc_mode)
self._CACHE.setdefault(key_1, {})

if key_2 not in self._CACHE[key_1]:
self._CACHE.setdefault(self.key_1, {})
if self.key_2 not in self._CACHE[self.key_1]:
self.set_globals() # Set the globals once before JIT compiling the function
func = numba.jit(self._SIGNATURE.signature, parallel=self._PARALLEL, nopython=True)(self.implementation)
self._CACHE[key_1][key_2] = func
self._CACHE[self.key_1][self.key_2] = func

return self._CACHE[key_1][key_2]
return self._CACHE[self.key_1][self.key_2]

@property
def python(self) -> Callable:
Expand Down

0 comments on commit 2019c05

Please sign in to comment.