- // Obtain termination conditions from user.
- remainingGenerations := flag.Int("generations", 2, "Number of generations of surreal numbers to breed.")
- flag.Parse()
- if *remainingGenerations < 1 {
- log.Fatal("ERROR: The argument to '-generations' must be greater than zero.")
- }
-
- // Build a universe to contain all the surreal numbers we breed.
- // Seed it by hand with the number zero as generation-0.
- var universe surrealUniverse
- universe.insert(surrealNumber{surrealValue{nil, nil}, surrealMetadata{0, 0}})
-
- for generation := 1; generation <= *remainingGenerations; generation++ {
- potentialNumbers := permuteExistingNumbers(generation, universe)
- validNumbers := pruneInvalidNumbers(potentialNumbers, universe)
- addNumbersToUniverse(validNumbers, &universe)
- }
-
- // TODO: These are just for checking progress. How do I want to render the
- // final generation vs magnitude chart?
- fmt.Println("Total Numbers:", len(universe.numbers))
- fmt.Println(universe.numbers)
+ // Flags to enable various performance profiling options.
+ cpuProfile := flag.String("cpuprofile", "", "Filename for saving CPU profile output.")
+ memProfile := flag.String("memprofile", "", "Filename for saving memory profile output.")
+ suppressOutput := flag.Bool("silent", false, "Suppress printing of numberline at end of simulation. Useful when profiling.")
+
+ // Obtain termination conditions from user.
+ totalGenerations := flag.Int("generations", 2, "Number of generations of surreal numbers to breed.")
+ flag.Parse()
+ if *totalGenerations < 1 {
+ log.Fatal("ERROR: The argument to '-generations' must be greater than zero.")
+ }
+ remainingGenerations := *totalGenerations - 1
+
+ // Setup any CPU performance profiling requested by the user. This will run
+ // throughout program execution.
+ if *cpuProfile != "" {
+ cpuProFile, err := os.Create(*cpuProfile)
+ if err != nil {
+ log.Fatal("ERROR: Unable to open CPU profiling output file:", err)
+ }
+ pprof.StartCPUProfile(cpuProFile)
+ defer pprof.StopCPUProfile()
+ }
+
+ // Build a universe to contain all the surreal numbers we breed.
+ // Seed it by hand with the number zero as generation-0.
+ var universe surrealUniverse
+ universe.nextUniqueID = 0
+ fmt.Println("Seeding generation 0 by hand.")
+ universe.insert(surrealNumber{surrealSet{}, surrealSet{}, 0.0, 0, 0})
+
+ // Breed however many generations of numbers were requested by the user and
+ // add them all to the universe.
+ fmt.Printf("Breeding Generation:")
+ for generation := 1; generation <= remainingGenerations; generation++ {
+ // Give the user some idea of overall progress during long jobs.
+ if generation != 1 {
+ fmt.Printf(",")
+ }
+ fmt.Printf(" %d", generation)
+
+ // First generate all possible new numbers in this generation.
+ potentiallyNewNumbers := permuteExistingNumbers(generation, universe)
+ // Attempt to add the new numbers to the universe. Any duplicates will
+ // be weeded out in the attempt.
+ addNumbersToUniverse(potentiallyNewNumbers, &universe)
+ }
+ fmt.Printf(".\n")
+
+ // Setup any memory profiling requested by the user. This will snapshot
+ // the heap only at this point, after all numbers were generated.
+ if *memProfile != "" {
+ memProFile, err := os.Create(*memProfile)
+ if err != nil {
+ log.Fatal("ERROR: Unable to open heap profile output file:", err)
+ }
+ pprof.WriteHeapProfile(memProFile)
+ memProFile.Close()
+ }
+
+ // Print the number line with generation on the horizontal axis and
+ // magnitude on the vertical axis.
+ if !(*suppressOutput) {
+ for i := 0; i < universe.cardinality(); i++ {
+ printlnNumber(universe.numbers[i])
+ }
+ }
+ fmt.Println("After", *totalGenerations, "generations, the universe contains", len(universe.numbers), "numbers.")
+ fmt.Println("If output looks poor, ensure tabstop is eight spaces and that output doesn't wrap.")