Commit 27e9d65b authored by Frederik Hennig's avatar Frederik Hennig
Browse files

advanced boundaries demo

parent b5d64f28
Pipeline #26801 failed with stage
in 15 minutes and 51 seconds
This diff is collapsed.
......@@ -10,7 +10,7 @@ from lbmpy.fieldaccess import StreamPullTwoFieldsAccessor, \
EsoTwistEvenTimeStepAccessor, \
EsoTwistOddTimeStepAccessor
from pystencils import Assignment
from pystencils import Assignment, AssignmentCollection
from pystencils.data_types import TypedSymbol, create_type
from pystencils.backends.cbackend import CustomCodeNode
from pystencils import create_indexed_kernel
......@@ -37,8 +37,26 @@ class AdvancedStreamingBoundaryOffsetInfo(CustomCodeNode):
def inverse_inward_index_from_dir(dir_idx):
return sp.IndexedBase(AdvancedStreamingBoundaryOffsetInfo.INV_INWARD_INDEX_SYMBOL, shape=(1,))[dir_idx]
@staticmethod
def stream_out_pdfs(stencil):
return sp.symbols('f_out_:{}'.format(len(stencil)))
@staticmethod
def stream_in_pdfs(stencil):
return sp.symbols('f_in_:{}'.format(len(stencil)))
# ---------------------------------- Internal ---------------------------------------------
@staticmethod
def stream_out_pdf_symbolic_dir_indexed(dir_index):
return sp.IndexedBase('f_out', shape=(1,))[dir_index]
@staticmethod
def stream_in_pdf_symbolic_inverse_dir_indexed(dir_index):
return sp.IndexedBase('f_in', shape=(1,))[dir_index]
@staticmethod
def inverse_dir_symbol(dir_symbol):
symbol_type = dir_symbol.dtype if isinstance(dir_symbol, TypedSymbol) else create_type(np.int64)
return TypedSymbol("{}_inv".format(dir_symbol.name), symbol_type)
def __init__(self, pdf_field, stencil, between_timesteps='both', kernel_type='pull'):
if between_timesteps not in ['both', 'odd_to_even', 'even_to_odd']:
......@@ -75,11 +93,17 @@ class AdvancedStreamingBoundaryOffsetInfo(CustomCodeNode):
if between_timesteps == 'both':
assert even_accessor == odd_accessor
self.pdf_field = pdf_field
self.stencil = stencil
write_field_accesses = (
odd_accessor if odd_to_even else even_accessor).write(pdf_field, stencil)
read_field_accesses = (
even_accessor if odd_to_even else odd_accessor).read(pdf_field, stencil)
self.write_field_accesses = write_field_accesses
self.read_field_accesses = read_field_accesses
dim = len(stencil[0])
code = "\n"
......@@ -92,7 +116,7 @@ class AdvancedStreamingBoundaryOffsetInfo(CustomCodeNode):
outward_offset_sym[i].name, offset_str)
out_acc_indices = [str(a.index[0]) for a in write_field_accesses]
code += "const int %s [] = { %s };\n" % (
code += "const int64_t %s [] = { %s };\n" % (
self.OUTWARD_INDEX_SYMBOL.name, ", ".join(out_acc_indices))
# Offsets and Indices for the stream-in-populations
......@@ -110,7 +134,7 @@ class AdvancedStreamingBoundaryOffsetInfo(CustomCodeNode):
inv_inward_offset_sym[i].name, offset_str)
inv_in_acc_indices = [str(a.index[0]) for a in inv_read_field_accesses]
code += "const int %s [] = { %s };\n" % (
code += "const int64_t %s [] = { %s };\n" % (
self.INV_INWARD_INDEX_SYMBOL.name, ", ".join(inv_in_acc_indices))
defined_symbols = set(outward_offset_sym + inv_inward_offset_sym +
......@@ -119,6 +143,41 @@ class AdvancedStreamingBoundaryOffsetInfo(CustomCodeNode):
super(AdvancedStreamingBoundaryOffsetInfo, self).__init__(
code, symbols_read=set(), symbols_defined=defined_symbols)
def substitute_pdf_proxies(self, boundary_assignments, dir_symbol):
if not isinstance(boundary_assignments, AssignmentCollection):
boundary_assignments = AssignmentCollection(boundary_assignments)
out_pdfs = self.stream_out_pdfs(self.stencil)
in_pdfs = self.stream_in_pdfs(self.stencil)
dir_indexed_out_pdf = self.stream_out_pdf_symbolic_dir_indexed(dir_symbol)
inv_dir_symbol = self.inverse_dir_symbol(dir_symbol)
inv_dir_indexed_in_pdf = self.stream_in_pdf_symbolic_inverse_dir_indexed(inv_dir_symbol)
q = len(self.stencil)
dim = len(self.stencil[0])
pdf_subs = dict()
# Direct accesses
for i in range(q):
pdf_subs[out_pdfs[i]] = self.write_field_accesses[i]
pdf_subs[in_pdfs[i]] = self.read_field_accesses[i]
# Symbolically indexed accesses
outward_offset = self.outward_offset_from_dir(dir_symbol, dim)
outward_idx = self.outward_index_from_dir(dir_symbol)
inv_inward_offset = self.inverse_inward_offset_from_dir(dir_symbol, dim)
inv_inward_idx = self.inverse_inward_index_from_dir(dir_symbol)
pdf_out_access_by_dir = self.pdf_field[outward_offset](outward_idx)
pdf_subs[dir_indexed_out_pdf] = pdf_out_access_by_dir
pdf_inv_in_access_by_dir = self.pdf_field[inv_inward_offset](inv_inward_idx)
pdf_subs[inv_dir_indexed_in_pdf] = pdf_inv_in_access_by_dir
return boundary_assignments.new_with_substitutions(pdf_subs)
@staticmethod
def _inverse_dir_index(stencil, dir):
return stencil.index(tuple(-d for d in stencil[dir]))
......@@ -152,7 +211,8 @@ class FlexibleNoSlip(Boundary):
outward_offset = AdvancedStreamingBoundaryOffsetInfo.outward_offset_from_dir(direction_symbol, lb_method.dim)
outward_idx = AdvancedStreamingBoundaryOffsetInfo.outward_index_from_dir(direction_symbol)
inv_inward_offset = AdvancedStreamingBoundaryOffsetInfo.inverse_inward_offset_from_dir(direction_symbol, lb_method.dim)
inv_inward_offset = AdvancedStreamingBoundaryOffsetInfo.inverse_inward_offset_from_dir(
direction_symbol, lb_method.dim)
inv_inward_idx = AdvancedStreamingBoundaryOffsetInfo.inverse_inward_index_from_dir(direction_symbol)
return [Assignment(pdf_field[inv_inward_offset](inv_inward_idx), pdf_field[outward_offset](outward_idx))]
......@@ -168,11 +228,12 @@ class FlexibleNoSlip(Boundary):
# end class FlexibleNoSlip
def create_flexible_lbm_boundary_kernel(pdf_field, index_field, lb_method, boundary_functor,
between_timesteps='both', kernel_type='pull',
target='cpu', openmp=True):
elements = [AdvancedStreamingBoundaryOffsetInfo(pdf_field, lb_method.stencil, between_timesteps, kernel_type),
elements = [AdvancedStreamingBoundaryOffsetInfo(pdf_field, lb_method.stencil, between_timesteps, kernel_type),
LbmWeightInfo(lb_method)]
index_arr_dtype = index_field.dtype.numpy_dtype
dir_symbol = TypedSymbol("dir", index_arr_dtype.fields['dir'][0])
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment