# Finite element method interpolation performance

**URL:** <https://discourse.julialang.org/t/finite-element-method-interpolation-performance/70944>\
**Category:** Performance\
**Tags:** memory-allocation, staticarrays\
**Created:** [November 4, 2021, 10:24am UTC](https://discourse.julialang.org/t/finite-element-method-interpolation-performance/70944 "2021-11-04T10:24:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![maphdze](https://avatars.discourse-cdn.com/v4/letter/m/ea5d25/32.png) [@maphdze](https://discourse.julialang.org/u/maphdze)\
**Post date:** [November 4, 2021, 10:24am UTC](https://discourse.julialang.org/t/finite-element-method-interpolation-performance/70944/1 "2021-11-04T10:24:19Z")

</div>

I have a question about the performance in interpolation in finite element method.  
In the Official Document, it is suggested to do small size array operations with `StaticArrays.jl`.  
In my finite element program, the global node displacement is calculated first and stored in a `Vector`, and I want to calculate the displacement in the integral points inside the element. For each integral point, I get the node displacement of the element from the global displacement vector and store it as a `MVector` like

```julia
@views poidis.disfre_nod .= nodfre_nod[ithdim_poi,poidis.elecon_nod]
@views poidis.noddis_ele .= MVector{numnod_ele}(noddis_nod[poidis.disfre_nod])

```

The shape function is also a `MVector`. Then I calculate the displacement of integral point by

```julia
poidis.poidis_poi[ithdim_poi,ithpoi_poi] = dot(poidis.shafun_ele,poidis.noddis_ele)

```

I need to loop over all the elements, then I test the memory allocation ,the result is

```julia
        0 @views poidis.disfre_nod .= nodfre_nod[ithdim_poi,poidis.elecon_nod]
 13747184 @views poidis.noddis_ele .= MVector{numnod_ele}(noddis_nod[poidis.disfre_nod])
  3436800 poidis.poidis_poi[ithdim_poi,ithpoi_poi] = dot(poidis.shafun_ele,poidis.noddis_ele)

```

As we can see, it is a huge number. I thought maybe the type conversion from `Vector` to `MVector` is time consuming, then I make a little change like

```julia
        0 @views poidis.disfre_nod .= nodfre_nod[ithdim_poi,poidis.elecon_nod]
        - # @views poidis.noddis_ele .= MVector{numnod_ele}(noddis_nod[poidis.disfre_nod])
 10310352 @views poidis.noddis_ele .= noddis_nod[poidis.disfre_nod]
  3436800 poidis.poidis_poi[ithdim_poi,ithpoi_poi] = dot(poidis.shafun_ele,poidis.noddis_ele)

```

The memory allocation is lower, but still a big number. What is the possible reason?  
Another question is that why the `dot` of two `MVector` need to allocate so much memory?  
Thanks for your suggestions! If necessary, I will post the code.

---

<div class="post-metadata">

**Author:** ![maphdze](https://avatars.discourse-cdn.com/v4/letter/m/ea5d25/32.png) [@maphdze](https://discourse.julialang.org/u/maphdze)\
**Post date:** [November 4, 2021, 12:17pm UTC](https://discourse.julialang.org/t/finite-element-method-interpolation-performance/70944/2 "2021-11-04T12:17:30Z")

</div>

I just solve it! I preallocated the `poidis.disfre_nod` and `poidis.noddis_ele` as `MVector`, but did not claim the dimention and numerical type, so the complier need to judge the type. After I claim the type of `poidis.disfre_nod` as `MVector{4,Float64}` and `poidis.noddis_ele` as `MVector{4,Int64}`, the memory allocation becomes little.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 4, 2021, 9:40pm UTC](https://discourse.julialang.org/t/finite-element-method-interpolation-performance/70944/3 "2021-11-04T21:40:31Z")

</div>

> [@maphdze](#):
>
> In the Official Document, it is suggested to do small size array operations with `StaticArrays.jl` .

What is the “Official Document”?

---

<div class="post-metadata">

**Author:** ![maphdze](https://avatars.discourse-cdn.com/v4/letter/m/ea5d25/32.png) [@maphdze](https://discourse.julialang.org/u/maphdze)\
**Post date:** [November 5, 2021, 12:52am UTC](https://discourse.julialang.org/t/finite-element-method-interpolation-performance/70944/4 "2021-11-05T00:52:26Z")

</div>

Just the Documentation in [julialang.org](http://julialang.org). See [Julia Documentation · The Julia Language](https://docs.julialang.org/en/v1/).
