Skip to content

Enable parallel CUDA simulations for grids#1

Open
faezs wants to merge 6 commits into
masterfrom
claude/parallel-cuda-simulations-01GU9L82sytj5vzmtUnXvNPp
Open

Enable parallel CUDA simulations for grids#1
faezs wants to merge 6 commits into
masterfrom
claude/parallel-cuda-simulations-01GU9L82sytj5vzmtUnXvNPp

Conversation

@faezs

@faezs faezs commented Dec 6, 2025

Copy link
Copy Markdown
  • Add accelerate and CUDA dependencies to package.yaml and stack.yaml
  • Create CUDA.Types module with GPU-compatible data types
  • Create CUDA.Kernels module with parallel computation kernels
  • Create CUDA.Simulation module for high-level CUDA orchestration
  • Create Grid/CUDA.hs for grid-level CUDA integration
  • Update Main.hs with --cuda and --cuda-steps CLI options

This enables parallel execution of household simulations (battery, PV, consumption) across all nodes in any grid generated by the library.

- Add accelerate and CUDA dependencies to package.yaml and stack.yaml
- Create CUDA.Types module with GPU-compatible data types
- Create CUDA.Kernels module with parallel computation kernels
- Create CUDA.Simulation module for high-level CUDA orchestration
- Create Grid/CUDA.hs for grid-level CUDA integration
- Update Main.hs with --cuda and --cuda-steps CLI options

This enables parallel execution of household simulations (battery, PV,
consumption) across all nodes in any grid generated by the library.
Architecture refactor to support batched simulation across many environments:

- CUDA.Types: Add VecEnvConfig, VecEnvState, StepResult for vectorized envs
- CUDA.Kernels: Add vec* kernels operating on [num_envs, num_nodes] arrays
  - vecGridStepKernel: Steps all envs × all nodes in single kernel launch
  - vecInitStatesKernel: Initialize states for all environments
  - vecResetKernel: Auto-reset done environments
  - vecComputeRewardsKernel: Per-env and per-node reward computation
  - vecExtractObsKernel: Extract observations from states
  - vecCheckDoneKernel: Check done/truncated conditions
- CUDA.Simulation: Add VecEnv interface
  - makeVecEnv: Create N parallel environments from single grid
  - makeVecEnvFromGrids: Create from multiple different grids
  - vecStep: Step all envs with batched actions [num_envs, num_nodes]
  - vecReset: Reset all environments
- Main.hs: Add --vecenv and --vecenv-bench commands for testing

This enables high-throughput RL training by parallelizing across
environments (not just nodes), similar to PufferLib/EnvPool/Isaac Gym.

Example: 4096 envs × 100 nodes = 409,600 parallel ops per step
Change voltage update from incorrect absolute formula (vNom * SoC/100) to
correct proportional formula matching CPU physics (v * SoC_next/SoC_current).

Reference: Sandia module temperature model docs at
https://pvpmc.sandia.gov/modeling-steps/2-dc-module-iv/module-temperature/sandia-module-temperature-model/
Implements scatter/gather based message passing using Accelerate's permute
and gather primitives:
- gatherNodeFeatures: read neighbor values
- scatterAddMessages: aggregate messages at nodes
- computePowerFlowMessages: compute edge power flows
- messagePassingRound: single-env message passing
- vecMessagePassingKernel: batched message passing [num_envs, num_nodes]
- vecApplyPowerExchange: update HH states with grid power flow

This enables proper simulation of power transmission across grid topology.
Previous implementation used the same flow value for both source and
destination, which didn't model transmission losses correctly.

Now properly implements:
- Source loses: P_sent (full amount)
- Destination gains: η * P_sent (reduced by efficiency)
- Lost to transmission: (1-η) * P_sent

This ensures total system energy decreases appropriately when power
flows through lossy transmission lines.
Previous implementation was O(N × E) - each node iterated over all edges.
Now O(E + N) using proper scatter/gather pattern:

1. GATHER: O(E) parallel reads per env
2. COMPUTE: O(E) parallel edge flow calculations
3. SCATTER: O(E) atomic adds via permute(+)

Flattens [num_envs, num_edges] → [num_envs * num_edges] for scatter,
uses (env * numNodes + node) indexing, then reshapes back.

Total work: O(num_envs × num_edges) fully parallel GPU operations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants