blob: d6c9f6d9e11967bb0d880fc6545d1c4a420389d7 [file] [log] [blame]
Yann Collet32fb4072017-08-18 16:52:05 -07001/*
Nick Terrella4943082021-03-29 14:23:36 -07002 * Copyright (c) Yann Collet, Facebook, Inc.
Yann Collet4ded9e52016-08-30 10:04:33 -07003 * 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
Yann Collet59a71162019-04-10 12:37:03 -070032#include "timefn.h" /* UTIL_time_t, UTIL_clockSpanMicro, UTIL_getTime */
W. Felix Handte7dcca6b2020-05-01 16:20:40 -040033#include "../lib/common/mem.h" /* read */
34#include "../lib/common/error_private.h"
inikep23a08892016-04-22 12:43:18 +020035#include "dibio.h"
Yann Collet71eafdd2016-02-12 02:31:57 +010036
37
38/*-*************************************
39* Constants
40***************************************/
41#define KB *(1 <<10)
42#define MB *(1 <<20)
43#define GB *(1U<<30)
44
Yann Collet1496c3d2016-12-18 11:58:23 +010045#define SAMPLESIZE_MAX (128 KB)
46#define MEMMULT 11 /* rough estimation : memory cost to analyze 1 byte of sample */
Nick Terrelldf8415c2016-12-31 21:08:24 -080047#define COVER_MEMMULT 9 /* rough estimation : memory cost to analyze 1 byte of sample */
Jennifer Liu9d6ed9d2018-08-23 12:06:20 -070048#define FASTCOVER_MEMMULT 1 /* rough estimation : memory cost to analyze 1 byte of sample */
Yann Collet77c137b2017-09-14 15:12:57 -070049static 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 +010050
51#define NOISELENGTH 32
Yann Collet71eafdd2016-02-12 02:31:57 +010052
53
54/*-*************************************
55* Console display
56***************************************/
57#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
Yann Collet086b9592017-09-14 16:45:10 -070058#define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); }
Yann Collet71eafdd2016-02-12 02:31:57 +010059
Nick Terrell9a2f6f42017-11-29 19:11:12 -080060static const U64 g_refreshRate = SEC_TO_MICRO / 6;
61static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER;
Yann Colletf6ca09b2016-05-09 04:44:45 +020062
Nick Terrell9a2f6f42017-11-29 19:11:12 -080063#define DISPLAYUPDATE(l, ...) { if (displayLevel>=l) { \
64 if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || (displayLevel>=4)) \
65 { g_displayClock = UTIL_getTime(); DISPLAY(__VA_ARGS__); \
66 if (displayLevel>=4) fflush(stderr); } } }
Yann Collet71eafdd2016-02-12 02:31:57 +010067
68/*-*************************************
69* Exceptions
70***************************************/
71#ifndef DEBUG
72# define DEBUG 0
73#endif
74#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__);
75#define EXM_THROW(error, ...) \
76{ \
77 DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
Yann Collet086b9592017-09-14 16:45:10 -070078 DISPLAY("Error %i : ", error); \
79 DISPLAY(__VA_ARGS__); \
80 DISPLAY("\n"); \
Yann Collet71eafdd2016-02-12 02:31:57 +010081 exit(error); \
82}
83
84
85/* ********************************************************
86* Helper functions
87**********************************************************/
Sean Purcell42bac7f2017-04-13 15:35:05 -070088#undef MIN
89#define MIN(a,b) ((a) < (b) ? (a) : (b))
Yann Colletbcb5f772016-07-06 15:41:03 +020090
Yann Collet71eafdd2016-02-12 02:31:57 +010091
92/* ********************************************************
93* File related operations
94**********************************************************/
Yann Collet290aaa72016-05-30 21:18:52 +020095/** DiB_loadFiles() :
Yann Colletc68d17f2017-09-15 15:31:31 -070096 * load samples from files listed in fileNamesTable into buffer.
97 * works even if buffer is too small to load all samples.
98 * Also provides the size of each sample into sampleSizes table
99 * which must be sized correctly, using DiB_fileStats().
100 * @return : nb of samples effectively loaded into `buffer`
101 * *bufferSizePtr is modified, it provides the amount data loaded within buffer.
102 * sampleSizes is filled with the size of each sample.
103 */
Yann Colletbcb5f772016-07-06 15:41:03 +0200104static unsigned DiB_loadFiles(void* buffer, size_t* bufferSizePtr,
Yann Colletc68d17f2017-09-15 15:31:31 -0700105 size_t* sampleSizes, unsigned sstSize,
Yann Collet086b9592017-09-14 16:45:10 -0700106 const char** fileNamesTable, unsigned nbFiles, size_t targetChunkSize,
107 unsigned displayLevel)
Yann Collet71eafdd2016-02-12 02:31:57 +0100108{
Yann Collet290aaa72016-05-30 21:18:52 +0200109 char* const buff = (char*)buffer;
Yann Collet71eafdd2016-02-12 02:31:57 +0100110 size_t pos = 0;
Yann Collet086b9592017-09-14 16:45:10 -0700111 unsigned nbLoadedChunks = 0, fileIndex;
Yann Collet71eafdd2016-02-12 02:31:57 +0100112
Yann Collet086b9592017-09-14 16:45:10 -0700113 for (fileIndex=0; fileIndex<nbFiles; fileIndex++) {
114 const char* const fileName = fileNamesTable[fileIndex];
Yann Colletbcb5f772016-07-06 15:41:03 +0200115 unsigned long long const fs64 = UTIL_getFileSize(fileName);
Yann Collet18b79532017-10-17 16:14:25 -0700116 unsigned long long remainingToLoad = (fs64 == UTIL_FILESIZE_UNKNOWN) ? 0 : fs64;
Yann Collet086b9592017-09-14 16:45:10 -0700117 U32 const nbChunks = targetChunkSize ? (U32)((fs64 + (targetChunkSize-1)) / targetChunkSize) : 1;
118 U64 const chunkSize = targetChunkSize ? MIN(targetChunkSize, fs64) : fs64;
Yann Collet25a60482017-09-15 11:55:13 -0700119 size_t const maxChunkSize = (size_t)MIN(chunkSize, SAMPLESIZE_MAX);
Yann Collet086b9592017-09-14 16:45:10 -0700120 U32 cnb;
121 FILE* const f = fopen(fileName, "rb");
122 if (f==NULL) EXM_THROW(10, "zstd: dictBuilder: %s %s ", fileName, strerror(errno));
123 DISPLAYUPDATE(2, "Loading %s... \r", fileName);
124 for (cnb=0; cnb<nbChunks; cnb++) {
Yann Collet25a60482017-09-15 11:55:13 -0700125 size_t const toLoad = (size_t)MIN(maxChunkSize, remainingToLoad);
Yann Collet086b9592017-09-14 16:45:10 -0700126 if (toLoad > *bufferSizePtr-pos) break;
127 { size_t const readSize = fread(buff+pos, 1, toLoad, f);
128 if (readSize != toLoad) EXM_THROW(11, "Pb reading %s", fileName);
129 pos += readSize;
Yann Colletc68d17f2017-09-15 15:31:31 -0700130 sampleSizes[nbLoadedChunks++] = toLoad;
Yann Collet086b9592017-09-14 16:45:10 -0700131 remainingToLoad -= targetChunkSize;
Yann Colletc68d17f2017-09-15 15:31:31 -0700132 if (nbLoadedChunks == sstSize) { /* no more space left in sampleSizes table */
133 fileIndex = nbFiles; /* stop there */
134 break;
135 }
Yann Collet086b9592017-09-14 16:45:10 -0700136 if (toLoad < targetChunkSize) {
Yann Colleta9694232017-09-15 10:16:26 -0700137 fseek(f, (long)(targetChunkSize - toLoad), SEEK_CUR);
Yann Collet086b9592017-09-14 16:45:10 -0700138 } } }
139 fclose(f);
140 }
Nick Terrelldf8415c2016-12-31 21:08:24 -0800141 DISPLAYLEVEL(2, "\r%79s\r", "");
Yann Colletbcb5f772016-07-06 15:41:03 +0200142 *bufferSizePtr = pos;
Yann Colletededcfc2018-12-21 16:19:44 -0800143 DISPLAYLEVEL(4, "loaded : %u KB \n", (unsigned)(pos >> 10))
Yann Collet086b9592017-09-14 16:45:10 -0700144 return nbLoadedChunks;
Yann Collet71eafdd2016-02-12 02:31:57 +0100145}
146
Nick Terrelldf8415c2016-12-31 21:08:24 -0800147#define DiB_rotl32(x,r) ((x << r) | (x >> (32 - r)))
148static U32 DiB_rand(U32* src)
149{
150 static const U32 prime1 = 2654435761U;
151 static const U32 prime2 = 2246822519U;
152 U32 rand32 = *src;
153 rand32 *= prime1;
154 rand32 ^= prime2;
155 rand32 = DiB_rotl32(rand32, 13);
156 *src = rand32;
157 return rand32 >> 5;
158}
159
Yann Collet77c137b2017-09-14 15:12:57 -0700160/* DiB_shuffle() :
161 * shuffle a table of file names in a semi-random way
162 * It improves dictionary quality by reducing "locality" impact, so if sample set is very large,
163 * it will load random elements from it, instead of just the first ones. */
Nick Terrelldf8415c2016-12-31 21:08:24 -0800164static void DiB_shuffle(const char** fileNamesTable, unsigned nbFiles) {
Yann Collet77c137b2017-09-14 15:12:57 -0700165 U32 seed = 0xFD2FB528;
166 unsigned i;
Yann Collet42a02ab2018-08-15 14:35:38 -0700167 assert(nbFiles >= 1);
Yann Collet77c137b2017-09-14 15:12:57 -0700168 for (i = nbFiles - 1; i > 0; --i) {
169 unsigned const j = DiB_rand(&seed) % (i + 1);
170 const char* const tmp = fileNamesTable[j];
171 fileNamesTable[j] = fileNamesTable[i];
172 fileNamesTable[i] = tmp;
173 }
Nick Terrelldf8415c2016-12-31 21:08:24 -0800174}
175
Yann Collet71eafdd2016-02-12 02:31:57 +0100176
177/*-********************************************************
178* Dictionary training functions
179**********************************************************/
180static size_t DiB_findMaxMem(unsigned long long requiredMem)
181{
Yann Collet290aaa72016-05-30 21:18:52 +0200182 size_t const step = 8 MB;
Yann Collet71eafdd2016-02-12 02:31:57 +0100183 void* testmem = NULL;
184
185 requiredMem = (((requiredMem >> 23) + 1) << 23);
Yann Colletbcb5f772016-07-06 15:41:03 +0200186 requiredMem += step;
Yann Collet77c137b2017-09-14 15:12:57 -0700187 if (requiredMem > g_maxMemory) requiredMem = g_maxMemory;
Yann Collet71eafdd2016-02-12 02:31:57 +0100188
189 while (!testmem) {
Yann Collet71eafdd2016-02-12 02:31:57 +0100190 testmem = malloc((size_t)requiredMem);
Yann Colletbcb5f772016-07-06 15:41:03 +0200191 requiredMem -= step;
Yann Collet71eafdd2016-02-12 02:31:57 +0100192 }
193
194 free(testmem);
Yann Colletbcb5f772016-07-06 15:41:03 +0200195 return (size_t)requiredMem;
Yann Collet71eafdd2016-02-12 02:31:57 +0100196}
197
198
199static void DiB_fillNoise(void* buffer, size_t length)
200{
Yann Colletbcb5f772016-07-06 15:41:03 +0200201 unsigned const prime1 = 2654435761U;
202 unsigned const prime2 = 2246822519U;
203 unsigned acc = prime1;
Ed Masteb81d7cc2019-08-15 21:17:06 -0400204 size_t p=0;
Yann Collet71eafdd2016-02-12 02:31:57 +0100205
206 for (p=0; p<length; p++) {
Yann Colletbcb5f772016-07-06 15:41:03 +0200207 acc *= prime2;
Yann Collet71eafdd2016-02-12 02:31:57 +0100208 ((unsigned char*)buffer)[p] = (unsigned char)(acc >> 21);
209 }
210}
211
212
213static void DiB_saveDict(const char* dictFileName,
214 const void* buff, size_t buffSize)
215{
Yann Collet290aaa72016-05-30 21:18:52 +0200216 FILE* const f = fopen(dictFileName, "wb");
Yann Collet71eafdd2016-02-12 02:31:57 +0100217 if (f==NULL) EXM_THROW(3, "cannot open %s ", dictFileName);
218
Yann Colletf6ca09b2016-05-09 04:44:45 +0200219 { size_t const n = fwrite(buff, 1, buffSize, f);
220 if (n!=buffSize) EXM_THROW(4, "%s : write error", dictFileName) }
Yann Collet71eafdd2016-02-12 02:31:57 +0100221
Yann Colletf6ca09b2016-05-09 04:44:45 +0200222 { size_t const n = (size_t)fclose(f);
223 if (n!=0) EXM_THROW(5, "%s : flush error", dictFileName) }
Yann Collet71eafdd2016-02-12 02:31:57 +0100224}
225
226
Yann Collet086b9592017-09-14 16:45:10 -0700227typedef struct {
228 U64 totalSizeToLoad;
229 unsigned oneSampleTooLarge;
Yann Colletc68d17f2017-09-15 15:31:31 -0700230 unsigned nbSamples;
Yann Collet086b9592017-09-14 16:45:10 -0700231} fileStats;
232
Yann Colletc68d17f2017-09-15 15:31:31 -0700233/*! DiB_fileStats() :
234 * Given a list of files, and a chunkSize (0 == no chunk, whole files)
235 * provides the amount of data to be loaded and the resulting nb of samples.
236 * This is useful primarily for allocation purpose => sample buffer, and sample sizes table.
237 */
Yann Collet086b9592017-09-14 16:45:10 -0700238static fileStats DiB_fileStats(const char** fileNamesTable, unsigned nbFiles, size_t chunkSize, unsigned displayLevel)
Yann Collet1496c3d2016-12-18 11:58:23 +0100239{
Yann Collet086b9592017-09-14 16:45:10 -0700240 fileStats fs;
Yann Collet1496c3d2016-12-18 11:58:23 +0100241 unsigned n;
Yann Collet086b9592017-09-14 16:45:10 -0700242 memset(&fs, 0, sizeof(fs));
Yann Collet1496c3d2016-12-18 11:58:23 +0100243 for (n=0; n<nbFiles; n++) {
244 U64 const fileSize = UTIL_getFileSize(fileNamesTable[n]);
Yann Collet18b79532017-10-17 16:14:25 -0700245 U64 const srcSize = (fileSize == UTIL_FILESIZE_UNKNOWN) ? 0 : fileSize;
246 U32 const nbSamples = (U32)(chunkSize ? (srcSize + (chunkSize-1)) / chunkSize : 1);
247 U64 const chunkToLoad = chunkSize ? MIN(chunkSize, srcSize) : srcSize;
Yann Collet25a60482017-09-15 11:55:13 -0700248 size_t const cappedChunkSize = (size_t)MIN(chunkToLoad, SAMPLESIZE_MAX);
Yann Colletc68d17f2017-09-15 15:31:31 -0700249 fs.totalSizeToLoad += cappedChunkSize * nbSamples;
Yann Collet086b9592017-09-14 16:45:10 -0700250 fs.oneSampleTooLarge |= (chunkSize > 2*SAMPLESIZE_MAX);
Yann Colletc68d17f2017-09-15 15:31:31 -0700251 fs.nbSamples += nbSamples;
Yann Collet1496c3d2016-12-18 11:58:23 +0100252 }
Yann Colletededcfc2018-12-21 16:19:44 -0800253 DISPLAYLEVEL(4, "Preparing to load : %u KB \n", (unsigned)(fs.totalSizeToLoad >> 10));
Yann Collet086b9592017-09-14 16:45:10 -0700254 return fs;
Yann Collet1496c3d2016-12-18 11:58:23 +0100255}
256
257
Yann Collet71eafdd2016-02-12 02:31:57 +0100258int DiB_trainFromFiles(const char* dictFileName, unsigned maxDictSize,
Yann Collet086b9592017-09-14 16:45:10 -0700259 const char** fileNamesTable, unsigned nbFiles, size_t chunkSize,
Jennifer Liu9d6ed9d2018-08-23 12:06:20 -0700260 ZDICT_legacy_params_t* params, ZDICT_cover_params_t* coverParams,
261 ZDICT_fastCover_params_t* fastCoverParams, int optimize)
Yann Collet71eafdd2016-02-12 02:31:57 +0100262{
Yann Colletc68d17f2017-09-15 15:31:31 -0700263 unsigned const displayLevel = params ? params->zParams.notificationLevel :
264 coverParams ? coverParams->zParams.notificationLevel :
Jennifer Liu9d6ed9d2018-08-23 12:06:20 -0700265 fastCoverParams ? fastCoverParams->zParams.notificationLevel :
Yann Colletc68d17f2017-09-15 15:31:31 -0700266 0; /* should never happen */
Yann Collet290aaa72016-05-30 21:18:52 +0200267 void* const dictBuffer = malloc(maxDictSize);
Yann Collet086b9592017-09-14 16:45:10 -0700268 fileStats const fs = DiB_fileStats(fileNamesTable, nbFiles, chunkSize, displayLevel);
Yann Colletc68d17f2017-09-15 15:31:31 -0700269 size_t* const sampleSizes = (size_t*)malloc(fs.nbSamples * sizeof(size_t));
Jennifer Liu9d6ed9d2018-08-23 12:06:20 -0700270 size_t const memMult = params ? MEMMULT :
271 coverParams ? COVER_MEMMULT:
272 FASTCOVER_MEMMULT;
Yann Collet086b9592017-09-14 16:45:10 -0700273 size_t const maxMem = DiB_findMaxMem(fs.totalSizeToLoad * memMult) / memMult;
274 size_t loadedSize = (size_t) MIN ((unsigned long long)maxMem, fs.totalSizeToLoad);
275 void* const srcBuffer = malloc(loadedSize+NOISELENGTH);
Yann Collet71eafdd2016-02-12 02:31:57 +0100276 int result = 0;
277
Yann Collet290aaa72016-05-30 21:18:52 +0200278 /* Checks */
Yann Colletc68d17f2017-09-15 15:31:31 -0700279 if ((!sampleSizes) || (!srcBuffer) || (!dictBuffer))
Yann Collet77c137b2017-09-14 15:12:57 -0700280 EXM_THROW(12, "not enough memory for DiB_trainFiles"); /* should not happen */
Yann Collet086b9592017-09-14 16:45:10 -0700281 if (fs.oneSampleTooLarge) {
282 DISPLAYLEVEL(2, "! Warning : some sample(s) are very large \n");
283 DISPLAYLEVEL(2, "! Note that dictionary is only useful for small samples. \n");
284 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 +0100285 }
Yann Colletc68d17f2017-09-15 15:31:31 -0700286 if (fs.nbSamples < 5) {
Yann Collet49d105c2016-08-18 15:02:11 +0200287 DISPLAYLEVEL(2, "! Warning : nb of samples too low for proper processing ! \n");
288 DISPLAYLEVEL(2, "! Please provide _one file per sample_. \n");
Yann Collet17220552017-09-15 16:23:50 -0700289 DISPLAYLEVEL(2, "! Alternatively, split files into fixed-size blocks representative of samples, with -B# \n");
Yann Collet086b9592017-09-14 16:45:10 -0700290 EXM_THROW(14, "nb of samples too low"); /* we now clearly forbid this case */
291 }
ihsinmef37896d2020-12-13 12:08:31 +0300292 if (fs.totalSizeToLoad < (unsigned long long)maxDictSize * 8) {
Yann Collet086b9592017-09-14 16:45:10 -0700293 DISPLAYLEVEL(2, "! Warning : data size of samples too small for target dictionary size \n");
294 DISPLAYLEVEL(2, "! Samples should be about 100x larger than target dictionary size \n");
Yann Colletdd25a272016-07-27 12:35:29 +0200295 }
Yann Collet290aaa72016-05-30 21:18:52 +0200296
Yann Collet71eafdd2016-02-12 02:31:57 +0100297 /* init */
Yann Collet086b9592017-09-14 16:45:10 -0700298 if (loadedSize < fs.totalSizeToLoad)
299 DISPLAYLEVEL(1, "Not enough memory; training on %u MB only...\n", (unsigned)(loadedSize >> 20));
Yann Collet71eafdd2016-02-12 02:31:57 +0100300
Yann Collet71eafdd2016-02-12 02:31:57 +0100301 /* Load input buffer */
Nick Terrelldf8415c2016-12-31 21:08:24 -0800302 DISPLAYLEVEL(3, "Shuffling input files\n");
303 DiB_shuffle(fileNamesTable, nbFiles);
Jennifer Liu9d6ed9d2018-08-23 12:06:20 -0700304
Yann Collet42a02ab2018-08-15 14:35:38 -0700305 DiB_loadFiles(srcBuffer, &loadedSize, sampleSizes, fs.nbSamples, fileNamesTable, nbFiles, chunkSize, displayLevel);
Yann Collet71eafdd2016-02-12 02:31:57 +0100306
Yann Collet77c137b2017-09-14 15:12:57 -0700307 { size_t dictSize;
Nick Terrelldf8415c2016-12-31 21:08:24 -0800308 if (params) {
Yann Collet086b9592017-09-14 16:45:10 -0700309 DiB_fillNoise((char*)srcBuffer + loadedSize, NOISELENGTH); /* guard band, for end of buffer condition */
Yann Collet890d85b2021-01-06 16:19:42 -0800310 dictSize = ZDICT_trainFromBuffer_legacy(dictBuffer, maxDictSize,
311 srcBuffer, sampleSizes, fs.nbSamples,
312 *params);
Jennifer Liu9d6ed9d2018-08-23 12:06:20 -0700313 } else if (coverParams) {
314 if (optimize) {
315 dictSize = ZDICT_optimizeTrainFromBuffer_cover(dictBuffer, maxDictSize,
316 srcBuffer, sampleSizes, fs.nbSamples,
317 coverParams);
318 if (!ZDICT_isError(dictSize)) {
319 unsigned splitPercentage = (unsigned)(coverParams->splitPoint * 100);
320 DISPLAYLEVEL(2, "k=%u\nd=%u\nsteps=%u\nsplit=%u\n", coverParams->k, coverParams->d,
321 coverParams->steps, splitPercentage);
322 }
323 } else {
324 dictSize = ZDICT_trainFromBuffer_cover(dictBuffer, maxDictSize, srcBuffer,
325 sampleSizes, fs.nbSamples, *coverParams);
Nick Terrelldf8415c2016-12-31 21:08:24 -0800326 }
327 } else {
Jennifer Liu9d6ed9d2018-08-23 12:06:20 -0700328 assert(fastCoverParams != NULL);
329 if (optimize) {
330 dictSize = ZDICT_optimizeTrainFromBuffer_fastCover(dictBuffer, maxDictSize,
331 srcBuffer, sampleSizes, fs.nbSamples,
332 fastCoverParams);
333 if (!ZDICT_isError(dictSize)) {
334 unsigned splitPercentage = (unsigned)(fastCoverParams->splitPoint * 100);
335 DISPLAYLEVEL(2, "k=%u\nd=%u\nf=%u\nsteps=%u\nsplit=%u\naccel=%u\n", fastCoverParams->k,
336 fastCoverParams->d, fastCoverParams->f, fastCoverParams->steps, splitPercentage,
337 fastCoverParams->accel);
338 }
339 } else {
340 dictSize = ZDICT_trainFromBuffer_fastCover(dictBuffer, maxDictSize, srcBuffer,
341 sampleSizes, fs.nbSamples, *fastCoverParams);
342 }
Nick Terrelldf8415c2016-12-31 21:08:24 -0800343 }
Yann Collet290aaa72016-05-30 21:18:52 +0200344 if (ZDICT_isError(dictSize)) {
345 DISPLAYLEVEL(1, "dictionary training failed : %s \n", ZDICT_getErrorName(dictSize)); /* should not happen */
346 result = 1;
347 goto _cleanup;
348 }
349 /* save dict */
Yann Colletededcfc2018-12-21 16:19:44 -0800350 DISPLAYLEVEL(2, "Save dictionary of size %u into file %s \n", (unsigned)dictSize, dictFileName);
Yann Collet290aaa72016-05-30 21:18:52 +0200351 DiB_saveDict(dictFileName, dictBuffer, dictSize);
Yann Collet71eafdd2016-02-12 02:31:57 +0100352 }
353
Yann Collet71eafdd2016-02-12 02:31:57 +0100354 /* clean up */
355_cleanup:
356 free(srcBuffer);
Yann Colletc68d17f2017-09-15 15:31:31 -0700357 free(sampleSizes);
Yann Collet71eafdd2016-02-12 02:31:57 +0100358 free(dictBuffer);
Yann Collet71eafdd2016-02-12 02:31:57 +0100359 return result;
360}