SciML / SciML/ComponentArrays.jl

Looping over nested fields

Open
#338 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
375
Forks
42
Avg merge
7h 25m
Merged PRs (30d)
17

Description

This is related to issue: #337

Let's say we have the ComponentArray:

n_cells = 100

u = ComponenArray(mass_fractions = (methanol = zeros(n_cells), water = zeros(n_cells))

And we wanted to loop over mass fractions to apply species_diffusion, advection, a chemical reaction, etc.

for cell_id in 1:n_cells
    for species_name in u.mass_fractions
        u.mass_fractions[species_name][cell_id] += 1.0
    end
end

Well right now, this pattern is terrible for performance and also doesn't work with Polyester's @batch.

I've also tried:

for cell_id in 1:n_cells
    map(keys(u.mass_fractions) do species_name
        u.mass_fractions[species_name][cell_id] += 1.0
    end
end

but this also performs terribly and also performs terribly with polyester.

Pure NamedTuples perform really well when not using @batch. For example:

u = (mass_fractions = (methanol = zeros(n_cells), water = zeros(n_cells))

for cell_id in 1:n_cells
    map(keys(u.mass_fractions) do species_name
        u.mass_fractions[species_name][cell_id] += 1.0
    end
end

doesn't allocate at all and performs amazingly, but if you add @batch to the loop, it allocates and performance goes down the drain. However, NamedTuples aren't ideal as non-vector quantities stored inside a single field cannot be mutated.

If you want to see the performance differences see here:
https://gist.github.com/Jolt8/3b7cf79326f9ffbee6626a0d39e6e428

If there was a dedicated method for looping over nested fields in a ComponentArray that also worked with Polyester, that would be absolutely amazing!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading related issue #337 and the linked performance gist, then investigate how ComponentArray nested fields are currently iterated alongside Polyester's @batch. Done should be a dedicated nested-field iteration approach that avoids the reported allocations and works with @batch.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
data
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.