|
1 | | -using LinearAlgebra, OrdinaryDiffEq, Test, PreallocationTools, CUDA |
| 1 | +using LinearAlgebra, OrdinaryDiffEq, Test, PreallocationTools, CUDA, ForwardDiff, ArrayInterface |
2 | 2 |
|
3 | 3 | #Dispatch tests |
| 4 | +chunk_size = 5 |
4 | 5 | u0_CU = cu(ones(5,5)) |
5 | | -dual_CU = cu(zeros(ForwardDiff.Dual{ForwardDiff.Tag{typeof(something), Float64}, Float64, chunk_size}, 2, 2)) |
| 6 | +dual_CU = cu(zeros(ForwardDiff.Dual{ForwardDiff.Tag{typeof(something), Float32}, Float32, chunk_size}, 2, 2)) |
| 7 | +dual_N = ForwardDiff.Dual{ForwardDiff.Tag{typeof(something), Float32}, Float32, 5}(0) |
6 | 8 | cache_CU = dualcache(u0_CU, chunk_size) |
7 | 9 | tmp_du_CUA = get_tmp(cache_CU, u0_CU) |
8 | 10 | tmp_dual_du_CUA = get_tmp(cache_CU, dual_CU) |
9 | | -tmp_du_CUN = get_tmp(cache_CU, u0_CU[1]) |
10 | | -tmp_dual_du_CUN = get_tmp(cache_CU, dual_CU[1]) |
11 | | -@test typeof(cache_CU.dual_du) == typeof(u0_CU) #check that dual cache array is a GPU array for performance reasons. |
| 11 | +tmp_du_CUN = get_tmp(cache_CU, 0.0) |
| 12 | +tmp_dual_du_CUN = get_tmp(cache_CU, dual_N) |
| 13 | +@test ArrayInterface.parameterless_type(typeof(cache_CU.dual_du)) == ArrayInterface.parameterless_type(typeof(u0_CU)) #check that dual cache array is a GPU array for performance reasons. |
12 | 14 | @test size(tmp_du_CUA) == size(u0_CU) |
13 | 15 | @test typeof(tmp_du_CUA) == typeof(u0_CU) |
14 | 16 | @test eltype(tmp_du_CUA) == eltype(u0_CU) |
|
33 | 35 | chunk_size = 10 |
34 | 36 | u0 = cu(rand(10,10)) #example kept small for test purposes. |
35 | 37 | A = cu(-randn(10,10)) |
36 | | -cache = dualcache(A, chunk_size) |
37 | | -prob = ODEProblem(foo, u0, (0.0f0,1.0f0), (A, cache)) |
| 38 | +cache = dualcache(cu(zeros(10, 10)), chunk_size) |
| 39 | +prob = ODEProblem(foo, u0, (0.0f0, 1.0f0), (A, cache)) |
38 | 40 | sol = solve(prob, TRBDF2(chunk_size = chunk_size)) |
39 | 41 | @test sol.retcode == :Success |
40 | 42 |
|
41 | 43 | #with auto-detected chunk_size |
42 | 44 | u0 = cu(rand(10,10)) #example kept small for test purposes. |
43 | 45 | A = cu(-randn(10,10)) |
44 | | -cache = dualcache(A) |
| 46 | +cache = dualcache(cu(zeros(10, 10))) |
45 | 47 | prob = ODEProblem(foo, u0, (0.0f0,1.0f0), (A, cache)) |
46 | 48 | sol = solve(prob, TRBDF2()) |
47 | 49 | @test sol.retcode == :Success |
48 | 50 |
|
| 51 | +#resizing tests |
49 | 52 | randmat = cu(rand(5, 3)) |
50 | 53 | sto = similar(randmat) |
51 | 54 | stod = dualcache(sto) |
52 | 55 | function claytonsample!(sto, τ, α; randmat=randmat) |
53 | 56 | sto = get_tmp(sto, τ) |
54 | | - sto .= randmat |
| 57 | + sto .= randmat |
55 | 58 | τ == 0 && return sto |
56 | | - n = size(sto, 1) |
57 | | - for i in 1:n |
58 | | - v = sto[i, 2] |
59 | | - u = sto[i, 1] |
60 | | - sto[i, 1] = (1 - u^(-τ) + u^(-τ)*v^(-(τ/(1 + τ))))^(-1/τ)*α |
61 | | - sto[i, 2] = (1 - u^(-τ) + u^(-τ)*v^(-(τ/(1 + τ))))^(-1/τ) |
62 | | - end |
| 59 | + v = @view sto[:, 2] |
| 60 | + u = @view sto[:, 1] |
| 61 | + @. v = (1 - u^(-τ) + u^(-τ)*v^(-(τ/(1 + τ))))^(-1/τ)*α |
| 62 | + @. u = (1 - u^(-τ) + u^(-τ)*v^(-(τ/(1 + τ))))^(-1/τ) |
63 | 63 | return sto |
64 | 64 | end |
65 | 65 |
|
66 | | -#resizing tests |
67 | 66 | #taking the derivative of claytonsample! with respect to τ only |
68 | 67 | df1 = ForwardDiff.derivative(τ -> claytonsample!(stod, τ, 0.0), 0.3) |
69 | 68 | @test size(randmat) == size(df1) |
|
0 commit comments