-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
164 lines (115 loc) · 4.19 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"flag"
"fmt"
"log"
// "math"
"math/rand"
"os"
"runtime"
"runtime/pprof"
// "sync"
"time"
. "github.com/bawjensen/dataplay/api"
. "github.com/bawjensen/dataplay/utility"
// . "github.com/bawjensen/dataplay/constants"
)
// ------------------------------------ Global variables -------------------------------------------
// ------------------------------------ Helper logic -----------------------------------------------
// ------------------------------------ Search logic -----------------------------------------------
func createSearchHandler(mapper func(interface{}, []*IntSet) *IntSet, prepper func(*IntSet, []*IntSet) []interface{}, visited []*IntSet) (inChan, outChan chan *IntSet) {
inChan, outChan = make(chan *IntSet), make(chan *IntSet)
go func() {
resultsChan := make(chan *IntSet)
for input := range inChan {
prepped := prepper(input, visited)
searchSet := NewIntSet()
for _, mapperInput := range prepped {
go func(mapperInput interface{}) {
resultsChan <- mapper(mapperInput, visited)
}(mapperInput)
}
for _ = range prepped {
searchSet.Union(<-resultsChan)
}
outChan <- searchSet
}
}()
return inChan, outChan
}
func createSearchIterator() (inChan, outChan chan *IntSet, visited []*IntSet) {
inChan, outChan = make(chan *IntSet), make(chan *IntSet)
visited = make([]*IntSet, NUM_VISITED_SETS, NUM_VISITED_SETS)
visited[MATCHES] = NewIntSet()
visited[PLAYERS] = NewIntSet()
visited[LEAGUE_BY_PLAYERS] = NewIntSet()
go func() {
leagueIn, leagueOut := createSearchHandler(SearchPlayerLeague, InputPrepperLeague, visited)
matchIn, matchOut := createSearchHandler(SearchPlayerMatch, InputPrepperMatch, visited)
rejectIn, rejectOut := createSearchHandler(SearchPlayerReject, InputPrepperReject, visited)
for input := range inChan {
leagueIn <- input
matchIn <- input
outputMatch := <-matchOut
if FILTERING_BY_LEAGUE {
fmt.Printf("\nGot match output (%d), sending into reject\n", outputMatch.Size())
rejectIn <- outputMatch // Reject all low-tier people gotten from matches
outputReject := <-rejectOut
outputMatch.Subtract(outputReject)
fmt.Printf("\nGot reject output (%d), removing from match output (%d)\n", outputReject.Size(), outputMatch.Size())
} else {
fmt.Printf("\nGot match output (%d)\n", outputMatch.Size())
}
outputLeague := <-leagueOut
fmt.Printf("\nGot league output (%d)\n", outputLeague.Size())
outputMatch.Union(outputLeague)
fmt.Printf("\nGot total unioned output (%d)\n", outputMatch.Size())
outChan <- outputMatch
}
}()
return inChan, outChan, visited
}
func search() {
in, out, visited := createSearchIterator()
initialSeeds := NewIntSet(51405, 10077)
newPlayers := initialSeeds
var start time.Time
for newPlayers.Size() > 0 {
start = time.Now()
visited[PLAYERS].Union(newPlayers)
fmt.Printf("\nvisited[PLAYERS]: %d\n", visited[PLAYERS].Size())
fmt.Printf( "visited[MATCHES]: %d\n", visited[MATCHES].Size())
fmt.Printf( "visited[LEAGUE_BY_PLAYERS]: %d\n", visited[LEAGUE_BY_PLAYERS].Size())
fmt.Printf( "newPlayers: %d\n\n", newPlayers.Size())
in <- newPlayers
newPlayers = <-out
log.Println("Number of goroutines after iteration:", runtime.NumGoroutine())
fmt.Printf("\nIteration: %v\n", time.Since(start))
}
}
func printTimeSince(startTime time.Time) {
fmt.Println("Total elapsedTime:", time.Since(startTime))
}
func main() {
// TImer for entire program
defer printTimeSince(time.Now())
// Seed random with nanoseconds
rand.Seed(time.Now().UTC().UnixNano())
// Set up cmd line flag
var cpuprofile = flag.String("prof", "", "write cpu profile to file")
// Handle cmd line flag behavior
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
// Set the number of parallel threads to use all CPUs
fmt.Println("Default GOMAXPROCS:", runtime.GOMAXPROCS(runtime.NumCPU())) // Note: Setting, but returns the old for output
fmt.Println("Running with GOMAXPROCS:", runtime.GOMAXPROCS(0))
// Run
search()
}