Skip to content

Commit a10b555

Browse files
committed
Make warning parameter threadsafe
1 parent caef765 commit a10b555

File tree

4 files changed

+37
-13
lines changed

4 files changed

+37
-13
lines changed

docs/src/advanced.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ y = Variable()
1313
x*y
1414
```
1515

16-
To disable this, set the module-level parameter `DCP_WARNINGS` via
16+
These warnings can be very useful to know if a problem has been formulated
17+
correctly. However, in some circumstances, one may wish to disable these
18+
warnings. To do so, call [`Convex.disable_DCP_warnings()`](@ref). To restore the
19+
warnings, call [`Convex.enable_DCP_warnings()`](@ref).
1720

18-
```julia
19-
Convex.DCP_WARNINGS[] = false
21+
```@docs
22+
Convex.enable_DCP_warnings()
23+
Convex.disable_DCP_warnings()
2024
```
2125

2226

src/Convex.jl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,13 @@ export Positive, Negative, ComplexSign, NoSign
3434
# Problems
3535
export add_constraint!, add_constraints!, maximize, minimize, Problem, satisfy, solve!
3636

37-
"""
38-
const DCP_WARNINGS = Ref(true)
37+
const DCP_WARNINGS = Bool[]
3938

40-
Controls whether or not warnings are emitted for when an expression fails to be
41-
of disciplined convex form. To turn warnings off, run
39+
function __init__()
40+
resize!(DCP_WARNINGS, Threads.nthreads())
41+
fill!(DCP_WARNINGS, true)
42+
end
4243

43-
Convex.DCP_WARNINGS[] = false
44-
"""
45-
const DCP_WARNINGS = Ref(true)
4644

4745
### modeling framework
4846
include("dcp.jl")

src/dcp.jl

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,31 @@ struct AffineVexity <: Vexity end
1919
struct ConvexVexity <: Vexity end
2020
struct ConcaveVexity <: Vexity end
2121

22+
23+
"""
24+
enable_DCP_warnings()
25+
26+
Enable warnings from the current thread when an expression fails to be of
27+
disciplined convex form. This setting is enabled by default. See also
28+
[`disable_DCP_warnings`](@ref).
29+
"""
30+
function enable_DCP_warnings()
31+
DCP_WARNINGS[Threads.threadid()] = true
32+
end
33+
34+
"""
35+
disable_DCP_warnings()
36+
37+
Disable warnings from the current thread when an expression fails to be of
38+
disciplined convex form. See also [`enable_DCP_warnings`](@ref).
39+
"""
40+
function disable_DCP_warnings()
41+
DCP_WARNINGS[Threads.threadid()] = false
42+
end
43+
2244
struct NotDcp <: Vexity
2345
function NotDcp()
24-
if DCP_WARNINGS[]
46+
if DCP_WARNINGS[Threads.threadid()]
2547
@warn "Expression not DCP compliant. Trying to solve non-DCP compliant problems can lead to unexpected behavior."
2648
end
2749
return new()

test/test_utilities.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,9 @@ using Convex: AbstractExpr, ConicObj
255255
# default is to log
256256
@test_logs (:warn, r"not DCP compliant") Convex.NotDcp()
257257

258-
Convex.DCP_WARNINGS[] = false
258+
Convex.disable_DCP_warnings()
259259
@test_logs Convex.NotDcp()
260-
Convex.DCP_WARNINGS[] = true
260+
Convex.enable_DCP_warnings()
261261
@test_logs (:warn, r"not DCP compliant") Convex.NotDcp()
262262

263263
end

0 commit comments

Comments
 (0)