-
Notifications
You must be signed in to change notification settings - Fork 8
/
interface.jl
492 lines (426 loc) · 16.6 KB
/
interface.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# Example of mutable types that can implement this API: BigInt, Array, JuMP.AffExpr, MultivariatePolynomials.AbstractPolynomial
# `mutable_operate!(add_mul, ...)` is similar to `JuMP.add_to_expression(...)`
# `operate!(add_mul, ...)` is similar to `JuMP.destructive_add(...)`
# `operate!` is similar to `MOI.Utilities.operate!`
"""
promote_operation(op::Function, ArgsTypes::Type...)
Returns the type returned to the call `operate(op, args...)` where the types of
the arguments `args` are `ArgsTypes`.
"""
function promote_operation end
function promote_operation(op::Function, x::Type{<:AbstractArray}, y::Type{<:AbstractArray})
# `zero` is not defined for `AbstractArray` so the fallback would fail with a cryptic MethodError.
# We replace it by a more helpful error here.
error("`promote_operation($op, $x, $y)` not implemented yet, please report this.")
end
function promote_operation(::typeof(/), ::Type{S}, ::Type{T}) where {S,T}
return typeof(zero(S) / oneunit(T))
end
# Julia v1.0.x has trouble with inference with the `Vararg` method, see
# https://travis-ci.org/jump-dev/JuMP.jl/jobs/617606373
function promote_operation(op::Function, ::Type{S}, ::Type{T}) where {S,T}
return typeof(op(zero(S), zero(T)))
end
function promote_operation(op::Function, args::Vararg{Type,N}) where {N}
return typeof(op(zero.(args)...))
end
promote_operation(::typeof(*), ::Type{T}) where {T} = T
function promote_operation(
::typeof(*),
::Type{S},
::Type{T},
::Type{U},
args::Vararg{Type,N},
) where {S,T,U,N}
return promote_operation(*, promote_operation(*, S, T), U, args...)
end
# Helpful error for common mistake
function promote_operation(
op::Union{typeof(+),typeof(-),AddSubMul},
A::Type{<:Array},
α::Type{<:Number},
)
error("Operation `$op` between `$A` and `$α` is not allowed. You should use broadcast.")
end
function promote_operation(
op::Union{typeof(+),typeof(-),AddSubMul},
α::Type{<:Number},
A::Type{<:Array},
)
error("Operation `$op` between `$α` and `$A` is not allowed. You should use broadcast.")
end
"""
operate(op::Function, args...)
Return an object equal to the result of `op(args...)` that can be mutated
through the MultableArithmetics API without affecting the arguments.
By default:
* `operate(+, x)` and `operate(+, x)` redirect to `copy_if_mutable(x)` so a
mutable type `T` can return the same instance from unary operators
`+(x::T) = x` and `*(x::T) = x`.
* `operate(+, args...)` (resp. `operate(-, args...)` and `operate(*, args...)`)
redirect to `+(args...)` (resp. `-(args...)` and `*(args...)`) if `length(args)`
is at least 2 (or the operation is `-`).
Note that when `op` is a `Base` function whose implementation can be improved
for mutable arguments, `operate(op, args...)` may have an implementation in
this package relying on the MutableArithmetics API instead of redirecting to
`op(args...)`. This is the case for instance:
* for `Base.sum`,
* for `LinearAlgebra.dot` and
* for matrix-matrix product and matrix-vector product.
Therefore, for mutable arguments, there may be a performance advantage to call
`operate(op, args...)` instead of `op(args...)`.
## Example
If for a mutable type `T`, the following is defined:
```julia
function Base.:*(a::Bool, x::T)
return a ? x : zero(T)
end
```
then `operate(*, a, x)` will return the instance `x` whose modification will
affect the argument of `operate`. Therefore, the following method need to
be implemented
```julia
function MA.operate(::typeof(*), a::Bool, x::T)
return a ? MA.mutable_copy(x) : zero(T)
end
```
"""
function operate end
# /!\ We assume these three return an object that can be modified through the MA
# API without altering `x` and `y`. If it is not the case, implement a
# custom `operate` method.
operate(::typeof(-), x) = -x
operate(
op::Union{typeof(+),typeof(-),typeof(*),AddSubMul,typeof(add_dot)},
x,
y,
args::Vararg{Any,N},
) where {N} = op(x, y, args...)
operate(::typeof(convert), ::Type{T}, x) where {T} = convert(T, x)
operate(::typeof(convert), ::Type{T}, x::T) where {T} = copy_if_mutable(x)
operate(::Union{typeof(+),typeof(*)}, x) = copy_if_mutable(x)
# We could only give `typeof(x)` to `zero` and `one` to be sure that modifying the
# returned object cannot alter `x` but for some objects, `one` and `zero` depends
# on some values of the fields (e.g. square matrices), elements of the cyclic group
# of order `n` (`n` is one of the field).
operate(::typeof(zero), x) = zero(x)
operate(::typeof(one), x) = one(x)
# Define Traits
"""
abstract type MutableTrait end
Abstract type for [`IsMutable`](@ref) and [`NotMutable`](@ref) that are
returned by [`mutability`](@ref).
"""
abstract type MutableTrait end
"""
struct IsMutable <: MutableTrait end
When this is returned by [`mutability`](@ref), it means that object of the given
type can always be mutated to equal the result of the operation.
"""
struct IsMutable <: MutableTrait end
"""
struct NotMutable <: MutableTrait end
When this is returned by [`mutability`](@ref), it means that object of the given
type cannot be mutated to equal the result of the operation.
"""
struct NotMutable <: MutableTrait end
"""
mutability(T::Type, ::typeof(op), args::Type...)::MutableTrait
Return either [`IsMutable`](@ref) to indicate an object of type `T` can be
modified to be equal to `op(args...)` or [`NotMutable`](@ref) otherwise.
"""
function mutability(T::Type, op, args::Vararg{Type,N}) where {N}
if mutability(T) isa IsMutable && promote_operation(op, args...) == T
return IsMutable()
else
return NotMutable()
end
end
mutability(x, op, args::Vararg{Any,N}) where {N} =
mutability(typeof(x), op, typeof.(args)...)
mutability(::Type) = NotMutable()
"""
mutable_copy(x)
Return a copy of `x` that can be mutated with MultableArithmetics's API without
altering `x`.
## Examples
The copy of a JuMP affine expression does not copy the underlying model as it
cannot be modified though the MultableArithmetics's API, however, it calls
[`copy_if_mutable`](@ref) on the coefficients and on the constant as they could
be mutated.
"""
function mutable_copy end
mutable_copy(A::AbstractArray) = mutable_copy.(A)
copy_if_mutable_fallback(::NotMutable, x) = x
copy_if_mutable_fallback(::IsMutable, x) = mutable_copy(x)
"""
copy_if_mutable(x)
Return a copy of `x` that can be mutated with MultableArithmetics's API without
altering `x`. If `mutability(x)` is `NotMutable` then `x` is returned as none of
`x` can be mutated. Otherwise, it redirects to [`mutable_copy`](@ref).
Mutable types should not implement a method for this function but should
implement a method for [`mutable_copy`](@ref) instead.
"""
copy_if_mutable(x) = copy_if_mutable_fallback(mutability(typeof(x)), x)
function mutable_operate_to_fallback(::NotMutable, output, op::Function, args...)
throw(
ArgumentError(
"Cannot call `mutable_operate_to!(::$(typeof(output)), $op, ::$(join(typeof.(args), ", ::")))` as objects of type `$(typeof(output))` cannot be modifed to equal the result of the operation. Use `operate_to!` instead which returns the value of the result (possibly modifying the first argument) to write generic code that also works when the type cannot be modified.",
),
)
end
function mutable_operate_to_fallback(::IsMutable, output, op::AddSubMul, x, y)
return mutable_operate_to!(output, add_sub_op(op), x, y)
end
function mutable_operate_to_fallback(::IsMutable, output, op::Function, args...)
error(
"`mutable_operate_to!(::$(typeof(output)), $op, ::",
join(typeof.(args), ", ::"),
")` is not implemented yet.",
)
end
"""
mutable_operate_to!(output, op::Function, args...)
Modify the value of `output` to be equal to the value of `op(args...)`. Can
only be called if `mutability(output, op, args...)` returns `true`.
If `output === args[i]` for some `i`,
* The user should expect to get an error. `operate!` or `mutable_operate!` should be used instead.
* Any method not supporting this case should throw an error.
For instance, in DynamicPolynomials, `mutable_operate_to!(p, +, p, q)` throws an
error because otherwise, the algorithm would fill `p` while iterating over the
terms of `p` and `q` hence it will never terminate. On the other hand
`mutable_operate!(+, p, q)` uses a different algorithm that efficiently inserts
the terms of `q` in the sorted list of terms of `p` with minimal displacement.
"""
function mutable_operate_to!(output, op::Function, args::Vararg{Any,N}) where {N}
mutable_operate_to_fallback(mutability(output, op, args...), output, op, args...)
end
function mutable_operate_fallback(::NotMutable, op::Function, args...)
throw(
ArgumentError(
"Cannot call `mutable_operate!($op, ::$(join(typeof.(args), ", ::")))` as objects of type `$(typeof(args[1]))` cannot be modifed to equal the result of the operation. Use `operate!` instead which returns the value of the result (possibly modifying the first argument) to write generic code that also works when the type cannot be modified.",
),
)
end
function mutable_operate_fallback(::IsMutable, op::AddSubMul, x, y)
return mutable_operate!(add_sub_op(op), x, y)
end
function mutable_operate_fallback(::IsMutable, op::Function, args...)
error(
"`mutable_operate!($op, ::",
join(typeof.(args), ", ::"),
")` is not implemented yet.",
)
end
"""
mutable_operate!(op::Function, args...)
Modify the value of `args[1]` to be equal to the value of `op(args...)`. Can
only be called if `mutability(args[1], op, args...)` returns `true`.
"""
function mutable_operate!(op::Function, args::Vararg{Any,N}) where {N}
mutable_operate_fallback(mutability(args[1], op, args...), op, args...)
end
buffer_for(::Function, args::Vararg{Type,N}) where {N} = nothing
function mutable_buffered_operate_to_fallback(::NotMutable, buffer, output, op::Function, args...)
throw(
ArgumentError(
"Cannot call `mutable_buffered_operate_to!(::$(typeof(buffer)), ::$(typeof(output)), $op, ::$(join(typeof.(args), ", ::")))` as objects of type `$(typeof(output))` cannot be modifed to equal the result of the operation. Use `buffered_operate_to!` instead which returns the value of the result (possibly modifying the first argument) to write generic code that also works when the type cannot be modified.",
),
)
end
function mutable_buffered_operate_to_fallback(::IsMutable, buffer, output, op::Function, args...)
error(
"`mutable_buffered_operate_to!(::$(typeof(buffer)), ::$(typeof(output)), $op, ::",
join(typeof.(args), ", ::"),
")` is not implemented.",
)
end
function mutable_buffered_operate_to_fallback(
buffer,
output,
op::Function,
args::Vararg{Any,N},
) where {N}
return mutable_buffered_operate_to_fallback(
mutability(output, op, args...),
buffer,
output,
op,
args...
)
end
function mutable_buffered_operate_to_fallback(
::Nothing,
output,
op::Function,
args::Vararg{Any,N},
) where {N}
return mutable_operate_to!(output, op, args...)
end
"""
mutable_buffered_operate_to!(buffer, output, op::Function, args...)
Modify the value of `output` to be equal to the value of `op(args...)`,
possibly modifying `buffer`. Can only be called if
`mutability(output, op, args...)` returns `true`.
"""
function mutable_buffered_operate_to!(
buffer,
output,
op::Function,
args::Vararg{Any,N},
) where {N}
return mutable_buffered_operate_to_fallback(buffer, output, op, args...)
end
function mutable_buffered_operate_fallback(::NotMutable, buffer, op::Function, args...)
throw(
ArgumentError(
"Cannot call `mutable_buffered_operate!(::$(typeof(buffer)), $op, ::$(join(typeof.(args), ", ::")))` as objects of type `$(typeof(args[1]))` cannot be modifed to equal the result of the operation. Use `buffered_operate!` instead which returns the value of the result (possibly modifying the first argument) to write generic code that also works when the type cannot be modified.",
),
)
end
function mutable_buffered_operate_fallback(::IsMutable, buffer, op::Function, args...)
error(
"`mutable_buffered_operate!(::$(typeof(buffer)), $op, ::",
join(typeof.(args), ", ::"),
")` is not implemented.",
)
end
function mutable_buffered_operate_fallback(
buffer,
op::Function,
args::Vararg{Any,N},
) where {N}
return mutable_buffered_operate_fallback(
mutability(args[1], op, args...),
buffer,
op,
args...
)
end
function mutable_buffered_operate_fallback(
::Nothing,
op::Function,
args::Vararg{Any,N},
) where {N}
return mutable_operate!(op, args...)
end
"""
mutable_buffered_operate!(buffer, op::Function, args...)
Modify the value of `args[1]` to be equal to the value of `op(args...)`,
possibly modifying `buffer`. Can only be called if
`mutability(args[1], op, args...)` returns `true`.
"""
function mutable_buffered_operate! end
function mutable_buffered_operate!(buffer, op::Function, args::Vararg{Any,N}) where {N}
return mutable_buffered_operate_fallback(buffer, op, args...)
end
"""
operate_to!(output, op::Function, args...)
Returns the value of `op(args...)`, possibly modifying `output`.
"""
function operate_to!(output, op::Function, args::Vararg{Any,N}) where {N}
return operate_to_fallback!(mutability(output, op, args...), output, op, args...)
end
function operate_to_fallback!(
::NotMutable,
output,
op::Function,
args::Vararg{Any,N},
) where {N}
return operate(op, args...)
end
function operate_to_fallback!(
::IsMutable,
output,
op::Function,
args::Vararg{Any,N},
) where {N}
return mutable_operate_to!(output, op, args...)
end
"""
operate!(op::Function, args...)
Returns the value of `op(args...)`, possibly modifying `args[1]`.
"""
function operate!(op::Function, args::Vararg{Any,N}) where {N}
return operate_fallback!(mutability(args[1], op, args...), op, args...)
end
function operate_fallback!(::NotMutable, op::Function, args::Vararg{Any,N}) where {N}
return operate(op, args...)
end
function operate_fallback!(::IsMutable, op::Function, args::Vararg{Any,N}) where {N}
return mutable_operate!(op, args...)
end
"""
buffered_operate_to!(buffer, output, op::Function, args...)
Returns the value of `op(args...)`, possibly modifying `buffer` and `output`.
"""
function buffered_operate_to!(buffer, output, op::Function, args::Vararg{Any,N}) where {N}
return buffered_operate_to_fallback!(
mutability(output, op, args...),
buffer,
output,
op,
args...,
)
end
function buffered_operate_to_fallback!(
::NotMutable,
buffer,
output,
op::Function,
args::Vararg{Any,N},
) where {N}
return operate(op, args...)
end
function buffered_operate_to_fallback!(
::IsMutable,
buffer,
output,
op::Function,
args::Vararg{Any,N},
) where {N}
return mutable_buffered_operate_to!(buffer, output, op, args...)
end
"""
buffered_operate!(buffer, op::Function, args...)
Returns the value of `op(args...)`, possibly modifying `buffer`.
"""
function buffered_operate!(buffer, op::Function, args::Vararg{Any,N}) where {N}
return buffered_operate_fallback!(mutability(args[1], op, args...), buffer, op, args...)
end
function buffered_operate_fallback!(
::NotMutable,
buffer,
op::Function,
args::Vararg{Any,N},
) where {N}
return operate(op, args...)
end
function buffered_operate_fallback!(
::IsMutable,
buffer,
op::Function,
args::Vararg{Any,N},
) where {N}
return mutable_buffered_operate!(buffer, op, args...)
end
# For most types, `dot(b, c) = adjoint(b) * c`.
promote_operation(::typeof(adjoint), a::Type) = a
function promote_operation(::typeof(LinearAlgebra.dot), b::Type, c::Type)
return promote_operation(*, promote_operation(adjoint, b), c)
end
function buffer_for(::typeof(add_dot), a::Type, b::Type, c::Type)
return buffer_for(add_mul, a, promote_operation(adjoint, b), c)
end
function mutable_operate_to_fallback(::IsMutable, output, ::typeof(add_dot), a, b, c) where {N}
return mutable_operate_to!(output, add_mul, a, adjoint(b), c)
end
function mutable_operate_fallback(::IsMutable, ::typeof(add_dot), a, b, c) where {N}
return mutable_operate!(add_mul, a, adjoint(b), c)
end
function mutable_buffered_operate_to_fallback(::IsMutable, buffer, output, ::typeof(add_dot), a, b, c)
return mutable_buffered_operate_to!(buffer, output, add_mul, a, adjoint(b), c)
end
function mutable_buffered_operate_fallback(::IsMutable, buffer, ::typeof(add_dot), a, b, c)
return mutable_buffered_operate!(buffer, add_mul, a, adjoint(b), c)
end