# Simple CuArray conversion, reverse, and transpose taking too long?

**URL:** <https://discourse.julialang.org/t/simple-cuarray-conversion-reverse-and-transpose-taking-too-long/86337>\
**Category:** GPU\
**Created:** [August 25, 2022, 3:24pm UTC](https://discourse.julialang.org/t/simple-cuarray-conversion-reverse-and-transpose-taking-too-long/86337 "2022-08-25T15:24:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aaron\_pung](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaron_pung/32/39094_2.png) [@aaron\_pung](https://discourse.julialang.org/u/aaron_pung)\
**Post date:** [August 25, 2022, 3:24pm UTC](https://discourse.julialang.org/t/simple-cuarray-conversion-reverse-and-transpose-taking-too-long/86337/1 "2022-08-25T15:24:38Z")

</div>

I am ingesting a NetCDF file using the `NCDatasets` package; the resulting array of type `Matrix{Float32}` has size `(32768,1000)`. Afterwards, I’m converting the array to a CuArray:

````
```using CUDA
using NCDatasets

CUDA.allowscalar(false)

# Read in data
t0 = Dates.now()
ds = Dataset(data_path)["power"][1,:,:]
t1 = Dates.now()

# Convert to CUDA array
cu_ds = CuArray(ds)
t2 = Dates.now()

# Transpose CUDA array
cu_ts = transpose(cu_ds)
t3 = Dates.now()

# Reverse CUDA array
cu_rs = reverse!(cu_ts,dims=1)
t4 = Dates.now()```

````

Times for each of these steps is as follows:

````
```[ Info: Ingest: 1235 milliseconds
[ Info: CUDA array: 2695 milliseconds
[ Info: Transpose: 2718 milliseconds
[ Info: Reverse!: 16385 milliseconds```

````

I know I’m doing something wrong, but I cannot figure out what it is. I’ve checked file types to ensure all arrays are CuArrays. I’ve also looked through the [CUDA GitHub script](https://github.com/JuliaGPU/CUDA.jl/blob/master/test/array.jl) to make sure I’m calling the functions correctly.

---

<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:** [August 25, 2022, 7:48pm UTC](https://discourse.julialang.org/t/simple-cuarray-conversion-reverse-and-transpose-taking-too-long/86337/2 "2022-08-25T19:48:23Z")

</div>

In Julia, the first execution of everything takes longer. Here, the GPU compiler is being compiled for the `reverse!` kernel. Call it a second time, it’ll be instantaneous.

---

<div class="post-metadata">

**Author:** ![aaron\_pung](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaron_pung/32/39094_2.png) [@aaron\_pung](https://discourse.julialang.org/u/aaron_pung)\
**Post date:** [August 25, 2022, 7:58pm UTC](https://discourse.julialang.org/t/simple-cuarray-conversion-reverse-and-transpose-taking-too-long/86337/3 "2022-08-25T19:58:45Z")

</div>

Thanks! I’m aware that the compiler takes a bit longer the first iteration, but I only need to call this function once at the beginning of my script.

Is there any way I can initialize `reverse!` by calling it on a very small or empty array, pay this “time tax” early on, and have the `reverse!` function work instantaneously when I actually need to use it? Or will it have a large time penalty regardless?

---

<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:** [August 29, 2022, 7:57am UTC](https://discourse.julialang.org/t/simple-cuarray-conversion-reverse-and-transpose-taking-too-long/86337/4 "2022-08-29T07:57:03Z")

</div>

> [@aaron\_pung](#):
>
> Is there any way I can initialize `reverse!` by calling it on a very small or empty array, pay this “time tax” early on, and have the `reverse!` function work instantaneously when I actually need to use it? Or will it have a large time penalty regardless?

That would work, as long as the types of objects that will be passed to the kernel are identical. It’s not yet possible to precompile those invocations though, due to a number of missing features and bugs in Julia, so for now it needs to happen at run time.
