# CuArray find first negative value along columns

**URL:** <https://discourse.julialang.org/t/cuarray-find-first-negative-value-along-columns/45012>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [August 15, 2020, 9:21pm UTC](https://discourse.julialang.org/t/cuarray-find-first-negative-value-along-columns/45012 "2020-08-15T21:21:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkarikom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkarikom/32/7096_2.png) [@mkarikom](https://discourse.julialang.org/u/mkarikom)\
**Post date:** [August 15, 2020, 9:21pm UTC](https://discourse.julialang.org/t/cuarray-find-first-negative-value-along-columns/45012/1 "2020-08-15T21:21:58Z")

</div>

I have the following which gives the correct result, but I need to keep the result on the device.

```julia
diffs = CuArray([-0.1 0.2 0.1;-0.2 -0.2 -0.3])
firstmatch = map(x->CUDA.findfirst(y->y<0,x),CUDA.eachslice(diffs,dims=2))

```

The output is no longer on the device

```julia
3-element Array{Int64,1}:
 1
 2
 2

```

Casting `firstmatch` as a CuArray (moving back to the device) is counterproductive for me here.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [August 15, 2020, 11:06pm UTC](https://discourse.julialang.org/t/cuarray-find-first-negative-value-along-columns/45012/2 "2020-08-15T23:06:34Z")

</div>

I think you can / should do this by writing a kernel similar to [CUDA.findfirst](https://github.com/JuliaGPU/CUDA.jl/blob/f2ee969226759fc15c8092dcd180f155f7305269/src/indexing.jl#L96).

---

<div class="post-metadata">

**Author:** ![mkarikom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkarikom/32/7096_2.png) [@mkarikom](https://discourse.julialang.org/u/mkarikom)\
**Post date:** [August 15, 2020, 11:19pm UTC](https://discourse.julialang.org/t/cuarray-find-first-negative-value-along-columns/45012/3 "2020-08-15T23:19:09Z")

</div>

Thanks I’ll try that approach.  
Can you comment on the difference between CuArray and CuDeviceArray and why the latter was specified for `xs` on [line 101](https://github.com/JuliaGPU/CUDA.jl/blob/f2ee969226759fc15c8092dcd180f155f7305269/src/indexing.jl#L101)?  
The [documentation on CuDeviceArray](https://juliagpu.gitlab.io/CUDA.jl/api/kernel/#Device-arrays-1) didn’t really help clear this up for me.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [August 15, 2020, 11:31pm UTC](https://discourse.julialang.org/t/cuarray-find-first-negative-value-along-columns/45012/4 "2020-08-15T23:31:32Z")

</div>

I’m not sure but I’m guessing it refers to an object that is known to reside on the device when used inside device code.

---

<div class="post-metadata">

**Author:** ![mkarikom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkarikom/32/7096_2.png) [@mkarikom](https://discourse.julialang.org/u/mkarikom)\
**Post date:** [August 16, 2020, 10:38pm UTC](https://discourse.julialang.org/t/cuarray-find-first-negative-value-along-columns/45012/5 "2020-08-16T22:38:03Z")

</div>

Yeah that seems reasonable. I’ve [created another post](https://discourse.julialang.org/t/findfirst-on-2d-cuarray/45069) which addresses the first part of this problem: how to call kernels vector-wise on a device matrix that update elements of a column vector. Once that is done I will update this thread.
