16  Performance & Memory Management

16.1 Memory Management

16.1.1 Clean Up Old Generations

# Remove data from old generations
pop <- clean.up(pop, gen = 1:5)  # Keep gens 6+

16.1.2 New Base Generation

# Reset generation numbering, free memory
pop <- new.base.generation(pop, base.gen = 10)
# Gen 10 becomes new "gen 1"

16.2 Computational Speed

16.2.1 Use miraculix

# Install for fast matrix operations
devtools::install_github("tpook92/miraculix")

# Automatically used when available

16.2.2 Optimize Cores

# Find optimal number of cores for your system
optimal <- optimize.cores(pop)

16.2.3 Reduce Marker Number

# Fewer SNPs = faster
pop <- creating.diploid(
  nsnp = 1000,     # Instead of 50000
  nindi = 1000,
  n.additive = 100
)

16.2.4 Disable Copying

# Don't store full breeding values in memory
pop <- breeding.diploid(
  pop,
  copy.breeding.values = FALSE
)

16.3 Parallel Processing

# Run multiple simulations in parallel
library(parallel)

results <- mclapply(1:100, function(i) {
  pop <- creating.diploid(nsnp = 5000, nindi = 200)
  # ... run simulation ...
  return(mean(get.bv(pop, gen = 10)))
}, mc.cores = 4)

16.4 Summary

  • Memory management techniques
  • Speed optimization
  • Parallel simulation

Continue to Chapter 17: Function Reference!