Debian Codegraph @

meryl/merge.listmerge.C

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "meryl.H"
6 #include "libmeryl.H"
7
8
9 using namespace std;
10
11 #include <algorithm>
12
13 struct mMer {
14 kMer _mer;
15 uint32 _cnt;
16 uint32 _off;
17 uint32 _nxt;
18 uint32 _stp;
19 };
20
21
22 class mMerList {
23 public:
24 mMerList(uint32 maxSize) {
25 _posLen = 0;
26 _posMax = 2 * maxSize;
27 _pos = new uint32 [_posMax];
28
29 _mmmLen = 0;
30 _mmmMax = maxSize;
31 _mmm = new mMer [_mmmMax];
32
33 _tip = ~uint32ZERO;
34 _fre = 0;
35
36 for (uint32 i=0; i<_mmmMax; i++) {
37 _mmm[i]._cnt = 0;
38 _mmm[i]._off = 0;
39 _mmm[i]._nxt = i+1;
40 _mmm[i]._stp = 0;
41 }
42
43 _mmm[_mmmMax-1]._nxt = ~uint32ZERO;
44 };
45 ~mMerList() {
46 delete [] _pos;
47 delete [] _mmm;
48 };
49
50 bool loadMore(void) { return((_mmmMax < _tip) || (_mmm[_tip]._stp == 1)); };
51
52 uint32 length(void) { return(_mmmLen); };
53
54 kMer *pop(uint32 &cnt, uint32* &pos) {
55 kMer *ret = 0L;
56
57 //fprintf(stderr, "POP tip="uint32FMT"\n", _tip);
58
59 if (_tip < _mmmMax) {
60 uint32 f = _tip;
61
62 ret = &_mmm[f]._mer;
63 cnt = _mmm[f]._cnt;
64 pos = (_mmm[f]._off != ~uint32ZERO) ? _pos + _mmm[f]._off : 0L;
65
66 // Move tip to the next thing
67 _tip = _mmm[f]._nxt;
68
69 // And append this one to the free list.
70 _mmm[f]._nxt = _fre;
71 _fre = f;
72
73 _mmmLen--;
74
75 //fprintf(stderr, "POP f="uint32FMT" tip="uint32FMT" len="uint32FMT"\n", f, _tip, _mmmLen);
76 }
77
78 return(ret);
79 };
80
81
82 // rebuild the position list, squeezes out empty items
83 void rebuild(void) {
84 if (_posLen > 0) {
85 assert(0);
86 uint32 *np = new uint32 [_posMax];
87
88 _posLen = 0;
89
90 for (uint32 i=0; i<_mmmLen; i++) {
91 mMer *m = _mmm + i;
92
93 if (m->_off != ~uint32ZERO) {
94 _mmm[_mmmLen]._off = _posLen;
95
96 for (uint32 p=0; p<m->_cnt; p++, _posLen++)
97 np[_posLen] = _pos[p];
98 }
99 }
100
101 delete [] _pos;
102 _pos = np;
103 }
104 };
105
106
107
108 // Read more mers from the file
109 void read(merylStreamReader *R, uint32 num, bool loadAll) {
110 uint32 xxx = 0;
111 uint32 las = ~uint32ZERO;
112 uint32 pos = _tip;
113 bool stop = false;
114
115 //fprintf(stderr, "read()- loading "uint32FMT"\n", num);
116
117 assert(_mmmLen + num < _mmmMax);
118
119 // Load until we hit the sentinal.
120 if (loadAll == false)
121 num = ~uint32ZERO;
122
123 for (xxx=0; (xxx < num) && (stop == false) && (R->nextMer()); xxx++) {
124
125 // Insert into a free node
126 uint32 fre = _fre;
127 _fre = _mmm[fre]._nxt;
128
129 _mmm[fre]._mer = R->theFMer();
130 _mmm[fre]._cnt = R->theCount();
131 _mmm[fre]._off = ~uint32ZERO;
132 _mmm[fre]._stp = 0;
133
134 uint32 *ppp = R->thePositions();
135 if (ppp) {
136 _mmm[fre]._off = _posLen;
137
138 if (_posMax <= _posLen + _mmm[fre]._cnt) {
139 fprintf(stderr, "Reallocate _pos\n");
140 _posMax *= 2;
141 uint32 *tmp = new uint32 [_posMax];
142 memcpy(tmp, _pos, sizeof(uint32) * _posLen);
143 delete [] _pos;
144 _pos = tmp;
145 }
146
147 for (uint32 i=0; i<_mmm[fre]._cnt; i++, _posLen++)
148 _pos[_posLen] = ppp[i];
149 }
150
151 // Keep count
152 _mmmLen++;
153
154 // Figure out where to put it in the list. New duplicates must
155 // go AFTER the existing -- that's the job of <=.
156
157 while ((pos < _mmmMax) && (_mmm[pos]._mer <= R->theFMer())) {
158 las = pos;
159 pos = _mmm[pos]._nxt;
160 }
161
162 if (_mmmMax < _tip) {
163 // No tip, make new list.
164 _mmm[fre]._nxt = _tip;
165 _tip = fre;
166 las = ~uint32ZERO;
167 pos = _tip;
168 } else if (_mmmMax < las) {
169 // Valid list, but we want to insert before the start
170 _mmm[fre]._nxt = _tip;
171 _tip = fre;
172 las = ~uint32ZERO;
173 pos = _tip;
174 } else if (pos < _mmmMax) {
175 // Valid pos, insert in the middle (after las, before pos)
176 _mmm[fre]._nxt = _mmm[las]._nxt;
177 _mmm[las]._nxt = fre;
178 las = fre;
179 //pos = _mmm[las]._nxt;
180 } else {
181 // Have a list, but we ran off the end, append (after las)
182 _mmm[fre]._nxt = ~uint32ZERO;
183 _mmm[las]._nxt = fre;
184 pos = fre;
185
186 if (loadAll == false)
187 stop = true;
188 }
189 }
190
191 // Set the sentinal. This forces us to load more mers.
192 //
193 if (loadAll == true) {
194 //fprintf(stderr, "read()-- stop on tip = "uint32FMT"\n", las);
195 _mmm[las]._stp = 1;
196 }
197
198 //fprintf(stderr, "read()-- now up to "uint32FMT" mers ("uint32FMT" pos); loaded "uint32FMT" out of "uint32FMT" requested.\n", _mmmLen, _posLen, xxx, num);
199 };
200
201 private:
202 uint32 _posLen;
203 uint32 _posMax;
204 uint32 *_pos;
205
206 uint32 _mmmLen;
207 uint32 _mmmMax;
208 mMer *_mmm;
209
210 uint32 _tip;
211 uint32 _fre;
212 };
213
214
215
216
217 void
218 multipleOperations(merylArgs *args) {
219
220 if (args->mergeFilesLen < 2) {
221 fprintf(stderr, "ERROR - must have at least two databases (you gave "uint32FMT")!\n", args->mergeFilesLen);
222 exit(1);
223 }
224 if (args->outputFile == 0L) {
225 fprintf(stderr, "ERROR - no output file specified.\n");
226 exit(1);
227 }
228 if ((args->personality != PERSONALITY_MERGE) &&
229 (args->personality != PERSONALITY_MIN) &&
230 (args->personality != PERSONALITY_MINEXIST) &&
231 (args->personality != PERSONALITY_MAX) &&
232 (args->personality != PERSONALITY_ADD) &&
233 (args->personality != PERSONALITY_AND) &&
234 (args->personality != PERSONALITY_NAND) &&
235 (args->personality != PERSONALITY_OR) &&
236 (args->personality != PERSONALITY_XOR)) {
237 fprintf(stderr, "ERROR - only personalities min, minexist, max, add, and, nand, or, xor\n");
238 fprintf(stderr, "ERROR - are supported in multipleOperations().\n");
239 fprintf(stderr, "ERROR - this is a coding error, not a user error.\n");
240 exit(1);
241 }
242
243 uint32 maxSize = 64 * 1024 * 1024;
244
245 merylStreamReader **R = new merylStreamReader* [args->mergeFilesLen];
246 merylStreamWriter *W = 0L;
247 mMerList *M = new mMerList(maxSize + maxSize / 4);
248
249 for (uint32 i=0; i<args->mergeFilesLen; i++)
250 R[i] = new merylStreamReader(args->mergeFiles[i]);
251
252 // Verify that the mersizes are all the same
253 //
254 bool fail = false;
255 uint32 merSize = R[0]->merSize();
256 uint32 merComp = R[0]->merCompression();
257
258 for (uint32 i=0; i<args->mergeFilesLen; i++) {
259 fail |= (merSize != R[i]->merSize());
260 fail |= (merComp != R[i]->merCompression());
261 }
262
263 if (fail)
264 fprintf(stderr, "ERROR: mer size or compression level differ.\n"), exit(1);
265
266 // Open the output file, using the largest prefix size found in the
267 // input/mask files.
268 //
269 uint32 prefixSize = 0;
270 for (uint32 i=0; i<args->mergeFilesLen; i++)
271 if (prefixSize < R[i]->prefixSize())
272 prefixSize = R[i]->prefixSize();
273
274 W = new merylStreamWriter(args->outputFile, merSize, merComp, prefixSize);
275
276 // Load mers from all files, remember the largest mer we load.
277 //
278 bool loadAll = true;
279 for (uint32 i=0; i<args->mergeFilesLen; i++) {
280 M->read(R[i], maxSize / args->mergeFilesLen, loadAll);
281 loadAll = false;
282 }
283
284 fprintf(stderr, "Initial load: length="uint32FMT"\n", M->length());
285
286 bool moreStuff = true;
287
288 kMer currentMer; // The current mer we're operating on
289 uint32 currentCount = uint32ZERO; // The count (operation dependent) of this mer
290 uint32 currentTimes = uint32ZERO; // Number of files it's in
291
292 uint32 currentPositionsMax = 0;
293 uint32 *currentPositions = 0L;
294
295 kMer *thisMer; // The mer we just read
296 uint32 thisCount = uint32ZERO; // The count of the mer we just read
297 uint32 *thisPositions = 0L;
298
299 speedCounter *C = new speedCounter(" %7.2f Mmers -- %5.2f Mmers/second\r", 1000000.0, 0x1fffff, args->beVerbose);
300
301 currentMer.setMerSize(merSize);
302
303 while (moreStuff) {
304
305 // Load more stuff if needed.
306 //
307 if (M->loadMore() == true) {
308 M->rebuild();
309
310 uint32 additionalLoading = 8192;
311
312 if (maxSize / args->mergeFilesLen > M->length())
313 additionalLoading = maxSize / args->mergeFilesLen - M->length();
314
315 loadAll = true;
316
317 for (uint32 i=0; i<args->mergeFilesLen; i++) {
318 if (R[i]->validMer()) {
319 M->read(R[i], additionalLoading, loadAll);
320 loadAll = false;
321 }
322 }
323 }
324
325 // All done? Exit.
326 if (M->length() == 0)
327 moreStuff = false;
328
329 thisMer = M->pop(thisCount, thisPositions);
330
331 // If we've hit a different mer, write out the last one
332 if ((M->length() == 0) || (*thisMer != currentMer)) {
333 switch (args->personality) {
334 case PERSONALITY_MIN:
335 if (currentTimes == args->mergeFilesLen)
336 W->addMer(currentMer, currentCount);
337 break;
338 case PERSONALITY_MERGE:
339 case PERSONALITY_MINEXIST:
340 case PERSONALITY_MAX:
341 case PERSONALITY_ADD:
342 W->addMer(currentMer, currentCount, currentPositions);
343 break;
344 case PERSONALITY_AND:
345 if (currentTimes == args->mergeFilesLen)
346 W->addMer(currentMer, currentCount);
347 break;
348 case PERSONALITY_NAND:
349 if (currentTimes != args->mergeFilesLen)
350 W->addMer(currentMer, currentCount);
351 break;
352 case PERSONALITY_OR:
353 W->addMer(currentMer, currentCount);
354 break;
355 case PERSONALITY_XOR:
356 if ((currentTimes % 2) == 1)
357 W->addMer(currentMer, currentCount);
358 break;
359 default:
360 fprintf(stderr, "ERROR - invalid personality in multipleOperations::write\n");
361 fprintf(stderr, "ERROR - this is a coding error, not a user error.\n");
362 exit(1);
363 break;
364 }
365
366 currentMer = *thisMer;
367
368 currentCount = uint32ZERO;
369 currentTimes = uint32ZERO;
370
371 C->tick();
372 }
373
374
375 if (moreStuff == false)
376 break;
377
378
379 // Perform the operation
380 switch (args->personality) {
381 case PERSONALITY_MERGE:
382 if (thisPositions) {
383
384 if (currentPositionsMax == 0) {
385 currentPositionsMax = 1048576;
386 currentPositions = new uint32 [currentPositionsMax];
387 }
388
389 if (currentPositionsMax < currentCount + thisCount) {
390 while (currentPositionsMax < currentCount + thisCount)
391 currentPositionsMax *= 2;
392
393 uint32 *t = new uint32 [currentPositionsMax];
394 memcpy(t, currentPositions, sizeof(uint32) * currentCount);
395 delete [] currentPositions;
396 currentPositions = t;
397 }
398
399 if (thisCount < 16) {
400 for (uint32 i=0; i<thisCount; i++)
401 currentPositions[currentCount + i] = thisPositions[i];
402 } else {
403 memcpy(currentPositions + currentCount, thisPositions, sizeof(uint32) * thisCount);
404 }
405 }
406 // Otherwise, we're the same as ADD.
407 currentCount += thisCount;
408 break;
409 case PERSONALITY_MIN:
410 case PERSONALITY_MINEXIST:
411 if (currentTimes == 0) {
412 currentCount = thisCount;
413 } else {
414 if (currentCount > thisCount)
415 currentCount = thisCount;
416 }
417 break;
418 case PERSONALITY_MAX:
419 if (currentCount < thisCount)
420 currentCount = thisCount;
421 break;
422 case PERSONALITY_ADD:
423 currentCount += thisCount;
424 break;
425 case PERSONALITY_AND:
426 case PERSONALITY_NAND:
427 case PERSONALITY_OR:
428 case PERSONALITY_XOR:
429 currentCount = 1;
430 break;
431 default:
432 fprintf(stderr, "ERROR - invalid personality in multipleOperations::operate\n");
433 fprintf(stderr, "ERROR - this is a coding error, not a user error.\n");
434 exit(1);
435 break;
436 }
437
438 currentTimes++;
439 }
440
441 for (uint32 i=0; i<args->mergeFilesLen; i++)
442 delete R[i];
443 delete R;
444 delete W;
445 delete M;
446 delete C;
447 }