# How would I retrieve the result from CUDA.@profile?

**URL:** <https://discourse.julialang.org/t/how-would-i-retrieve-the-result-from-cuda-profile/105548>\
**Category:** General Usage\
**Created:** [October 29, 2023, 9:23pm UTC](https://discourse.julialang.org/t/how-would-i-retrieve-the-result-from-cuda-profile/105548 "2023-10-29T21:23:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [October 29, 2023, 9:23pm UTC](https://discourse.julialang.org/t/how-would-i-retrieve-the-result-from-cuda-profile/105548/1 "2023-10-29T21:23:24Z")

</div>

Hi!

Checking out the new release of CUDA.jl, which really has made profiling a lot easier! Much appreciated. I was wondering though, how do I retrieve the result from the profiling?

```julia
#https://info.juliahub.com/cuda-jl-5-0-changes
julia> a = CUDA.rand(1024, 1024, 1024)
julia> CUDA.@profile trace=true CUDA.@sync a .+ a
Profiler ran for 12.29 ms, capturing 527 events.

Host-side activity: calling CUDA APIs took 11.75 ms (95.64% of the trace)
┌─────┬───────────┬───────────┬────────┬─────────────────────────┐
│ ID │ Start │ Time │ Thread │ Name │
├─────┼───────────┼───────────┼────────┼─────────────────────────┤
│ 5 │ 6.91 µs │ 13.59 µs │ 1 │ cuMemAllocFromPoolAsync │
│ 9 │ 36.72 µs │ 199.56 µs │ 1 │ cuLaunchKernel │
│ 525 │ 510.69 µs │ 11.75 ms │ 2 │ cuStreamSynchronize │
└─────┴───────────┴───────────┴────────┴─────────────────────────┘

```

What I wish is similar to BenchmarkTools, where one can output the timing into a variable, and display it later / manipulate the data etc. I am not sure how to do that with CUDA though?

Perhaps I am missing something obvious 🙂

Kind regards

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [October 30, 2023, 9:11am UTC](https://discourse.julialang.org/t/how-would-i-retrieve-the-result-from-cuda-profile/105548/2 "2023-10-30T09:11:00Z")

</div>

The reason we don’t have such an API, is that it’s not clear what ‘result from profiling’ should be returned. Is it the kernel times? The API times? The NVTX ranges? Or just the total execution time of the code on the CPU? Or on the GPU? The profiler UI reports all that information.

If you just want to measure the total execution time, you should use `Base.@elapsed CUDA.@sync ...` for the CPU time, and `CUDA.@elapsed ...` for the GPU time.

If you want more specific times, there’s `CUDA.@profiled` now (on CUDA.jl#master), which returns the internal state of the profiler, but the formatting of that isn’t documented. That’s on purpose, so it can change without requiring a breaking release.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [October 30, 2023, 4:16pm UTC](https://discourse.julialang.org/t/how-would-i-retrieve-the-result-from-cuda-profile/105548/3 "2023-10-30T16:16:59Z")

</div>

Thanks for the explanation!

Perfect, I will see if I can get those to work well for me - no matter what this is a huge step up from before, so still very pleased

* * *

On a side note. I am not sure who selected your answer as solution, and while I agree it is and very well explained, it would be nice if I could have the option to select the solution 🙂

---

<div class="post-metadata">

**Author:** ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)\
**Post date:** [October 31, 2023, 5:47pm UTC](https://discourse.julialang.org/t/how-would-i-retrieve-the-result-from-cuda-profile/105548/4 "2023-10-31T17:47:45Z")

</div>

Maybe we should return an object that contains the data and uses `show` to be nicely displayed.

I noticed the other day that the current output to stdout doesn’t look as nicely on Pluto.

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [October 31, 2023, 6:08pm UTC](https://discourse.julialang.org/t/how-would-i-retrieve-the-result-from-cuda-profile/105548/5 "2023-10-31T18:08:58Z")

</div>

That’s a good idea, and would allow unifying `@profile` and `@profiled`.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [October 31, 2023, 9:59pm UTC](https://discourse.julialang.org/t/how-would-i-retrieve-the-result-from-cuda-profile/105548/6 "2023-10-31T21:59:43Z")

</div>

> [@Ahmed\_Salih](#):
>
> On a side note. I am not sure who selected your answer as solution, and while I agree it is and very well explained, it would be nice if I could have the option to select the solution

Unfortunately, the solution button and the like button are very close to each other and it is very easy to misclick, especially on a mobile phone. So maybe that’s what happened. In any case, you should be able to revoke the solution tag (I think).

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [November 1, 2023, 4:19pm UTC](https://discourse.julialang.org/t/how-would-i-retrieve-the-result-from-cuda-profile/105548/7 "2023-11-01T16:19:51Z")

</div>

I implemented this in [Profiler: Improve compatibility with Pluto.jl and friends. by maleadt · Pull Request #2139 · JuliaGPU/CUDA.jl · GitHub](https://github.com/JuliaGPU/CUDA.jl/pull/2139). `CUDA.@profiled` is no more; `CUDA.@profile` now returns a `ProfileResult` object that displays like before. You can peek into that struct to get the data of the profile, but I’d recommend adding some accessors (e.g. `get_kernels(::ProfileResult)`, or an iterator, etc) based on your use case, and submitting a pull request for that. I’m happy to support accessors like that, but the internal fields can change between releases.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [November 1, 2023, 5:20pm UTC](https://discourse.julialang.org/t/how-would-i-retrieve-the-result-from-cuda-profile/105548/8 "2023-11-01T17:20:45Z")

</div>

Ah, thank you, I will imagine that that was the case 😊

I see @maleadt and others have been going on about implementing what I was asking about, so I think it is fair to mark his new answer at the bottom as the answer for now!

Kind regards
