# Scalar indexing is disallowed

**URL:** <https://discourse.julialang.org/t/scalar-indexing-is-disallowed/102808>\
**Category:** GPU\
**Tags:** cuda\
**Created:** [August 15, 2023, 1:07am UTC](https://discourse.julialang.org/t/scalar-indexing-is-disallowed/102808 "2023-08-15T01:07:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![A\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/a_m/32/46038_2.png) [@A\_M](https://discourse.julialang.org/u/A_M)\
**Post date:** [August 15, 2023, 1:07am UTC](https://discourse.julialang.org/t/scalar-indexing-is-disallowed/102808/1 "2023-08-15T01:07:29Z")

</div>

Can you tell me why I am getting it?

```julia
ERROR: LoadError: Scalar indexing is disallowed.
Invocation of getindex resulted in scalar indexing of a GPU array.
This is typically caused by calling an iterating implementation of a method.
Such implementations *do not* execute on the GPU, but very slowly on the CPU,
and therefore are only permitted from the REPL for prototyping purposes.
If you did intend to index this array, annotate the caller with @allowscalar.

```

in

```julia
function scale!(A::SparseArrays.SparseMatrixCSC{Float64,Int}, b::Vector{Float64})
    if CUDA.functional()
        A_gpu = CuSparseMatrixCSC(A)
        row_norms_gpu = sqrt.(sum(abs2, A_gpu, dims=2))
        scaling_factors_gpu = CUDA.ones(size(row_norms_gpu)) ./ row_norms_gpu
        scaling_factors = Array(scaling_factors_gpu) 
    else
        scaling_factors = 1.0 ./ sqrt.(sum(abs2, A, dims=2))
    end
    A .*= scaling_factors
    b .*= scaling_factors 
end

```

Thanks

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 15, 2023, 7:35am UTC](https://discourse.julialang.org/t/scalar-indexing-is-disallowed/102808/2 "2023-08-15T07:35:27Z")

</div>

I cannot install `CuArrays`, and cannot pinpoint the error. But can’t you start reducing the code until you find the problematic line.

First, remove the `else` clause. Then just write a function that does

```julia
A_gpu = CuSparseMatrixCSC(A)
row_norms_gpu = sqrt.(sum(abs2, A_gpu, dims=2))

```

etc.

BTW, this

```julia
scaling_factors_gpu = CUDA.ones(size(row_norms_gpu)) ./ row_norms_gpu

```

creates an unnecessary array of ones, just to use it for division. Instead just use the generic function `inv`, like this:

```julia
scaling_factors_gpu = inv.(row_norms_gpu)

```

---

<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 15, 2023, 7:39am UTC](https://discourse.julialang.org/t/scalar-indexing-is-disallowed/102808/3 "2023-08-15T07:39:43Z")

</div>

> [@A\_M](#):
>
> Can you tell me why I am getting it?

Look at the backtrace to find out which operation does not work properly, and try to avoid it. See [Workflow · CUDA.jl](https://cuda.juliagpu.org/stable/usage/workflow/#UsageWorkflowScalar) for details on scalar indexing.

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [August 15, 2023, 8:04am UTC](https://discourse.julialang.org/t/scalar-indexing-is-disallowed/102808/4 "2023-08-15T08:04:09Z")

</div>

The error seems to be from the line

```julia
scaling_factors_gpu = CUDA.ones(size(row_norms_gpu)) ./ row_norms_gpu

```

where `row_norms_gpu` is a normal (non-gpu) array creating some bad interactions. So either changing `CUDA.ones(...)` to a 1 or wrapping the `row_norms_gpu` in a `CuArray` seems to remove the error, but the first case does the calculation on cpu and the second does a dense sum, so neither might be very good if you operate on a very large matrix.

As to why `row_norms_gpu` is not already a CuArray it seems like the `sum` for CuSparseMatrixCSC results in a normal Array, while `sum(CuArray(A_gpu), dims=2)` results in a CuArray. Not sure what I would expect here (CuSparse or CuArray), but probably something that was still on the GPU at least.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 15, 2023, 12:50pm UTC](https://discourse.julialang.org/t/scalar-indexing-is-disallowed/102808/5 "2023-08-15T12:50:58Z")

</div>

> [@albheim](#):
>
> So either changing `CUDA.ones(...)` to a 1 or wrapping the `row_norms_gpu` in a `CuArray`

No matter how the CuSparse issue is solved, I believe one should use `inv.()` instead of `1 ./ ()`.
