Why does GPU addition slows down as the array get larger compared to other methods?

Consider the three functions below which add two arrays sequentially, using threads, and using GPU:

``````using BenchmarkTools
using CUDA

for i in eachindex(y, x)
@inbounds y[i] += x[i]
end
return nothing
end

@inbounds y[i] += x[i]
end
return nothing
end

CUDA.@sync y .+= x
return
end
``````

I ran the code below for array sizes ranging from 10^8 to 5*10^8 and saved the running time:

``````const num_trials = 5
res = zeros((num_trials,3));
for i in range(start=1, step=1, stop = num_trials)
println(i)
N = i*10^8
x_seq = fill(1.0f0, N)  # a vector filled with 1.0 (Float32)
y_seq = fill(2.0f0, N);  # a vector filled with 2.0

x_thred = fill(1.0f0, N)  # a vector filled with 1.0 (Float32)
y_thred = fill(2.0f0, N);  # a vector filled with 2.0

x_d = CUDA.fill(1.0f0, N)  # a vector stored on the GPU filled with 1.0 (Float32)
y_d = CUDA.fill(2.0f0, N);  # a vector stored on the GPU filled with 2.0

res[i, 1] = mean(t_seq).time

res[i,2] = mean(t_thred).time

res[i,3] = mean(t_cuda).time

end
``````

Here is the result, the y axis is time in milli second and the x axis is array size divided by 10^8:

Why does the GPU related computations slows down compared to the other two after some array sizes? Is it because now it takes more time for the data, the array, to go to GPU?

So youβre suggesting that a broadcast operation of 5*10^8 items takes longer on the GPU than it does on the CPU? Thatβs obviously not expected, and I cannot reproduce even on the lowest-end GPU I have. Hereβs the timings on my workstation:

``````julia> N = 5*10^8;

julia> x_seq = fill(1.0f0, N);

julia> y_seq = fill(2.0f0, N);

julia> x_d = CUDA.fill(1.0f0, N);

julia> y_d = CUDA.fill(2.0f0, N);

julia> @benchmark y_seq .+= x_seq
BenchmarkTools.Trial: 29 samples with 1 evaluation.
Range (min β¦ max):  172.270 ms β¦ 180.975 ms  β GC (min β¦ max): 0.00% β¦ 0.00%
Time  (median):     172.435 ms               β GC (median):    0.00%
Time  (mean Β± Ο):   173.179 ms Β±   1.840 ms  β GC (mean Β± Ο):  0.00% Β± 0.00%

ββ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
172 ms           Histogram: frequency by time          181 ms <

Memory estimate: 32 bytes, allocs estimate: 1.

julia> @benchmark CUDA.@sync y_d .+= x_d
BenchmarkTools.Trial: 649 samples with 1 evaluation.
Range (min β¦ max):  7.454 ms β¦   7.958 ms  β GC (min β¦ max): 0.00% β¦ 0.00%
Time  (median):     7.712 ms               β GC (median):    0.00%
Time  (mean Β± Ο):   7.705 ms Β± 118.687 ΞΌs  β GC (mean Β± Ο):  0.00% Β± 0.00%

βββ                β βββ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
7.45 ms         Histogram: frequency by time        7.93 ms <

Memory estimate: 1.42 KiB, allocs estimate: 26.
``````

Since youβre not actually including the time to copy memory, I cannot imagine a GPU broadcast taking longer than a CPU broadcast, especially with large inputs. Did you investigate yourself? What GPU are you using?

2 Likes

I have NVIDIA GeForce RTX 3050 Laptop GPU.
I realized if I run the code for GPU for the input with `N = 5*10^5` after a fresh restart of the notebook the timing is different than the case I run the same code after the inputs `[1,2,3,4]*10^8` in a loop. In the fresh start I get around 100 ms for `N=5*10^8` while in the loop for `N=5*10^8` I get around 350 ms.
Moreover, after a fresh start and running the code for `N=5*10^8` for multiple times every other iteration I get 100 ms and 600 ms. That is the first time 100 ms, the second time 600 ms, the third time 100 ms and so on.

So could it be a driver issue or anything from the GPU?

``````N = 5*10^8
x_d = CUDA.fill(1.0f0, N)  # a vector stored on the GPU filled with 1.0 (Float32)
y_d = CUDA.fill(2.0f0, N);  # a vector stored on the GPU filled with 2.0

BenchmarkTools.Trial: 50 samples with 1 evaluation.
Range (min β¦ max):  101.137 ms β¦ 101.689 ms  β GC (min β¦ max): 0.00% β¦ 0.00%
Time  (median):     101.265 ms               β GC (median):    0.00%
Time  (mean Β± Ο):   101.348 ms Β± 175.231 ΞΌs  β GC (mean Β± Ο):  0.00% Β± 0.00%

β ββ β β βββ  β                 β    β      β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
101 ms           Histogram: frequency by time          102 ms <

========================
Second attempt
========================

N = 5*10^8
x_d = CUDA.fill(1.0f0, N)  # a vector stored on the GPU filled with 1.0 (Float32)
y_d = CUDA.fill(2.0f0, N);  # a vector stored on the GPU filled with 2.0

BenchmarkTools.Trial: 9 samples with 1 evaluation.
Range (min β¦ max):  603.774 ms β¦ 608.049 ms  β GC (min β¦ max): 0.00% β¦ 0.00%
Time  (median):     603.979 ms               β GC (median):    0.00%
Time  (mean Β± Ο):   604.436 ms Β±   1.365 ms  β GC (mean Β± Ο):  0.00% Β± 0.00%

βββββ   β                                                   β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
604 ms           Histogram: frequency by time          608 ms <

``````

That still seems way too slow. I take it your GPU is also driving your output? Maybe you have some processes running (e.g. browsers) that consume parts of the GPU, either memory or compute resources?

Is it simply a RAM issue? A RTX 3050 Laptop GPU apparently has 4GB of RAM, while the data size you specify is exactly 4GB? How does for instance `N = 2*10^8` perform in the benchmark? Looking at your graph the issue doesnβt appear until you exceed ~75% of RAM.

I use Windows 11. Using the task manager I do not see any utilization or memory consumption before running the code.

For `N=2*10^8` it takes around 15 ms and this time no matter how many times I run the code, the timings are consistent. So as you suggested it might have something to do with the memory capacity (which in my case is 4 GB).

I also have an AMD Radeon Graphics on the laptop. The shared memory is 14 GB which plus the 4GB of dedicated memory makes it 18 GB. Could the shared memory has some effects? I do not think the code uses the shared memory since if I increase the array size I get an error which says GPU has 4GB of RAM and the array is too large.