"os"
"runtime/pprof"
"sort"
- "strconv"
)
// =============================================================================
func main() {
// Flags to enable various performance profiling options.
cpuProfile := flag.String("cpuprofile", "", "Filename for saving CPU profile output.")
- memProfilePrefix := flag.String("memprofileprefix", "", "Filename PREFIX for saving memory 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.
}
fmt.Printf(" %d", generation)
- // First generate all possible reduced form symbols per Axiom 1.
- potentialNumbers := permuteExistingNumbers(generation, universe)
- // Now prune out any symbols which are NOT valid numbers per Axiom 2.
- validNumbers := pruneInvalidNumbers(potentialNumbers, universe)
+ // 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(validNumbers, &universe)
-
- // Setup any memory profiling requested by the user. This will snapshot
- // the heap at the end of every generation.
- if *memProfilePrefix != "" {
- memProFile, err := os.Create(*memProfilePrefix + "-gen" + strconv.Itoa(generation) + ".mprof")
- if err != nil {
- log.Fatal("ERROR: Unable to write heap profile to disk:", err)
- }
- pprof.WriteHeapProfile(memProFile)
- memProFile.Close()
- }
+ 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.
leftSet.insert(universe.numbers[i], universe)
rightSet.insert(universe.numbers[j], universe)
newSurrealNumber := surrealNumber{leftSet,rightSet,0.0,0,generation}
- numbers = append(numbers, newSurrealNumber)
+ if isValidSurrealNumber(newSurrealNumber, universe) {
+ numbers = append(numbers, newSurrealNumber)
+ }
}
}
// Now build permutations with one empty set and one 1-element set.
return numbers
}
-func pruneInvalidNumbers(candidates []surrealNumber, universe surrealUniverse) []surrealNumber {
- var numbers []surrealNumber
- for i := 0; i < len(candidates); i++ {
- if isValidSurrealNumber(candidates[i], universe) {
- numbers = append(numbers, candidates[i])
- }
- }
- return numbers
-}
-
// Although a non-reduced-form number is technically valid, for the purposes of
// this program we're declaring it invalid.
func isValidSurrealNumber(candidate surrealNumber, universe surrealUniverse) bool {