# Getindex without a warning?

**URL:** <https://discourse.julialang.org/t/getindex-without-a-warning/42382>\
**Category:** GPU\
**Created:** [July 1, 2020, 4:32pm UTC](https://discourse.julialang.org/t/getindex-without-a-warning/42382 "2020-07-01T16:32:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sw72](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@sw72](https://discourse.julialang.org/u/sw72)\
**Post date:** [July 1, 2020, 4:32pm UTC](https://discourse.julialang.org/t/getindex-without-a-warning/42382/1 "2020-07-01T16:32:00Z")

</div>

I need to extract a single value from a CuArray for some calculation. Is there a way to do it without getting the “scalar operations” warning?

---

<div class="post-metadata">

**Author:** ![Ellipse0934](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellipse0934/32/4012_2.png) [@Ellipse0934](https://discourse.julialang.org/u/Ellipse0934)\
**Post date:** [July 1, 2020, 5:53pm UTC](https://discourse.julialang.org/t/getindex-without-a-warning/42382/2 "2020-07-01T17:53:04Z")

</div>

`copyto!` is your friend here.

for Example

```julia
 function my_getindex(a::CuArray{T}, index) where T
     out = Array{T}(undef, 1)
     copyto!(out, 1, a, index, 1)
     out[1]
 end

```

Usage:

```julia
julia> arr = CuArray(1.:32);

julia> my_getindex(a, 1)
1.0

julia> my_getindex(a, 2)
2.0

julia> my_getindex(a, 33)
ERROR: BoundsError: attempt to access 32-element CuArray{Float64,1,Nothing} at index [33]
Stacktrace:
 [1] throw_boundserror(::CuArray{Float64,1,Nothing}, ::Tuple{Int64}) at ./abstractarray.jl:537
 [2] checkbounds at ./abstractarray.jl:502 [inlined]
 [3] copyto! at /home/aditya/.julia/dev/CUDA/src/array.jl:294 [inlined]
 [4] my_getindex(::CuArray{Float64,1,Nothing}, ::Int64) at ./REPL[75]:3
 [5] top-level scope at REPL[79]:1

```

---

<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:** [July 2, 2020, 5:48am UTC](https://discourse.julialang.org/t/getindex-without-a-warning/42382/3 "2020-07-02T05:48:11Z")

</div>

Alternatively, you can `@allowscalar foo[i]`.

---

<div class="post-metadata">

**Author:** ![sw72](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@sw72](https://discourse.julialang.org/u/sw72)\
**Post date:** [July 2, 2020, 7:57pm UTC](https://discourse.julialang.org/t/getindex-without-a-warning/42382/4 "2020-07-02T19:57:25Z")

</div>

Thanks, this is exactly what I need.
