# Generic way to write array stencils/kernels for CPUs and GPUs?

**URL:** https://discourse.julialang.org/t/generic-way-to-write-array-stencils-kernels-for-cpus-and-gpus/15595
**Category:** General Usage
**Created:** [September 28, 2018, 8:28am UTC](https://discourse.julialang.org/t/generic-way-to-write-array-stencils-kernels-for-cpus-and-gpus/15595 "2018-09-28T08:28:46Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)
#### Post date: [September 28, 2018, 8:28am UTC](https://discourse.julialang.org/t/generic-way-to-write-array-stencils-kernels-for-cpus-and-gpus/15595/1 "2018-09-28T08:28:47Z")

</div>

Do we currently have an established way to write stencils for single-/multi-array operations, or kernels in general, in a way that’s compatible with both CPU and GPU array?

Regarding kernels/stencils in general, I found an old experiment of @timholy, [KernelTools.jl[([GitHub - timholy/KernelTools.jl: Fast kernel/stencil operations in Julia](https://github.com/timholy/KernelTools.jl)), and there was some support for stencils in [ParallelAccelerator.jl](https://github.com/IntelLabs/ParallelAccelerator.jl), but I’m not sure about the current situation.

I guess stencils could be seen as a generalized form of broadcasting (resp. broadcasting as applying a multi-array stencil of size one - broadcasting-based code we can already write in a CPU/GPU independent fashion, of course).

I have some vague ideas, but maybe someone is already working on this kind of thing?

---

<div class="post-metadata">

### Author: ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)
#### Post date: [September 28, 2018, 8:34am UTC](https://discourse.julialang.org/t/generic-way-to-write-array-stencils-kernels-for-cpus-and-gpus/15595/2 "2018-09-28T08:34:39Z")

</div>

[https://github.com/JuliaImages/ImageFiltering.jl](https://github.com/JuliaImages/ImageFiltering.jl) has some stuff in this direction.

---

<div class="post-metadata">

### Author: ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)
#### Post date: [September 28, 2018, 9:03am UTC](https://discourse.julialang.org/t/generic-way-to-write-array-stencils-kernels-for-cpus-and-gpus/15595/3 "2018-09-28T09:03:21Z")

</div>

> [GitHub - JuliaImages/ImageFiltering.jl: Julia implementations of multidimensional array convolution and nonlinear stencil operations](https://github.com/JuliaImages/ImageFiltering.jl)

Oh, sure, but that’s 2D stencils with linear combination of fixed coefficients on a single array right?

Maybe I chose the wrong term - when I wrote stencils, I meant kernels that have a fixed access pattern (but may read from multiple arrays) run a fixed but arbitrarily complex operation on the input values and write the result to a single entry in a single target array. Maybe I should have just termed it kernel in general - but then, a kernel in the more generic meaning of the term (e.g. CUDA) isn’t always restricted to a fixed access pattern or a single output array.

---

<div class="post-metadata">

### Author: ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)
#### Post date: [September 28, 2018, 9:08am UTC](https://discourse.julialang.org/t/generic-way-to-write-array-stencils-kernels-for-cpus-and-gpus/15595/4 "2018-09-28T09:08:51Z")

</div>

Yeah its single array, but not restricted to 2d and (on CPU) allows arbitrary operations via `mapwindow`.

---

<div class="post-metadata">

### Author: ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)
#### Post date: [September 28, 2018, 9:27am UTC](https://discourse.julialang.org/t/generic-way-to-write-array-stencils-kernels-for-cpus-and-gpus/15595/5 "2018-09-28T09:27:20Z")

</div>

Oh, neat, good to know (does `mapwindow` use views?)

---

<div class="post-metadata">

### Author: ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)
#### Post date: [September 28, 2018, 9:36am UTC](https://discourse.julialang.org/t/generic-way-to-write-array-stencils-kernels-for-cpus-and-gpus/15595/6 "2018-09-28T09:36:36Z")

</div>

No, it copys to a buffer, so `mapwindow(f!, arr)` will not modify `arr`.
