# Benchmark function that uses CUDA.jl

**URL:** <https://discourse.julialang.org/t/benchmark-function-that-uses-cuda-jl/71093>\
**Category:** Performance\
**Tags:** cuda, benchmark, precompilation\
**Created:** [November 7, 2021, 1:05pm UTC](https://discourse.julialang.org/t/benchmark-function-that-uses-cuda-jl/71093 "2021-11-07T13:05:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![UndefinedBehavior](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/undefinedbehavior/32/30586_2.png) [@UndefinedBehavior](https://discourse.julialang.org/u/UndefinedBehavior)\
**Post date:** [November 7, 2021, 1:05pm UTC](https://discourse.julialang.org/t/benchmark-function-that-uses-cuda-jl/71093/1 "2021-11-07T13:05:00Z")

</div>

Hello all,

I have created a function that computes the eigenvalues of large sparse matrices. I have implemented it both on CPU and GPU. I used CUDA.jl for portions of code that were computationally expensive. Now, I want to compare the two implementations but I am not sure about the handling of the GPU version.

I have created the following function in order to benchmark my operation:

```julia
function bench()
    file = matopen("path_to_file")
    Problem = read(file,"Problem");
    A::SparseMatrixCSC{FLOAT} = Problem["A"];
    @timeit to "RBL_gpu" CUDA.@time d,_ = RBL_gpu(A,25,10);
end

```

I call it with that way:

```julia
# d,_ = RBL_gpu(sprandn(FLOAT,50,50,0.5),1,10);
to = TimerOutput();
bench();
show(to);

```

I have noticed that if I call my function first with a small matrix, just to warm up things, the execution time is improved. For example,

with the warm up: 12 seconds  
without the warm up: 25 seconds

So, my question is which of the two is the most accurate? Am I cheating with the “warm up” or it is a good practice?  
I am thinking that a user would need to call the function only once. Maybe I could create an interface and place the warm-up call inside it.

Sorry for the long text and thank you in advance.

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [November 7, 2021, 2:41pm UTC](https://discourse.julialang.org/t/benchmark-function-that-uses-cuda-jl/71093/2 "2021-11-07T14:41:17Z")

</div>

First of all, welcome on Discourse and hopefully you had a great experience with Julia so far!

What you see, is the typical Julia phenomenon (and in any other [JIT compiled languages](https://en.m.wikipedia.org/wiki/Just-in-time_compilation)).  
Unless you don’t include the compilation time in C, Fortran etc. it would be fair to take the second measurement.

In principle you can ship a compiled version to the user with [PackageCompiler.jl](https://github.com/JuliaLang/PackageCompiler)

---

<div class="post-metadata">

**Author:** ![UndefinedBehavior](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/undefinedbehavior/32/30586_2.png) [@UndefinedBehavior](https://discourse.julialang.org/u/UndefinedBehavior)\
**Post date:** [November 7, 2021, 5:34pm UTC](https://discourse.julialang.org/t/benchmark-function-that-uses-cuda-jl/71093/3 "2021-11-07T17:34:56Z")

</div>

That was the answer I was hoping for! Furthermore, the package seems very useful. Thank you!
