| 1 | package irelatescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/ |
| 2 | |
| 3 | // parallel implements a parallel chrom-sweep. |
| 4 | // broad design is covered in design.md in the irelate package directory. |
| 5 | // In actual fact, there are a number of complexities; most of them relate to |
| 6 | // maintaining intervals in sorted order (and keeping chunks in sorted order) |
| 7 | // while allowing a good level of parallelism. |
| 8 | |
| 9 | // more detailed explanations are provided whenever a channel is initialized |
| 10 | // as channels are our main means of keeping order. |
| 11 | // For example |
| 12 | // tochannels := make(chan chan chan []interfaces.Relatable, 2+nprocs/2) |
| 13 | // Seems to have excessive use of channels, but we actually do need this since |
| 14 | // we have 2 levels of parallelization. |
| 15 | // One level is by chunk of query intervals. |
| 16 | // The next is by sub-chunk within the query chunks. |
| 17 | // The 3rd chan is a place-holder so that the work() function, which calls |
| 18 | // the user-defined fn() can be done concurrently (in a go routine). |
| 19 | |
| 20 | // The broad pattern used throughout is to send a channel (K) into another |
| 21 | // channel (PARENT) to keep order and then send K into a worker goroutine |
| 22 | // that sends intervals or []intervals into K. |
| 23 | |
| 24 | // I have done much tuning; the areas that affect performance are how the work() |
| 25 | // is parallelized (see the code-block that calls work()). And how the query |
| 26 | // chunks are determined. If the query chunks are too small (< 100 intervals), |
| 27 | // we have a lot of overhead in tracking that chunk that only requires a little |
| 28 | // computation. Unless the databases are very dense, then having the query chunks |
| 29 | // quite large helps parallelization. This is an area of potential optimization, |
| 30 | // though no obvious candidates have emerged. |
| 31 | |
| 32 | import ( |
| 33 | "fmtscip-go gomod github.com/golang/go/src go1.23.0 fmt/" |
| 34 | "ioscip-go gomod github.com/golang/go/src go1.23.0 io/" |
| 35 | "logscip-go gomod github.com/golang/go/src go1.23.0 log/" |
| 36 | "osscip-go gomod github.com/golang/go/src go1.23.0 os/" |
| 37 | "runtimescip-go gomod github.com/golang/go/src go1.23.0 runtime/" |
| 38 | "sortscip-go gomod github.com/golang/go/src go1.23.0 sort/" |
| 39 | |
| 40 | "github.com/brentp/irelate/interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/" |
| 41 | ) |
| 42 | |
| 43 | func getStartscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/getStart().(v interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, s int) int { |
| 44 | if ci, ok := v.(interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.CIFacescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/CIFace#); ok { |
| 45 | a, _, ok := ci.CIPosscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/CIFace#CIPos.() |
| 46 | if ok && int(a) < s { |
| 47 | return int(a) |
| 48 | } |
| 49 | } |
| 50 | return s |
| 51 | } |
| 52 | |
| 53 | func getEndscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/getEnd().(v interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, e int) int { |
| 54 | if ci, ok := v.(interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.CIFacescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/CIFace#); ok { |
| 55 | _, b, ok := ci.CIEndscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/CIFace#CIEnd.() |
| 56 | if ok && int(b) > e { |
| 57 | return int(b) |
| 58 | } |
| 59 | } |
| 60 | return e |
| 61 | } |
| 62 | |
| 63 | func minscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/min().(a, b int) int { |
| 64 | if a < b { |
| 65 | return a |
| 66 | } |
| 67 | return b |
| 68 | } |
| 69 | |
| 70 | func maxscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/max().(a, b int) int { |
| 71 | if a > b { |
| 72 | return a |
| 73 | } |
| 74 | return b |
| 75 | } |
| 76 | |
| 77 | type sliceItscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/sliceIt# struct { |
| 78 | slicescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/sliceIt#slice. []interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable# |
| 79 | iscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/sliceIt#i. int |
| 80 | } |
| 81 | |
| 82 | func (s *sliceItscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/sliceIt#) Nextscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/sliceIt#Next().() (interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, error) { |
| 83 | if s.iscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/sliceIt#i. < len(s.slicescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/sliceIt#slice.) { |
| 84 | v := s.slicescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/sliceIt#slice.[s.iscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/sliceIt#i.] |
| 85 | s.iscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/sliceIt#i. += 1 |
| 86 | return v, nil |
| 87 | } |
| 88 | s.slicescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/sliceIt#slice. = nil |
| 89 | return nil, ioscip-go gomod github.com/golang/go/src go1.23.0 io/.EOFscip-go gomod github.com/golang/go/src go1.23.0 io/EOF. |
| 90 | |
| 91 | } |
| 92 | func (s *sliceItscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/sliceIt#) Closescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/sliceIt#Close().() error { |
| 93 | return nil |
| 94 | } |
| 95 | |
| 96 | func sliceToIteratorscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/sliceToIterator().(A []interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#) interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.RelatableIteratorscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/RelatableIterator# { |
| 97 | return &sliceItscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/sliceIt#{A, 0} |
| 98 | } |
| 99 | |
| 100 | // islice makes []interfaces.Relatable sortable. |
| 101 | type islicescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/islice# []interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable# |
| 102 | |
| 103 | func (i islicescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/islice#) Lenscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/islice#Len().() int { |
| 104 | return len(i) |
| 105 | } |
| 106 | |
| 107 | func (i islicescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/islice#) Lessscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/islice#Less().(a, b int) bool { |
| 108 | if i[a].Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.() < i[b].Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.() { |
| 109 | return true |
| 110 | } |
| 111 | if i[a].Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.() == i[b].Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.() && i[a].Endscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#End.() <= i[b].Endscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#End.() { |
| 112 | return true |
| 113 | } |
| 114 | return false |
| 115 | } |
| 116 | |
| 117 | func (is islicescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/islice#) Swapscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/islice#Swap().(i, j int) { |
| 118 | is[i], is[j] = is[j], is[i] |
| 119 | } |
| 120 | |
| 121 | type posscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/pos# struct { |
| 122 | chromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/pos#chrom. string |
| 123 | startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/pos#start. int |
| 124 | endscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/pos#end. int |
| 125 | } |
| 126 | |
| 127 | func (p posscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/pos#) Chromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/pos#Chrom().() string { |
| 128 | return p.chromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/pos#chrom. |
| 129 | } |
| 130 | func (p posscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/pos#) Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/pos#Start().() uint32 { |
| 131 | return uint32(p.startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/pos#start.) |
| 132 | } |
| 133 | func (p posscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/pos#) Endscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/pos#End().() uint32 { |
| 134 | return uint32(p.endscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/pos#end.) |
| 135 | } |
| 136 | |
| 137 | // make a set of streams ready to be sent to irelate. |
| 138 | func makeStreamsscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/makeStreams().(receiver chan []interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.RelatableIteratorscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/RelatableIterator#, mustSort bool, A []interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, lastChrom string, minStart int, maxEnd int, dbs ...interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Queryablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Queryable#) { |
| 139 | |
| 140 | if mustSort { |
| 141 | sortscip-go gomod github.com/golang/go/src go1.23.0 sort/.Sortscip-go gomod github.com/golang/go/src go1.23.0 sort/Sort().(islicescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/islice#(A)) |
| 142 | } |
| 143 | |
| 144 | streams := make([]interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.RelatableIteratorscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/RelatableIterator#, 0, len(dbs)+1) |
| 145 | streams = append(streams, sliceToIteratorscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/sliceToIterator().(A)) |
| 146 | p := posscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/pos#{lastChrom, minStart, maxEnd} |
| 147 | |
| 148 | for _, db := range dbs { |
| 149 | stream, err := db.Queryscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Queryable#Query.(p) |
| 150 | if err != nil { |
| 151 | logscip-go gomod github.com/golang/go/src go1.23.0 log/.Fatalscip-go gomod github.com/golang/go/src go1.23.0 log/Fatal().(err) |
| 152 | } |
| 153 | streams = append(streams, stream) |
| 154 | } |
| 155 | receiver <- streams |
| 156 | close(receiver) |
| 157 | } |
| 158 | |
| 159 | func checkOverlapscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/checkOverlap().(a, b interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#) bool { |
| 160 | return b.Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.() < a.Endscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#End.() |
| 161 | } |
| 162 | |
| 163 | func lessscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/less().(a, b interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#) bool { |
| 164 | return a.Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.() < b.Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.() || (a.Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.() == b.Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.() && a.Endscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#End.() < b.Endscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#End.()) |
| 165 | } |
| 166 | |
| 167 | type ciRelscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/ciRel# struct { |
| 168 | interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/ciRel#Relatable. |
| 169 | indexscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/ciRel#index. int |
| 170 | } |
| 171 | |
| 172 | func (ci ciRelscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/ciRel#) Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/ciRel#Start().() uint32 { |
| 173 | return uint32(getStartscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/getStart().(ci.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/ciRel#Relatable., int(ci.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/ciRel#Relatable..Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.()))) |
| 174 | } |
| 175 | |
| 176 | func (ci ciRelscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/ciRel#) Endscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/ciRel#End().() uint32 { |
| 177 | return uint32(getEndscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/getEnd().(ci.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/ciRel#Relatable., int(ci.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/ciRel#Relatable..Endscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#End.()))) |
| 178 | } |
| 179 | |
| 180 | // PIRelate implements a parallel IRelate |
| 181 | func PIRelatescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/PIRelate().(chunk int, maxGap int, qstream interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.RelatableIteratorscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/RelatableIterator#, ciExtend bool, fn func(interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#), dbs ...interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Queryablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Queryable#) interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.RelatableChannelscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/RelatableChannel# { |
| 182 | nprocs := runtimescip-go gomod github.com/golang/go/src go1.23.0 runtime/.GOMAXPROCSscip-go gomod github.com/golang/go/src go1.23.0 runtime/GOMAXPROCS().(-1) |
| 183 | // final interval stream sent back to caller. |
| 184 | intersected := make(chan interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, 2048) |
| 185 | |
| 186 | // receivers keeps the interval chunks in order. |
| 187 | receivers := make(chan chan []interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.RelatableIteratorscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/RelatableIterator#, 1) |
| 188 | |
| 189 | // to channels recieves channels that accept intervals from IRelate to be sent for merging. |
| 190 | // we send slices of intervals to reduce locking. |
| 191 | tochannels := make(chan chan chan []interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, 2+nprocs/2) |
| 192 | |
| 193 | verbose := osscip-go gomod github.com/golang/go/src go1.23.0 os/.Getenvscip-go gomod github.com/golang/go/src go1.23.0 os/Getenv().("IRELATE_VERBOSE") == "TRUE" |
| 194 | |
| 195 | // the user-defined callback runs int it's own goroutine. |
| 196 | // call on the relatable itself. but with all of the associated intervals. |
| 197 | work := func(rels []interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, fn func(interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#)) chan []interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable# { |
| 198 | ch := make(chan []interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, 0) |
| 199 | go func() { |
| 200 | if fn != nil { |
| 201 | for _, r := range rels { |
| 202 | fn(r) |
| 203 | } |
| 204 | } |
| 205 | ch <- rels |
| 206 | close(ch) |
| 207 | }() |
| 208 | return ch |
| 209 | } |
| 210 | if ciExtend { |
| 211 | |
| 212 | work = func(rels []interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, fn func(interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#)) chan []interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable# { |
| 213 | ch := make(chan []interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, 0) |
| 214 | go func() { |
| 215 | if fn != nil { |
| 216 | for _, r := range rels { |
| 217 | fn(r.(ciRelscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/ciRel#).Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/ciRel#Relatable.) |
| 218 | } |
| 219 | } |
| 220 | ch <- rels |
| 221 | close(ch) |
| 222 | }() |
| 223 | return ch |
| 224 | |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | // pull the intervals from IRelate, call fn() and (via work()) send chunks to be merged. |
| 229 | // calling fn() is a bottleneck. so we make sub-chunks and process them in a separate go-routine |
| 230 | // in work() |
| 231 | // inner channel keeps track of the order for each big chunk |
| 232 | go func() { |
| 233 | |
| 234 | for streamsChan := range receivers { |
| 235 | |
| 236 | inner := make(chan chan []interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, nprocs) |
| 237 | tochannels <- inner |
| 238 | |
| 239 | // push a channel to to channels out here |
| 240 | // and then push to that channel inside this goroutine. |
| 241 | // this maintains order of the intervals. |
| 242 | go func(streams []interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.RelatableIteratorscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/RelatableIterator#) { |
| 243 | N := 400 |
| 244 | //saved := make([]interfaces.Relatable, N) |
| 245 | iterator := IRelatescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/IRelate().(checkOverlapscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/checkOverlap()., 0, lessscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/less()., streams...) |
| 246 | saved := make([]interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, N) |
| 247 | k := 0 |
| 248 | |
| 249 | for { |
| 250 | interval, err := iterator.Nextscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/RelatableIterator#Next.() |
| 251 | if err == ioscip-go gomod github.com/golang/go/src go1.23.0 io/.EOFscip-go gomod github.com/golang/go/src go1.23.0 io/EOF. { |
| 252 | iterator.Closescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/RelatableIterator#Close.() |
| 253 | break |
| 254 | } |
| 255 | saved[k] = interval |
| 256 | k++ |
| 257 | |
| 258 | if k == N { |
| 259 | inner <- work(saved, fn) |
| 260 | k = 0 |
| 261 | saved = make([]interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, N) |
| 262 | } |
| 263 | |
| 264 | } |
| 265 | if k > 0 { |
| 266 | inner <- work(saved[:k], fn) |
| 267 | } |
| 268 | close(inner) |
| 269 | }(<-streamsChan) // only one, just used a chan for ordering. |
| 270 | } |
| 271 | close(tochannels) |
| 272 | }() |
| 273 | |
| 274 | go mergeIntervalsscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/mergeIntervals().(tochannels, intersected, ciExtend) |
| 275 | |
| 276 | // split the query intervals into chunks and send for processing to irelate. |
| 277 | go func() { |
| 278 | |
| 279 | A := make([]interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, 0, chunk/2) |
| 280 | |
| 281 | lastStart := -10 |
| 282 | lastChrom := "" |
| 283 | minStart := int(^uint32(0) >> 1) |
| 284 | maxEnd := 0 |
| 285 | var totalParsed, totalSkipped, c, idx int |
| 286 | for { |
| 287 | v, err := qstream.Nextscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/RelatableIterator#Next.() |
| 288 | if err == ioscip-go gomod github.com/golang/go/src go1.23.0 io/.EOFscip-go gomod github.com/golang/go/src go1.23.0 io/EOF. { |
| 289 | qstream.Closescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/RelatableIterator#Close.() |
| 290 | } |
| 291 | if v == nil { |
| 292 | break |
| 293 | } |
| 294 | |
| 295 | if ciExtend { |
| 296 | // turn it into an object that will return the ci bounds for Start(), End() |
| 297 | v = ciRelscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/ciRel#{v, idx} |
| 298 | idx++ |
| 299 | } |
| 300 | |
| 301 | // these will be based on CIPOS, CIEND if ciExtend is true |
| 302 | s, e := int(v.Startscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Start.()), int(v.Endscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#End.()) |
| 303 | |
| 304 | // end chunk when: |
| 305 | // 1. switch chroms |
| 306 | // 2. see maxGap bases between adjacent intervals (currently looks at start only) |
| 307 | // 3. reaches chunkSize (and has at least a gap of 2 bases from last interval). |
| 308 | if v.Chromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Chrom.() != lastChrom || (len(A) > 2048 && s-lastStart > maxGap) || ((s-lastStart > 25 && len(A) >= chunk) || len(A) >= chunk+200) || s-lastStart > 10*maxGap { |
| 309 | if len(A) > 0 { |
| 310 | // we push a channel onto a queue (another channel) and use that as the output order. |
| 311 | ch := make(chan []interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.RelatableIteratorscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/RelatableIterator#, 0) |
| 312 | receivers <- ch |
| 313 | // send work to IRelate |
| 314 | go makeStreamsscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/makeStreams().(ch, ciExtend, A, lastChrom, minStart, maxEnd, dbs...) |
| 315 | c++ |
| 316 | if verbose { |
| 317 | if lastChrom == v.Chromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Chrom.() { |
| 318 | totalSkipped += s - lastStart |
| 319 | } |
| 320 | totalParsed += maxEnd - minStart |
| 321 | var mem runtimescip-go gomod github.com/golang/go/src go1.23.0 runtime/.MemStatsscip-go gomod github.com/golang/go/src go1.23.0 runtime/MemStats# |
| 322 | runtimescip-go gomod github.com/golang/go/src go1.23.0 runtime/.ReadMemStatsscip-go gomod github.com/golang/go/src go1.23.0 runtime/ReadMemStats().(&mem) |
| 323 | logscip-go gomod github.com/golang/go/src go1.23.0 log/.Printlnscip-go gomod github.com/golang/go/src go1.23.0 log/Println().("intervals in current chunk:", len(A), fmtscip-go gomod github.com/golang/go/src go1.23.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.23.0 fmt/Sprintf().("%s:%d-%d", lastChrom, minStart, maxEnd), "gap:", s-lastStart) |
| 324 | logscip-go gomod github.com/golang/go/src go1.23.0 log/.Printlnscip-go gomod github.com/golang/go/src go1.23.0 log/Println().("\tc:", c, "receivers:", len(receivers), "tochannels:", len(tochannels), "intersected:", len(intersected)) |
| 325 | logscip-go gomod github.com/golang/go/src go1.23.0 log/.Printfscip-go gomod github.com/golang/go/src go1.23.0 log/Printf().("\tmemory use: %dMB , heap in use: %dMB\n", mem.Allocscip-go gomod github.com/golang/go/src go1.23.0 runtime/MemStats#Alloc./uint64(1000*1000), |
| 326 | mem.HeapInusescip-go gomod github.com/golang/go/src go1.23.0 runtime/MemStats#HeapInuse./uint64(1000*1000)) |
| 327 | logscip-go gomod github.com/golang/go/src go1.23.0 log/.Printfscip-go gomod github.com/golang/go/src go1.23.0 log/Printf().("\ttotal bases skipped / parsed: %d / %d (%.2f)\n", totalSkipped, totalParsed, float64(totalSkipped)/float64(totalParsed)) |
| 328 | } |
| 329 | |
| 330 | } |
| 331 | lastStart = s |
| 332 | lastChrom, minStart, maxEnd = v.Chromscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/IPosition#Chrom.(), s, e |
| 333 | A = make([]interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, 0, chunk/2) |
| 334 | } else { |
| 335 | lastStart = s |
| 336 | maxEnd = maxscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/max().(e, maxEnd) |
| 337 | minStart = minscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/min().(s, minStart) |
| 338 | } |
| 339 | |
| 340 | A = append(A, v) |
| 341 | } |
| 342 | |
| 343 | if len(A) > 0 { |
| 344 | ch := make(chan []interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.RelatableIteratorscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/RelatableIterator#, 0) |
| 345 | receivers <- ch |
| 346 | go makeStreamsscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/makeStreams().(ch, ciExtend, A, lastChrom, minStart, maxEnd, dbs...) |
| 347 | c++ |
| 348 | } |
| 349 | close(receivers) |
| 350 | }() |
| 351 | return intersected |
| 352 | } |
| 353 | |
| 354 | func mergeIntervalsscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/mergeIntervals().(tochannels chan chan chan []interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, intersected chan interfacesscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate/interfaces`/Relatable#, ciExtend bool) { |
| 355 | // merge the intervals from different channels keeping order. |
| 356 | // 2 separate function code-blocks so there is no performance hit when they don't |
| 357 | // care about the cipos. |
| 358 | if ciExtend { |
| 359 | nextPrint := 0 |
| 360 | q := make(map[int]ciRelscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/ciRel#, 100) |
| 361 | for och := range tochannels { |
| 362 | for ch := range och { |
| 363 | for intervals := range ch { |
| 364 | for _, interval := range intervals { |
| 365 | ci := interval.(ciRelscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/ciRel#) |
| 366 | if ci.indexscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/ciRel#index. == nextPrint { |
| 367 | intersected <- ci.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/ciRel#Relatable. |
| 368 | nextPrint++ |
| 369 | } else { |
| 370 | q[ci.indexscip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/ciRel#index.] = ci |
| 371 | for { |
| 372 | n, ok := q[nextPrint] |
| 373 | if !ok { |
| 374 | break |
| 375 | } |
| 376 | delete(q, nextPrint) |
| 377 | intersected <- n.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/ciRel#Relatable. |
| 378 | nextPrint++ |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | // empty out the q |
| 383 | for { |
| 384 | n, ok := q[nextPrint] |
| 385 | if !ok { |
| 386 | break |
| 387 | } |
| 388 | delete(q, nextPrint) |
| 389 | intersected <- n.Relatablescip-go gomod github.com/brentp/irelate . `github.com/brentp/irelate`/ciRel#Relatable. |
| 390 | nextPrint++ |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | } else { |
| 396 | for och := range tochannels { |
| 397 | for ch := range och { |
| 398 | for intervals := range ch { |
| 399 | for _, interval := range intervals { |
| 400 | intersected <- interval |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | close(intersected) |
| 407 | } |