compressedlevenshteinmatrix.cpp
13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
* ASCLITE
* Author: Jerome Ajot, Jon Fiscus, Nicolas Radde, Chris Laprun
*
* This software was developed at the National Institute of Standards and Technology by
* employees of the Federal Government in the course of their official duties. Pursuant
* to title 17 Section 105 of the United States Code this software is not subject to
* copyright protection and is in the public domain. ASCLITE is an experimental system.
* NIST assumes no responsibility whatsoever for its use by other parties, and makes no
* guarantees, expressed or implied, about its quality, reliability, or any other
* characteristic. We would appreciate acknowledgement if the software is used.
*
* THIS SOFTWARE IS PROVIDED "AS IS." With regard to this software, NIST MAKES NO EXPRESS
* OR IMPLIED WARRANTY AS TO ANY MATTER WHATSOEVER, INCLUDING MERCHANTABILITY,
* OR FITNESS FOR A PARTICULAR PURPOSE.
*/
/**
* Represent the Levenshtein Distance Matrix with compression
*/
#include "compressedlevenshteinmatrix.h"
Logger* CompressedLevenshteinMatrix::m_pLogger = Logger::getLogger();
CompressedLevenshteinMatrix::CompressedLevenshteinMatrix(const size_t& _NbrDimensions, size_t* _TabDimensionDeep)
{
m_MaxMemoryKBProp = static_cast<size_t>(ceil(1024*1024*atof(Properties::GetProperty("recording.maxnbofgb").c_str())));
m_BlockSizeKB = static_cast<uint>(atoi(Properties::GetProperty("align.memorycompressionblock").c_str()));
if(m_BlockSizeKB > 1048576)
m_BlockSizeKB = 1048575;
/* LZMA Properties ; see lzma/LzmaLib.h */
m_lzmaLevel = 4;
m_lzmaDictionarySize = 1 << 24;
m_lzmaLc = 3;
m_lzmaLp = 0;
m_lzmaPb = 2;
m_lzmaFb = 32;
m_lzmaNumberThreads = 2;
m_lzmaPropertiesSize = LZMA_PROPS_SIZE;
m_NbrDimensions = _NbrDimensions;
m_TabDimensionDeep = new size_t[m_NbrDimensions];
m_MultiplicatorDimension = new ullint[m_NbrDimensions];
m_TabBlockDivider = new size_t[m_NbrDimensions];
m_TabBlockDimensionDeep = new size_t[m_NbrDimensions];
m_MultiplicatorDimension[0] = 1;
m_TabDimensionDeep[0] = _TabDimensionDeep[0] - 1;
m_MaxSize = m_TabDimensionDeep[0];
for(size_t i=1; i<m_NbrDimensions; ++i)
{
m_TabDimensionDeep[i] = _TabDimensionDeep[i] - 1;
m_MultiplicatorDimension[i] = m_MultiplicatorDimension[i-1]*m_TabDimensionDeep[i-1];
m_MaxSize = m_MaxSize * m_TabDimensionDeep[i];
}
BlockComputation(0);
if(m_BaseLengthIn < 0.2*m_BlockSizeKB*1024)
BlockComputation(1);
/* To guarantee that the compressed data will fit in its buffer, allocate
an output buffer of size 2% larger than the uncompressed data, plus extra
size for the compression properties. */
m_BaseLengthOut = m_BaseLengthIn + m_BaseLengthIn / 50 + m_lzmaPropertiesSize;
m_MultiplicatorBlockDimension = new size_t[m_NbrDimensions];
m_MultiplicatorDivider = new size_t[m_NbrDimensions];
m_MultiplicatorBlockDimension[0] = 1;
m_MultiplicatorDivider[0] = 1;
for(size_t i=1; i<m_NbrDimensions; ++i)
{
m_MultiplicatorBlockDimension[i] = m_MultiplicatorBlockDimension[i-1]*m_TabBlockDimensionDeep[i-1];
m_MultiplicatorDivider[i] = m_MultiplicatorDivider[i-1]*m_TabBlockDivider[i-1];
}
m_TabStartByte = new int * [m_NbrCompressedTabs];
m_TabStartByteCompressed = new int * [m_NbrCompressedTabs];
m_TabSizes = new uint[m_NbrCompressedTabs];
m_TabbIsCompressed = new bool[m_NbrCompressedTabs];
m_TabHitsTimer = new ulint[m_NbrCompressedTabs];
m_TabIsCreated = new bool[m_NbrCompressedTabs];
m_CurrentMemorySize = 0;
m_Decompressions = 0;
m_Compressions = 0;
m_NbrCompressedBlocks = 0;
m_NbrDecompressedBlocks = 0;
m_Accesses = 0;
for(size_t i=0; i<m_NbrCompressedTabs; ++i)
{
m_TabIsCreated[i] = false;
m_TabSizes[i] = 0;
m_TabStartByte[i] = NULL;
m_TabStartByteCompressed[i] = NULL;
m_CurrentMemorySize += 0;
}
m_SizeOfArray = 0;
m_NbrCreatedBlocks = 0;
m_UsableMemoryKB = 0.98*((double) m_MaxMemoryKBProp);
m_PercentageMemoryTriggerStart = 0.01;
m_PercentageMemoryTriggerStop = 0.2;
LOG_DEBUG(m_pLogger, "Allocation done!");
char buffer[BUFFER_SIZE];
sprintf(buffer, "Compressed Levenshtein Matrix: %lu blocks of %.1fKB, Usable: %.0fKB, StartGC: %.0fKB, StopGC: %.0fKB",
(ulint) m_NbrCompressedTabs, ((double)(m_BaseLengthIn))/1024.0, m_UsableMemoryKB, m_UsableMemoryKB*(1.0-m_PercentageMemoryTriggerStart), m_UsableMemoryKB*(1.0-m_PercentageMemoryTriggerStop));
LOG_DEBUG(m_pLogger, buffer);
}
CompressedLevenshteinMatrix::~CompressedLevenshteinMatrix()
{
char buffer[BUFFER_SIZE];
sprintf(buffer, "Compressed Levenshtein Matrix: TotalNbrCells: %llu, CalculatedCells: %llu, RatioCells: %.1f%%, TheoryBlocks: %lu, CreatedBlocks: %lu, RatioBlocks: %.1f%%, ActualSize: %.1fKB, ExpendedSize: %.1fKB", (ullint) m_MaxSize, (ullint) m_SizeOfArray, 100.0*((double)m_SizeOfArray)/((double)m_MaxSize), (ulint) m_NbrCompressedTabs, (ulint) m_NbrCreatedBlocks, 100.0*((double)m_NbrCreatedBlocks)/((double)m_NbrCompressedTabs), ((double) m_CurrentMemorySize)/1024.0, ((double) m_NbrCreatedBlocks)*((double)(m_BaseLengthIn))/1024.0);
LOG_DEBUG(m_pLogger, buffer);
for(size_t i=0; i<m_NbrCompressedTabs; ++i)
{
if(isBlockCreated(i))
{
if(m_TabbIsCompressed[i])
{
if(m_TabStartByteCompressed[i])
free(m_TabStartByteCompressed[i]);
}
else
{
if(m_TabStartByte[i])
free(m_TabStartByte[i]);
}
}
}
delete [] m_TabStartByte;
delete [] m_TabStartByteCompressed;
delete [] m_TabSizes;
delete [] m_TabbIsCompressed;
delete [] m_TabHitsTimer;
delete [] m_TabIsCreated;
delete [] m_TabBlockDimensionDeep;
delete [] m_MultiplicatorBlockDimension;
delete [] m_TabBlockDivider;
delete [] m_TabDimensionDeep;
delete [] m_MultiplicatorDivider;
delete [] m_MultiplicatorDimension;
}
void CompressedLevenshteinMatrix::CreateBlock(const size_t& block_index)
{
if(! isBlockCreated(block_index))
{
uint decomp_lengh = m_BaseLengthIn;
m_TabStartByte[block_index] = (int*) malloc(m_BaseLengthIn);
memset(m_TabStartByte[block_index], C_UNCALCULATED, decomp_lengh);
m_TabSizes[block_index] = decomp_lengh;
m_CurrentMemorySize += decomp_lengh;
m_TabbIsCompressed[block_index] = false;
++m_NbrDecompressedBlocks;
m_TabIsCreated[block_index] = true;
TouchBlock(block_index);
++m_NbrCreatedBlocks;
GarbageCollection();
}
}
void CompressedLevenshteinMatrix::CompressBlock(const size_t& block_index)
{
CreateBlock(block_index);
if(!m_TabbIsCompressed[block_index])
{
// Block is not compressed, then compress it;
size_t decomp_lengh = m_TabSizes[block_index];
size_t comp_lengh = m_BaseLengthOut;
m_TabStartByteCompressed[block_index] = (int*) malloc(m_BaseLengthOut);
size_t outPropsize = m_lzmaPropertiesSize;
if( LzmaCompress( (unsigned char*) m_TabStartByteCompressed[block_index] + m_lzmaPropertiesSize, &comp_lengh,
(unsigned char*) m_TabStartByte[block_index], decomp_lengh,
(unsigned char*) m_TabStartByteCompressed[block_index], &outPropsize,
m_lzmaLevel,
m_lzmaDictionarySize,
m_lzmaLc,
m_lzmaLp,
m_lzmaPb,
m_lzmaFb,
m_lzmaNumberThreads ) != SZ_OK)
{
LOG_FATAL(m_pLogger, "Compression: 'LzmaCompress()' failed!");
exit(EXIT_FAILURE);
}
if( (comp_lengh + m_lzmaPropertiesSize >= decomp_lengh) || (outPropsize > m_lzmaPropertiesSize) )
{
//Incompressible data
LOG_DEBUG(m_pLogger, "Compression: Incompressible block ignoring compression!");
free(m_TabStartByteCompressed[block_index]);
m_TabStartByteCompressed[block_index] = NULL;
}
else
{
free(m_TabStartByte[block_index]);
m_TabStartByte[block_index] = NULL;
m_TabSizes[block_index] = comp_lengh + m_lzmaPropertiesSize;
m_TabbIsCompressed[block_index] = true;
m_CurrentMemorySize += comp_lengh + m_lzmaPropertiesSize - decomp_lengh;
++m_Compressions;
++m_NbrCompressedBlocks;
--m_NbrDecompressedBlocks;
}
}
}
bool CompressedLevenshteinMatrix::DecompressBlock(const size_t& block_index)
{
CreateBlock(block_index);
bool decomp = false;
if(decomp = m_TabbIsCompressed[block_index])
{
// Block is compressed, then decompress it;
size_t comp_lengh = m_TabSizes[block_index] - m_lzmaPropertiesSize;
size_t decomp_lengh = m_BaseLengthIn;
m_TabStartByte[block_index] = (int*) malloc(m_BaseLengthIn);
if(LzmaUncompress( (unsigned char*) m_TabStartByte[block_index], &decomp_lengh,
(unsigned char*) m_TabStartByteCompressed[block_index] + m_lzmaPropertiesSize, &comp_lengh,
(unsigned char*) m_TabStartByteCompressed[block_index], m_lzmaPropertiesSize) != SZ_OK)
{
LOG_FATAL(m_pLogger, "Decompression: 'LzmaUncompress()' failed!");
exit(EXIT_FAILURE);
}
free(m_TabStartByteCompressed[block_index]);
m_TabStartByteCompressed[block_index] = NULL;
m_TabSizes[block_index] = decomp_lengh;
m_TabbIsCompressed[block_index] = false;
m_CurrentMemorySize += decomp_lengh - comp_lengh;
++m_Decompressions;
--m_NbrCompressedBlocks;
++m_NbrDecompressedBlocks;
}
TouchBlock(block_index);
return decomp;
}
void CompressedLevenshteinMatrix::GarbageCollection()
{
char buffer[BUFFER_SIZE];
sprintf(buffer, "Compressed Levenshtein Matrix: Current: %lu KB, Limit: %lu KB, CompressedBlocks: %lu, UncompressedBlocks: %lu", m_CurrentMemorySize/1024,
static_cast<size_t>(m_UsableMemoryKB),
m_NbrCompressedBlocks,
m_NbrDecompressedBlocks);
LOG_DEBUG(m_pLogger, buffer);
if(isCallGarbageCollector())
{
bool found = false;
ulint count = 0;
do
{
if(found = ForcedGarbageCollection())
++count;
}
while(found && !isStopGarbageCollector());
char buffer[BUFFER_SIZE];
sprintf(buffer, "Garbage collection: %lu blocks compressed", count);
LOG_DEBUG(m_pLogger, buffer);
}
}
bool CompressedLevenshteinMatrix::ForcedGarbageCollection()
{
ulint mintouch = ULONG_MAX;
size_t min_index = 0;
// Do the ugly Java GC
bool found = false;
for(size_t i=0; i<m_NbrCompressedTabs; ++i)
{
if(isBlockCreated(i))
{
if(!m_TabbIsCompressed[i])
{
// not compressed
if(m_TabHitsTimer[i] < mintouch)
{
mintouch = m_TabHitsTimer[i];
min_index = i;
found = true;
}
}
}
}
if(found)
CompressBlock(min_index);
return found;
}
string CompressedLevenshteinMatrix::ToString()
{
return string("");
}
void CompressedLevenshteinMatrix::CoordinatesToBlockOffset(size_t* coordinates, size_t& blockNum, size_t& blockOffset)
{
blockNum = 0;
blockOffset = 0;
for(size_t i=0; i<m_NbrDimensions; ++i)
{
blockNum += (coordinates[i]/m_TabBlockDimensionDeep[i])*m_MultiplicatorDivider[i];
blockOffset += (coordinates[i]%m_TabBlockDimensionDeep[i])*m_MultiplicatorBlockDimension[i];
}
}
int CompressedLevenshteinMatrix::GetCostFor(size_t* coordinates)
{
size_t coord_x;
size_t coord_y;
CoordinatesToBlockOffset(coordinates, coord_x, coord_y);
bool decomp = DecompressBlock(coord_x);
int out = m_TabStartByte[coord_x][coord_y];
if(decomp)
GarbageCollection();
return (out);
}
void CompressedLevenshteinMatrix::SetCostFor(size_t* coordinates, const int& cost)
{
size_t coord_x;
size_t coord_y;
CoordinatesToBlockOffset(coordinates, coord_x, coord_y);
bool decomp = DecompressBlock(coord_x);
if(m_TabStartByte[coord_x][coord_y] == C_UNCALCULATED)
++m_SizeOfArray;
m_TabStartByte[coord_x][coord_y] = cost;
if(decomp)
GarbageCollection();
}
void CompressedLevenshteinMatrix::BlockComputation(const size_t& levelopt)
{
// Declaration Vars
size_t* Cursor = new size_t[m_NbrDimensions];
vector <size_t>* PrimeDiv = new vector <size_t>[m_NbrDimensions];
size_t* tmpDivider = new size_t[m_NbrDimensions];
size_t* tmpBlockDimensions = new size_t[m_NbrDimensions];
size_t blocksize = m_BlockSizeKB*256;
// Computation
// Initialization
for(size_t i=0; i<m_NbrDimensions; ++i)
{
if(m_TabDimensionDeep[i] == 1)
PrimeDiv[i].push_back(1);
for(size_t j=2; j<=m_TabDimensionDeep[i]; ++j)
if( (m_TabDimensionDeep[i] % j == 0) ||
( (levelopt >= 1) && ((m_TabDimensionDeep[i]+1) % 2 == 0) && ((m_TabDimensionDeep[i]+1) % j == 0) ) ||
( (levelopt >= 2) && ((m_TabDimensionDeep[i]+levelopt) % j == 0) )
)
PrimeDiv[i].push_back(j);
Cursor[i] = 0;
}
// End Initialization
// Main research
bool finished = false;
size_t closestsize = ULONG_MAX;
do
{
if(Cursor[0] == PrimeDiv[0].size())
{
finished = true;
}
else
{
size_t size = 1;
for(size_t i=0; i<m_NbrDimensions; ++i)
{
tmpDivider[i] = PrimeDiv[i][Cursor[i]];
tmpBlockDimensions[i] = m_TabDimensionDeep[i]/tmpDivider[i];
if(m_TabDimensionDeep[i] % tmpDivider[i] != 0)
++(tmpBlockDimensions[i]);
size *= tmpBlockDimensions[i];
}
size_t closer = labs(blocksize - size);
if(closer < closestsize)
{
closestsize = closer;
for(size_t i=0; i<m_NbrDimensions; ++i)
{
m_TabBlockDivider[i] = tmpDivider[i];
m_TabBlockDimensionDeep[i] = tmpBlockDimensions[i];
}
}
// Next
size_t currdim = m_NbrDimensions - 1;
++(Cursor[currdim]);
while( (currdim > 0) && (Cursor[currdim] == PrimeDiv[currdim].size()) )
{
Cursor[currdim] = 0;
--currdim;
++(Cursor[currdim]);
}
}
}
while(!finished);
// Main research
m_BlockSizeElts = 1;
m_NbrCompressedTabs = 1;
for(size_t i=0; i<m_NbrDimensions; ++i)
{
m_BlockSizeElts *= m_TabBlockDimensionDeep[i];
m_NbrCompressedTabs *= m_TabBlockDivider[i];
}
if(m_BlockSizeElts*sizeof(int) < 16)
m_BlockSizeElts = 16/sizeof(int);
m_BaseLengthIn = m_BlockSizeElts * sizeof(int);
// End Computation
// Destruction Vars
delete [] Cursor;
for(size_t i=0; i<m_NbrDimensions; ++i)
PrimeDiv[i].clear();
delete [] PrimeDiv;
delete [] tmpBlockDimensions;
delete [] tmpDivider;
//return isIncreasable;
}