21139: Reduce memory usage in tiling. 21139-import-mem
authorTom Clegg <tom@curii.com>
Thu, 26 Oct 2023 20:33:37 +0000 (16:33 -0400)
committerTom Clegg <tom@curii.com>
Thu, 26 Oct 2023 20:33:37 +0000 (16:33 -0400)
Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tom@curii.com>

import.go
slicenumpy.go
taglib.go
taglib_test.go
tilelib.go

index 2f1a268511c0e1cc709a2e43656788e5116c9e1c..9bc336af107acf0c4c3ffea9f95f39de0a4b3a40 100644 (file)
--- a/import.go
+++ b/import.go
@@ -185,7 +185,7 @@ func (cmd *importer) runBatches(stdout io.Writer, inputs []string) error {
                Client:      arvadosClientFromEnv,
                ProjectUUID: cmd.projectUUID,
                APIAccess:   true,
-               RAM:         700000000000,
+               RAM:         350000000000,
                VCPUs:       96,
                Priority:    cmd.priority,
                KeepCache:   1,
@@ -448,7 +448,7 @@ func (cmd *importer) tileInputs(tilelib *tileLibrary, infiles []string) error {
        go close(todo)
        var tileJobs sync.WaitGroup
        var running int64
-       for i := 0; i < runtime.GOMAXPROCS(-1)*2; i++ {
+       for i := 0; i < runtime.GOMAXPROCS(-1); i++ {
                tileJobs.Add(1)
                atomic.AddInt64(&running, 1)
                go func() {
index e60c50adb1e883999c3e0024a5cd460282f42972..34cd777458ab93d60505b5dfa19b3aaa0280dd32 100644 (file)
@@ -361,7 +361,7 @@ func (cmd *sliceNumpy) run(prog string, args []string, stdin io.Reader, stdout,
                                return err
                        }
                        foundthistag := false
-                       taglib.FindAll(tiledata[:len(tiledata)-1], func(tagid tagID, offset, _ int) {
+                       taglib.FindAll(bufio.NewReader(bytes.NewReader(tiledata[:len(tiledata)-1])), nil, func(tagid tagID, offset, _ int) {
                                if !foundthistag && tagid == libref.Tag {
                                        foundthistag = true
                                        return
index 7ccce151871f7b4f178e3727d4acfe09ef5b1ab2..36e1d2775d6b8372e63ef0bddd065dbdc580fc1d 100644 (file)
--- a/taglib.go
+++ b/taglib.go
@@ -6,7 +6,6 @@ package lightning
 
 import (
        "bufio"
-       "bytes"
        "fmt"
        "io"
 )
@@ -44,29 +43,58 @@ func (taglib *tagLibrary) Load(rdr io.Reader) error {
        return taglib.setTags(seqs)
 }
 
-func (taglib *tagLibrary) FindAll(buf []byte, fn func(id tagID, pos, taglen int)) {
+func (taglib *tagLibrary) FindAll(in *bufio.Reader, passthrough io.Writer, fn func(id tagID, pos, taglen int)) error {
+       var window = make([]byte, 0, taglib.keylen*1000)
        var key tagmapKey
-       valid := 0 // if valid < taglib.keylen, key has "no data" zeroes that are otherwise indistinguishable from "A"
-       for i, base := range buf {
+       for offset := 0; ; {
+               base, err := in.ReadByte()
+               if err == io.EOF {
+                       return nil
+               } else if err != nil {
+                       return err
+               } else if base == '\r' || base == '\n' {
+                       if buf, err := in.Peek(1); err == nil && len(buf) > 0 && buf[0] == '>' {
+                               return nil
+                       } else if err == io.EOF {
+                               return nil
+                       }
+                       continue
+               } else if base == '>' || base == ' ' {
+                       return fmt.Errorf("unexpected char %q at offset %d in fasta data", base, offset)
+               }
+
+               if passthrough != nil {
+                       _, err = passthrough.Write([]byte{base})
+                       if err != nil {
+                               return err
+                       }
+               }
                if !isbase[int(base)] {
-                       valid = 0
+                       // 'N' or various other chars meaning exact
+                       // base not known
+                       window = window[:0]
                        continue
                }
+               offset++
+               window = append(window, base)
+               if len(window) == cap(window) {
+                       copy(window, window[len(window)-taglib.keylen:])
+                       window = window[:taglib.keylen]
+               }
                key = ((key << 2) | twobit[int(base)]) & taglib.keymask
-               valid++
 
-               if valid < taglib.keylen {
+               if len(window) < taglib.keylen {
                        continue
                } else if taginfo, ok := taglib.tagmap[key]; !ok {
                        continue
-               } else if tagstart := i - taglib.keylen + 1; len(taginfo.tagseq) > taglib.keylen && (len(buf) < i+len(taginfo.tagseq) || !bytes.Equal(taginfo.tagseq, buf[i:i+len(taginfo.tagseq)])) {
-                       // key portion matches, but not the entire tag
-                       continue
+               } else if len(taginfo.tagseq) != taglib.keylen {
+                       return fmt.Errorf("assertion failed: len(%q) != keylen %d", taginfo.tagseq, taglib.keylen)
                } else {
-                       fn(taginfo.id, tagstart, len(taginfo.tagseq))
-                       valid = 0 // don't try to match overlapping tags
+                       fn(taginfo.id, offset-taglib.keylen, len(taginfo.tagseq))
+                       window = window[:0] // don't try to match overlapping tags
                }
        }
+       return nil
 }
 
 func (taglib *tagLibrary) Len() int {
index bd9361482fae7c4eabe337407249567ed0f8e27b..3ab5c453e2207f3426d1b86927a00c6e108e40de 100644 (file)
@@ -6,6 +6,7 @@ package lightning
 
 import (
        "bufio"
+       "bytes"
        "fmt"
        "io"
        "math/rand"
@@ -45,7 +46,7 @@ gactctagcagagtggccagccac
        c.Assert(err, check.IsNil)
        haystack := []byte(`ggagaactgtgctccgccttcagaccccccccccccccccccccacacatgctagcgcgtcggggtgggggggggggggggggggggggggggactctagcagagtggccagccac`)
        var matches []tagMatch
-       taglib.FindAll(haystack, func(id tagID, pos, taglen int) {
+       taglib.FindAll(bufio.NewReader(bytes.NewBuffer(haystack)), nil, func(id tagID, pos, taglen int) {
                matches = append(matches, tagMatch{id, pos, taglen})
        })
        c.Check(matches, check.DeepEquals, []tagMatch{{0, 0, 24}, {1, 44, 24}, {2, 92, 24}})
@@ -90,7 +91,7 @@ func (s *taglibSuite) TestFindAllRealisticSize(c *check.C) {
        c.Assert(err, check.IsNil)
        c.Logf("@%v find tags in input", time.Since(start))
        var matches []tagMatch
-       taglib.FindAll(haystack, func(id tagID, pos, taglen int) {
+       taglib.FindAll(bufio.NewReader(bytes.NewBuffer(haystack)), nil, func(id tagID, pos, taglen int) {
                matches = append(matches, tagMatch{id, pos, taglen})
        })
        c.Logf("@%v done", time.Since(start))
index b6dc3671207c619d6ffe578e5a292f69eab334af..8d35fa4ef8599ab99b7df54937d2b3061f993acd 100644 (file)
@@ -555,31 +555,6 @@ type importStats struct {
 
 func (tilelib *tileLibrary) TileFasta(filelabel string, rdr io.Reader, matchChromosome *regexp.Regexp, isRef bool) (tileSeq, []importStats, error) {
        ret := tileSeq{}
-       type jobT struct {
-               label string
-               fasta []byte
-       }
-       todo := make(chan jobT, 1)
-       scanner := bufio.NewScanner(rdr)
-       scanner.Buffer(make([]byte, 256), 1<<29) // 512 MiB, in case fasta does not have line breaks
-       go func() {
-               defer close(todo)
-               var fasta []byte
-               var seqlabel string
-               for scanner.Scan() {
-                       buf := scanner.Bytes()
-                       if len(buf) > 0 && buf[0] == '>' {
-                               todo <- jobT{seqlabel, append([]byte(nil), fasta...)}
-                               seqlabel, fasta = strings.SplitN(string(buf[1:]), " ", 2)[0], fasta[:0]
-                               log.Debugf("%s %s reading fasta", filelabel, seqlabel)
-                       } else if len(buf) > 0 && buf[0] == '#' {
-                               // ignore testdata comment
-                       } else {
-                               fasta = append(fasta, bytes.ToLower(buf)...)
-                       }
-               }
-               todo <- jobT{seqlabel, fasta}
-       }()
        type foundtag struct {
                pos   int
                tagid tagID
@@ -591,22 +566,50 @@ func (tilelib *tileLibrary) TileFasta(filelabel string, rdr io.Reader, matchChro
        skippedSequences := 0
        taglen := tilelib.taglib.TagLen()
        var stats []importStats
-       for job := range todo {
-               if len(job.fasta) == 0 {
-                       continue
-               } else if !matchChromosome.MatchString(job.label) {
+
+       in := bufio.NewReader(rdr)
+readall:
+       for {
+               var seqlabel string
+               // Advance to next '>', then
+               // read seqlabel up to \r?\n
+       readseqlabel:
+               for seqlabelStarted := false; ; {
+                       rune, _, err := in.ReadRune()
+                       if err == io.EOF {
+                               break readall
+                       } else if err != nil {
+                               return nil, nil, err
+                       }
+                       switch {
+                       case rune == '\r':
+                       case seqlabelStarted && rune == '\n':
+                               break readseqlabel
+                       case seqlabelStarted:
+                               seqlabel += string(rune)
+                       case rune == '>':
+                               seqlabelStarted = true
+                       default:
+                       }
+               }
+               log.Debugf("%s %s reading fasta", filelabel, seqlabel)
+               if !matchChromosome.MatchString(seqlabel) {
                        skippedSequences++
                        continue
                }
-               log.Debugf("%s %s tiling", filelabel, job.label)
+               log.Debugf("%s %s tiling", filelabel, seqlabel)
 
+               fasta := bytes.NewBuffer(nil)
                found = found[:0]
-               tilelib.taglib.FindAll(job.fasta, func(tagid tagID, pos, taglen int) {
+               err := tilelib.taglib.FindAll(in, fasta, func(tagid tagID, pos, taglen int) {
                        found = append(found, foundtag{pos: pos, tagid: tagid})
                })
+               if err != nil {
+                       return nil, nil, err
+               }
                totalFoundTags += len(found)
                if len(found) == 0 {
-                       log.Warnf("%s %s no tags found", filelabel, job.label)
+                       log.Warnf("%s %s no tags found", filelabel, seqlabel)
                }
 
                droppedDup := 0
@@ -624,7 +627,7 @@ func (tilelib *tileLibrary) TileFasta(filelabel string, rdr io.Reader, matchChro
                                }
                        }
                        droppedDup = len(found) - dst
-                       log.Infof("%s %s dropping %d non-unique tags", filelabel, job.label, droppedDup)
+                       log.Infof("%s %s dropping %d non-unique tags", filelabel, seqlabel, droppedDup)
                        found = found[:dst]
                }
 
@@ -635,11 +638,11 @@ func (tilelib *tileLibrary) TileFasta(filelabel string, rdr io.Reader, matchChro
                                found[i] = found[x]
                        }
                        droppedOOO = len(found) - len(keep)
-                       log.Infof("%s %s dropping %d out-of-order tags", filelabel, job.label, droppedOOO)
+                       log.Infof("%s %s dropping %d out-of-order tags", filelabel, seqlabel, droppedOOO)
                        found = found[:len(keep)]
                }
 
-               log.Infof("%s %s getting %d librefs", filelabel, job.label, len(found))
+               log.Infof("%s %s getting %d librefs", filelabel, seqlabel, len(found))
                throttle := &throttle{Max: runtime.NumCPU()}
                path = path[:len(found)]
                var lowquality int64
@@ -655,30 +658,30 @@ func (tilelib *tileLibrary) TileFasta(filelabel string, rdr io.Reader, matchChro
                                        startpos = f.pos
                                }
                                if i == len(found)-1 {
-                                       endpos = len(job.fasta)
+                                       endpos = fasta.Len()
                                } else {
                                        endpos = found[i+1].pos + taglen
                                }
-                               path[i] = tilelib.getRef(f.tagid, job.fasta[startpos:endpos], isRef)
-                               if countBases(job.fasta[startpos:endpos]) != endpos-startpos {
+                               path[i] = tilelib.getRef(f.tagid, fasta.Bytes()[startpos:endpos], isRef)
+                               if countBases(fasta.Bytes()[startpos:endpos]) != endpos-startpos {
                                        atomic.AddInt64(&lowquality, 1)
                                }
                        }()
                }
                throttle.Wait()
 
-               log.Infof("%s %s copying path", filelabel, job.label)
+               log.Infof("%s %s copying path", filelabel, seqlabel)
 
                pathcopy := make([]tileLibRef, len(path))
                copy(pathcopy, path)
-               ret[job.label] = pathcopy
+               ret[seqlabel] = pathcopy
 
-               basesIn := countBases(job.fasta)
-               log.Infof("%s %s fasta in %d coverage in %d path len %d low-quality %d", filelabel, job.label, len(job.fasta), basesIn, len(path), lowquality)
+               basesIn := countBases(fasta.Bytes())
+               log.Infof("%s %s fasta in %d coverage in %d path len %d low-quality %d", filelabel, seqlabel, fasta.Len(), basesIn, len(path), lowquality)
                stats = append(stats, importStats{
                        InputFile:             filelabel,
-                       InputLabel:            job.label,
-                       InputLength:           len(job.fasta),
+                       InputLabel:            seqlabel,
+                       InputLength:           fasta.Len(),
                        InputCoverage:         basesIn,
                        PathLength:            len(path),
                        DroppedOutOfOrderTags: droppedOOO,
@@ -688,7 +691,7 @@ func (tilelib *tileLibrary) TileFasta(filelabel string, rdr io.Reader, matchChro
                totalPathLen += len(path)
        }
        log.Printf("%s tiled with total path len %d in %d sequences (skipped %d sequences that did not match chromosome regexp, skipped %d out-of-order tags)", filelabel, totalPathLen, len(ret), skippedSequences, totalFoundTags-totalPathLen)
-       return ret, stats, scanner.Err()
+       return ret, stats, nil
 }
 
 func (tilelib *tileLibrary) Len() int64 {