| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | |
| 4 | #include "bio++.H" |
| 5 | #include "seqStream.H" |
| 6 | #include "merStream.H" |
| 7 | #include "libmeryl.H" |
| 8 | #include "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 | uint64 |
| 19 | estimateNumMersInMemorySize(uint32 merSize, |
| 20 | uint64 mem, |
| 21 | bool positionsEnabled, |
| 22 | bool beVerbose) { |
| 23 | uint64 maxN = 0; |
| 24 | uint64 bestT = 0; |
| 25 | |
| 26 | uint64 memLimt = mem * 8; // Memory limit, in bits. |
| 27 | uint64 posPerMer = (positionsEnabled == false) ? 0 : 32; // Positions consume space, if enabled. |
| 28 | 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 (uint64 t=2; t < tMax; t++) { |
| 34 | for (uint64 N=1; N<40; N++) { |
| 35 | uint64 Nmin = uint64ONE << (N - 1); // Minimum number of mers we want to fit in the table |
| 36 | uint64 Nmax = uint64ONE << (N); // Maximum number of mers that can fit in the table |
| 37 | |
| 38 | uint64 bucketsize = (uint64ONE << t) * N; // Size, in bits, of the pointer table |
| 39 | |
| 40 | 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(stdout, "Can fit "uint64FMT" mers into table with prefix of "uint64FMT" bits, using %.3fMB (%.3fMB for positions)\n", |
| 55 | maxN, |
| 56 | bestT, |
| 57 | (((uint64ONE << bestT) * logBaseTwo64(maxN) + maxN * (2*merSize - bestT + posPerMer)) >> 3) / 1048576.0, |
| 58 | ((maxN * posPerMer) >> 3) / 1048576.0); |
| 59 | |
| 60 | return(maxN); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | |
| 65 | uint64 |
| 66 | estimateMemory(uint32 merSize, |
| 67 | uint64 numMers, |
| 68 | bool positionsEnabled) { |
| 69 | |
| 70 | uint64 posPerMer = (positionsEnabled == false) ? 0 : 32; |
| 71 | uint64 tMax = (merSize > 25) ? 50 : 2 * merSize - 2; |
| 72 | |
| 73 | uint64 tMin = tMax; |
| 74 | uint64 memMin = UINT64_MAX; |
| 75 | |
| 76 | for (uint64 t=2; t < tMax; t++) { |
| 77 | uint64 N = logBaseTwo64(numMers); // Width of the bucket pointer table |
| 78 | uint64 memUsed = ((uint64ONE << t) * logBaseTwo64(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 | uint32 |
| 97 | optimalNumberOfBuckets(uint32 merSize, |
| 98 | uint64 numMers, |
| 99 | bool positionsEnabled) { |
| 100 | uint64 opth = ~uint64ZERO; |
| 101 | uint64 opts = ~uint64ZERO; |
| 102 | uint64 h = 0; |
| 103 | uint64 s = 0; |
| 104 | uint64 hwidth = logBaseTwo64(numMers); |
| 105 | |
| 106 | // Positions consume space too, but only if enabled. Probably |
| 107 | // doesn't matter here. |
| 108 | // |
| 109 | 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 | uint64 hmax = 64 - logBaseTwo64(hwidth + numMers * (2 * merSize - h)); |
| 121 | for (h=2; h<=hmax && h<2*merSize; h++) { |
| 122 | s = (uint64ONE << 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((uint32)opth); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | |
| 137 | void |
| 138 | estimate(merylArgs *args) { |
| 139 | |
| 140 | if (args->inputFile) { |
| 141 | merStream M(new kMerBuilder(args->merSize, args->merComp), |
| 142 | new seqStream(args->inputFile), |
| 143 | true, true); |
| 144 | speedCounter C(" %7.2f Mmers -- %5.2f Mmers/second\r", 1000000.0, 0x1fffff, args->beVerbose); |
| 145 | |
| 146 | if (args->beVerbose) |
| 147 | fprintf(stderr, "Counting mers in '%s'\n", args->inputFile); |
| 148 | |
| 149 | args->numMersEstimated = 0; |
| 150 | |
| 151 | while (M.nextMer()) { |
| 152 | C.tick(); |
| 153 | args->numMersEstimated++; |
| 154 | } |
| 155 | |
| 156 | C.finish(); |
| 157 | } |
| 158 | |
| 159 | uint32 opth = optimalNumberOfBuckets(args->merSize, args->numMersEstimated, args->positionsEnabled); |
| 160 | uint64 memu = ((uint64ONE << opth) * logBaseTwo64(args->numMersEstimated+1) + args->numMersEstimated * (2 * args->merSize - opth)); |
| 161 | |
| 162 | fprintf(stderr, uint64FMT" "uint32FMT"-mers can be computed using "uint64FMT"MB memory.\n", |
| 163 | args->numMersEstimated, args->merSize, memu >> 23); |
| 164 | } |