Skip to content

Warn in docs about sync on inner loops #550

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

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions docs/src/use-cases/parallel-nested-loops.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ end
```

In this code we have job interdependence. Firstly, we are calculating the
standard deviation `σ` and than we are using that value in the function `f`.
Since `Dagger.@spawn` yields an `DTask` rather than actual values, we need
standard deviation `σ`, and then we are using that value in the function `f`.
Since `Dagger.@spawn` yields a `DTask` rather than actual values, we need
to use the `fetch` function to obtain those values. In this example, the value
fetching is performed once all computations are completed (note that `@sync`
preceding the loop forces the loop to wait for all jobs to complete). Also,
Expand All @@ -84,3 +84,9 @@ The above use case scenario has been tested by running `julia -t 8` (or with
`JULIA_NUM_THREADS=8` as environment variable). The `Threads.@threads` code
takes 1.8 seconds to run, while the Dagger code, which is also one line
shorter, runs around 0.9 seconds, resulting in a 2x speedup.

!!! warning
Annotating an inner loop with `@sync` will block the outer loop from
iterating until the inner @sync loop is fully completed, negating some
potential parallelism. `@sync` should only be applied to the outermost loop
before a `fetch`.