CliMA / CliMA/ClimaCore.jl

Dependency Graph Framework for Automatic Tendency Ordering and Caching

Open
#2,453 2 comments 0 reactions 0 assignees View on GitHub
SDI
Dominant language
Julia
Stars
117
Forks
19
Avg merge
3d 4h
Merged PRs (30d)
41

Description

## Purpose
This SDI describes a system to replace manual orchestration of tendency computations with an automatic, declarative system.

`DependencyGraph` constructs dependency graphs for computing tendencies from prognostic variables, topologically sorts them, and allows configurable evaluation of nodes with caching.

```
Y (prognostic) → computed vars → Yₜ (tendencies)
```

## Core Strategy

ClimaAtmos computes tendencies through the chain of precomputed quantities. These are manually ordered, all cached in global memory, and recomputed even when not needed.

VarManager provides a **declarative dependency system** to redefine how tendencies are computed.

1. Users define what each variable depends on using `var_dependencies` and `tendency_dependencies`:
```
var_dependencies(::AtmosModel, ::FieldName{(:c, :J)}) = (@name(c.uₕ),)
tendency_dependencies(::AtmosModel, ::FieldName{(:c, :ρ)}) = (@name(f.ρ), @name(f.u³))
```
2. Users define how to compute vars and tendencies:
```
compute_var(::AtmosModel, ::FieldName{(:c, :J)}, vars, t) =
Fields.local_geometry_field(vars[@name(c.uₕ)]).J
compute_tendency(::AtmosModel, ::FieldName{(:c, :ρ)}, vars, t) =
@. lazy(-(ᶜdivᵥ(vars[@name(f.ρ)] * vars[@name(f.u³)])))
```
3. This is used to build an acyclic dependency graph that is sorted to obtain the evaluation order for computed variables.
```
build_dependency_graph(tend_names, prog_names, model)
get_evaluation_order(graph)
```
4. The graph (or a specific tendency) can be evaluated using a specific caching strategy. The initial strategies will be to cache everything or lazify everything.
```
evaluate_graph!(Yₜ, Y, graph, model, t, EagerGlobalCaching())
```

## Benefits

- **Automatic ordering**: Topological graph sort ensures correct and consistent evaluation order
- **Configurable caching**: Choose what to cache vs. keep lazy. This will allow us to separate performance concerns from ClimaAtmos code.
- **Error tracing**: Failures report the full dependency chain. The dependency graph will be easily to inspect.
- **Kernel fusion**: Independent computations in the tree can be fused automatically in the future.

## Risks
- Ensuring this system is performant, especially when using eager caching
- This may be difficult to switch to and may require more boilerplate code.
- Requiring acyclic graph may limit timestepping schemes. For example, how will this work with callbacks and implicit/explicit splitting?
- ClimaAtmos can not be changed incrementally to use this, will need full overhaul.

## Producers
@nefrathenrici @dennisYatunin

## Components
- `DependencyGraph`: DAG of variable dependencies
- `VarCache`: Runtime storage for computed variables
- `VarCachingStrategy`: Abstract strategy for caching policies
- Debugging and graph inspecting utilities

## Inputs
A description of the inputs to the solution (designs, references, equations, closures, discussions etc).

## Results and deliverables
A description of the key results, deliverables, quality expectations, and performance metrics.

## Task breakdown
A preliminary list of PRs and a preliminary timeline of PRs, milestones, and key results.
- [ ] Task 1
- [ ] Task 2
...

## Reviewers
@dennisYatunin

Contributor guide

No contributing guide indexed for this repository

Research direction

No files or tests are named. Start by reviewing the proposed VarManager interfaces and the DependencyGraph, VarCache, and VarCachingStrategy components described in the issue; the Results and deliverables and Task breakdown sections remain placeholders, so the definition of done is not yet specified.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.