From b7bdf983bd47198c4118408df25f8419b645e861 Mon Sep 17 00:00:00 2001 From: ferreol Date: Fri, 16 May 2025 18:44:07 +0200 Subject: [PATCH 1/3] docs: add a message about preparation being not thread safe in documentation --- DifferentiationInterface/docs/src/explanation/operators.md | 1 + DifferentiationInterface/src/docstrings.jl | 2 ++ 2 files changed, 3 insertions(+) diff --git a/DifferentiationInterface/docs/src/explanation/operators.md b/DifferentiationInterface/docs/src/explanation/operators.md index 2629df5d2..4d1c740d5 100644 --- a/DifferentiationInterface/docs/src/explanation/operators.md +++ b/DifferentiationInterface/docs/src/explanation/operators.md @@ -117,6 +117,7 @@ op(f, prep, backend, x, [t]) # fast because it skips preparation !!! warning The `prep` object is the last argument before `backend` and it is always mutated, regardless of the bang `!` in the operator name. + As a consequence preparation is **not thread safe** and sharing `prep` object between threads may lead to undefined behavior. If you need to run differentiation concurrently, prepare separate `prep` objects for each thread. ### Reusing preparation diff --git a/DifferentiationInterface/src/docstrings.jl b/DifferentiationInterface/src/docstrings.jl index 6e22c2f1c..132274e1d 100644 --- a/DifferentiationInterface/src/docstrings.jl +++ b/DifferentiationInterface/src/docstrings.jl @@ -25,6 +25,8 @@ function docstring_prepare(operator; samepoint=false, inplace=false) The preparation result `prep` is only reusable as long as the arguments to `$operator` do not change type or size, and the function and backend themselves are not modified. Otherwise, preparation becomes invalid and you need to run it again. In some settings, invalid preparations may still give correct results (e.g. for backends that require no preparation), but this is not a semantic guarantee and should not be relied upon. + !!! danger + The preparation result `prep` is **not thread safe**. Sharing it between threads may lead to undefined behavior. If you need to run differentiations concurrently, prepare separate `prep` objects for each thread. When `strict=Val(true)` (the default), type checking is enforced between preparation and execution (but size checking is left to the user). While your code may work for different types by setting `strict=Val(false)`, this is not guaranteed by the API and can break without warning. From 2c9781181d02e626835c99c04666bdf7a50af5d3 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Sat, 17 May 2025 10:57:32 +0200 Subject: [PATCH 2/3] Apply suggestions from code review --- DifferentiationInterface/docs/src/explanation/operators.md | 2 +- DifferentiationInterface/src/docstrings.jl | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/DifferentiationInterface/docs/src/explanation/operators.md b/DifferentiationInterface/docs/src/explanation/operators.md index 4d1c740d5..6ca844ae5 100644 --- a/DifferentiationInterface/docs/src/explanation/operators.md +++ b/DifferentiationInterface/docs/src/explanation/operators.md @@ -117,7 +117,7 @@ op(f, prep, backend, x, [t]) # fast because it skips preparation !!! warning The `prep` object is the last argument before `backend` and it is always mutated, regardless of the bang `!` in the operator name. - As a consequence preparation is **not thread safe** and sharing `prep` object between threads may lead to undefined behavior. If you need to run differentiation concurrently, prepare separate `prep` objects for each thread. + As a consequence, preparation is **not thread-safe** and sharing `prep` objects between threads may lead to unexpected behavior. If you need to run differentiation concurrently, prepare separate `prep` objects for each thread. ### Reusing preparation diff --git a/DifferentiationInterface/src/docstrings.jl b/DifferentiationInterface/src/docstrings.jl index 132274e1d..d7bfb1395 100644 --- a/DifferentiationInterface/src/docstrings.jl +++ b/DifferentiationInterface/src/docstrings.jl @@ -25,8 +25,9 @@ function docstring_prepare(operator; samepoint=false, inplace=false) The preparation result `prep` is only reusable as long as the arguments to `$operator` do not change type or size, and the function and backend themselves are not modified. Otherwise, preparation becomes invalid and you need to run it again. In some settings, invalid preparations may still give correct results (e.g. for backends that require no preparation), but this is not a semantic guarantee and should not be relied upon. + !!! danger - The preparation result `prep` is **not thread safe**. Sharing it between threads may lead to undefined behavior. If you need to run differentiations concurrently, prepare separate `prep` objects for each thread. + The preparation result `prep` is **not thread-safe**. Sharing it between threads may lead to unexpected behavior. If you need to run differentiations concurrently, prepare separate `prep` objects for each thread. When `strict=Val(true)` (the default), type checking is enforced between preparation and execution (but size checking is left to the user). While your code may work for different types by setting `strict=Val(false)`, this is not guaranteed by the API and can break without warning. From 5ce30ea1e5373c4d9c8214c6c09018dffa9d122b Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Sat, 17 May 2025 10:58:05 +0200 Subject: [PATCH 3/3] Update DifferentiationInterface/src/docstrings.jl --- DifferentiationInterface/src/docstrings.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DifferentiationInterface/src/docstrings.jl b/DifferentiationInterface/src/docstrings.jl index d7bfb1395..ff3359884 100644 --- a/DifferentiationInterface/src/docstrings.jl +++ b/DifferentiationInterface/src/docstrings.jl @@ -27,7 +27,7 @@ function docstring_prepare(operator; samepoint=false, inplace=false) In some settings, invalid preparations may still give correct results (e.g. for backends that require no preparation), but this is not a semantic guarantee and should not be relied upon. !!! danger - The preparation result `prep` is **not thread-safe**. Sharing it between threads may lead to unexpected behavior. If you need to run differentiations concurrently, prepare separate `prep` objects for each thread. + The preparation result `prep` is **not thread-safe**. Sharing it between threads may lead to unexpected behavior. If you need to run differentiation concurrently, prepare separate `prep` objects for each thread. When `strict=Val(true)` (the default), type checking is enforced between preparation and execution (but size checking is left to the user). While your code may work for different types by setting `strict=Val(false)`, this is not guaranteed by the API and can break without warning.