# How to access field values in ParallelStencil.jl custom struct

**URL:** <https://discourse.julialang.org/t/how-to-access-field-values-in-parallelstencil-jl-custom-struct/123172>\
**Category:** GPU\
**Tags:** question\
**Created:** [November 27, 2024, 2:07pm UTC](https://discourse.julialang.org/t/how-to-access-field-values-in-parallelstencil-jl-custom-struct/123172 "2024-11-27T14:07:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pchaubal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pchaubal/32/213778_2.png) [@pchaubal](https://discourse.julialang.org/u/pchaubal)\
**Post date:** [November 27, 2024, 2:07pm UTC](https://discourse.julialang.org/t/how-to-access-field-values-in-parallelstencil-jl-custom-struct/123172/1 "2024-11-27T14:07:23Z")

</div>

I am trying to use a custom struct with ParallelStencil.jl. I define the custom struct as

```julia
@CellType Field2D fieldnames=(xx, yy, zz)

```

Then I initialize it for 2 variables as

```julia
F1 = @zeros( nx, ny, celltype=Field2D)
F2 = @zeros( nx, ny, celltype=Field2D)

```

Finally I would like to use its fields in a computation kernel as

```julia
@parallel_indices (ix, iy) function Evolve!(F1, F2)
    F1.xx [ix, iy] = F2.yy[ix, iy] - F2.zz[ix, iy] 

```

However, the last step fails. I am assuming i do not know the right way to access the field value of F1 and F2 variables. If so, can someone please point the correct way to access field values?

Or am I completely mistaken about the use case of @CellType macro and define a variable for each component separately?

---

<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:** [November 27, 2024, 5:30pm UTC](https://discourse.julialang.org/t/how-to-access-field-values-in-parallelstencil-jl-custom-struct/123172/2 "2024-11-27T17:30:13Z")

</div>

There is currently an issue with this which I’m working on. It already works on CPU if you use the `main` branch of CellArrays (`] add CellArrays#main`) - for GPU some work in CUDA.jl is required it seems:

```julia
julia> using ParallelStencil

julia> @init_parallel_stencil(package=Threads, numbertype=Float64)

julia> @CellType Field2D fieldnames=(xx, yy, zz)

julia> nx = ny = 2
2

julia> F1 = @zeros(nx, ny, celltype=Field2D)
2×2 CellArrays.CPUCellArray{Field2D, 2, 1, Float64}:
 [0.0, 0.0, 0.0] [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0] [0.0, 0.0, 0.0]

julia> F2 = @rand(nx, ny, celltype=Field2D)
2×2 CellArrays.CPUCellArray{Field2D, 2, 1, Float64}:
 [0.326463, 0.451149, 0.802571] [0.280195, 0.0683425, 0.166727]
 [0.239031, 0.230775, 0.0981809] [0.636965, 0.886454, 0.62971]

julia> @parallel_indices (ix, iy) function evolve!(F1, F2)
           F1.xx[ix, iy] = F2.yy[ix, iy] - F2.zz[ix, iy]
           return
       end
evolve! (generic function with 1 method)

julia> @parallel evolve!(F1, F2)

julia> F1
2×2 CellArrays.CPUCellArray{Field2D, 2, 1, Float64}:
 [-0.351422, 0.0, 0.0] [-0.0983844, 0.0, 0.0]
 [0.132594, 0.0, 0.0] [0.256745, 0.0, 0.0]

```

I will try to fix it as soon as possible for GPU.

---

<div class="post-metadata">

**Author:** ![pchaubal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pchaubal/32/213778_2.png) [@pchaubal](https://discourse.julialang.org/u/pchaubal)\
**Post date:** [November 28, 2024, 2:04pm UTC](https://discourse.julialang.org/t/how-to-access-field-values-in-parallelstencil-jl-custom-struct/123172/3 "2024-11-28T14:04:25Z")

</div>

Thanks! looking forward to the new functionality.

Kudos for writing the awesome package!
