|
| 1 | +#= |
| 2 | +julia --project=.buildkite |
| 3 | +using Revise; include(joinpath("benchmarks", "scripts", "benchmark_field_last.jl")) |
| 4 | +
|
| 5 | +# Info |
| 6 | +
|
| 7 | +# Benchmark results: |
| 8 | +
|
| 9 | +Clima A100: |
| 10 | +``` |
| 11 | +Kernel `add3(x1, x2, x3) = x1+x2+x3` and `n_reads_writes=4`: |
| 12 | +[ Info: ArrayType = CuArray |
| 13 | +Problem size: (63, 4, 4, 5400, 1), float_type = Float32, device_bandwidth_GBs=2039 |
| 14 | +┌─────────────────────────────────────────────────────────────────────┬──────────────────────────────────┬─────────┬─────────────┬────────────────┬────────┐ |
| 15 | +│ funcs │ time per call │ bw % │ achieved bw │ n-reads/writes │ n-reps │ |
| 16 | +├─────────────────────────────────────────────────────────────────────┼──────────────────────────────────┼─────────┼─────────────┼────────────────┼────────┤ |
| 17 | +│ FLD.aos_cart_offset!(X_aos_ref, Y_aos_ref, us; bm, nreps = 100) │ 72 microseconds, 899 nanoseconds │ 54.568 │ 1112.64 │ 4 │ 100 │ |
| 18 | +│ FLD.aos_lin_offset!(X_aos, Y_aos, us; bm, nreps = 100) │ 56 microseconds, 259 nanoseconds │ 70.708 │ 1441.74 │ 4 │ 100 │ |
| 19 | +│ FLD.soa_linear_index!(X_soa, Y_soa, us; bm, nreps = 100) │ 56 microseconds, 515 nanoseconds │ 70.3877 │ 1435.21 │ 4 │ 100 │ |
| 20 | +│ FLD.soa_cart_index!(X_soa, Y_soa, us; bm, nreps = 100) │ 67 microseconds, 462 nanoseconds │ 58.9663 │ 1202.32 │ 4 │ 100 │ |
| 21 | +└─────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴─────────┴─────────────┴────────────────┴────────┘ |
| 22 | +
|
| 23 | +Kernel `add3(x1, x2, x3) = x1+x2+x3` and `n_reads_writes=4`: |
| 24 | +[ Info: ArrayType = CuArray |
| 25 | +Problem size: (63, 4, 4, 5400, 1), float_type = Float64, device_bandwidth_GBs=2039 |
| 26 | +┌─────────────────────────────────────────────────────────────────────┬───────────────────────────────────┬─────────┬─────────────┬────────────────┬────────┐ |
| 27 | +│ funcs │ time per call │ bw % │ achieved bw │ n-reads/writes │ n-reps │ |
| 28 | +├─────────────────────────────────────────────────────────────────────┼───────────────────────────────────┼─────────┼─────────────┼────────────────┼────────┤ |
| 29 | +│ FLD.aos_cart_offset!(X_aos_ref, Y_aos_ref, us; bm, nreps = 100) │ 106 microseconds, 783 nanoseconds │ 74.5051 │ 1519.16 │ 4 │ 100 │ |
| 30 | +│ FLD.aos_lin_offset!(X_aos, Y_aos, us; bm, nreps = 100) │ 102 microseconds, 472 nanoseconds │ 77.6396 │ 1583.07 │ 4 │ 100 │ |
| 31 | +│ FLD.soa_linear_index!(X_soa, Y_soa, us; bm, nreps = 100) │ 102 microseconds, 523 nanoseconds │ 77.6008 │ 1582.28 │ 4 │ 100 │ |
| 32 | +│ FLD.soa_cart_index!(X_soa, Y_soa, us; bm, nreps = 100) │ 106 microseconds, 834 nanoseconds │ 74.4694 │ 1518.43 │ 4 │ 100 │ |
| 33 | +└─────────────────────────────────────────────────────────────────────┴───────────────────────────────────┴─────────┴─────────────┴────────────────┴────────┘ |
| 34 | +
|
| 35 | +Kernel `add3(x1, x2, x3) = x1` and `n_reads_writes=2`: |
| 36 | +[ Info: ArrayType = CuArray |
| 37 | +Problem size: (63, 4, 4, 5400, 1), float_type = Float32, device_bandwidth_GBs=2039 |
| 38 | +┌─────────────────────────────────────────────────────────────────────┬──────────────────────────────────┬─────────┬─────────────┬────────────────┬────────┐ |
| 39 | +│ funcs │ time per call │ bw % │ achieved bw │ n-reads/writes │ n-reps │ |
| 40 | +├─────────────────────────────────────────────────────────────────────┼──────────────────────────────────┼─────────┼─────────────┼────────────────┼────────┤ |
| 41 | +│ FLD.aos_cart_offset!(X_aos_ref, Y_aos_ref, us; bm, nreps = 100) │ 61 microseconds, 185 nanoseconds │ 32.5079 │ 662.837 │ 2 │ 100 │ |
| 42 | +│ FLD.aos_lin_offset!(X_aos, Y_aos, us; bm, nreps = 100) │ 31 microseconds, 376 nanoseconds │ 63.3926 │ 1292.57 │ 2 │ 100 │ |
| 43 | +│ FLD.soa_linear_index!(X_soa, Y_soa, us; bm, nreps = 100) │ 31 microseconds, 120 nanoseconds │ 63.9141 │ 1303.21 │ 2 │ 100 │ |
| 44 | +│ FLD.soa_cart_index!(X_soa, Y_soa, us; bm, nreps = 100) │ 44 microseconds, 53 nanoseconds │ 45.1499 │ 920.607 │ 2 │ 100 │ |
| 45 | +└─────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴─────────┴─────────────┴────────────────┴────────┘ |
| 46 | +``` |
| 47 | +
|
| 48 | +# CPU (Mac M1) |
| 49 | +``` |
| 50 | +[ Info: ArrayType = identity |
| 51 | +Problem size: (63, 4, 4, 5400, 1), float_type = Float32, device_bandwidth_GBs=2039 |
| 52 | +┌─────────────────────────────────────────────────────────────────────┬───────────────────────────────────┬──────────┬─────────────┬────────────────┬────────┐ |
| 53 | +│ funcs │ time per call (CPU) │ bw % │ achieved bw │ n-reads/writes │ n-reps │ |
| 54 | +├─────────────────────────────────────────────────────────────────────┼───────────────────────────────────┼──────────┼─────────────┼────────────────┼────────┤ |
| 55 | +│ FLD.aos_cart_offset!(X_aos_ref, Y_aos_ref, us; bm, nreps = 100) │ 16 milliseconds, 494 microseconds │ 0.241171 │ 4.91747 │ 4 │ 100 │ |
| 56 | +│ FLD.aos_lin_offset!(X_aos, Y_aos, us; bm, nreps = 100) │ 783 microseconds, 256 nanoseconds │ 5.07871 │ 103.555 │ 4 │ 100 │ |
| 57 | +│ FLD.soa_linear_index!(X_soa, Y_soa, us; bm, nreps = 100) │ 790 microseconds, 894 nanoseconds │ 5.02966 │ 102.555 │ 4 │ 100 │ |
| 58 | +│ FLD.soa_cart_index!(X_soa, Y_soa, us; bm, nreps = 100) │ 12 milliseconds, 522 microseconds │ 0.317663 │ 6.47714 │ 4 │ 100 │ |
| 59 | +└─────────────────────────────────────────────────────────────────────┴───────────────────────────────────┴──────────┴─────────────┴────────────────┴────────┘ |
| 60 | +``` |
| 61 | +
|
| 62 | +=# |
| 63 | + |
| 64 | +#! format: off |
| 65 | +module BenchmarkFieldLastIndex |
| 66 | + |
| 67 | +include("benchmark_utils.jl") |
| 68 | + |
| 69 | +@inline function const_linear_index(us::UniversalSizesStatic, I, field_index) |
| 70 | + n = (get_Nv(us), get_Nij(us), get_Nij(us), get_Nh(us), 1) |
| 71 | + i = I + prod(n)*field_index |
| 72 | + return i |
| 73 | +end |
| 74 | + |
| 75 | +@inline function const_linear_index_reference(us::UniversalSizesStatic, I, field_index) |
| 76 | + CI = CartesianIndices((get_Nv(us), get_Nij(us), get_Nij(us), get_Nh(us), 1)) |
| 77 | + LI = LinearIndices((get_Nv(us), get_Nij(us), get_Nij(us), get_Nh(us), field_index+1)) |
| 78 | + return LI[CI[I] + CartesianIndex((0, 0, 0, 0, field_index))] |
| 79 | +end |
| 80 | + |
| 81 | +# add3(x1, x2, x3) = x1 + x2 + x3 |
| 82 | +add3(x1, x2, x3) = x1 |
| 83 | + |
| 84 | +function aos_cart_offset!(X, Y, us; nreps = 1, bm=nothing, n_trials = 30) |
| 85 | + if Y isa Array |
| 86 | + e = Inf |
| 87 | + CI = CartesianIndices((get_Nv(us), get_Nij(us), get_Nij(us), get_Nh(us), 1)) |
| 88 | + for t in 1:n_trials |
| 89 | + et = Base.@elapsed begin |
| 90 | + for i in 1:nreps |
| 91 | + @inbounds @simd for I in 1:get_N(us) |
| 92 | + CI1 = CI[I] |
| 93 | + CI2 = CI1 + CartesianIndex((0, 0, 0, 0, 1)) |
| 94 | + CI3 = CI1 + CartesianIndex((0, 0, 0, 0, 2)) |
| 95 | + Y[CI1] = add3(X[CI1], X[CI2], X[CI3]) |
| 96 | + end |
| 97 | + end |
| 98 | + end |
| 99 | + e = min(e, et) |
| 100 | + end |
| 101 | + else |
| 102 | + e = Inf |
| 103 | + kernel = CUDA.@cuda always_inline = true launch = false aos_cart_offset_kernel!(X,Y,us) |
| 104 | + config = CUDA.launch_configuration(kernel.fun) |
| 105 | + threads = min(get_N(us), config.threads) |
| 106 | + blocks = cld(get_N(us), threads) |
| 107 | + for t in 1:n_trials |
| 108 | + et = CUDA.@elapsed begin |
| 109 | + for i in 1:nreps # reduce variance / impact of launch latency |
| 110 | + kernel(X,Y,us; threads, blocks) |
| 111 | + end |
| 112 | + end |
| 113 | + e = min(e, et) |
| 114 | + end |
| 115 | + end |
| 116 | + push_info(bm; e, nreps, caller = @caller_name(@__FILE__),n_reads_writes=4) |
| 117 | + return nothing |
| 118 | +end; |
| 119 | +function aos_cart_offset_kernel!(X, Y, us) |
| 120 | + @inbounds begin |
| 121 | + I = (CUDA.blockIdx().x - Int32(1)) * CUDA.blockDim().x + CUDA.threadIdx().x |
| 122 | + if I ≤ get_N(us) |
| 123 | + n = (get_Nv(us), get_Nij(us), get_Nij(us), get_Nh(us), 1) |
| 124 | + CI1 = CartesianIndices(map(x -> Base.OneTo(x), n))[I] |
| 125 | + CI2 = CI1 + CartesianIndex((0, 0, 0, 0, 1)) |
| 126 | + CI3 = CI1 + CartesianIndex((0, 0, 0, 0, 2)) |
| 127 | + Y[CI1] = add3(X[CI1], X[CI2], X[CI3]) |
| 128 | + end |
| 129 | + end |
| 130 | + return nothing |
| 131 | +end; |
| 132 | + |
| 133 | +function aos_lin_offset!(X, Y, us; nreps = 1, bm=nothing, n_trials = 30) |
| 134 | + if Y isa Array |
| 135 | + e = Inf |
| 136 | + for t in 1:n_trials |
| 137 | + et = Base.@elapsed begin |
| 138 | + for i in 1:nreps |
| 139 | + @inbounds @simd for I in 1:get_N(us) |
| 140 | + LY1 = const_linear_index(us, I, 0) |
| 141 | + LX1 = const_linear_index(us, I, 0) |
| 142 | + LX2 = const_linear_index(us, I, 1) |
| 143 | + LX3 = const_linear_index(us, I, 2) |
| 144 | + Y[LY1] = add3(X[LX1], X[LX2], X[LX3]) |
| 145 | + end |
| 146 | + end |
| 147 | + end |
| 148 | + e = min(e, et) |
| 149 | + end |
| 150 | + else |
| 151 | + e = Inf |
| 152 | + kernel = CUDA.@cuda always_inline = true launch = false aos_lin_offset_kernel!(X,Y,us) |
| 153 | + config = CUDA.launch_configuration(kernel.fun) |
| 154 | + threads = min(get_N(us), config.threads) |
| 155 | + blocks = cld(get_N(us), threads) |
| 156 | + for t in 1:n_trials |
| 157 | + et = CUDA.@elapsed begin |
| 158 | + for i in 1:nreps |
| 159 | + kernel(X,Y,us; threads, blocks) |
| 160 | + end |
| 161 | + end |
| 162 | + e = min(e, et) |
| 163 | + end |
| 164 | + end |
| 165 | + push_info(bm; e, nreps, caller = @caller_name(@__FILE__),n_reads_writes=4) |
| 166 | + return nothing |
| 167 | +end; |
| 168 | +function aos_lin_offset_kernel!(X, Y, us) |
| 169 | + @inbounds begin |
| 170 | + I = (CUDA.blockIdx().x - Int32(1)) * CUDA.blockDim().x + CUDA.threadIdx().x |
| 171 | + if I ≤ get_N(us) |
| 172 | + LY1 = const_linear_index(us, I, 0) |
| 173 | + LX1 = const_linear_index(us, I, 0) |
| 174 | + LX2 = const_linear_index(us, I, 1) |
| 175 | + LX3 = const_linear_index(us, I, 2) |
| 176 | + Y[LY1] = add3(X[LX1], X[LX2], X[LX3]) |
| 177 | + end |
| 178 | + end |
| 179 | + return nothing |
| 180 | +end; |
| 181 | + |
| 182 | +function soa_cart_index!(X, Y, us; nreps = 1, bm=nothing, n_trials = 30) |
| 183 | + e = Inf |
| 184 | + if first(Y) isa Array |
| 185 | + CI = CartesianIndices((get_Nv(us), get_Nij(us), get_Nij(us), get_Nh(us))) |
| 186 | + for t in 1:n_trials |
| 187 | + et = Base.@elapsed begin |
| 188 | + for i in 1:nreps |
| 189 | + (y1,) = Y |
| 190 | + (x1, x2, x3) = X |
| 191 | + @inbounds @simd for I in 1:get_N(us) |
| 192 | + y1[CI[I]] = add3(x1[CI[I]], x2[CI[I]], x3[CI[I]]) |
| 193 | + end |
| 194 | + end |
| 195 | + end |
| 196 | + e = min(e, et) |
| 197 | + end |
| 198 | + else |
| 199 | + kernel = CUDA.@cuda always_inline = true launch = false soa_cart_index_kernel!(X,Y,us) |
| 200 | + config = CUDA.launch_configuration(kernel.fun) |
| 201 | + threads = min(get_N(us), config.threads) |
| 202 | + blocks = cld(get_N(us), threads) |
| 203 | + for t in 1:n_trials |
| 204 | + et = CUDA.@elapsed begin |
| 205 | + for i in 1:nreps # reduce variance / impact of launch latency |
| 206 | + kernel(X,Y,us; threads, blocks) |
| 207 | + end |
| 208 | + end |
| 209 | + e = min(e, et) |
| 210 | + end |
| 211 | + end |
| 212 | + push_info(bm; e, nreps, caller = @caller_name(@__FILE__),n_reads_writes=4) |
| 213 | + return nothing |
| 214 | +end; |
| 215 | +function soa_cart_index_kernel!(X, Y, us) |
| 216 | + @inbounds begin |
| 217 | + I = (CUDA.blockIdx().x - Int32(1)) * CUDA.blockDim().x + CUDA.threadIdx().x |
| 218 | + if I ≤ get_N(us) |
| 219 | + CI = CartesianIndices((get_Nv(us), get_Nij(us), get_Nij(us), get_Nh(us))) |
| 220 | + (y1,) = Y |
| 221 | + (x1, x2, x3) = X |
| 222 | + y1[CI[I]] = add3(x1[CI[I]], x2[CI[I]], x3[CI[I]]) |
| 223 | + end |
| 224 | + end |
| 225 | + return nothing |
| 226 | +end; |
| 227 | + |
| 228 | +function soa_linear_index!(X, Y, us; nreps = 1, bm=nothing, n_trials = 30) |
| 229 | + e = Inf |
| 230 | + if first(Y) isa Array |
| 231 | + for t in 1:n_trials |
| 232 | + et = Base.@elapsed begin |
| 233 | + for i in 1:nreps |
| 234 | + (y1,) = Y |
| 235 | + (x1, x2, x3) = X |
| 236 | + @inbounds @simd for I in 1:get_N(us) |
| 237 | + y1[I] = add3(x1[I], x2[I], x3[I]) |
| 238 | + end |
| 239 | + end |
| 240 | + end |
| 241 | + e = min(e, et) |
| 242 | + end |
| 243 | + else |
| 244 | + kernel = CUDA.@cuda always_inline = true launch = false soa_linear_index_kernel!(X,Y,us) |
| 245 | + config = CUDA.launch_configuration(kernel.fun) |
| 246 | + threads = min(get_N(us), config.threads) |
| 247 | + blocks = cld(get_N(us), threads) |
| 248 | + for t in 1:n_trials |
| 249 | + et = CUDA.@elapsed begin |
| 250 | + for i in 1:nreps # reduce variance / impact of launch latency |
| 251 | + kernel(X,Y,us; threads, blocks) |
| 252 | + end |
| 253 | + end |
| 254 | + e = min(e, et) |
| 255 | + end |
| 256 | + end |
| 257 | + push_info(bm; e, nreps, caller = @caller_name(@__FILE__),n_reads_writes=4) |
| 258 | + return nothing |
| 259 | +end; |
| 260 | +function soa_linear_index_kernel!(X, Y, us) |
| 261 | + @inbounds begin |
| 262 | + I = (CUDA.blockIdx().x - Int32(1)) * CUDA.blockDim().x + CUDA.threadIdx().x |
| 263 | + if I ≤ get_N(us) |
| 264 | + (y1,) = Y |
| 265 | + (x1, x2, x3) = X |
| 266 | + y1[I] = add3(x1[I], x2[I], x3[I]) |
| 267 | + end |
| 268 | + end |
| 269 | + return nothing |
| 270 | +end; |
| 271 | + |
| 272 | +end # module |
| 273 | + |
| 274 | +import .BenchmarkFieldLastIndex as FLD |
| 275 | + |
| 276 | +function fill_with_rand!(arr) |
| 277 | + FT = eltype(arr) |
| 278 | + T = typeof(arr) |
| 279 | + s = size(arr) |
| 280 | + arr .= T(rand(FT, s)) |
| 281 | +end |
| 282 | + |
| 283 | +using CUDA |
| 284 | +using Test |
| 285 | +@testset "Field last dim benchmark" begin |
| 286 | + bm = FLD.Benchmark(;problem_size=(63,4,4,5400,1), float_type=Float32) # size(problem_size, 4) == 1 to avoid double counting reads/writes |
| 287 | + ArrayType = CUDA.CuArray; |
| 288 | + # ArrayType = Base.identity; |
| 289 | + arr(float_type, problem_size, T) = T(zeros(float_type, problem_size...)) |
| 290 | + |
| 291 | + s = (63,4,4,5400,3); |
| 292 | + sY = (63,4,4,5400,1); |
| 293 | + st = (63,4,4,5400); |
| 294 | + ndofs = prod(st); |
| 295 | + us = FLD.UniversalSizesStatic(s[1], s[2], s[end-1]); |
| 296 | + |
| 297 | + X_aos = arr(bm.float_type, s, ArrayType); |
| 298 | + Y_aos = arr(bm.float_type, sY, ArrayType); |
| 299 | + X_aos_ref = arr(bm.float_type, s, ArrayType); |
| 300 | + Y_aos_ref = arr(bm.float_type, sY, ArrayType); |
| 301 | + X_soa = ntuple(_ -> arr(bm.float_type, st, ArrayType), 3); |
| 302 | + Y_soa = ntuple(_ -> arr(bm.float_type, st, ArrayType), 1); |
| 303 | + fill_with_rand!(X_aos) |
| 304 | + fill_with_rand!(Y_aos) |
| 305 | + X_aos_ref .= X_aos |
| 306 | + Y_aos_ref .= Y_aos |
| 307 | + for i in 1:3; X_soa[i] .= X_aos[:,:,:,:, i]; end |
| 308 | + for i in 1:1; Y_soa[i] .= Y_aos[:,:,:,:, i]; end |
| 309 | + @info "ArrayType = $ArrayType" |
| 310 | + |
| 311 | + FLD.aos_cart_offset!(X_aos_ref, Y_aos_ref, us; n_trials = 1, nreps = 1) |
| 312 | + FLD.aos_lin_offset!(X_aos, Y_aos, us; n_trials = 1, nreps = 1) |
| 313 | + FLD.soa_linear_index!(X_soa, Y_soa, us; n_trials = 1, nreps = 1) |
| 314 | + |
| 315 | + @test all(X_aos .== X_aos_ref) |
| 316 | + @test all(Y_aos .== Y_aos_ref) |
| 317 | + for i in 1:3; @test all(X_soa[i] .== X_aos_ref[:,:,:,:,i]); end |
| 318 | + for i in 1:1; @test all(Y_soa[i] .== Y_aos_ref[:,:,:,:,i]); end |
| 319 | + |
| 320 | + FLD.soa_cart_index!(X_soa, Y_soa, us; n_trials = 1, nreps = 1) |
| 321 | + |
| 322 | + for i in 1:3; @test all(X_soa[i] .== X_aos_ref[:,:,:,:,i]); end |
| 323 | + for i in 1:1; @test all(Y_soa[i] .== Y_aos_ref[:,:,:,:,i]); end |
| 324 | + |
| 325 | + FLD.aos_cart_offset!(X_aos_ref, Y_aos_ref, us; bm, nreps = 100) |
| 326 | + FLD.aos_lin_offset!(X_aos, Y_aos, us; bm, nreps = 100) |
| 327 | + FLD.soa_linear_index!(X_soa, Y_soa, us; bm, nreps = 100) |
| 328 | + FLD.soa_cart_index!(X_soa, Y_soa, us; bm, nreps = 100) |
| 329 | + |
| 330 | + FLD.tabulate_benchmark(bm) |
| 331 | +end |
| 332 | + |
| 333 | +# #! format: on |
0 commit comments