# Generic Kernels for CLArrays

**URL:** <https://discourse.julialang.org/t/generic-kernels-for-clarrays/12361>\
**Category:** GPU\
**Created:** [July 13, 2018, 10:10pm UTC](https://discourse.julialang.org/t/generic-kernels-for-clarrays/12361 "2018-07-13T22:10:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![goretkin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goretkin/32/167_2.png) [@goretkin](https://discourse.julialang.org/u/goretkin)\
**Post date:** [July 13, 2018, 10:10pm UTC](https://discourse.julialang.org/t/generic-kernels-for-clarrays/12361/1 "2018-07-13T22:10:05Z")

</div>

I’d like to take the minimum of a scalar field `z` subject to some mask `d`. To illustrate, `z` is a checkerboard pattern and `d` is a circle centered at `c_xy`.

```julia
N = 10
xy = [(i, j) for i=1:N, j=1:N] # coordinates
z = [(i+j)%2 + 7 for i=1:N, j=1:N] # checkerboard

# render z masked by a circle
function render(xy, z)
    function d(xy, z)
        c_xy = (5,5)
        r = (xy[1] - c_xy[1])^2 + (xy[2] - c_xy[2])^2
        mask = (sqrt(Float32(r)) < 3.5)
        return mask * z
    end
    return map(d, xy, z)
end

# render z masked by a circle at c_xy
function render_c(xy, z, c_xy)
    function d(xy, z)
        r = (xy[1] - c_xy[1])^2 + (xy[2] - c_xy[2])^2
        mask = (sqrt(Float32(r)) < 3.5)
        return mask * z
    end
    return map(d, xy, z)
end

# get maximum of z within the mask
function query_c(xy, z, c_xy)
    function d(xy, z)
        r = (xy[1] - c_xy[1])^2 + (xy[2] - c_xy[2])^2
        mask = (sqrt(Float32(r)) < 3.5)
        return mask * z
    end
    #return maximum(map(d, xy, z))
    return mapreduce(xyz->d(xyz[1], xyz[2]), max, zip(xy, z))
end

```

`render` runs fine on the GPU with `GPUArrays` interface of `CLArrays`. 1. I wish there were a way to avoid constructing `xy` and just get the indices. 2. I don’t know how to get `render_c` working, because it relies on `d` closing over the value `c_xy`. I could construct an array full of identical `c_xy` values, but I’m hoping to have a better solution. (For example, setting the stride of some array to 0 in all dimensions).

Do I need to write a custom kernel? I succeeded once upon using [https://mikeinnes.github.io/2017/08/24/cudanative.html](https://mikeinnes.github.io/2017/08/24/cudanative.html) as a guide for CuArrays. Are there examples of generic kernels for CLArrays?

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [July 16, 2018, 8:38am UTC](https://discourse.julialang.org/t/generic-kernels-for-clarrays/12361/2 "2018-07-16T08:38:38Z")

</div>

Sure, there are lots of generic kernel in GPUArrays, which work both with CuArrays + CLArrays:  
[https://github.com/JuliaGPU/GPUArrays.jl/blob/master/src/base.jl#L102](https://github.com/JuliaGPU/GPUArrays.jl/blob/master/src/base.jl#L102)  
[https://github.com/JuliaGPU/GPUArrays.jl/blob/master/src/linalg.jl#L26](https://github.com/JuliaGPU/GPUArrays.jl/blob/master/src/linalg.jl#L26)

I also wrote this blog post a while ago:

> **[Writing extendable and hardware agnostic GPU libraries](https://medium.com/techburst/writing-extendable-and-hardware-agnostic-gpu-libraries-b21c145a8dad)**
>
> When I started programming in Julia around 4 years ago, GPU support and the ability to easily extend libraries was a big factor in my…
