# CUDA.jl with missing data?

**URL:** https://discourse.julialang.org/t/cuda-jl-with-missing-data/49563
**Category:** GPU
**Created:** [November 4, 2020, 2:56am UTC](https://discourse.julialang.org/t/cuda-jl-with-missing-data/49563 "2020-11-04T02:56:14Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![zlewko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zlewko/32/19176_2.png) [@zlewko](https://discourse.julialang.org/u/zlewko)
#### Post date: [November 4, 2020, 2:56am UTC](https://discourse.julialang.org/t/cuda-jl-with-missing-data/49563/1 "2020-11-04T02:56:14Z")

</div>

I’m trying to work with a dataset with missing data using the CUDA.jl. I’d like to skip the missing data in some statistics (mean, covariance etc). Is there any general solution to this that performs well on a GPU? I have written custom kernels to do this but I think I may be missing something and there could be a cleaner way to do it.

I’d like to be able to write something short like:

```julia
using CUDA
a = rand(1000,1000) #random data
a[a.>0.5] .= NaN # simulate missing data using nan since no missings support
a = cu(a)     
mapslices( x->mean(filter(!isnan,x)),a,dims=1) #average non-missing data in each colum

```

This works but is extremely slow on the GPU. Everything I have tried is either very slow or won’t compile for the GPU. Is there a reasonably fast way to do this without custom kernels?

Thanks

---

<div class="post-metadata">

### Author: ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)
#### Post date: [November 4, 2020, 9:05am UTC](https://discourse.julialang.org/t/cuda-jl-with-missing-data/49563/2 "2020-11-04T09:05:54Z")

</div>

I don’t have a GPU to test this right now, but `reduce` and `mapreduce` should be fast on CUDA arrays, so things like

```julia
reduce(a, dims=1) do acc, val
    isnan(val) ? acc : val + acc
end

```

should be an efficient way to filter NaNs out in a sum. For more complex statistics, it’s worth checking if it’s easy to get them using say [Transducers](https://github.com/JuliaFolds/Transducers.jl) or [OnlineStats](https://github.com/joshday/OnlineStats.jl). In principle both packages should work with `reduce` and thus be GPU compatible (haven’t checked though).

---

<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: [November 4, 2020, 4:56pm UTC](https://discourse.julialang.org/t/cuda-jl-with-missing-data/49563/3 "2020-11-04T16:56:35Z")

</div>

> [@zlewko](#):
>
> This works but is extremely slow on the GPU.

You should `CUDA.allowscalar(false)`, which will probably reveal that `mapslices` isn’t available for CuArray. Generally, `missing` isn’t supported either since `CuArray` doesn’t support Union eltypes.

What @piever mentions should work though.

---

<div class="post-metadata">

### Author: ![zlewko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zlewko/32/19176_2.png) [@zlewko](https://discourse.julialang.org/u/zlewko)
#### Post date: [November 5, 2020, 2:02am UTC](https://discourse.julialang.org/t/cuda-jl-with-missing-data/49563/4 "2020-11-05T02:02:33Z")

</div>

Thanks, I knew missing wasn’t supported. I’ll turn that switch on, I suspected that is why it was slow but never checked.

---

<div class="post-metadata">

### Author: ![zlewko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zlewko/32/19176_2.png) [@zlewko](https://discourse.julialang.org/u/zlewko)
#### Post date: [November 5, 2020, 2:06am UTC](https://discourse.julialang.org/t/cuda-jl-with-missing-data/49563/5 "2020-11-05T02:06:54Z")

</div>

Thanks this does work and gives me a good starting point. I did need to add an init to your example.

I’m familiar with OnlineStats, I’ll try that with reduce. I’ll also look into Transducers, I’m not familiar with that one.
