forked from PTsolvers/Galileo23-MC1-GPU
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffusion_1D_enzyme_cuda.jl
More file actions
78 lines (61 loc) · 1.58 KB
/
diffusion_1D_enzyme_cuda.jl
File metadata and controls
78 lines (61 loc) · 1.58 KB
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
using Enzyme
using CUDA
function residual_1!(R,C,dc,dx)
ix = (blockIdx().x-1)*blockDim().x + threadIdx().x
??
return
end
function grad_residual_1!(R̄,C̄,R,C,dc,dx)
Enzyme.autodiff_deferred(Reverse,residual_1!,Duplicated(R,R̄),Duplicated(C,C̄),Const(dc),Const(dx))
return
end
function update_q!(qx,C,dc,dx)
ix = (blockIdx().x-1)*blockDim().x + threadIdx().x
??
return
end
function grad_update_q!(q̄x,C̄,qx,C,dc,dx)
??
return
end
function update_R!(R,qx,dx)
ix = (blockIdx().x-1)*blockDim().x + threadIdx().x
??
return
end
function grad_update_R!(R̄,q̄x,R,qx,dx)
??
return
end
function residual!(R,C,qx,dc,dx,nblocks,nthreads)
??
return
end
function grad_residual!(R̄,q̄x,C̄,R,C,qx,dc,dx,nblocks,nthreads)
??
return
end
function main()
nx = 10
C = CUDA.rand(Float64,nx)
C̄ = CUDA.zeros(Float64,nx)
C̄_1 = CUDA.zeros(Float64,nx)
qx = CUDA.zeros(Float64,nx-1)
q̄x = CUDA.zeros(Float64,nx-1)
R = CUDA.zeros(Float64,nx)
R̄ = CUDA.ones(Float64,nx)
R̄_1 = CUDA.ones(Float64,nx)
dx = 1.0/nx
dc = 1.0
nthreads = 256
nblocks = cld(nx,nthreads)
CUDA.@sync @cuda threads=nthreads blocks=nblocks residual_1!(R,C,dc,dx)
CUDA.@sync @cuda threads=nthreads blocks=nblocks grad_residual_1!(R̄_1,C̄_1,R,C,dc,dx)
CUDA.@sync @cuda threads=nthreads blocks=nblocks update_q!(qx,C,dc,dx)
CUDA.@sync @cuda threads=nthreads blocks=nblocks update_R!(R,qx,dx)
residual!(??)
grad_residual!(??)
@assert C̄ ≈ C̄_1
return
end
main()