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

POC: Automatically parallelize map(f, arrays...) #43910

Draft
wants to merge 6 commits into
base: avi/infer_effects
Choose a base branch
from

Conversation

tkf
Copy link
Member

@tkf tkf commented Jan 24, 2022

Thanks to @Keno's work on purity modeling #43852, we can check the data-race-freedom (DRF) of user-defined function by asserting that it is :effect_free as defined in #43852 (presumably). This already covers a nontrivial interesting subset of DRF functions. (Edit: The direction of this PR seems to be OK but calling an :effect_free method a "DRF function" is wrong: #43910 (comment)) This PR is a POC implementation of automatic parallelization of map(f, arrays...) by detecting that f and getindex are effect-free. For example, map(sin, xs) is parallelized for "well-behaving" array types like xs::Array and xs::UnitRange.

A major obstacle for automatic parallelization is (apart from that we don't have JuliaFolds in Base) that, since map on arrays is used everywhere, we can't add overhead to the workloads that are not "big enough." So, this PR currently uses a rather strict bound for parallelism (the length of the array has to be larger than 2^18 ATM). However, it means that the automatic parallelization kicks in very rarely. For this reason, I'm not entirely sure if this is the right direction. Maybe it's much better to let the user declare that a certain call is "throughput oriented" so that we can increase the parallelism a bit more while still doing the automatic DRF check (so, the user doesn't have to be nervous about introducing data races).

@Keno Is it correct to assume that :effect_free implies DRF (given that the caller does not "touch" (mutate) the returned object)?

@oscardssmith
Copy link
Member

To get a better tradeoff, could we @time the first function application and base the decision on that?

@Keno
Copy link
Member

Keno commented Jan 24, 2022

@Keno Is it correct to assume that :effect_free implies DRF (given that the caller does not "touch" (mutate) the returned object)?

Well, not in general, a data race is not an effect. You are guaranteed that it won't have the write side of a data race, but it could have the read side. However, if you are only concerned about data races among the different execution of the functions (which seems like you might be for this case, since any other data race would have been there before), I do think you can conclude that the different function executions do not race with each other.

@tkf
Copy link
Member Author

tkf commented Jan 24, 2022

To get a better tradeoff, could we @time the first function application and base the decision on that?

Ooooh, this is interesting. That sounds like a very good idea!

(We can perhaps even do something clever like exponentially increase base case size a la galloping search to get a good measurement or sth like that.)

You are guaranteed that it won't have the write side of a data race, but it could have the read side.

Thanks! Yes, I should've phrased it carefully. I think a more accurate way to say what I wanted to say is that parallel map(f, arrays...) does not introduce new data races if f and getindex are :effect_free. (If the user program was DRF, it stays DRF.)

@tkf tkf added the multithreading Base.Threads and related functionality label Jan 24, 2022
@Keno Keno force-pushed the kf/effectsstaging branch 3 times, most recently from e98e5a6 to 081bef4 Compare February 1, 2022 19:16
@Keno Keno force-pushed the kf/effectsstaging branch from 081bef4 to e591ba0 Compare February 2, 2022 07:35
@Keno Keno force-pushed the kf/effectsstaging branch 2 times, most recently from 3281ce4 to b30e99c Compare February 9, 2022 14:35
@Keno Keno deleted the branch JuliaLang:avi/infer_effects February 9, 2022 22:24
@Keno Keno closed this Feb 9, 2022
@giordano
Copy link
Contributor

giordano commented Feb 9, 2022

Was this closed intentionally or it's just an accident of merging #43852?

@vchuravy
Copy link
Member

vchuravy commented Feb 9, 2022

Accidental due to the deletion of the base branch.

@tkf looks like we can't reopen. Mind opening a new PR?

@tkf tkf reopened this Feb 16, 2022
@tkf tkf changed the base branch from kf/effectsstaging to master February 16, 2022 08:38
@aviatesk aviatesk self-assigned this Mar 30, 2022
This new reflection utility is similar to `Core.Compiler.return_type` but
for the effect analysis. It comes with its own tfunc and similar compiler
optimizations so that we can fold inferred effects at compiler time when
possible (albeit it is as unreliable as `Core.Compiler.return_type`).
@aviatesk aviatesk changed the base branch from master to avi/infer_effects April 1, 2022 22:50
aviatesk added 3 commits April 2, 2022 07:51
This new reflection utility is similar to `Core.Compiler.return_type` but
for the effect analysis. It comes with its own tfunc and similar compiler
optimizations so that we can fold inferred effects at compiler time when
possible (albeit it is as unreliable as `Core.Compiler.return_type`).
@aviatesk aviatesk force-pushed the avi/infer_effects branch from 0d2c6b8 to 1604714 Compare April 4, 2022 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
multithreading Base.Threads and related functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants