# Indexing in GPU kernel

**URL:** <https://discourse.julialang.org/t/indexing-in-gpu-kernel/96902>\
**Category:** GPU\
**Tags:** kernel\
**Created:** [March 31, 2023, 5:34pm UTC](https://discourse.julialang.org/t/indexing-in-gpu-kernel/96902 "2023-03-31T17:34:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shakedregev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shakedregev/32/45821_2.png) [@shakedregev](https://discourse.julialang.org/u/shakedregev)\
**Post date:** [March 31, 2023, 5:34pm UTC](https://discourse.julialang.org/t/indexing-in-gpu-kernel/96902/1 "2023-03-31T17:34:34Z")

</div>

I’m building a custom GPU kernel and am having trouble indexing into a GPU vector. Q is a matrix stored in CSR format  
qrows - row pointers  
qcols - nonzero column indices  
qvals - nonzero values  
Here is a code snippet:

```julia
function q_kernel!(qrows::CuDeviceVector{Int64}, qcols::CuDeviceVector{Int64}, qvals::CuDeviceVector{Float64})
    index = (blockIdx().x - 1) * blockDim().x + threadIdx().x
    stride = gridDim().x * blockDim().x
    tot::Float64=0.0
    @simd for i = index:stride:length(qrows)-1 #loop of the rows of the matrix (same number of rows for J and Q)
        @inbounds colind = qrows[i]:qrows[i+1]-1
        @inbounds indj = qcols[colind] #find column indices of all the non zero elements of row i in Q
 for j in indj # this loops over all the non zero elements of row i in Q 

```

The second to last line is getting the errors:

```julia
LoadError: InvalidIRError: compiling kernel
Reason: unsupported dynamic function invocation (call to print_to_string(xs...) in Base at strings/io.jl:133)
Reason: unsupported call through a literal pointer (call to ijl_alloc_array_1d)

```

What is the correct way to index into qcols on the GPU?

---

<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:** [March 31, 2023, 6:36pm UTC](https://discourse.julialang.org/t/indexing-in-gpu-kernel/96902/2 "2023-03-31T18:36:57Z")

</div>

You can try using `view`. `qcols[coldind]` creates a copy.

---

<div class="post-metadata">

**Author:** ![shakedregev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shakedregev/32/45821_2.png) [@shakedregev](https://discourse.julialang.org/u/shakedregev)\
**Post date:** [March 31, 2023, 7:07pm UTC](https://discourse.julialang.org/t/indexing-in-gpu-kernel/96902/3 "2023-03-31T19:07:11Z")

</div>

Thanks, this worked!

```julia
@inbounds indj = @view qcols[colind] #find column indices of all the non zero elements of row i in Q

```
