blob: 9cc5264d27ac56b9a86cdd73d10dd192139c6867 [file] [log] [blame]
Yann Collet32fb4072017-08-18 16:52:05 -07001/*
Yann Collet4ded9e52016-08-30 10:04:33 -07002 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3 * All rights reserved.
4 *
Yann Collet32fb4072017-08-18 16:52:05 -07005 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
Yann Collet3128e032017-09-08 00:09:23 -07008 * You may select, at your option, one of the above-listed licenses.
Yann Collet4ded9e52016-08-30 10:04:33 -07009 */
Yann Collet71eafdd2016-02-12 02:31:57 +010010
Yann Collet71eafdd2016-02-12 02:31:57 +010011
Yann Collet71eafdd2016-02-12 02:31:57 +010012
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010013/* **************************************
14* Compiler Warnings
15****************************************/
16#ifdef _MSC_VER
Yann Collet77c137b2017-09-14 15:12:57 -070017# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010018#endif
19
20
Yann Collet71eafdd2016-02-12 02:31:57 +010021/*-*************************************
22* Includes
23***************************************/
Przemyslaw Skibinski7a8a03c2016-12-21 15:08:44 +010024#include "platform.h" /* Large Files support */
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010025#include "util.h" /* UTIL_getFileSize, UTIL_getTotalFileSize */
Yann Collet71eafdd2016-02-12 02:31:57 +010026#include <stdlib.h> /* malloc, free */
27#include <string.h> /* memset */
28#include <stdio.h> /* fprintf, fopen, ftello64 */
Yann Colleta3d03a32016-07-06 16:27:17 +020029#include <errno.h> /* errno */
Yann Collet71eafdd2016-02-12 02:31:57 +010030
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010031#include "mem.h" /* read */
Yann Collet71eafdd2016-02-12 02:31:57 +010032#include "error_private.h"
inikep23a08892016-04-22 12:43:18 +020033#include "dibio.h"
Yann Collet71eafdd2016-02-12 02:31:57 +010034
35
36/*-*************************************
37* Constants
38***************************************/
39#define KB *(1 <<10)
40#define MB *(1 <<20)
41#define GB *(1U<<30)
42
Yann Collet1496c3d2016-12-18 11:58:23 +010043#define SAMPLESIZE_MAX (128 KB)
44#define MEMMULT 11 /* rough estimation : memory cost to analyze 1 byte of sample */
Nick Terrelldf8415c2016-12-31 21:08:24 -080045#define COVER_MEMMULT 9 /* rough estimation : memory cost to analyze 1 byte of sample */
Yann Collet77c137b2017-09-14 15:12:57 -070046static const size_t g_maxMemory = (sizeof(size_t) == 4) ? (2 GB - 64 MB) : ((size_t)(512 MB) << sizeof(size_t));
Yann Collet71eafdd2016-02-12 02:31:57 +010047
48#define NOISELENGTH 32
Yann Collet71eafdd2016-02-12 02:31:57 +010049
50
51/*-*************************************
52* Console display
53***************************************/
54#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
Yann Collet086b9592017-09-14 16:45:10 -070055#define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); }
Yann Collet71eafdd2016-02-12 02:31:57 +010056
Nick Terrell9a2f6f42017-11-29 19:11:12 -080057#define SEC_TO_MICRO 1000000
58static const U64 g_refreshRate = SEC_TO_MICRO / 6;
59static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER;
Yann Colletf6ca09b2016-05-09 04:44:45 +020060
Nick Terrell9a2f6f42017-11-29 19:11:12 -080061#define DISPLAYUPDATE(l, ...) { if (displayLevel>=l) { \
62 if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || (displayLevel>=4)) \
63 { g_displayClock = UTIL_getTime(); DISPLAY(__VA_ARGS__); \
64 if (displayLevel>=4) fflush(stderr); } } }
Yann Collet71eafdd2016-02-12 02:31:57 +010065
66/*-*************************************
67* Exceptions
68***************************************/
69#ifndef DEBUG
70# define DEBUG 0
71#endif
72#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__);
73#define EXM_THROW(error, ...) \
74{ \
75 DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
Yann Collet086b9592017-09-14 16:45:10 -070076 DISPLAY("Error %i : ", error); \
77 DISPLAY(__VA_ARGS__); \
78 DISPLAY("\n"); \
Yann Collet71eafdd2016-02-12 02:31:57 +010079 exit(error); \
80}
81
82
83/* ********************************************************
84* Helper functions
85**********************************************************/
86unsigned DiB_isError(size_t errorCode) { return ERR_isError(errorCode); }
87
88const char* DiB_getErrorName(size_t errorCode) { return ERR_getErrorName(errorCode); }
89
Sean Purcell42bac7f2017-04-13 15:35:05 -070090#undef MIN
91#define MIN(a,b) ((a) < (b) ? (a) : (b))
Yann Colletbcb5f772016-07-06 15:41:03 +020092
Yann Collet71eafdd2016-02-12 02:31:57 +010093
94/* ********************************************************
95* File related operations
96**********************************************************/
Yann Collet290aaa72016-05-30 21:18:52 +020097/** DiB_loadFiles() :
Yann Colletc68d17f2017-09-15 15:31:31 -070098 * load samples from files listed in fileNamesTable into buffer.
99 * works even if buffer is too small to load all samples.
100 * Also provides the size of each sample into sampleSizes table
101 * which must be sized correctly, using DiB_fileStats().
102 * @return : nb of samples effectively loaded into `buffer`
103 * *bufferSizePtr is modified, it provides the amount data loaded within buffer.
104 * sampleSizes is filled with the size of each sample.
105 */
Yann Colletbcb5f772016-07-06 15:41:03 +0200106static unsigned DiB_loadFiles(void* buffer, size_t* bufferSizePtr,
Yann Colletc68d17f2017-09-15 15:31:31 -0700107 size_t* sampleSizes, unsigned sstSize,
Yann Collet086b9592017-09-14 16:45:10 -0700108 const char** fileNamesTable, unsigned nbFiles, size_t targetChunkSize,
109 unsigned displayLevel)
Yann Collet71eafdd2016-02-12 02:31:57 +0100110{
Yann Collet290aaa72016-05-30 21:18:52 +0200111 char* const buff = (char*)buffer;
Yann Collet71eafdd2016-02-12 02:31:57 +0100112 size_t pos = 0;
Yann Collet086b9592017-09-14 16:45:10 -0700113 unsigned nbLoadedChunks = 0, fileIndex;
Yann Collet71eafdd2016-02-12 02:31:57 +0100114
Yann Collet086b9592017-09-14 16:45:10 -0700115 for (fileIndex=0; fileIndex<nbFiles; fileIndex++) {
116 const char* const fileName = fileNamesTable[fileIndex];
Yann Colletbcb5f772016-07-06 15:41:03 +0200117 unsigned long long const fs64 = UTIL_getFileSize(fileName);
Yann Collet18b79532017-10-17 16:14:25 -0700118 unsigned long long remainingToLoad = (fs64 == UTIL_FILESIZE_UNKNOWN) ? 0 : fs64;
Yann Collet086b9592017-09-14 16:45:10 -0700119 U32 const nbChunks = targetChunkSize ? (U32)((fs64 + (targetChunkSize-1)) / targetChunkSize) : 1;
120 U64 const chunkSize = targetChunkSize ? MIN(targetChunkSize, fs64) : fs64;
Yann Collet25a60482017-09-15 11:55:13 -0700121 size_t const maxChunkSize = (size_t)MIN(chunkSize, SAMPLESIZE_MAX);
Yann Collet086b9592017-09-14 16:45:10 -0700122 U32 cnb;
123 FILE* const f = fopen(fileName, "rb");
124 if (f==NULL) EXM_THROW(10, "zstd: dictBuilder: %s %s ", fileName, strerror(errno));
125 DISPLAYUPDATE(2, "Loading %s... \r", fileName);
126 for (cnb=0; cnb<nbChunks; cnb++) {
Yann Collet25a60482017-09-15 11:55:13 -0700127 size_t const toLoad = (size_t)MIN(maxChunkSize, remainingToLoad);
Yann Collet086b9592017-09-14 16:45:10 -0700128 if (toLoad > *bufferSizePtr-pos) break;
129 { size_t const readSize = fread(buff+pos, 1, toLoad, f);
130 if (readSize != toLoad) EXM_THROW(11, "Pb reading %s", fileName);
131 pos += readSize;
Yann Colletc68d17f2017-09-15 15:31:31 -0700132 sampleSizes[nbLoadedChunks++] = toLoad;
Yann Collet086b9592017-09-14 16:45:10 -0700133 remainingToLoad -= targetChunkSize;
Yann Colletc68d17f2017-09-15 15:31:31 -0700134 if (nbLoadedChunks == sstSize) { /* no more space left in sampleSizes table */
135 fileIndex = nbFiles; /* stop there */
136 break;
137 }
Yann Collet086b9592017-09-14 16:45:10 -0700138 if (toLoad < targetChunkSize) {
Yann Colleta9694232017-09-15 10:16:26 -0700139 fseek(f, (long)(targetChunkSize - toLoad), SEEK_CUR);
Yann Collet086b9592017-09-14 16:45:10 -0700140 } } }
141 fclose(f);
142 }
Nick Terrelldf8415c2016-12-31 21:08:24 -0800143 DISPLAYLEVEL(2, "\r%79s\r", "");
Yann Colletbcb5f772016-07-06 15:41:03 +0200144 *bufferSizePtr = pos;
Yann Collet086b9592017-09-14 16:45:10 -0700145 DISPLAYLEVEL(4, "loaded : %u KB \n", (U32)(pos >> 10))
146 return nbLoadedChunks;
Yann Collet71eafdd2016-02-12 02:31:57 +0100147}
148
Nick Terrelldf8415c2016-12-31 21:08:24 -0800149#define DiB_rotl32(x,r) ((x << r) | (x >> (32 - r)))
150static U32 DiB_rand(U32* src)
151{
152 static const U32 prime1 = 2654435761U;
153 static const U32 prime2 = 2246822519U;
154 U32 rand32 = *src;
155 rand32 *= prime1;
156 rand32 ^= prime2;
157 rand32 = DiB_rotl32(rand32, 13);
158 *src = rand32;
159 return rand32 >> 5;
160}
161
Yann Collet77c137b2017-09-14 15:12:57 -0700162/* DiB_shuffle() :
163 * shuffle a table of file names in a semi-random way
164 * It improves dictionary quality by reducing "locality" impact, so if sample set is very large,
165 * it will load random elements from it, instead of just the first ones. */
Nick Terrelldf8415c2016-12-31 21:08:24 -0800166static void DiB_shuffle(const char** fileNamesTable, unsigned nbFiles) {
Yann Collet77c137b2017-09-14 15:12:57 -0700167 U32 seed = 0xFD2FB528;
168 unsigned i;
169 for (i = nbFiles - 1; i > 0; --i) {
170 unsigned const j = DiB_rand(&seed) % (i + 1);
171 const char* const tmp = fileNamesTable[j];
172 fileNamesTable[j] = fileNamesTable[i];
173 fileNamesTable[i] = tmp;
174 }
Nick Terrelldf8415c2016-12-31 21:08:24 -0800175}
176
Yann Collet71eafdd2016-02-12 02:31:57 +0100177
178/*-********************************************************
179* Dictionary training functions
180**********************************************************/
181static size_t DiB_findMaxMem(unsigned long long requiredMem)
182{
Yann Collet290aaa72016-05-30 21:18:52 +0200183 size_t const step = 8 MB;
Yann Collet71eafdd2016-02-12 02:31:57 +0100184 void* testmem = NULL;
185
186 requiredMem = (((requiredMem >> 23) + 1) << 23);
Yann Colletbcb5f772016-07-06 15:41:03 +0200187 requiredMem += step;
Yann Collet77c137b2017-09-14 15:12:57 -0700188 if (requiredMem > g_maxMemory) requiredMem = g_maxMemory;
Yann Collet71eafdd2016-02-12 02:31:57 +0100189
190 while (!testmem) {
Yann Collet71eafdd2016-02-12 02:31:57 +0100191 testmem = malloc((size_t)requiredMem);
Yann Colletbcb5f772016-07-06 15:41:03 +0200192 requiredMem -= step;
Yann Collet71eafdd2016-02-12 02:31:57 +0100193 }
194
195 free(testmem);
Yann Colletbcb5f772016-07-06 15:41:03 +0200196 return (size_t)requiredMem;
Yann Collet71eafdd2016-02-12 02:31:57 +0100197}
198
199
200static void DiB_fillNoise(void* buffer, size_t length)
201{
Yann Colletbcb5f772016-07-06 15:41:03 +0200202 unsigned const prime1 = 2654435761U;
203 unsigned const prime2 = 2246822519U;
204 unsigned acc = prime1;
Yann Collet71eafdd2016-02-12 02:31:57 +0100205 size_t p=0;;
206
207 for (p=0; p<length; p++) {
Yann Colletbcb5f772016-07-06 15:41:03 +0200208 acc *= prime2;
Yann Collet71eafdd2016-02-12 02:31:57 +0100209 ((unsigned char*)buffer)[p] = (unsigned char)(acc >> 21);
210 }
211}
212
213
214static void DiB_saveDict(const char* dictFileName,
215 const void* buff, size_t buffSize)
216{
Yann Collet290aaa72016-05-30 21:18:52 +0200217 FILE* const f = fopen(dictFileName, "wb");
Yann Collet71eafdd2016-02-12 02:31:57 +0100218 if (f==NULL) EXM_THROW(3, "cannot open %s ", dictFileName);
219
Yann Colletf6ca09b2016-05-09 04:44:45 +0200220 { size_t const n = fwrite(buff, 1, buffSize, f);
221 if (n!=buffSize) EXM_THROW(4, "%s : write error", dictFileName) }
Yann Collet71eafdd2016-02-12 02:31:57 +0100222
Yann Colletf6ca09b2016-05-09 04:44:45 +0200223 { size_t const n = (size_t)fclose(f);
224 if (n!=0) EXM_THROW(5, "%s : flush error", dictFileName) }
Yann Collet71eafdd2016-02-12 02:31:57 +0100225}
226
227
Yann Collet086b9592017-09-14 16:45:10 -0700228typedef struct {
229 U64 totalSizeToLoad;
230 unsigned oneSampleTooLarge;
Yann Colletc68d17f2017-09-15 15:31:31 -0700231 unsigned nbSamples;
Yann Collet086b9592017-09-14 16:45:10 -0700232} fileStats;
233
Yann Colletc68d17f2017-09-15 15:31:31 -0700234/*! DiB_fileStats() :
235 * Given a list of files, and a chunkSize (0 == no chunk, whole files)
236 * provides the amount of data to be loaded and the resulting nb of samples.
237 * This is useful primarily for allocation purpose => sample buffer, and sample sizes table.
238 */
Yann Collet086b9592017-09-14 16:45:10 -0700239static fileStats DiB_fileStats(const char** fileNamesTable, unsigned nbFiles, size_t chunkSize, unsigned displayLevel)
Yann Collet1496c3d2016-12-18 11:58:23 +0100240{
Yann Collet086b9592017-09-14 16:45:10 -0700241 fileStats fs;
Yann Collet1496c3d2016-12-18 11:58:23 +0100242 unsigned n;
Yann Collet086b9592017-09-14 16:45:10 -0700243 memset(&fs, 0, sizeof(fs));
Yann Collet1496c3d2016-12-18 11:58:23 +0100244 for (n=0; n<nbFiles; n++) {
245 U64 const fileSize = UTIL_getFileSize(fileNamesTable[n]);
Yann Collet18b79532017-10-17 16:14:25 -0700246 U64 const srcSize = (fileSize == UTIL_FILESIZE_UNKNOWN) ? 0 : fileSize;
247 U32 const nbSamples = (U32)(chunkSize ? (srcSize + (chunkSize-1)) / chunkSize : 1);
248 U64 const chunkToLoad = chunkSize ? MIN(chunkSize, srcSize) : srcSize;
Yann Collet25a60482017-09-15 11:55:13 -0700249 size_t const cappedChunkSize = (size_t)MIN(chunkToLoad, SAMPLESIZE_MAX);
Yann Colletc68d17f2017-09-15 15:31:31 -0700250 fs.totalSizeToLoad += cappedChunkSize * nbSamples;
Yann Collet086b9592017-09-14 16:45:10 -0700251 fs.oneSampleTooLarge |= (chunkSize > 2*SAMPLESIZE_MAX);
Yann Colletc68d17f2017-09-15 15:31:31 -0700252 fs.nbSamples += nbSamples;
Yann Collet1496c3d2016-12-18 11:58:23 +0100253 }
Yann Collet086b9592017-09-14 16:45:10 -0700254 DISPLAYLEVEL(4, "Preparing to load : %u KB \n", (U32)(fs.totalSizeToLoad >> 10));
255 return fs;
Yann Collet1496c3d2016-12-18 11:58:23 +0100256}
257
258
Nick Terrell5b7fd7c2017-06-26 21:07:14 -0700259/*! ZDICT_trainFromBuffer_unsafe_legacy() :
Yann Collet6f3acba2016-02-12 20:19:48 +0100260 Strictly Internal use only !!
Nick Terrell5b7fd7c2017-06-26 21:07:14 -0700261 Same as ZDICT_trainFromBuffer_legacy(), but does not control `samplesBuffer`.
Yann Collet6f3acba2016-02-12 20:19:48 +0100262 `samplesBuffer` must be followed by noisy guard band to avoid out-of-buffer reads.
263 @return : size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
264 or an error code.
265*/
Nick Terrell5b7fd7c2017-06-26 21:07:14 -0700266size_t ZDICT_trainFromBuffer_unsafe_legacy(void* dictBuffer, size_t dictBufferCapacity,
267 const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples,
268 ZDICT_legacy_params_t parameters);
Yann Collet6f3acba2016-02-12 20:19:48 +0100269
270
Yann Collet71eafdd2016-02-12 02:31:57 +0100271int DiB_trainFromFiles(const char* dictFileName, unsigned maxDictSize,
Yann Collet086b9592017-09-14 16:45:10 -0700272 const char** fileNamesTable, unsigned nbFiles, size_t chunkSize,
Nick Terrell5b7fd7c2017-06-26 21:07:14 -0700273 ZDICT_legacy_params_t *params, ZDICT_cover_params_t *coverParams,
Nick Terrelldf8415c2016-12-31 21:08:24 -0800274 int optimizeCover)
Yann Collet71eafdd2016-02-12 02:31:57 +0100275{
Yann Colletc68d17f2017-09-15 15:31:31 -0700276 unsigned const displayLevel = params ? params->zParams.notificationLevel :
277 coverParams ? coverParams->zParams.notificationLevel :
278 0; /* should never happen */
Yann Collet290aaa72016-05-30 21:18:52 +0200279 void* const dictBuffer = malloc(maxDictSize);
Yann Collet086b9592017-09-14 16:45:10 -0700280 fileStats const fs = DiB_fileStats(fileNamesTable, nbFiles, chunkSize, displayLevel);
Yann Colletc68d17f2017-09-15 15:31:31 -0700281 size_t* const sampleSizes = (size_t*)malloc(fs.nbSamples * sizeof(size_t));
Nick Terrellc220d4c2017-01-09 16:49:04 -0800282 size_t const memMult = params ? MEMMULT : COVER_MEMMULT;
Yann Collet086b9592017-09-14 16:45:10 -0700283 size_t const maxMem = DiB_findMaxMem(fs.totalSizeToLoad * memMult) / memMult;
284 size_t loadedSize = (size_t) MIN ((unsigned long long)maxMem, fs.totalSizeToLoad);
285 void* const srcBuffer = malloc(loadedSize+NOISELENGTH);
Yann Collet71eafdd2016-02-12 02:31:57 +0100286 int result = 0;
287
Yann Collet290aaa72016-05-30 21:18:52 +0200288 /* Checks */
Yann Colletc68d17f2017-09-15 15:31:31 -0700289 if ((!sampleSizes) || (!srcBuffer) || (!dictBuffer))
Yann Collet77c137b2017-09-14 15:12:57 -0700290 EXM_THROW(12, "not enough memory for DiB_trainFiles"); /* should not happen */
Yann Collet086b9592017-09-14 16:45:10 -0700291 if (fs.oneSampleTooLarge) {
292 DISPLAYLEVEL(2, "! Warning : some sample(s) are very large \n");
293 DISPLAYLEVEL(2, "! Note that dictionary is only useful for small samples. \n");
294 DISPLAYLEVEL(2, "! As a consequence, only the first %u bytes of each sample are loaded \n", SAMPLESIZE_MAX);
Yann Collet1496c3d2016-12-18 11:58:23 +0100295 }
Yann Colletc68d17f2017-09-15 15:31:31 -0700296 if (fs.nbSamples < 5) {
Yann Collet49d105c2016-08-18 15:02:11 +0200297 DISPLAYLEVEL(2, "! Warning : nb of samples too low for proper processing ! \n");
298 DISPLAYLEVEL(2, "! Please provide _one file per sample_. \n");
Yann Collet17220552017-09-15 16:23:50 -0700299 DISPLAYLEVEL(2, "! Alternatively, split files into fixed-size blocks representative of samples, with -B# \n");
Yann Collet086b9592017-09-14 16:45:10 -0700300 EXM_THROW(14, "nb of samples too low"); /* we now clearly forbid this case */
301 }
302 if (fs.totalSizeToLoad < (unsigned long long)(8 * maxDictSize)) {
303 DISPLAYLEVEL(2, "! Warning : data size of samples too small for target dictionary size \n");
304 DISPLAYLEVEL(2, "! Samples should be about 100x larger than target dictionary size \n");
Yann Colletdd25a272016-07-27 12:35:29 +0200305 }
Yann Collet290aaa72016-05-30 21:18:52 +0200306
Yann Collet71eafdd2016-02-12 02:31:57 +0100307 /* init */
Yann Collet086b9592017-09-14 16:45:10 -0700308 if (loadedSize < fs.totalSizeToLoad)
309 DISPLAYLEVEL(1, "Not enough memory; training on %u MB only...\n", (unsigned)(loadedSize >> 20));
Yann Collet71eafdd2016-02-12 02:31:57 +0100310
Yann Collet71eafdd2016-02-12 02:31:57 +0100311 /* Load input buffer */
Nick Terrelldf8415c2016-12-31 21:08:24 -0800312 DISPLAYLEVEL(3, "Shuffling input files\n");
313 DiB_shuffle(fileNamesTable, nbFiles);
Yann Colletc68d17f2017-09-15 15:31:31 -0700314 nbFiles = DiB_loadFiles(srcBuffer, &loadedSize, sampleSizes, fs.nbSamples, fileNamesTable, nbFiles, chunkSize, displayLevel);
Yann Collet71eafdd2016-02-12 02:31:57 +0100315
Yann Collet77c137b2017-09-14 15:12:57 -0700316 { size_t dictSize;
Nick Terrelldf8415c2016-12-31 21:08:24 -0800317 if (params) {
Yann Collet086b9592017-09-14 16:45:10 -0700318 DiB_fillNoise((char*)srcBuffer + loadedSize, NOISELENGTH); /* guard band, for end of buffer condition */
Nick Terrell5b7fd7c2017-06-26 21:07:14 -0700319 dictSize = ZDICT_trainFromBuffer_unsafe_legacy(dictBuffer, maxDictSize,
Yann Colletc68d17f2017-09-15 15:31:31 -0700320 srcBuffer, sampleSizes, fs.nbSamples,
Nick Terrell5b7fd7c2017-06-26 21:07:14 -0700321 *params);
Nick Terrelldf8415c2016-12-31 21:08:24 -0800322 } else if (optimizeCover) {
Nick Terrell5b7fd7c2017-06-26 21:07:14 -0700323 dictSize = ZDICT_optimizeTrainFromBuffer_cover(dictBuffer, maxDictSize,
Yann Colletc68d17f2017-09-15 15:31:31 -0700324 srcBuffer, sampleSizes, fs.nbSamples,
Nick Terrell5b7fd7c2017-06-26 21:07:14 -0700325 coverParams);
Nick Terrelldf8415c2016-12-31 21:08:24 -0800326 if (!ZDICT_isError(dictSize)) {
Nick Terrell5b7fd7c2017-06-26 21:07:14 -0700327 DISPLAYLEVEL(2, "k=%u\nd=%u\nsteps=%u\n", coverParams->k, coverParams->d, coverParams->steps);
Nick Terrelldf8415c2016-12-31 21:08:24 -0800328 }
329 } else {
Yann Collet77c137b2017-09-14 15:12:57 -0700330 dictSize = ZDICT_trainFromBuffer_cover(dictBuffer, maxDictSize, srcBuffer,
Yann Colletc68d17f2017-09-15 15:31:31 -0700331 sampleSizes, fs.nbSamples, *coverParams);
Nick Terrelldf8415c2016-12-31 21:08:24 -0800332 }
Yann Collet290aaa72016-05-30 21:18:52 +0200333 if (ZDICT_isError(dictSize)) {
334 DISPLAYLEVEL(1, "dictionary training failed : %s \n", ZDICT_getErrorName(dictSize)); /* should not happen */
335 result = 1;
336 goto _cleanup;
337 }
338 /* save dict */
339 DISPLAYLEVEL(2, "Save dictionary of size %u into file %s \n", (U32)dictSize, dictFileName);
340 DiB_saveDict(dictFileName, dictBuffer, dictSize);
Yann Collet71eafdd2016-02-12 02:31:57 +0100341 }
342
Yann Collet71eafdd2016-02-12 02:31:57 +0100343 /* clean up */
344_cleanup:
345 free(srcBuffer);
Yann Colletc68d17f2017-09-15 15:31:31 -0700346 free(sampleSizes);
Yann Collet71eafdd2016-02-12 02:31:57 +0100347 free(dictBuffer);
Yann Collet71eafdd2016-02-12 02:31:57 +0100348 return result;
349}