# Indexing adjoints of CuArrays

**URL:** <https://discourse.julialang.org/t/indexing-adjoints-of-cuarrays/97215>\
**Category:** GPU\
**Created:** [April 7, 2023, 11:32am UTC](https://discourse.julialang.org/t/indexing-adjoints-of-cuarrays/97215 "2023-04-07T11:32:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MattSainsbury-Dale](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattsainsbury-dale/32/221714_2.png) [@MattSainsbury-Dale](https://discourse.julialang.org/u/MattSainsbury-Dale)\
**Post date:** [April 7, 2023, 11:32am UTC](https://discourse.julialang.org/t/indexing-adjoints-of-cuarrays/97215/1 "2023-04-07T11:32:42Z")

</div>

I was surprised by the following behaviour when indexing CuArrays:

```julia
using CUDA
CUDA.allowscalar(false)
x = CUDA.fill(1.0f0, 3, 3)
x[1:2, :] # works 
y = x'
y[1:2, :] # error: scalar indexing is disallowed

```

Is there a reason that `adjoint`s of `CuArray`s cannot be indexed in this manner, or is this a bug?

---

<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:** [April 7, 2023, 12:15pm UTC](https://discourse.julialang.org/t/indexing-adjoints-of-cuarrays/97215/2 "2023-04-07T12:15:39Z")

</div>

That’s because `y` is a Transpose object, and not all CuArray operations have been implemented for it (even though it often only involves forwarding the operation to the contained CuArray).

---

<div class="post-metadata">

**Author:** ![MattSainsbury-Dale](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattsainsbury-dale/32/221714_2.png) [@MattSainsbury-Dale](https://discourse.julialang.org/u/MattSainsbury-Dale)\
**Post date:** [April 10, 2023, 12:17am UTC](https://discourse.julialang.org/t/indexing-adjoints-of-cuarrays/97215/3 "2023-04-10T00:17:23Z")

</div>

Thanks for your answer. In this case would you convert `y` to a `CuArray` to allow indexing, or is there a better approach?

---

<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:** [April 10, 2023, 3:55pm UTC](https://discourse.julialang.org/t/indexing-adjoints-of-cuarrays/97215/4 "2023-04-10T15:55:09Z")

</div>

Yes, you could materialize the Transpose, that’s probably the easiest solution. Depending on what you want to do with `y`, you can also e.g. write a custom kernel that indexes the Transpose on the device: Transpose objects are supported on the GPU, it’s just this specific `getindex` call that isn’t implemented. You could of course also look into implementing or extending the relevant `getindex` method to `Transpose` objects.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [April 10, 2023, 10:21pm UTC](https://discourse.julialang.org/t/indexing-adjoints-of-cuarrays/97215/5 "2023-04-10T22:21:14Z")

</div>

> [@maleadt](#):
>
> implementing or extending the relevant `getindex` method to `Transpose` objects.

I’m surprised there aren’t already `getindex(::Transpose...` methods that forward the reversed sequence of indices of whatever type to the parent array and re-transpose the result, must be some reason that wouldn’t work?
