Last active
January 9, 2020 04:32
-
-
Save matsueushi/be3071f6b6be040dd7ae9e51cf74b1e5 to your computer and use it in GitHub Desktop.
gpu, similar, cpu, adapt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Adapt | |
using CuArrays | |
using Flux | |
using InteractiveUtils | |
function f1(x) | |
fill(1f0, size(x)) |> gpu | |
end | |
function f2(x) | |
fill!(similar(x), 1f0) | |
end | |
function f3(x) | |
fill(1f0, size(x)) |> cpu | |
end | |
function f4(x) | |
adapt(Array, fill(1f0, size(x))) | |
end | |
function f5(x) | |
cu(fill(1f0, size(x))) | |
end | |
function main() | |
x = randn(Float32, 32, 32, 3, 32) |> gpu | |
n = 10^4 | |
f1(x) | |
@info "Case1" typeof(f1(x)) | |
@time for i in 1:n f1(x) end | |
@code_warntype f1(x) | |
println() | |
f2(x) | |
@info "Case2" typeof(f2(x)) | |
@time for i in 1:n f2(x) end | |
@code_warntype f2(x) | |
println() | |
f3(x) | |
@info "Case3" typeof(f3(x)) | |
@time for i in 1:n f3(x) end | |
@code_warntype f3(x) | |
println() | |
f4(x) | |
@info "Case4" typeof(f4(x)) | |
@time for i in 1:n f4(x) end | |
@code_warntype f4(x) | |
println() | |
f5(x) | |
@info "Case5" typeof(f5(x)) | |
@time for i in 1:n f5(x) end | |
@code_warntype f5(x) | |
end | |
main() |
GPU
┌ Info: Case1
└ typeof(f1(x)) = CuArray{Float32,4,Nothing}
1.798704 seconds (201.18 k allocations: 7.334 GiB, 20.54% gc time)
Variables
#self#::Core.Compiler.Const(f1, false)
x::CuArray{Float32,4,Nothing}
Body::ANY
1 ─ %1 = Main.size(x)::NTuple{4,Int64}
│ %2 = Main.fill(1.0f0, %1)::Array{Float32,4}
│ %3 = (%2 |> Main.gpu)::ANY
└── return %3
┌ Info: Case2
└ typeof(f2(x)) = CuArray{Float32,4,Nothing}
1.250865 seconds (130.00 k allocations: 4.765 MiB)
Variables
#self#::Core.Compiler.Const(f2, false)
x::CuArray{Float32,4,Nothing}
Body::CuArray{Float32,4,Nothing}
1 ─ %1 = Main.similar(x)::CuArray{Float32,4,Nothing}
│ %2 = Main.fill!(%1, 1.0f0)::CuArray{Float32,4,Nothing}
└── return %2
┌ Info: Case3
└ typeof(f3(x)) = Array{Float32,4}
0.668655 seconds (90.02 k allocations: 3.668 GiB, 26.45% gc time)
Variables
#self#::Core.Compiler.Const(f3, false)
x::CuArray{Float32,4,Nothing}
Body::ANY
1 ─ %1 = Main.size(x)::NTuple{4,Int64}
│ %2 = Main.fill(1.0f0, %1)::Array{Float32,4}
│ %3 = (%2 |> Main.cpu)::ANY
└── return %3
┌ Info: Case4
└ typeof(f4(x)) = Array{Float32,4}
0.659487 seconds (30.00 k allocations: 3.663 GiB, 25.06% gc time)
Variables
#self#::Core.Compiler.Const(f4, false)
x::CuArray{Float32,4,Nothing}
Body::Array{Float32,4}
1 ─ %1 = Main.size(x)::NTuple{4,Int64}
│ %2 = Main.fill(1.0f0, %1)::Array{Float32,4}
│ %3 = Main.adapt(Main.Array, %2)::Array{Float32,4}
└── return %3
┌ Info: Case5
└ typeof(f5(x)) = CuArray{Float32,4,Nothing}
1.932783 seconds (180.67 k allocations: 7.330 GiB, 19.17% gc time)
Variables
#self#::Core.Compiler.Const(f5, false)
x::CuArray{Float32,4,Nothing}
Body::UNION{ARRAY{FLOAT32,4}, CUARRAY{FLOAT32,4,NOTHING}}
1 ─ %1 = Main.size(x)::NTuple{4,Int64}
│ %2 = Main.fill(1.0f0, %1)::Array{Float32,4}
│ %3 = Main.cu(%2)::UNION{ARRAY{FLOAT32,4}, CUARRAY{FLOAT32,4,NOTHING}}
└── return %3
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CPU