blob: 4b68be6c97aba4a88354f98ad1144c053e3429fc [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 Collet42a02ab2018-08-15 14:35:38 -070030#include <assert.h>
Yann Collet71eafdd2016-02-12 02:31:57 +010031
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010032#include "mem.h" /* read */
Yann Collet71eafdd2016-02-12 02:31:57 +010033#include "error_private.h"
inikep23a08892016-04-22 12:43:18 +020034#include "dibio.h"
Yann Collet71eafdd2016-02-12 02:31:57 +010035
36
37/*-*************************************
38* Constants
39***************************************/
40#define KB *(1 <<10)
41#define MB *(1 <<20)
42#define GB *(1U<<30)
43
Yann Collet1496c3d2016-12-18 11:58:23 +010044#define SAMPLESIZE_MAX (128 KB)
45#define MEMMULT 11 /* rough estimation : memory cost to analyze 1 byte of sample */
Nick Terrelldf8415c2016-12-31 21:08:24 -080046#define COVER_MEMMULT 9 /* rough estimation : memory cost to analyze 1 byte of sample */
Jennifer Liu9d6ed9d2018-08-23 12:06:20 -070047#define FASTCOVER_MEMMULT 1 /* rough estimation : memory cost to analyze 1 byte of sample */
Yann Collet77c137b2017-09-14 15:12:57 -070048static 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 +010049
50#define NOISELENGTH 32
Yann Collet71eafdd2016-02-12 02:31:57 +010051
52
53/*-*************************************
54* Console display
55***************************************/
56#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
Yann Collet086b9592017-09-14 16:45:10 -070057#define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); }
Yann Collet71eafdd2016-02-12 02:31:57 +010058
Nick Terrell9a2f6f42017-11-29 19:11:12 -080059static const U64 g_refreshRate = SEC_TO_MICRO / 6;
60static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER;
Yann Colletf6ca09b2016-05-09 04:44:45 +020061
Nick Terrell9a2f6f42017-11-29 19:11:12 -080062#define DISPLAYUPDATE(l, ...) { if (displayLevel>=l) { \
63 if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || (displayLevel>=4)) \
64 { g_displayClock = UTIL_getTime(); DISPLAY(__VA_ARGS__); \
65 if (displayLevel>=4) fflush(stderr); } } }
Yann Collet71eafdd2016-02-12 02:31:57 +010066
67/*-*************************************
68* Exceptions
69***************************************/
70#ifndef DEBUG
71# define DEBUG 0
72#endif
73#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__);
74#define EXM_THROW(error, ...) \
75{ \
76 DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
Yann Collet086b9592017-09-14 16:45:10 -070077 DISPLAY("Error %i : ", error); \
78 DISPLAY(__VA_ARGS__); \
79 DISPLAY("\n"); \
Yann Collet71eafdd2016-02-12 02:31:57 +010080 exit(error); \
81}
82
83
84/* ********************************************************
85* Helper functions
86**********************************************************/
87unsigned DiB_isError(size_t errorCode) { return ERR_isError(errorCode); }
88
89const char* DiB_getErrorName(size_t errorCode) { return ERR_getErrorName(errorCode); }
90
Sean Purcell42bac7f2017-04-13 15:35:05 -070091#undef MIN
92#define MIN(a,b) ((a) < (b) ? (a) : (b))
Yann Colletbcb5f772016-07-06 15:41:03 +020093
Yann Collet71eafdd2016-02-12 02:31:57 +010094
95/* ********************************************************
96* File related operations
97**********************************************************/
Yann Collet290aaa72016-05-30 21:18:52 +020098/** DiB_loadFiles() :
Yann Colletc68d17f2017-09-15 15:31:31 -070099 * load samples from files listed in fileNamesTable into buffer.
100 * works even if buffer is too small to load all samples.
101 * Also provides the size of each sample into sampleSizes table
102 * which must be sized correctly, using DiB_fileStats().
103 * @return : nb of samples effectively loaded into `buffer`
104 * *bufferSizePtr is modified, it provides the amount data loaded within buffer.
105 * sampleSizes is filled with the size of each sample.
106 */
Yann Colletbcb5f772016-07-06 15:41:03 +0200107static unsigned DiB_loadFiles(void* buffer, size_t* bufferSizePtr,
Yann Colletc68d17f2017-09-15 15:31:31 -0700108 size_t* sampleSizes, unsigned sstSize,
Yann Collet086b9592017-09-14 16:45:10 -0700109 const char** fileNamesTable, unsigned nbFiles, size_t targetChunkSize,
110 unsigned displayLevel)
Yann Collet71eafdd2016-02-12 02:31:57 +0100111{
Yann Collet290aaa72016-05-30 21:18:52 +0200112 char* const buff = (char*)buffer;
Yann Collet71eafdd2016-02-12 02:31:57 +0100113 size_t pos = 0;
Yann Collet086b9592017-09-14 16:45:10 -0700114 unsigned nbLoadedChunks = 0, fileIndex;
Yann Collet71eafdd2016-02-12 02:31:57 +0100115
Yann Collet086b9592017-09-14 16:45:10 -0700116 for (fileIndex=0; fileIndex<nbFiles; fileIndex++) {
117 const char* const fileName = fileNamesTable[fileIndex];
Yann Colletbcb5f772016-07-06 15:41:03 +0200118 unsigned long long const fs64 = UTIL_getFileSize(fileName);
Yann Collet18b79532017-10-17 16:14:25 -0700119 unsigned long long remainingToLoad = (fs64 == UTIL_FILESIZE_UNKNOWN) ? 0 : fs64;
Yann Collet086b9592017-09-14 16:45:10 -0700120 U32 const nbChunks = targetChunkSize ? (U32)((fs64 + (targetChunkSize-1)) / targetChunkSize) : 1;
121 U64 const chunkSize = targetChunkSize ? MIN(targetChunkSize, fs64) : fs64;
Yann Collet25a60482017-09-15 11:55:13 -0700122 size_t const maxChunkSize = (size_t)MIN(chunkSize, SAMPLESIZE_MAX);
Yann Collet086b9592017-09-14 16:45:10 -0700123 U32 cnb;
124 FILE* const f = fopen(fileName, "rb");
125 if (f==NULL) EXM_THROW(10, "zstd: dictBuilder: %s %s ", fileName, strerror(errno));
126 DISPLAYUPDATE(2, "Loading %s... \r", fileName);
127 for (cnb=0; cnb<nbChunks; cnb++) {
Yann Collet25a60482017-09-15 11:55:13 -0700128 size_t const toLoad = (size_t)MIN(maxChunkSize, remainingToLoad);
Yann Collet086b9592017-09-14 16:45:10 -0700129 if (toLoad > *bufferSizePtr-pos) break;
130 { size_t const readSize = fread(buff+pos, 1, toLoad, f);
131 if (readSize != toLoad) EXM_THROW(11, "Pb reading %s", fileName);
132 pos += readSize;
Yann Colletc68d17f2017-09-15 15:31:31 -0700133 sampleSizes[nbLoadedChunks++] = toLoad;
Yann Collet086b9592017-09-14 16:45:10 -0700134 remainingToLoad -= targetChunkSize;
Yann Colletc68d17f2017-09-15 15:31:31 -0700135 if (nbLoadedChunks == sstSize) { /* no more space left in sampleSizes table */
136 fileIndex = nbFiles; /* stop there */
137 break;
138 }
Yann Collet086b9592017-09-14 16:45:10 -0700139 if (toLoad < targetChunkSize) {
Yann Colleta9694232017-09-15 10:16:26 -0700140 fseek(f, (long)(targetChunkSize - toLoad), SEEK_CUR);
Yann Collet086b9592017-09-14 16:45:10 -0700141 } } }
142 fclose(f);
143 }
Nick Terrelldf8415c2016-12-31 21:08:24 -0800144 DISPLAYLEVEL(2, "\r%79s\r", "");
Yann Colletbcb5f772016-07-06 15:41:03 +0200145 *bufferSizePtr = pos;
Yann Collet086b9592017-09-14 16:45:10 -0700146 DISPLAYLEVEL(4, "loaded : %u KB \n", (U32)(pos >> 10))
147 return nbLoadedChunks;
Yann Collet71eafdd2016-02-12 02:31:57 +0100148}
149
Nick Terrelldf8415c2016-12-31 21:08:24 -0800150#define DiB_rotl32(x,r) ((x << r) | (x >> (32 - r)))
151static U32 DiB_rand(U32* src)
152{
153 static const U32 prime1 = 2654435761U;
154 static const U32 prime2 = 2246822519U;
155 U32 rand32 = *src;
156 rand32 *= prime1;
157 rand32 ^= prime2;
158 rand32 = DiB_rotl32(rand32, 13);
159 *src = rand32;
160 return rand32 >> 5;
161}
162
Yann Collet77c137b2017-09-14 15:12:57 -0700163/* DiB_shuffle() :
164 * shuffle a table of file names in a semi-random way
165 * It improves dictionary quality by reducing "locality" impact, so if sample set is very large,
166 * it will load random elements from it, instead of just the first ones. */
Nick Terrelldf8415c2016-12-31 21:08:24 -0800167static void DiB_shuffle(const char** fileNamesTable, unsigned nbFiles) {
Yann Collet77c137b2017-09-14 15:12:57 -0700168 U32 seed = 0xFD2FB528;
169 unsigned i;
Yann Collet42a02ab2018-08-15 14:35:38 -0700170 assert(nbFiles >= 1);
Yann Collet77c137b2017-09-14 15:12:57 -0700171 for (i = nbFiles - 1; i > 0; --i) {
172 unsigned const j = DiB_rand(&seed) % (i + 1);
173 const char* const tmp = fileNamesTable[j];
174 fileNamesTable[j] = fileNamesTable[i];
175 fileNamesTable[i] = tmp;
176 }
Nick Terrelldf8415c2016-12-31 21:08:24 -0800177}
178
Yann Collet71eafdd2016-02-12 02:31:57 +0100179
180/*-********************************************************
181* Dictionary training functions
182**********************************************************/
183static size_t DiB_findMaxMem(unsigned long long requiredMem)
184{
Yann Collet290aaa72016-05-30 21:18:52 +0200185 size_t const step = 8 MB;
Yann Collet71eafdd2016-02-12 02:31:57 +0100186 void* testmem = NULL;
187
188 requiredMem = (((requiredMem >> 23) + 1) << 23);
Yann Colletbcb5f772016-07-06 15:41:03 +0200189 requiredMem += step;
Yann Collet77c137b2017-09-14 15:12:57 -0700190 if (requiredMem > g_maxMemory) requiredMem = g_maxMemory;
Yann Collet71eafdd2016-02-12 02:31:57 +0100191
192 while (!testmem) {
Yann Collet71eafdd2016-02-12 02:31:57 +0100193 testmem = malloc((size_t)requiredMem);
Yann Colletbcb5f772016-07-06 15:41:03 +0200194 requiredMem -= step;
Yann Collet71eafdd2016-02-12 02:31:57 +0100195 }
196
197 free(testmem);
Yann Colletbcb5f772016-07-06 15:41:03 +0200198 return (size_t)requiredMem;
Yann Collet71eafdd2016-02-12 02:31:57 +0100199}
200
201
202static void DiB_fillNoise(void* buffer, size_t length)
203{
Yann Colletbcb5f772016-07-06 15:41:03 +0200204 unsigned const prime1 = 2654435761U;
205 unsigned const prime2 = 2246822519U;
206 unsigned acc = prime1;
Yann Collet71eafdd2016-02-12 02:31:57 +0100207 size_t p=0;;
208
209 for (p=0; p<length; p++) {
Yann Colletbcb5f772016-07-06 15:41:03 +0200210 acc *= prime2;
Yann Collet71eafdd2016-02-12 02:31:57 +0100211 ((unsigned char*)buffer)[p] = (unsigned char)(acc >> 21);
212 }
213}
214
215
216static void DiB_saveDict(const char* dictFileName,
217 const void* buff, size_t buffSize)
218{
Yann Collet290aaa72016-05-30 21:18:52 +0200219 FILE* const f = fopen(dictFileName, "wb");
Yann Collet71eafdd2016-02-12 02:31:57 +0100220 if (f==NULL) EXM_THROW(3, "cannot open %s ", dictFileName);
221
Yann Colletf6ca09b2016-05-09 04:44:45 +0200222 { size_t const n = fwrite(buff, 1, buffSize, f);
223 if (n!=buffSize) EXM_THROW(4, "%s : write error", dictFileName) }
Yann Collet71eafdd2016-02-12 02:31:57 +0100224
Yann Colletf6ca09b2016-05-09 04:44:45 +0200225 { size_t const n = (size_t)fclose(f);
226 if (n!=0) EXM_THROW(5, "%s : flush error", dictFileName) }
Yann Collet71eafdd2016-02-12 02:31:57 +0100227}
228
229
Yann Collet086b9592017-09-14 16:45:10 -0700230typedef struct {
231 U64 totalSizeToLoad;
232 unsigned oneSampleTooLarge;
Yann Colletc68d17f2017-09-15 15:31:31 -0700233 unsigned nbSamples;
Yann Collet086b9592017-09-14 16:45:10 -0700234} fileStats;
235
Yann Colletc68d17f2017-09-15 15:31:31 -0700236/*! DiB_fileStats() :
237 * Given a list of files, and a chunkSize (0 == no chunk, whole files)
238 * provides the amount of data to be loaded and the resulting nb of samples.
239 * This is useful primarily for allocation purpose => sample buffer, and sample sizes table.
240 */
Yann Collet086b9592017-09-14 16:45:10 -0700241static fileStats DiB_fileStats(const char** fileNamesTable, unsigned nbFiles, size_t chunkSize, unsigned displayLevel)
Yann Collet1496c3d2016-12-18 11:58:23 +0100242{
Yann Collet086b9592017-09-14 16:45:10 -0700243 fileStats fs;
Yann Collet1496c3d2016-12-18 11:58:23 +0100244 unsigned n;
Yann Collet086b9592017-09-14 16:45:10 -0700245 memset(&fs, 0, sizeof(fs));
Yann Collet1496c3d2016-12-18 11:58:23 +0100246 for (n=0; n<nbFiles; n++) {
247 U64 const fileSize = UTIL_getFileSize(fileNamesTable[n]);
Yann Collet18b79532017-10-17 16:14:25 -0700248 U64 const srcSize = (fileSize == UTIL_FILESIZE_UNKNOWN) ? 0 : fileSize;
249 U32 const nbSamples = (U32)(chunkSize ? (srcSize + (chunkSize-1)) / chunkSize : 1);
250 U64 const chunkToLoad = chunkSize ? MIN(chunkSize, srcSize) : srcSize;
Yann Collet25a60482017-09-15 11:55:13 -0700251 size_t const cappedChunkSize = (size_t)MIN(chunkToLoad, SAMPLESIZE_MAX);
Yann Colletc68d17f2017-09-15 15:31:31 -0700252 fs.totalSizeToLoad += cappedChunkSize * nbSamples;
Yann Collet086b9592017-09-14 16:45:10 -0700253 fs.oneSampleTooLarge |= (chunkSize > 2*SAMPLESIZE_MAX);
Yann Colletc68d17f2017-09-15 15:31:31 -0700254 fs.nbSamples += nbSamples;
Yann Collet1496c3d2016-12-18 11:58:23 +0100255 }
Yann Collet086b9592017-09-14 16:45:10 -0700256 DISPLAYLEVEL(4, "Preparing to load : %u KB \n", (U32)(fs.totalSizeToLoad >> 10));
257 return fs;
Yann Collet1496c3d2016-12-18 11:58:23 +0100258}
259
260
Nick Terrell5b7fd7c2017-06-26 21:07:14 -0700261/*! ZDICT_trainFromBuffer_unsafe_legacy() :
Yann Collet6f3acba2016-02-12 20:19:48 +0100262 Strictly Internal use only !!
Nick Terrell5b7fd7c2017-06-26 21:07:14 -0700263 Same as ZDICT_trainFromBuffer_legacy(), but does not control `samplesBuffer`.
Yann Collet6f3acba2016-02-12 20:19:48 +0100264 `samplesBuffer` must be followed by noisy guard band to avoid out-of-buffer reads.
265 @return : size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
266 or an error code.
267*/
Nick Terrell5b7fd7c2017-06-26 21:07:14 -0700268size_t ZDICT_trainFromBuffer_unsafe_legacy(void* dictBuffer, size_t dictBufferCapacity,
269 const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples,
270 ZDICT_legacy_params_t parameters);
Yann Collet6f3acba2016-02-12 20:19:48 +0100271
272
Yann Collet71eafdd2016-02-12 02:31:57 +0100273int DiB_trainFromFiles(const char* dictFileName, unsigned maxDictSize,
Yann Collet086b9592017-09-14 16:45:10 -0700274 const char** fileNamesTable, unsigned nbFiles, size_t chunkSize,
Jennifer Liu9d6ed9d2018-08-23 12:06:20 -0700275 ZDICT_legacy_params_t* params, ZDICT_cover_params_t* coverParams,
276 ZDICT_fastCover_params_t* fastCoverParams, int optimize)
Yann Collet71eafdd2016-02-12 02:31:57 +0100277{
Yann Colletc68d17f2017-09-15 15:31:31 -0700278 unsigned const displayLevel = params ? params->zParams.notificationLevel :
279 coverParams ? coverParams->zParams.notificationLevel :
Jennifer Liu9d6ed9d2018-08-23 12:06:20 -0700280 fastCoverParams ? fastCoverParams->zParams.notificationLevel :
Yann Colletc68d17f2017-09-15 15:31:31 -0700281 0; /* should never happen */
Yann Collet290aaa72016-05-30 21:18:52 +0200282 void* const dictBuffer = malloc(maxDictSize);
Yann Collet086b9592017-09-14 16:45:10 -0700283 fileStats const fs = DiB_fileStats(fileNamesTable, nbFiles, chunkSize, displayLevel);
Yann Colletc68d17f2017-09-15 15:31:31 -0700284 size_t* const sampleSizes = (size_t*)malloc(fs.nbSamples * sizeof(size_t));
Jennifer Liu9d6ed9d2018-08-23 12:06:20 -0700285 size_t const memMult = params ? MEMMULT :
286 coverParams ? COVER_MEMMULT:
287 FASTCOVER_MEMMULT;
Yann Collet086b9592017-09-14 16:45:10 -0700288 size_t const maxMem = DiB_findMaxMem(fs.totalSizeToLoad * memMult) / memMult;
289 size_t loadedSize = (size_t) MIN ((unsigned long long)maxMem, fs.totalSizeToLoad);
290 void* const srcBuffer = malloc(loadedSize+NOISELENGTH);
Yann Collet71eafdd2016-02-12 02:31:57 +0100291 int result = 0;
292
Yann Collet290aaa72016-05-30 21:18:52 +0200293 /* Checks */
Yann Colletc68d17f2017-09-15 15:31:31 -0700294 if ((!sampleSizes) || (!srcBuffer) || (!dictBuffer))
Yann Collet77c137b2017-09-14 15:12:57 -0700295 EXM_THROW(12, "not enough memory for DiB_trainFiles"); /* should not happen */
Yann Collet086b9592017-09-14 16:45:10 -0700296 if (fs.oneSampleTooLarge) {
297 DISPLAYLEVEL(2, "! Warning : some sample(s) are very large \n");
298 DISPLAYLEVEL(2, "! Note that dictionary is only useful for small samples. \n");
299 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 +0100300 }
Yann Colletc68d17f2017-09-15 15:31:31 -0700301 if (fs.nbSamples < 5) {
Yann Collet49d105c2016-08-18 15:02:11 +0200302 DISPLAYLEVEL(2, "! Warning : nb of samples too low for proper processing ! \n");
303 DISPLAYLEVEL(2, "! Please provide _one file per sample_. \n");
Yann Collet17220552017-09-15 16:23:50 -0700304 DISPLAYLEVEL(2, "! Alternatively, split files into fixed-size blocks representative of samples, with -B# \n");
Yann Collet086b9592017-09-14 16:45:10 -0700305 EXM_THROW(14, "nb of samples too low"); /* we now clearly forbid this case */
306 }
307 if (fs.totalSizeToLoad < (unsigned long long)(8 * maxDictSize)) {
308 DISPLAYLEVEL(2, "! Warning : data size of samples too small for target dictionary size \n");
309 DISPLAYLEVEL(2, "! Samples should be about 100x larger than target dictionary size \n");
Yann Colletdd25a272016-07-27 12:35:29 +0200310 }
Yann Collet290aaa72016-05-30 21:18:52 +0200311
Yann Collet71eafdd2016-02-12 02:31:57 +0100312 /* init */
Yann Collet086b9592017-09-14 16:45:10 -0700313 if (loadedSize < fs.totalSizeToLoad)
314 DISPLAYLEVEL(1, "Not enough memory; training on %u MB only...\n", (unsigned)(loadedSize >> 20));
Yann Collet71eafdd2016-02-12 02:31:57 +0100315
Yann Collet71eafdd2016-02-12 02:31:57 +0100316 /* Load input buffer */
Nick Terrelldf8415c2016-12-31 21:08:24 -0800317 DISPLAYLEVEL(3, "Shuffling input files\n");
318 DiB_shuffle(fileNamesTable, nbFiles);
Jennifer Liu9d6ed9d2018-08-23 12:06:20 -0700319
Yann Collet42a02ab2018-08-15 14:35:38 -0700320 DiB_loadFiles(srcBuffer, &loadedSize, sampleSizes, fs.nbSamples, fileNamesTable, nbFiles, chunkSize, displayLevel);
Yann Collet71eafdd2016-02-12 02:31:57 +0100321
Yann Collet77c137b2017-09-14 15:12:57 -0700322 { size_t dictSize;
Nick Terrelldf8415c2016-12-31 21:08:24 -0800323 if (params) {
Yann Collet086b9592017-09-14 16:45:10 -0700324 DiB_fillNoise((char*)srcBuffer + loadedSize, NOISELENGTH); /* guard band, for end of buffer condition */
Nick Terrell5b7fd7c2017-06-26 21:07:14 -0700325 dictSize = ZDICT_trainFromBuffer_unsafe_legacy(dictBuffer, maxDictSize,
Yann Colletc68d17f2017-09-15 15:31:31 -0700326 srcBuffer, sampleSizes, fs.nbSamples,
Nick Terrell5b7fd7c2017-06-26 21:07:14 -0700327 *params);
Jennifer Liu9d6ed9d2018-08-23 12:06:20 -0700328 } else if (coverParams) {
329 if (optimize) {
330 dictSize = ZDICT_optimizeTrainFromBuffer_cover(dictBuffer, maxDictSize,
331 srcBuffer, sampleSizes, fs.nbSamples,
332 coverParams);
333 if (!ZDICT_isError(dictSize)) {
334 unsigned splitPercentage = (unsigned)(coverParams->splitPoint * 100);
335 DISPLAYLEVEL(2, "k=%u\nd=%u\nsteps=%u\nsplit=%u\n", coverParams->k, coverParams->d,
336 coverParams->steps, splitPercentage);
337 }
338 } else {
339 dictSize = ZDICT_trainFromBuffer_cover(dictBuffer, maxDictSize, srcBuffer,
340 sampleSizes, fs.nbSamples, *coverParams);
Nick Terrelldf8415c2016-12-31 21:08:24 -0800341 }
342 } else {
Jennifer Liu9d6ed9d2018-08-23 12:06:20 -0700343 assert(fastCoverParams != NULL);
344 if (optimize) {
345 dictSize = ZDICT_optimizeTrainFromBuffer_fastCover(dictBuffer, maxDictSize,
346 srcBuffer, sampleSizes, fs.nbSamples,
347 fastCoverParams);
348 if (!ZDICT_isError(dictSize)) {
349 unsigned splitPercentage = (unsigned)(fastCoverParams->splitPoint * 100);
350 DISPLAYLEVEL(2, "k=%u\nd=%u\nf=%u\nsteps=%u\nsplit=%u\naccel=%u\n", fastCoverParams->k,
351 fastCoverParams->d, fastCoverParams->f, fastCoverParams->steps, splitPercentage,
352 fastCoverParams->accel);
353 }
354 } else {
355 dictSize = ZDICT_trainFromBuffer_fastCover(dictBuffer, maxDictSize, srcBuffer,
356 sampleSizes, fs.nbSamples, *fastCoverParams);
357 }
Nick Terrelldf8415c2016-12-31 21:08:24 -0800358 }
Yann Collet290aaa72016-05-30 21:18:52 +0200359 if (ZDICT_isError(dictSize)) {
360 DISPLAYLEVEL(1, "dictionary training failed : %s \n", ZDICT_getErrorName(dictSize)); /* should not happen */
361 result = 1;
362 goto _cleanup;
363 }
364 /* save dict */
365 DISPLAYLEVEL(2, "Save dictionary of size %u into file %s \n", (U32)dictSize, dictFileName);
366 DiB_saveDict(dictFileName, dictBuffer, dictSize);
Yann Collet71eafdd2016-02-12 02:31:57 +0100367 }
368
Yann Collet71eafdd2016-02-12 02:31:57 +0100369 /* clean up */
370_cleanup:
371 free(srcBuffer);
Yann Colletc68d17f2017-09-15 15:31:31 -0700372 free(sampleSizes);
Yann Collet71eafdd2016-02-12 02:31:57 +0100373 free(dictBuffer);
Yann Collet71eafdd2016-02-12 02:31:57 +0100374 return result;
375}