# KernelAbstraction ndrange using UnitRange

**URL:** <https://discourse.julialang.org/t/kernelabstraction-ndrange-using-unitrange/96713>\
**Category:** General Usage\
**Tags:** cuda, kernelabstractions\
**Created:** [March 28, 2023, 12:57pm UTC](https://discourse.julialang.org/t/kernelabstraction-ndrange-using-unitrange/96713 "2023-03-28T12:57:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![weymouth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weymouth/32/15839_2.png) [@weymouth](https://discourse.julialang.org/u/weymouth)\
**Post date:** [March 28, 2023, 12:57pm UTC](https://discourse.julialang.org/t/kernelabstraction-ndrange-using-unitrange/96713/1 "2023-03-28T12:57:14Z")

</div>

I have an array with `size(A)=(N,M)` and I would like to run a kernel over `ndrange=(2:N-1,3:M-1)`. Is there a way to do this other than defining an OffsetArray wrapper?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 28, 2023, 2:25pm UTC](https://discourse.julialang.org/t/kernelabstraction-ndrange-using-unitrange/96713/2 "2023-03-28T14:25:01Z")

</div>

Use `I = CartesianIndices(ndrange)` and do `for i in I`, or `A[I] = ...`, `@view A[I]`, etcetera? Not sure about CUDA support, though.

---

<div class="post-metadata">

**Author:** ![weymouth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weymouth/32/15839_2.png) [@weymouth](https://discourse.julialang.org/u/weymouth)\
**Post date:** [April 24, 2023, 9:38am UTC](https://discourse.julialang.org/t/kernelabstraction-ndrange-using-unitrange/96713/3 "2023-04-24T09:38:22Z")

</div>

For future reference, what we ended up doing was adding the starting index of the range within the kernel. So if we want to iterate over `R::CartesianIndices`

```julia
@kernel function kern(A,@Const(I0))
  I = @index(Global,Cartesian)
  I += I0
  A[I] = func(I)
end
kern(backend,64)(A,R[1]-oneunit(R[1]),ndrange=size(R))

```

Which seems to work on general backends without a performance penalty.
