# Search in CUDA vector

**URL:** <https://discourse.julialang.org/t/search-in-cuda-vector/94833>\
**Category:** GPU\
**Tags:** cuda\
**Created:** [February 18, 2023, 12:03pm UTC](https://discourse.julialang.org/t/search-in-cuda-vector/94833 "2023-02-18T12:03:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MohHizzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohhizzani/32/11264_2.png) [@MohHizzani](https://discourse.julialang.org/u/MohHizzani)\
**Post date:** [February 18, 2023, 12:03pm UTC](https://discourse.julialang.org/t/search-in-cuda-vector/94833/1 "2023-02-18T12:03:14Z")

</div>

I have a vector of binary values, need to find all indices that are one, and randomly select one to update another vector at the same index selected.

---

<div class="post-metadata">

**Author:** ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)\
**Post date:** [February 22, 2023, 8:59pm UTC](https://discourse.julialang.org/t/search-in-cuda-vector/94833/2 "2023-02-22T20:59:35Z")

</div>

Please provide a minimum working example (MWE) of your code, as I requested in the `#gpu` channel in Slack.

For reference, the following code was provided in Slack:

```julia
# These are the binary vectors for candidate flips as columns in a matrix
candflips = abs.(sOld .- s′) 
# Iterate over columns
for j=1:size(candflips, 2)
        ff = findall(isone, candflips[:, j])
        if isempty(ff)
            # This's just some random elment when stuck at local minima
            Eoffset[j] += offset
        else
            idx = rand(ff, 1)[1]
            # Here implement the flip
            sNew[idx, j] = abs(one(eltype(sOld)) - sOld[idx, j])
        end
    end

```

---

<div class="post-metadata">

**Author:** ![MohHizzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohhizzani/32/11264_2.png) [@MohHizzani](https://discourse.julialang.org/u/MohHizzani)\
**Post date:** [February 28, 2023, 12:44pm UTC](https://discourse.julialang.org/t/search-in-cuda-vector/94833/3 "2023-02-28T12:44:53Z")

</div>

This’s the **CPU** implementation of it

```julia
N, trials = 300, 10000
# These are the binary vectors for candidate flips as columns in a matrix
Eoffset = zeros(trials)
offset = 0.1
candflips = rand([0, 1], N, trials)
# Iterate over columns
for j=1:size(candflips, 2)
        ff = findall(isone, candflips[:, j])
        if isempty(ff)
            # This's just some random elment when stuck at local minima
            Eoffset[j] += offset
        else
            idx = rand(ff, 1)[1]
            # Here implement the flip
            sNew[idx, j] = abs(one(eltype(sOld)) - sOld[idx, j])
        end
end

```

---

<div class="post-metadata">

**Author:** ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)\
**Post date:** [March 1, 2023, 3:15pm UTC](https://discourse.julialang.org/t/search-in-cuda-vector/94833/4 "2023-03-01T15:15:40Z")

</div>

How are you initializing `sNew` and `sOld`?

---

<div class="post-metadata">

**Author:** ![MohHizzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohhizzani/32/11264_2.png) [@MohHizzani](https://discourse.julialang.org/u/MohHizzani)\
**Post date:** [March 1, 2023, 3:55pm UTC](https://discourse.julialang.org/t/search-in-cuda-vector/94833/5 "2023-03-01T15:55:24Z")

</div>

```julia
N, trials = 300, 10000
sNew = rand([0, 1], N, trials)
sOld = rand([0, 1], N, trials)
# These are the binary vectors for candidate flips as columns in a matrix
Eoffset = zeros(trials)
offset = 0.1
candflips = rand([0, 1], N, trials)
# Iterate over columns
for j=1:size(candflips, 2)
        ff = findall(isone, candflips[:, j])
        if isempty(ff)
            # This's just some random elment when stuck at local minima
            Eoffset[j] += offset
        else
            idx = rand(ff, 1)[1]
            # Here implement the flip
            sNew[idx, j] = abs(one(eltype(sOld)) - sOld[idx, j])
        end
end

```

---

<div class="post-metadata">

**Author:** ![MohHizzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohhizzani/32/11264_2.png) [@MohHizzani](https://discourse.julialang.org/u/MohHizzani)\
**Post date:** [March 23, 2023, 12:35pm UTC](https://discourse.julialang.org/t/search-in-cuda-vector/94833/6 "2023-03-23T12:35:46Z")

</div>

I solved with the following:

```julia
using CUDA
N, trials = 300, 10000
sNew = cu(rand([0, 1], N, trials))
sOld = cu(rand([0, 1], N, trials))
# These are the binary vectors for candidate flips as columns in a matrix
Eoffset = CUDA.zeros(trials)
offset = 0.1
candflips = cu(rand([0, 1], N, trials))
# find which trial (col in candflips) has a flip and who hasn't
hascandflips = sum(candflips; dims=1)' .> (zero(eltype(candflips)))
hasnotcandflips = sum(candflips; dims=1)' .== (zero(eltype(candflips)))
# use previous as a mask to update Eoffset vector
Eoffset .= hasnotcandflips .* (Eoffset .+ offset)
# generate rand matrix only with value (non-zero) is cand flip
# then select the max of these rand values and find the one that 
# equal and use it as a maks to update cusNew
maxes = CUDA.ones(1, trials)
mask = CUDA.rand(N, trials) .* candflips
maximum!(maxes, mask)
maskInv .= mask .== maxes
cusNew .= maskInv .* hascandflips' .* abs.(one(eltype(cusOld)) .- cusOld) .+ (one(eltype(maskInv)) .- (maskInv .* hascandflips')) .* cusOld

```
