-
Notifications
You must be signed in to change notification settings - Fork 229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
builtins: Support batched initialize_function #2176
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -216,6 +216,66 @@ def fset(f, g): | |
return f | ||
|
||
|
||
def _initialize_function(function, data, nbl, mapper=None, mode='constant'): | ||
""" | ||
Construct the symbolic objects for `initialize_function`. | ||
""" | ||
nbl, slices = nbl_to_padsize(nbl, function.ndim) | ||
if isinstance(data, dv.Function): | ||
function.data[slices] = data.data[:] | ||
else: | ||
function.data[slices] = data | ||
lhs = [] | ||
rhs = [] | ||
options = [] | ||
|
||
if mode == 'reflect' and function.grid.distributor.is_parallel: | ||
# Check that HALO size is appropriate | ||
halo = function.halo | ||
local_size = function.shape | ||
|
||
def buff(i, j): | ||
return [(i + k - 2*max(max(nbl))) for k in j] | ||
|
||
b = [min(l) for l in (w for w in (buff(i, j) for i, j in zip(local_size, halo)))] | ||
if any(np.array(b) < 0): | ||
raise ValueError("Function `%s` halo is not sufficiently thick." % function) | ||
|
||
for d, (nl, nr) in zip(function.space_dimensions, as_tuple(nbl)): | ||
dim_l = dv.SubDimension.left(name='abc_%s_l' % d.name, parent=d, thickness=nl) | ||
dim_r = dv.SubDimension.right(name='abc_%s_r' % d.name, parent=d, thickness=nr) | ||
if mode == 'constant': | ||
subsl = nl | ||
subsr = d.symbolic_max - nr | ||
elif mode == 'reflect': | ||
subsl = 2*nl - 1 - dim_l | ||
subsr = 2*(d.symbolic_max - nr) + 1 - dim_r | ||
else: | ||
raise ValueError("Mode not available") | ||
lhs.append(function.subs({d: dim_l})) | ||
lhs.append(function.subs({d: dim_r})) | ||
rhs.append(function.subs({d: subsl})) | ||
rhs.append(function.subs({d: subsr})) | ||
options.extend([None, None]) | ||
|
||
if mapper and d in mapper.keys(): | ||
exprs = mapper[d] | ||
lhs_extra = exprs['lhs'] | ||
rhs_extra = exprs['rhs'] | ||
lhs.extend(as_list(lhs_extra)) | ||
rhs.extend(as_list(rhs_extra)) | ||
options_extra = exprs.get('options', len(as_list(lhs_extra))*[None, ]) | ||
if isinstance(options_extra, list): | ||
options.extend(options_extra) | ||
else: | ||
options.extend([options_extra]) | ||
|
||
if all(options is None for i in options): | ||
options = None | ||
|
||
return lhs, rhs, options | ||
|
||
|
||
def initialize_function(function, data, nbl, mapper=None, mode='constant', | ||
name=None, pad_halo=True, **kwargs): | ||
""" | ||
|
@@ -225,9 +285,9 @@ def initialize_function(function, data, nbl, mapper=None, mode='constant', | |
|
||
Parameters | ||
---------- | ||
function : Function | ||
function : Function or list of Functions | ||
The initialised object. | ||
data : ndarray or Function | ||
data : ndarray or Function or list of ndarray/Function | ||
The data used for initialisation. | ||
nbl : int or tuple of int or tuple of tuple of int | ||
Number of outer layers (such as absorbing layers for boundary damping). | ||
|
@@ -286,73 +346,45 @@ def initialize_function(function, data, nbl, mapper=None, mode='constant', | |
[2, 3, 3, 3, 3, 2], | ||
[2, 2, 2, 2, 2, 2]], dtype=int32) | ||
""" | ||
name = name or 'pad_%s' % function.name | ||
if isinstance(function, dv.TimeFunction): | ||
if isinstance(function, (list, tuple)): | ||
if not isinstance(data, (list, tuple)): | ||
raise TypeError("Expected a list of `data`") | ||
elif len(function) != len(data): | ||
raise ValueError("Expected %d `data` items, got %d" % | ||
(len(function), len(data))) | ||
|
||
if mapper is not None: | ||
raise NotImplementedError("Unsupported `mapper` with batching") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not even one mapper per function? |
||
|
||
functions = function | ||
datas = data | ||
else: | ||
functions = (function,) | ||
datas = (data,) | ||
|
||
if any(isinstance(f, dv.TimeFunction) for f in functions): | ||
raise NotImplementedError("TimeFunctions are not currently supported.") | ||
|
||
if nbl == 0: | ||
if isinstance(data, dv.Function): | ||
function.data[:] = data.data[:] | ||
else: | ||
function.data[:] = data[:] | ||
if pad_halo: | ||
pad_outhalo(function) | ||
return | ||
|
||
nbl, slices = nbl_to_padsize(nbl, function.ndim) | ||
if isinstance(data, dv.Function): | ||
function.data[slices] = data.data[:] | ||
for f, data in zip(functions, datas): | ||
if isinstance(data, dv.Function): | ||
f.data[:] = data.data[:] | ||
else: | ||
f.data[:] = data[:] | ||
else: | ||
function.data[slices] = data | ||
lhs = [] | ||
rhs = [] | ||
options = [] | ||
|
||
if mode == 'reflect' and function.grid.distributor.is_parallel: | ||
# Check that HALO size is appropriate | ||
halo = function.halo | ||
local_size = function.shape | ||
|
||
def buff(i, j): | ||
return [(i + k - 2*max(max(nbl))) for k in j] | ||
lhss, rhss, optionss = [], [], [] | ||
for f, data in zip(functions, datas): | ||
lhs, rhs, options = _initialize_function(f, data, nbl, mapper, mode) | ||
|
||
b = [min(l) for l in (w for w in (buff(i, j) for i, j in zip(local_size, halo)))] | ||
if any(np.array(b) < 0): | ||
raise ValueError("Function `%s` halo is not sufficiently thick." % function) | ||
lhss.extend(lhs) | ||
rhss.extend(rhs) | ||
optionss.extend(options) | ||
|
||
for d, (nl, nr) in zip(function.space_dimensions, as_tuple(nbl)): | ||
dim_l = dv.SubDimension.left(name='abc_%s_l' % d.name, parent=d, thickness=nl) | ||
dim_r = dv.SubDimension.right(name='abc_%s_r' % d.name, parent=d, thickness=nr) | ||
if mode == 'constant': | ||
subsl = nl | ||
subsr = d.symbolic_max - nr | ||
elif mode == 'reflect': | ||
subsl = 2*nl - 1 - dim_l | ||
subsr = 2*(d.symbolic_max - nr) + 1 - dim_r | ||
else: | ||
raise ValueError("Mode not available") | ||
lhs.append(function.subs({d: dim_l})) | ||
lhs.append(function.subs({d: dim_r})) | ||
rhs.append(function.subs({d: subsl})) | ||
rhs.append(function.subs({d: subsr})) | ||
options.extend([None, None]) | ||
|
||
if mapper and d in mapper.keys(): | ||
exprs = mapper[d] | ||
lhs_extra = exprs['lhs'] | ||
rhs_extra = exprs['rhs'] | ||
lhs.extend(as_list(lhs_extra)) | ||
rhs.extend(as_list(rhs_extra)) | ||
options_extra = exprs.get('options', len(as_list(lhs_extra))*[None, ]) | ||
if isinstance(options_extra, list): | ||
options.extend(options_extra) | ||
else: | ||
options.extend([options_extra]) | ||
|
||
if all(options is None for i in options): | ||
options = None | ||
assert len(lhss) == len(rhss) == len(optionss) | ||
|
||
assign(lhs, rhs, options=options, name=name, **kwargs) | ||
name = name or 'initialize_%s' % '_'.join(f.name for f in functions) | ||
assign(lhss, rhss, options=optionss, name=name, **kwargs) | ||
|
||
if pad_halo: | ||
pad_outhalo(function) | ||
for f in functions: | ||
pad_outhalo(f) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could support this case for
init([f,g,h], data)
(ie all function initialized to smae data) with justdata = [data]*len(function)