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

feat[next]: use context vars instead of global state in embedded iterator execution #1120

Merged
merged 10 commits into from
May 8, 2023

Conversation

havogt
Copy link
Contributor

@havogt havogt commented Jan 9, 2023

Changed:

  • In the iterator embedded execution, both column_range and offset_provider global variables have been converted to context variables and fencil closures are now executed in isolated contexts with properly initialized values, to avoid using global state.

@havogt
Copy link
Contributor Author

havogt commented Jan 9, 2023

Opening as a reminder, instead of #896

@havogt havogt mentioned this pull request Jan 9, 2023
2 tasks
@egparedes
Copy link
Contributor

I can clean up the code and provide a final version of this prototype without too much effort. @tehrengruber do you also agree with using this implementation strategy to fix the problem?

@tehrengruber
Copy link
Contributor

Yes, given the model itself this is much better than the global variable.

@DropD DropD added the move to main This pull request to functional needs to be made to main instead. label Feb 24, 2023
@egparedes egparedes changed the base branch from functional to main April 28, 2023 15:35
@egparedes egparedes removed the move to main This pull request to functional needs to be made to main instead. label Apr 28, 2023
@egparedes egparedes changed the title Functional: embedded column_range via context_vars feature[next]: use context vars instead of global state in embedded iterator Apr 28, 2023
@egparedes egparedes changed the title feature[next]: use context vars instead of global state in embedded iterator feature[next]: use context vars instead of global state in embedded iterator execution Apr 28, 2023
@egparedes egparedes changed the title feature[next]: use context vars instead of global state in embedded iterator execution featnext]: use context vars instead of global state in embedded iterator execution Apr 28, 2023
@egparedes
Copy link
Contributor

I've requested a formal review from @tehrengruber because apparently @havogt can't approve it since he's the creator of the PR, but I think both of you should take a look.

@egparedes egparedes changed the title featnext]: use context vars instead of global state in embedded iterator execution feat[next]: use context vars instead of global state in embedded iterator execution Apr 28, 2023
src/gt4py/next/iterator/embedded.py Outdated Show resolved Hide resolved
src/gt4py/next/iterator/embedded.py Outdated Show resolved Hide resolved
src/gt4py/next/iterator/embedded.py Outdated Show resolved Hide resolved
src/gt4py/next/iterator/embedded.py Outdated Show resolved Hide resolved
src/gt4py/next/iterator/embedded.py Outdated Show resolved Hide resolved
src/gt4py/next/iterator/embedded.py Outdated Show resolved Hide resolved
src/gt4py/next/iterator/embedded.py Outdated Show resolved Hide resolved
src/gt4py/next/iterator/embedded.py Outdated Show resolved Hide resolved
src/gt4py/next/iterator/embedded.py Outdated Show resolved Hide resolved


def test_column_multithread():
def test_func(i: int, output: list) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this really a meaningful test? I have no clue how the thread scheduling works in Python, but this might very well never fail even if the column range is implemented incorrectly. If we want this to be meaningful how about a lock that ensure the column construction of the first thread happens after the context setup of a second thread? (If it's not worth the effort I would remove the test altogether to be honest.)

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, I agree and removed the test.

assert np.array_equal(res.data, a.data + b.data)
assert res.kstart == 1

_run_within_context(test_func)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is fine, but is there a reason to not use a context handler for this? Sure the columns would leak scope, but doesn't really matter right?

Copy link
Contributor

Choose a reason for hiding this comment

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

A context handler doesn't really work here because code running inside a context needs to be an actual function (line 36). Setting the values of the context vars before running some lines of code and then restoring their previous values does not test the context vars and it doesn't work in the general case (it will fail when running multiple threads, for example).

@egparedes egparedes merged commit 9241243 into GridTools:main May 8, 2023
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.

4 participants