# Higher order derivatives with ParallelStencil

**URL:** <https://discourse.julialang.org/t/higher-order-derivatives-with-parallelstencil/99647>\
**Category:** Julia at Scale\
**Created:** [May 31, 2023, 6:51am UTC](https://discourse.julialang.org/t/higher-order-derivatives-with-parallelstencil/99647 "2023-05-31T06:51:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Teerthal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teerthal/32/48021_2.png) [@Teerthal](https://discourse.julialang.org/u/Teerthal)\
**Post date:** [May 31, 2023, 6:51am UTC](https://discourse.julialang.org/t/higher-order-derivatives-with-parallelstencil/99647/1 "2023-05-31T06:51:49Z")

</div>

I am currently implementing a numerical simulation code on multiple GPUs using ParallelStencil+ImplicitGlobalGrid.  
I wanted to use higher order numerical differentiation schemes which I could easily do by extending the the example on ImplicitGlobalGrid’s page:  
@views d\_xa(A) = A[2:end , : , :] .- A[1:end-1, : , :];

I wanted to make sure that the @update\_halo routine updates the appropriate boundary elements. Say, for a 6th order differentiation scheme for example, the boundary layer would have 3 elements on either side along each dimension.  
I believe(not sure) the @update\_halo macro works for updating single boundary elements along each dimension. Is there a method to extend this to a boundary layer with arbitrary width?  
I assume this could be done using the @hide\_communication macro in ParallelStencil but I was not sure about how I would go about using it.

I would appreciate if somebody has attempted to do so and could give any suggestions or advice.

---

<div class="post-metadata">

**Author:** ![samo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samo/32/35398_2.png) [@samo](https://discourse.julialang.org/u/samo)\
**Post date:** [May 31, 2023, 11:28am UTC](https://discourse.julialang.org/t/higher-order-derivatives-with-parallelstencil/99647/2 "2023-05-31T11:28:12Z")

</div>

The thickness of the halo is currently fixed to one cell in ImplicitGlobalGrid. It is on the road map to remove this limitation; however it will for sure not be feasible before the JuliaCon 2023 conference.

If you badly and urgently need it **and you are not working with staggered grid** , it might be possible to cast the arrays you pass to `update_halo` to [CellArrays](https://github.com/omlins/CellArrays.jl) with cells as big has the required halo thickness and the global grid would have to be initialized accordingly. This would however also need to update ImplicitGlobalGrid to accept CellArrays. Then, I’m still not sure if it would work out of the box, and if yes, if the performance would be good out of the box. I think this would be the fastest track to get something working; a proper implementation will certainly take more time.

> I wanted to use higher order numerical differentiation schemes which I could easily do by extending the the example on ImplicitGlobalGrid’s page:  
> @views d\_xa(A) = A[2:end , : , :] .- A[1:end-1, : , :];

This is an example without ParallelStencil. With ParallelStencil you can do something as discussed here:

> <https://github.com/omlins/ParallelStencil.jl/issues/90>
>
> I am interested in using this package to multithread some hyperbolic PDEs starti…ng from the example of the 1D acoustic wave equation. The first step is to figure out how to implement my own finite difference operators (I use summation by parts operators that are not diagonal for example)
> 
> The documentation states that "Custom macros to extend the finite differences submodules or for other stencil-based numerical methods can be readily plugged in"
> 
> It is not obvious to me how to do this. Say I have a non-diagonal but sparse matrix D that I want to apply to the current state vector, how can I write a macro to do this?

---

<div class="post-metadata">

**Author:** ![Teerthal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teerthal/32/48021_2.png) [@Teerthal](https://discourse.julialang.org/u/Teerthal)\
**Post date:** [May 31, 2023, 6:24pm UTC](https://discourse.julialang.org/t/higher-order-derivatives-with-parallelstencil/99647/3 "2023-05-31T18:24:01Z")

</div>

Thank you for the clarification and the references.  
I look forward to the higher order differentiation schemes being implemented.
