# Using CuArrays with Iterators.product()

**URL:** <https://discourse.julialang.org/t/using-cuarrays-with-iterators-product/64824>\
**Category:** General Usage\
**Tags:** question, cuda\
**Created:** [July 17, 2021, 6:09pm UTC](https://discourse.julialang.org/t/using-cuarrays-with-iterators-product/64824 "2021-07-17T18:09:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregory](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregory/32/18969_2.png) [@gregory](https://discourse.julialang.org/u/gregory)\
**Post date:** [July 17, 2021, 6:09pm UTC](https://discourse.julialang.org/t/using-cuarrays-with-iterators-product/64824/1 "2021-07-17T18:09:39Z")

</div>

Hello,

I am trying to port some CPU code to GPUs where I run over a high-dimensional array and perform some operations. Below is a MWE of what I am doing on the CPU:

```julia
function sequential_rand!(x, test)

    for i in Iterators.product((1:length(j) for j in test)...)
        x[i...] += rand()
    end

end

N = 10

chromArray = [1:5 for i in 1:N]

myarr = ones(5*ones(Int,N)...)

sequential_rand!(myarr,chromArray)

```

For the GPU, I saw the documentation for a generic for loop ([https://juliagpu.gitlab.io/CUDA.jl/tutorials/introduction/](https://juliagpu.gitlab.io/CUDA.jl/tutorials/introduction/))

```julia
function gpu_add3!(y, x)
    index = (blockIdx().x - 1) * blockDim().x + threadIdx().x
    stride = blockDim().x * gridDim().x
    for i = index:stride:length(y)
        @inbounds y[i] += x[i]
    end
    return
end

numblocks = ceil(Int, N/256)

fill!(y_d, 2)
@cuda threads=256 blocks=numblocks gpu_add3!(y_d, x_d)
@test all(Array(y_d) .== 3.0f0)

```

Is there a way to do this with `Iterators.product`?

---

<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 19, 2021, 6:24am UTC](https://discourse.julialang.org/t/using-cuarrays-with-iterators-product/64824/2 "2021-07-19T06:24:34Z")

</div>

> [@gregory](#):
>
> I saw the documentation for a generic for loop

That’s not a generic for loop. Most of the iteration is performed implicitly by the function being called by multiple threads, the loop just serves to extend that to input sizes that are too large.

> [@gregory](#):
>
> Is there a way to do this with `Iterators.product` ?

Iteration with `Iterators.product` won’t get mapped to efficient GPU iteration automatically.  
f `ProductIterator` would support `getindex`, it would be possible to create the iterator on the CPU and ‘index’ it from a GPU thread to get an appropriate index, but it doesn’t look like that’s supported.
