Skip to content

Commit

Permalink
Merge pull request #69 from observerly/perf/solver/FindSourceMatches
Browse files Browse the repository at this point in the history
perf: amend (ps *PlateSolver) FindSourceMatches in @observerly/skysolve
  • Loading branch information
michealroberts authored Nov 8, 2024
2 parents 5c1b731 + ada92ce commit 66e7c15
Showing 1 changed file with 24 additions and 41 deletions.
65 changes: 24 additions & 41 deletions pkg/solver/solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"errors"
"fmt"
"math"
"runtime"
"sort"
"sync"

Expand Down Expand Up @@ -282,10 +281,10 @@ func (ps *PlateSolver) MatchAsterismsWithCatalog(
tolerance geometry.InvariantFeatureTolerance,
) ([]Match, error) {
// Define the stars for the asterism:
stars := []photometry.Star{asterism.A, asterism.B, asterism.C}
stars := [3]photometry.Star{asterism.A, asterism.B, asterism.C}

// Define the sources for the source asterism:
sources := []catalog.Source{sourceAsterism.A, sourceAsterism.B, sourceAsterism.C}
sources := [3]catalog.Source{sourceAsterism.A, sourceAsterism.B, sourceAsterism.C}

// Define the reference Right Ascension coordinates for the image:
CRVAL1 := ps.RA
Expand Down Expand Up @@ -383,61 +382,45 @@ func (ps *PlateSolver) FindSourceMatches(tolerance geometry.InvariantFeatureTole
precision := 4

// Create a map to index source triangles by their quantized invariant features:
sourceTriangleIndex := make(map[string][]catalog.SourceAsterism)
sourceTriangleIndex := make(map[string][]catalog.SourceAsterism, len(sourceAsterisms))

// Compute invariant features for source triangles and index them:
for _, source := range sourceAsterisms {
key := utils.QuantizeFeatures(source.Features, precision)
sourceTriangleIndex[key] = append(sourceTriangleIndex[key], source)
}

// Define the tolerance for matching the invariant features:
matches := make([]Match, 0)

// Limit the number of concurrent goroutines to the number of CPUs times 20:
// This seems to be an optimal number of goroutines to run concurrently:
limit := runtime.NumCPU() * 20
// Setup a wait group for the goroutines:
wg.Add(len(asterisms))

// Semaphore to safely limit concurrent goroutines:
semaphore := make(chan struct{}, limit)

for i, asterism := range asterisms {
// Quantize the asterism's features to create a key:
key := utils.QuantizeFeatures(asterism.Features, precision)

// Get the source triangles with the same invariant features, e.g. by looking for matching source
// triangles in the index:
if sources, found := sourceTriangleIndex[key]; found {
var wg sync.WaitGroup
wg.Add(len(sources))

for _, source := range sources {
semaphore <- struct{}{}
// Iterate over the asterisms and attempt to match them with the catalog:
for _, asterism := range asterisms {
go func() {
defer wg.Done()
// Quantize the asterism's features to create a key:
key := utils.QuantizeFeatures(asterism.Features, precision)

// Get the source triangles with the same invariant features, e.g. by looking for matching source
// triangles in the index:
if sources, found := sourceTriangleIndex[key]; found {
// Attempt to match the individual stars to sources in the catalog, using the star triangle and source triangle:
go func(asterism astrometry.Asterism, source catalog.SourceAsterism) {
defer wg.Done()
defer func() { <-semaphore }()
source := sources[0]

match, err := ps.MatchAsterismsWithCatalog(asterism, source, tolerance)
match, err := ps.MatchAsterismsWithCatalog(asterism, source, tolerance)

if err == nil && match != nil {
matches = append(matches, match...)
}
}(asterism, source)
if err == nil && match != nil {
matches = append(matches, match...)
}
}

// Wait for all goroutines to finish:
wg.Wait()
}

// If we have more than 32 matches, we can stop looking for more, this seems to be an optimal
// amount of matches to plate solve to a high degree of accuracy, this covers more than 30% of
// the total source count.
if len(matches) >= 32 && i < 40 {
break
}
}()
}

// Wait for all goroutines to finish:
wg.Wait()

return matches, nil
}

Expand Down

0 comments on commit 66e7c15

Please sign in to comment.