Skip to content

About

The layer that lets CliMA simulations run unchanged on a laptop, GPUs, or a thousand MPI ranks.

Topics

Resources

Stars

7 stars

Watchers

1 watching

Forks

Latest commit

 

History

207 Commits

Folders and files

Repository files navigation

ClimaComms.jl Logo

ClimaComms.jl

ClimaComms.jl provides the abstractions for computing devices and communication contexts that the CliMA Earth System Model is built on. It lets the same simulation code run unchanged on a single CPU thread, on multiple CPU threads, on NVIDIA and Apple GPUs, and across many nodes with MPI: the device and the parallelism are selected at runtime, typically through environment variables.

Documentation stable dev
Version version
License license
Tests gha ci buildkite
Downloads Downloads

Quick Start

Installation

using Pkg
Pkg.add("ClimaComms")

To run on GPUs or with MPI, also install the corresponding backend packages (ClimaComms loads them through package extensions, so they are not hard dependencies):

Pkg.add("CUDA")   # for NVIDIA GPUs
Pkg.add("Metal")  # for Apple GPUs (Float32 only)
Pkg.add("MPI")    # for distributed runs

Basic Usage

ClimaComms.jl is built around two objects: a device (the hardware a computation runs on) and a context (the environment through which processes communicate). Both are selected at runtime from the CLIMACOMMS_DEVICE and CLIMACOMMS_CONTEXT environment variables, so the same script runs anywhere:

import ClimaComms

# Load MPI.jl, CUDA.jl, or Metal.jl, as the environment variables request.
ClimaComms.@import_required_backends

context = ClimaComms.context()          # e.g., SingletonCommsContext or MPICommsContext
device = ClimaComms.device(context)     # e.g., CPUSingleThreaded() or CUDADevice()

ClimaComms.init(context)                # initialize (e.g., MPI setup, GPU assignment)

# Allocate arrays on the right device.
ArrayType = ClimaComms.array_type(device)
x = ArrayType([1.0, 2.0, 3.0])

# Communicate across processes (a no-op in single-process runs).
total = ClimaComms.reduce(context, sum(x), +)
ClimaComms.iamroot(context) && @show total

To run the same script on a GPU, or on four MPI processes:

CLIMACOMMS_DEVICE=CUDA julia script.jl
CLIMACOMMS_DEVICE=Metal julia script.jl
CLIMACOMMS_CONTEXT=MPI mpiexec -n 4 julia script.jl

Device-Agnostic Loops

The ClimaComms.@threaded macro generalizes Threads.@threads: the same loop runs serially on a single-threaded CPU, across threads on a multi-threaded CPU, and as a kernel on a GPU:

function threaded_add!(a, b, device)
    ClimaComms.@threaded device for i in eachindex(a, b)
        a[i] += b[i]
    end
end

Key Features

  • Device abstraction: CPUSingleThreaded, CPUMultiThreaded, CUDADevice, and MetalDevice types select hardware-specific implementations via dispatch.
  • Context abstraction: SingletonCommsContext and MPICommsContext provide a unified interface (reduce, gather, bcast, barrier, ...) for single-process and distributed runs.
  • Runtime configuration: devices and contexts are chosen through environment variables, so simulations move from a laptop to a GPU cluster without code changes.
  • Zero-cost fallbacks: communication primitives are no-ops in single-process runs, and single-threaded loops compile to plain for loops.
  • Optional backends: CUDA.jl, Metal.jl, and MPI.jl are weak dependencies, loaded through package extensions only when needed, which keeps load times low.
  • Distributed logging: loggers that silence non-root processes, prefix messages with MPI ranks, or write per-rank log files.

Core Design Principles

Devices and Contexts as Dispatch Tags

Devices and contexts are lightweight (usually empty) structs. They carry no state of their own; they exist so that Julia's multiple dispatch can select the right implementation — Array vs. CuArray, a serial loop vs. a CUDA kernel, a no-op vs. an MPI.Allreduce. Library code takes a device or context argument and remains agnostic about what hardware it runs on.

Write Once, Run Anywhere

Simulation code written against the ClimaComms interface does not mention CUDA or MPI directly. The concrete backend is chosen when the program starts, from environment variables, which makes the same driver script portable from serial debugging on a laptop to multi-GPU production runs on a cluster.

Backends as Extensions

Backend-specific code lives in package extensions (ext/), so ClimaComms itself depends only on Julia's standard logging packages and Adapt.jl. Users opt into GPU or MPI support by installing and loading CUDA.jl, Metal.jl, or MPI.jl; the ClimaComms.@import_required_backends macro automates this based on the requested configuration.

Documentation

Contributing

Contributors should follow the shared CliMA engineering standards in CliMA/DeveloperGuides, which cover architecture, performance, code quality, documentation, and workflows.

Integration with Climate Models

ClimaComms.jl is the device and communication layer for the CliMA ecosystem. Nearly every CliMA package accepts a ClimaComms device or context, including:

Getting Help

For questions, check the documentation or open an issue on GitHub.

About

The layer that lets CliMA simulations run unchanged on a laptop, GPUs, or a thousand MPI ranks.

Topics

Resources

Stars

7 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages