|
193 | 193 | end
|
194 | 194 | end
|
195 | 195 |
|
| 196 | + |
196 | 197 | @testset "sparse map/broadcast with result eltype not a concrete subtype of Number (#19561/#19589)" begin
|
197 | 198 | intoneorfloatzero(x) = x != 0.0 ? Int(1) : Float64(x)
|
198 | 199 | stringorfloatzero(x) = x != 0.0 ? "Hello" : Float64(x)
|
|
202 | 203 | @test broadcast(stringorfloatzero, speye(4)) == sparse(broadcast(stringorfloatzero, eye(4)))
|
203 | 204 | end
|
204 | 205 |
|
| 206 | +@testset "broadcast[!] over combinations of scalars and sparse vectors/matrices" begin |
| 207 | + N, M, p = 10, 12, 0.5 |
| 208 | + elT = Float64 |
| 209 | + s = Float32(2.0) |
| 210 | + V = sprand(elT, N, p) |
| 211 | + A = sprand(elT, N, M, p) |
| 212 | + fV, fA = Array(V), Array(A) |
| 213 | + # test combinations involving one to three scalars and one to five sparse vectors/matrices |
| 214 | + spargseq, dargseq = Iterators.cycle((A, V)), Iterators.cycle((fA, fV)) |
| 215 | + for nargs in 1:5 # number of tensor arguments |
| 216 | + nargsl = cld(nargs, 2) # number in "left half" of tensor arguments |
| 217 | + nargsr = fld(nargs, 2) # number in "right half" of tensor arguments |
| 218 | + spargsl = tuple(Iterators.take(spargseq, nargsl)...) # "left half" of tensor args |
| 219 | + spargsr = tuple(Iterators.take(spargseq, nargsr)...) # "right half" of tensor args |
| 220 | + dargsl = tuple(Iterators.take(dargseq, nargsl)...) # "left half" of tensor args, densified |
| 221 | + dargsr = tuple(Iterators.take(dargseq, nargsr)...) # "right half" of tensor args, densified |
| 222 | + for (sparseargs, denseargs) in ( # argument combinations including scalars |
| 223 | + # a few combinations involving one scalar |
| 224 | + ((s, spargsl..., spargsr...), (s, dargsl..., dargsr...)), |
| 225 | + ((spargsl..., s, spargsr...), (dargsl..., s, dargsr...)), |
| 226 | + ((spargsl..., spargsr..., s), (dargsl..., dargsr..., s)), |
| 227 | + # a few combinations involving two scalars |
| 228 | + ((s, spargsl..., s, spargsr...), (s, dargsl..., s, dargsr...)), |
| 229 | + ((s, spargsl..., spargsr..., s), (s, dargsl..., dargsr..., s)), |
| 230 | + ((spargsl..., s, spargsr..., s), (dargsl..., s, dargsr..., s)), |
| 231 | + ((s, s, spargsl..., spargsr...), (s, s, dargsl..., dargsr...)), |
| 232 | + ((spargsl..., s, s, spargsr...), (dargsl..., s, s, dargsr...)), |
| 233 | + ((spargsl..., spargsr..., s, s), (dargsl..., dargsr..., s, s)), |
| 234 | + # a few combinations involving three scalars |
| 235 | + ((s, spargsl..., s, spargsr..., s), (s, dargsl..., s, dargsr..., s)), |
| 236 | + ((s, spargsl..., s, s, spargsr...), (s, dargsl..., s, s, dargsr...)), |
| 237 | + ((spargsl..., s, s, spargsr..., s), (dargsl..., s, s, dargsr..., s)), |
| 238 | + ((spargsl..., s, s, s, spargsr...), (dargsl..., s, s, s, dargsr...)), ) |
| 239 | + # test broadcast entry point |
| 240 | + @test broadcast(*, sparseargs...) == sparse(broadcast(*, denseargs...)) |
| 241 | + @test isa(@inferred(broadcast(*, sparseargs...)), SparseMatrixCSC{elT}) |
| 242 | + # test broadcast! entry point |
| 243 | + fX = broadcast(*, sparseargs...); X = sparse(fX) |
| 244 | + @test broadcast!(*, X, sparseargs...) == sparse(broadcast!(*, fX, denseargs...)) |
| 245 | + @test isa(@inferred(broadcast!(*, X, sparseargs...)), SparseMatrixCSC{elT}) |
| 246 | + X = sparse(fX) # reset / warmup for @allocated test |
| 247 | + @test_broken (@allocated broadcast!(*, X, sparseargs...)) == 0 |
| 248 | + # This test (and the analog below) fails for three reasons: |
| 249 | + # (1) In all cases, generating the closures that capture the scalar arguments |
| 250 | + # results in allocation, not sure why. |
| 251 | + # (2) In some cases, though _broadcast_eltype (which wraps _return_type) |
| 252 | + # consistently provides the correct result eltype when passed the closure |
| 253 | + # that incorporates the scalar arguments to broadcast (and, with #19667, |
| 254 | + # is inferable, so the overall return type from broadcast is inferred), |
| 255 | + # in some cases inference seems unable to determine the return type of |
| 256 | + # direct calls to that closure. This issue causes variables in both the |
| 257 | + # broadcast[!] entry points (fofzeros = f(_zeros_eltypes(args...)...)) and |
| 258 | + # the driver routines (Cx in _map_zeropres! and _broadcast_zeropres!) to have |
| 259 | + # inferred type Any, resulting in allocation and lackluster performance. |
| 260 | + # (3) The sparseargs... splat in the call above allocates a bit, but of course |
| 261 | + # that issue is negligible and perhaps could be accounted for in the test. |
| 262 | + end |
| 263 | + end |
| 264 | + # test combinations at the limit of inference (eight arguments net) |
| 265 | + for (sparseargs, denseargs) in ( |
| 266 | + ((s, s, s, A, s, s, s, s), (s, s, s, fA, s, s, s, s)), # seven scalars, one sparse matrix |
| 267 | + ((s, s, V, s, s, A, s, s), (s, s, fV, s, s, fA, s, s)), # six scalars, two sparse vectors/matrices |
| 268 | + ((s, s, V, s, A, s, V, s), (s, s, fV, s, fA, s, fV, s)), # five scalars, three sparse vectors/matrices |
| 269 | + ((s, V, s, A, s, V, s, A), (s, fV, s, fA, s, fV, s, fA)), # four scalars, four sparse vectors/matrices |
| 270 | + ((s, V, A, s, V, A, s, A), (s, fV, fA, s, fV, fA, s, fA)), # three scalars, five sparse vectors/matrices |
| 271 | + ((V, A, V, s, A, V, A, s), (fV, fA, fV, s, fA, fV, fA, s)), # two scalars, six sparse vectors/matrices |
| 272 | + ((V, A, V, A, s, V, A, V), (fV, fA, fV, fA, s, fV, fA, fV)) ) # one scalar, seven sparse vectors/matrices |
| 273 | + # test broadcast entry point |
| 274 | + @test broadcast(*, sparseargs...) == sparse(broadcast(*, denseargs...)) |
| 275 | + @test isa(@inferred(broadcast(*, sparseargs...)), SparseMatrixCSC{elT}) |
| 276 | + # test broadcast! entry point |
| 277 | + fX = broadcast(*, sparseargs...); X = sparse(fX) |
| 278 | + @test broadcast!(*, X, sparseargs...) == sparse(broadcast!(*, fX, denseargs...)) |
| 279 | + @test isa(@inferred(broadcast!(*, X, sparseargs...)), SparseMatrixCSC{elT}) |
| 280 | + X = sparse(fX) # reset / warmup for @allocated test |
| 281 | + @test_broken (@allocated broadcast!(*, X, sparseargs...)) == 0 |
| 282 | + # please see the note a few lines above re. this @test_broken |
| 283 | + end |
| 284 | +end |
| 285 | + |
205 | 286 | # Older tests of sparse broadcast, now largely covered by the tests above
|
206 | 287 | @testset "assorted tests of sparse broadcast over two input arguments" begin
|
207 | 288 | N, p = 10, 0.3
|
|
0 commit comments