Skip to content
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

Broadcast array arg in binary ops if it's a valid leaf array type #51

Merged
merged 15 commits into from
Jul 6, 2021

Conversation

kaushikcfd
Copy link
Collaborator

@kaushikcfd kaushikcfd commented Jun 28, 2021

Closes #49.

@kaushikcfd kaushikcfd marked this pull request as ready for review June 28, 2021 01:35
@alexfikl
Copy link
Collaborator

Is the "not device scalar" case useful?

In the general case, DOFArray will have different groups with different numbers of elements and degrees of freedom, so doing DOFArray + my_cl_array is unlikely to work, right?

@kaushikcfd
Copy link
Collaborator Author

In the general case, DOFArray will have different groups with different numbers of elements and degrees of freedom, so doing DOFArray + my_cl_array is unlikely to work, right?

If the broadcasting for any of the leaf arrays is illegal, we would see an error, which IMO is a reasonable user experience.

arraycontext/container/arithmetic.py Outdated Show resolved Hide resolved
arraycontext/context.py Outdated Show resolved Hide resolved
Copy link
Owner

@inducer inducer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @alexfikl for taking a look. A few more thoughts below.

@@ -343,6 +343,17 @@ def {fname}(arg1):
else:
raise ValueError(msg)""")
gen(f"return cls({zip_init_args})")
if _cls_has_array_context_attr:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should get its own flag, defaulted to the same value as bcast_number.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, see here: 93013c9.

@@ -343,6 +343,17 @@ def {fname}(arg1):
else:
raise ValueError(msg)""")
gen(f"return cls({zip_init_args})")
if _cls_has_array_context_attr:
gen("if isinstance(arg2,"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ordering here matters a great deal. (Maybe there should be a comment stating this.) These cases should be sorted from most likely to least. Is this the second-most-likely case?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, this use-case would come just after host-scalars, as implemented in 93013c9.

lambda a: {op_str.format("a", "arg2")},
arg1))
""")

gen(f"""
if {bool(outer_bcast_type_names)}: # optimized away
if isinstance(arg2, {tup_str(outer_bcast_type_names)}):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also work (and be tested) for the reverse operators.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test in f0e0587.

arraycontext/container/arithmetic.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@alexfikl alexfikl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

Just left a very small nitpick.

arraycontext/container/arithmetic.py Outdated Show resolved Hide resolved
kaushikcfd and others added 2 commits June 30, 2021 17:10
replaced with only if

Co-authored-by: Alex Fikl <alexfikl@gmail.com>
Copy link
Owner

@inducer inducer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Some comments below.

Comment on lines 161 to 166
def get_array_types(self):
"""
Returns a :class:`tuple` of types that are valid base array classes
the context can operate on.
"""
return ()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I think this could be an attribute, to avoid the function call overhead. (If you need non-global import to set it, set it in the constructor.)
  • I think documenting this is probably OK.
  • Would it be sensible to only allow one type here, to avoid the splat above?

Copy link
Collaborator Author

@kaushikcfd kaushikcfd Jul 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. 9255f22

Would it be sensible to only allow one type here, to avoid the splat above?

I think there is some value in keeping it tuple, default value is much nicer since the only intent is to perform type checking using it + also accounts for the slightest of chances that an array context might not have a single base array type.

test/test_arraycontext.py Show resolved Hide resolved
test/test_arraycontext.py Show resolved Hide resolved
arraycontext/container/arithmetic.py Outdated Show resolved Hide resolved
arraycontext/container/arithmetic.py Outdated Show resolved Hide resolved
Comment on lines 377 to 382
if bcast_actx_array_type:
all_outer_bcast_type_names = (
outer_bcast_type_names
+ ("*arg1.array_context.get_array_types()",))
else:
all_outer_bcast_type_names = outer_bcast_type_names
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Move this closer to the usage site.
  • Make this produce a tuple that's either empty or has the actx array type(s) in it. Add both together in the argument of tup_str.

(Same below for reverse.)

Copy link
Collaborator Author

@kaushikcfd kaushikcfd Jul 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I think 2040a8d makes it better.

@inducer
Copy link
Owner

inducer commented Jul 5, 2021

Unsubscribing... @-mention or request review once it's ready for a look or needs attention.

Copy link
Owner

@inducer inducer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Just a few more minor things, then this is ready to go.

arraycontext/context.py Outdated Show resolved Hide resolved
arraycontext/context.py Outdated Show resolved Hide resolved
arraycontext/container/arithmetic.py Outdated Show resolved Hide resolved
arraycontext/container/arithmetic.py Outdated Show resolved Hide resolved
@inducer
Copy link
Owner

inducer commented Jul 6, 2021

Unsubscribing... @-mention or request review once it's ready for a look or needs attention.

@kaushikcfd kaushikcfd requested a review from inducer July 6, 2021 00:56
@inducer inducer merged commit be0f569 into main Jul 6, 2021
@inducer inducer deleted the fix_49 branch July 6, 2021 14:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Broadcasting rules distinguish between host scalar and device scalars
3 participants