| 1 | #cxx . . $ `<file>/meryl/estimate.C`/include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | |
| 4 | #include "bio++.H"cxx . . $ `<file>/libbio/bio++.H`/ |
| 5 | #include "seqStream.H"cxx . . $ `<file>/libseq/seqStream.H`/ |
| 6 | #include "merStream.H"cxx . . $ `<file>/libseq/merStream.H`/ |
| 7 | #include "libmeryl.H"cxx . . $ `<file>/libmeryl/libmeryl.H`/ |
| 8 | #include "meryl.H"cxx . . $ `<file>/meryl/meryl.H`/ |
| 9 | |
| 10 | // Takes a memory limit in MB, returns the number of mers that we can fit in that memory size, |
| 11 | // assuming optimalNumberOfBuckets() below uses the same algorithm. |
| 12 | // |
| 13 | // For each possible number of buckets, try all poissible pointer widths. First we compute the |
| 14 | // number of mers that fit in a bucket pointer table of size 2^t storing N bits in the mer data |
| 15 | // table, then we check that the number of mers in the mer data table agrees with the width of the |
| 16 | // pointer table. |
| 17 | // |
| 18 | uint64cxx . . $ uint64# |
| 19 | estimateNumMersInMemorySizecxx . . $ estimateNumMersInMemorySize(523f3ef4c24d4155).(uint32cxx . . $ uint32# merSize, |
| 20 | uint64cxx . . $ uint64# mem, |
| 21 | bool positionsEnabled, |
| 22 | bool beVerbose) { |
| 23 | uint64cxx . . $ uint64# maxN = 0; |
| 24 | uint64cxx . . $ uint64# bestT = 0; |
| 25 | |
| 26 | uint64cxx . . $ uint64# memLimt = mem * 8; // Memory limit, in bits. |
| 27 | uint64cxx . . $ uint64# posPerMer = (positionsEnabled == false) ? 0 : 32; // Positions consume space, if enabled. |
| 28 | uint64cxx . . $ uint64# tMax = (merSize > 25) ? 50 : 2 * merSize - 2; // Max width of bucket pointer table. |
| 29 | |
| 30 | // t - prefix stored in the bucket pointer table; number of entries in the table |
| 31 | // N - width of a bucket pointer |
| 32 | |
| 33 | for (uint64cxx . . $ uint64# t=2; t < tMax; t++) { |
| 34 | for (uint64cxx . . $ uint64# N=1; N<40; N++) { |
| 35 | uint64cxx . . $ uint64# Nmin = uint64ONEcxx . . $ `libutil/util.h:51:10`! << (N - 1); // Minimum number of mers we want to fit in the table |
| 36 | uint64cxx . . $ uint64# Nmax = uint64ONEcxx . . $ `libutil/util.h:51:10`! << (N); // Maximum number of mers that can fit in the table |
| 37 | |
| 38 | uint64cxx . . $ uint64# bucketsize = (uint64ONEcxx . . $ `libutil/util.h:51:10`! << t) * N; // Size, in bits, of the pointer table |
| 39 | |
| 40 | uint64cxx . . $ uint64# n = (memLimt - bucketsize) / (2*merSize - t + posPerMer); // Number of mers we can fit into mer data table. |
| 41 | |
| 42 | if ((memLimt > bucketsize) && // pointer table small enough to fit in memory |
| 43 | (n > 0) && // at least some space to store mers |
| 44 | (n <= Nmax) && // enough space for the mers in the data table |
| 45 | (Nmin <= n) && // ...but not more than enough space |
| 46 | (maxN < n)) { // this value of t fits more mers that any other seen so far |
| 47 | maxN = n; |
| 48 | bestT = t; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | if (beVerbose) |
| 54 | fprintf(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, "Can fit "uint64FMTcxx . . $ `libutil/util.h:55:10`!" mers into table with prefix of "uint64FMTcxx . . $ `libutil/util.h:55:10`!" bits, using %.3fMB (%.3fMB for positions)\n", |
| 55 | maxN, |
| 56 | bestT, |
| 57 | (((uint64ONEcxx . . $ `libutil/util.h:51:10`! << bestT) * logBaseTwo64cxx . . $ logBaseTwo64(ecdaa1632f49f591).(maxN) + maxN * (2*merSize - bestT + posPerMer)) >> 3) / 1048576.0, |
| 58 | ((maxN * posPerMer) >> 3) / 1048576.0); |
| 59 | |
| 60 | return(maxN); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | |
| 65 | uint64cxx . . $ uint64# |
| 66 | estimateMemorycxx . . $ estimateMemory(d0900995b195fb9f).(uint32cxx . . $ uint32# merSize, |
| 67 | uint64cxx . . $ uint64# numMers, |
| 68 | bool positionsEnabled) { |
| 69 | |
| 70 | uint64cxx . . $ uint64# posPerMer = (positionsEnabled == false) ? 0 : 32; |
| 71 | uint64cxx . . $ uint64# tMax = (merSize > 25) ? 50 : 2 * merSize - 2; |
| 72 | |
| 73 | uint64cxx . . $ uint64# tMin = tMax; |
| 74 | uint64cxx . . $ uint64# memMin = UINT64_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:119:10`!; |
| 75 | |
| 76 | for (uint64cxx . . $ uint64# t=2; t < tMax; t++) { |
| 77 | uint64cxx . . $ uint64# N = logBaseTwo64cxx . . $ logBaseTwo64(ecdaa1632f49f591).(numMers); // Width of the bucket pointer table |
| 78 | uint64cxx . . $ uint64# memUsed = ((uint64ONEcxx . . $ `libutil/util.h:51:10`! << t) * logBaseTwo64cxx . . $ logBaseTwo64(ecdaa1632f49f591).(numMers) + numMers * (2 * merSize - t + posPerMer)) >> 3; |
| 79 | |
| 80 | if (memUsed < memMin) { |
| 81 | tMin = t; |
| 82 | memMin = memUsed; |
| 83 | } |
| 84 | |
| 85 | //fprintf(stderr, "t=%2lu N=%2lu memUsed=%16lu -- tMin=%2lu memMin=%16lu\n", |
| 86 | // t, N, memUsed, tMin, memMin); |
| 87 | } |
| 88 | |
| 89 | return(memMin >> 20); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | uint32cxx . . $ uint32# |
| 97 | optimalNumberOfBucketscxx . . $ optimalNumberOfBuckets(5f3177df75efc44).(uint32cxx . . $ uint32# merSize, |
| 98 | uint64cxx . . $ uint64# numMers, |
| 99 | bool positionsEnabled) { |
| 100 | uint64cxx . . $ uint64# opth = ~uint64ZEROcxx . . $ `libutil/util.h:50:10`!; |
| 101 | uint64cxx . . $ uint64# opts = ~uint64ZEROcxx . . $ `libutil/util.h:50:10`!; |
| 102 | uint64cxx . . $ uint64# h = 0; |
| 103 | uint64cxx . . $ uint64# s = 0; |
| 104 | uint64cxx . . $ uint64# hwidth = logBaseTwo64cxx . . $ logBaseTwo64(ecdaa1632f49f591).(numMers); |
| 105 | |
| 106 | // Positions consume space too, but only if enabled. Probably |
| 107 | // doesn't matter here. |
| 108 | // |
| 109 | uint64cxx . . $ uint64# posPerMer = 0; |
| 110 | if (positionsEnabled) |
| 111 | posPerMer = 32; |
| 112 | |
| 113 | // Find the table size (in bits, h) that minimizes memory usage |
| 114 | // for the given merSize and numMers |
| 115 | // |
| 116 | // We have two tables: |
| 117 | // the bucket pointers num buckets * pointer width == 2 << h * hwidth |
| 118 | // the mer data: num mers * (mersize - hwidth) |
| 119 | // |
| 120 | uint64cxx . . $ uint64# hmax = 64 - logBaseTwo64cxx . . $ logBaseTwo64(ecdaa1632f49f591).(hwidth + numMers * (2 * merSize - h)); |
| 121 | for (h=2; h<=hmax && h<2*merSize; h++) { |
| 122 | s = (uint64ONEcxx . . $ `libutil/util.h:51:10`! << h) * hwidth + numMers * (2 * merSize - h + posPerMer); |
| 123 | |
| 124 | //fprintf(stderr, "optimalNumberOfBuckets()-- h="uint64FMT" s="uint64FMT"\n", h, s); |
| 125 | |
| 126 | if (s < opts) { |
| 127 | opth = h; |
| 128 | opts = s; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return((uint32cxx . . $ uint32#)opth); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | |
| 137 | void |
| 138 | estimatecxx . . $ estimate(aab238e627804bcb).(merylArgscxx . . $ merylArgs# *args) { |
| 139 | |
| 140 | if (args->inputFilecxx . . $ merylArgs#inputFile.) { |
| 141 | merStreamcxx . . $ merStream# Mcxx . . $ merStream#merStream(c5717b5f51363f86).(new kMerBuildercxx . . $ kMerBuilder#(args->merSizecxx . . $ merylArgs#merSize., args->merCompcxx . . $ merylArgs#merComp.), |
| 142 | new seqStreamcxx . . $ seqStream#(args->inputFilecxx . . $ merylArgs#inputFile.), |
| 143 | true, true); |
| 144 | speedCountercxx . . $ speedCounter# Ccxx . . $ speedCounter#speedCounter(cdc90e45b9a80ad5).(" %7.2f Mmers -- %5.2f Mmers/second\r", 1000000.0, 0x1fffff, args->beVerbosecxx . . $ merylArgs#beVerbose.); |
| 145 | |
| 146 | if (args->beVerbosecxx . . $ merylArgs#beVerbose.) |
| 147 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Counting mers in '%s'\n", args->inputFilecxx . . $ merylArgs#inputFile.); |
| 148 | |
| 149 | args->numMersEstimatedcxx . . $ merylArgs#numMersEstimated. = 0; |
| 150 | |
| 151 | while (M.nextMercxx . . $ merStream#nextMer(dfa6e707f7696010).()) { |
| 152 | C.tickcxx . . $ speedCounter#tick(c9857a73d68e59ba).(); |
| 153 | args->numMersEstimatedcxx . . $ merylArgs#numMersEstimated.++; |
| 154 | } |
| 155 | |
| 156 | C.finishcxx . . $ speedCounter#finish(49f6e7a06ebc5aa8).(); |
| 157 | } |
| 158 | |
| 159 | uint32cxx . . $ uint32# opth = optimalNumberOfBucketscxx . . $ optimalNumberOfBuckets(5f3177df75efc44).(args->merSizecxx . . $ merylArgs#merSize., args->numMersEstimatedcxx . . $ merylArgs#numMersEstimated., args->positionsEnabledcxx . . $ merylArgs#positionsEnabled.); |
| 160 | uint64cxx . . $ uint64# memu = ((uint64ONEcxx . . $ `libutil/util.h:51:10`! << opth) * logBaseTwo64cxx . . $ logBaseTwo64(ecdaa1632f49f591).(args->numMersEstimatedcxx . . $ merylArgs#numMersEstimated.+1) + args->numMersEstimatedcxx . . $ merylArgs#numMersEstimated. * (2 * args->merSizecxx . . $ merylArgs#merSize. - opth)); |
| 161 | |
| 162 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, uint64FMTcxx . . $ `libutil/util.h:55:10`!" "uint32FMTcxx . . $ `libutil/util.h:65:10`!"-mers can be computed using "uint64FMTcxx . . $ `libutil/util.h:55:10`!"MB memory.\n", |
| 163 | args->numMersEstimatedcxx . . $ merylArgs#numMersEstimated., args->merSizecxx . . $ merylArgs#merSize., memu >> 23); |
| 164 | } |