Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 1 | /* |
Nick Terrell | a494308 | 2021-03-29 14:23:36 -0700 | [diff] [blame] | 2 | * Copyright (c) Yann Collet, Facebook, Inc. |
Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 5 | * 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 Collet | 3128e03 | 2017-09-08 00:09:23 -0700 | [diff] [blame] | 8 | * You may select, at your option, one of the above-listed licenses. |
Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 9 | */ |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 10 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 11 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 12 | |
Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 13 | /* ************************************** |
| 14 | * Compiler Warnings |
| 15 | ****************************************/ |
| 16 | #ifdef _MSC_VER |
Yann Collet | 77c137b | 2017-09-14 15:12:57 -0700 | [diff] [blame] | 17 | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 18 | #endif |
| 19 | |
| 20 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 21 | /*-************************************* |
| 22 | * Includes |
| 23 | ***************************************/ |
Przemyslaw Skibinski | 7a8a03c | 2016-12-21 15:08:44 +0100 | [diff] [blame] | 24 | #include "platform.h" /* Large Files support */ |
Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 25 | #include "util.h" /* UTIL_getFileSize, UTIL_getTotalFileSize */ |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 26 | #include <stdlib.h> /* malloc, free */ |
| 27 | #include <string.h> /* memset */ |
| 28 | #include <stdio.h> /* fprintf, fopen, ftello64 */ |
Yann Collet | a3d03a3 | 2016-07-06 16:27:17 +0200 | [diff] [blame] | 29 | #include <errno.h> /* errno */ |
Yann Collet | 42a02ab | 2018-08-15 14:35:38 -0700 | [diff] [blame] | 30 | #include <assert.h> |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 31 | |
Yann Collet | 59a7116 | 2019-04-10 12:37:03 -0700 | [diff] [blame] | 32 | #include "timefn.h" /* UTIL_time_t, UTIL_clockSpanMicro, UTIL_getTime */ |
W. Felix Handte | 7dcca6b | 2020-05-01 16:20:40 -0400 | [diff] [blame] | 33 | #include "../lib/common/mem.h" /* read */ |
| 34 | #include "../lib/common/error_private.h" |
inikep | 23a0889 | 2016-04-22 12:43:18 +0200 | [diff] [blame] | 35 | #include "dibio.h" |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 36 | |
| 37 | |
| 38 | /*-************************************* |
| 39 | * Constants |
| 40 | ***************************************/ |
| 41 | #define KB *(1 <<10) |
| 42 | #define MB *(1 <<20) |
| 43 | #define GB *(1U<<30) |
| 44 | |
Yann Collet | 1496c3d | 2016-12-18 11:58:23 +0100 | [diff] [blame] | 45 | #define SAMPLESIZE_MAX (128 KB) |
| 46 | #define MEMMULT 11 /* rough estimation : memory cost to analyze 1 byte of sample */ |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 47 | #define COVER_MEMMULT 9 /* rough estimation : memory cost to analyze 1 byte of sample */ |
Jennifer Liu | 9d6ed9d | 2018-08-23 12:06:20 -0700 | [diff] [blame] | 48 | #define FASTCOVER_MEMMULT 1 /* rough estimation : memory cost to analyze 1 byte of sample */ |
Yann Collet | 77c137b | 2017-09-14 15:12:57 -0700 | [diff] [blame] | 49 | static const size_t g_maxMemory = (sizeof(size_t) == 4) ? (2 GB - 64 MB) : ((size_t)(512 MB) << sizeof(size_t)); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 50 | |
| 51 | #define NOISELENGTH 32 |
stanjo74 | 52598d5 | 2021-10-04 17:47:52 -0700 | [diff] [blame] | 52 | #define MAX_SAMPLES_SIZE (2 GB) /* training dataset limited to 2GB */ |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 53 | |
| 54 | |
| 55 | /*-************************************* |
| 56 | * Console display |
| 57 | ***************************************/ |
| 58 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 59 | #define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); } |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 60 | |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 61 | static const U64 g_refreshRate = SEC_TO_MICRO / 6; |
| 62 | static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER; |
Yann Collet | f6ca09b | 2016-05-09 04:44:45 +0200 | [diff] [blame] | 63 | |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 64 | #define DISPLAYUPDATE(l, ...) { if (displayLevel>=l) { \ |
| 65 | if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || (displayLevel>=4)) \ |
| 66 | { g_displayClock = UTIL_getTime(); DISPLAY(__VA_ARGS__); \ |
| 67 | if (displayLevel>=4) fflush(stderr); } } } |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 68 | |
| 69 | /*-************************************* |
| 70 | * Exceptions |
| 71 | ***************************************/ |
| 72 | #ifndef DEBUG |
| 73 | # define DEBUG 0 |
| 74 | #endif |
| 75 | #define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__); |
| 76 | #define EXM_THROW(error, ...) \ |
| 77 | { \ |
| 78 | DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \ |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 79 | DISPLAY("Error %i : ", error); \ |
| 80 | DISPLAY(__VA_ARGS__); \ |
| 81 | DISPLAY("\n"); \ |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 82 | exit(error); \ |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /* ******************************************************** |
| 87 | * Helper functions |
| 88 | **********************************************************/ |
Sean Purcell | 42bac7f | 2017-04-13 15:35:05 -0700 | [diff] [blame] | 89 | #undef MIN |
| 90 | #define MIN(a,b) ((a) < (b) ? (a) : (b)) |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 91 | |
stanjo74 | 52598d5 | 2021-10-04 17:47:52 -0700 | [diff] [blame] | 92 | /** |
| 93 | Returns the size of a file. |
| 94 | If error returns -1. |
| 95 | */ |
| 96 | static S64 DiB_getFileSize (const char * fileName) |
| 97 | { |
| 98 | U64 const fileSize = UTIL_getFileSize(fileName); |
| 99 | return (fileSize == UTIL_FILESIZE_UNKNOWN) ? -1 : (S64)fileSize; |
| 100 | } |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 101 | |
| 102 | /* ******************************************************** |
| 103 | * File related operations |
| 104 | **********************************************************/ |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 105 | /** DiB_loadFiles() : |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 106 | * load samples from files listed in fileNamesTable into buffer. |
| 107 | * works even if buffer is too small to load all samples. |
| 108 | * Also provides the size of each sample into sampleSizes table |
| 109 | * which must be sized correctly, using DiB_fileStats(). |
| 110 | * @return : nb of samples effectively loaded into `buffer` |
| 111 | * *bufferSizePtr is modified, it provides the amount data loaded within buffer. |
| 112 | * sampleSizes is filled with the size of each sample. |
| 113 | */ |
stanjo74 | 52598d5 | 2021-10-04 17:47:52 -0700 | [diff] [blame] | 114 | static int DiB_loadFiles( |
| 115 | void* buffer, size_t* bufferSizePtr, |
| 116 | size_t* sampleSizes, int sstSize, |
| 117 | const char** fileNamesTable, int nbFiles, |
| 118 | size_t targetChunkSize, int displayLevel ) |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 119 | { |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 120 | char* const buff = (char*)buffer; |
stanjo74 | 52598d5 | 2021-10-04 17:47:52 -0700 | [diff] [blame] | 121 | size_t totalDataLoaded = 0; |
| 122 | int nbSamplesLoaded = 0; |
| 123 | int fileIndex = 0; |
| 124 | FILE * f = NULL; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 125 | |
stanjo74 | 52598d5 | 2021-10-04 17:47:52 -0700 | [diff] [blame] | 126 | assert(targetChunkSize <= SAMPLESIZE_MAX); |
| 127 | |
| 128 | while ( nbSamplesLoaded < sstSize && fileIndex < nbFiles ) { |
| 129 | size_t fileDataLoaded; |
| 130 | S64 const fileSize = DiB_getFileSize(fileNamesTable[fileIndex]); |
| 131 | if (fileSize <= 0) /* skip if zero-size or file error */ |
| 132 | continue; |
| 133 | |
| 134 | f = fopen( fileNamesTable[fileIndex], "rb"); |
| 135 | if (f == NULL) |
| 136 | EXM_THROW(10, "zstd: dictBuilder: %s %s ", fileNamesTable[fileIndex], strerror(errno)); |
| 137 | DISPLAYUPDATE(2, "Loading %s... \r", fileNamesTable[fileIndex]); |
| 138 | |
| 139 | /* Load the first chunk of data from the file */ |
| 140 | fileDataLoaded = targetChunkSize > 0 ? |
| 141 | (size_t)MIN(fileSize, (S64)targetChunkSize) : |
| 142 | (size_t)MIN(fileSize, SAMPLESIZE_MAX ); |
| 143 | if (totalDataLoaded + fileDataLoaded > *bufferSizePtr) |
| 144 | break; |
| 145 | if (fread( buff+totalDataLoaded, 1, fileDataLoaded, f ) != fileDataLoaded) |
| 146 | EXM_THROW(11, "Pb reading %s", fileNamesTable[fileIndex]); |
| 147 | sampleSizes[nbSamplesLoaded++] = fileDataLoaded; |
| 148 | totalDataLoaded += fileDataLoaded; |
| 149 | |
| 150 | /* If file-chunking is enabled, load the rest of the file as more samples */ |
| 151 | if (targetChunkSize > 0) { |
| 152 | while( (S64)fileDataLoaded < fileSize && nbSamplesLoaded < sstSize ) { |
| 153 | size_t const chunkSize = MIN((size_t)(fileSize-fileDataLoaded), targetChunkSize); |
| 154 | if (totalDataLoaded + chunkSize > *bufferSizePtr) /* buffer is full */ |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 155 | break; |
stanjo74 | 52598d5 | 2021-10-04 17:47:52 -0700 | [diff] [blame] | 156 | |
| 157 | if (fread( buff+totalDataLoaded, 1, chunkSize, f ) != chunkSize) |
| 158 | EXM_THROW(11, "Pb reading %s", fileNamesTable[fileIndex]); |
| 159 | sampleSizes[nbSamplesLoaded++] = chunkSize; |
| 160 | totalDataLoaded += chunkSize; |
| 161 | fileDataLoaded += chunkSize; |
| 162 | } |
| 163 | } |
| 164 | fileIndex += 1; |
| 165 | fclose(f); f = NULL; |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 166 | } |
stanjo74 | 52598d5 | 2021-10-04 17:47:52 -0700 | [diff] [blame] | 167 | if (f != NULL) |
| 168 | fclose(f); |
| 169 | |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 170 | DISPLAYLEVEL(2, "\r%79s\r", ""); |
stanjo74 | 52598d5 | 2021-10-04 17:47:52 -0700 | [diff] [blame] | 171 | DISPLAYLEVEL(4, "Loaded %d KB total training data, %d nb samples \n", |
| 172 | (int)(totalDataLoaded / (1 KB)), nbSamplesLoaded ); |
| 173 | *bufferSizePtr = totalDataLoaded; |
| 174 | return nbSamplesLoaded; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 175 | } |
| 176 | |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 177 | #define DiB_rotl32(x,r) ((x << r) | (x >> (32 - r))) |
| 178 | static U32 DiB_rand(U32* src) |
| 179 | { |
| 180 | static const U32 prime1 = 2654435761U; |
| 181 | static const U32 prime2 = 2246822519U; |
| 182 | U32 rand32 = *src; |
| 183 | rand32 *= prime1; |
| 184 | rand32 ^= prime2; |
| 185 | rand32 = DiB_rotl32(rand32, 13); |
| 186 | *src = rand32; |
| 187 | return rand32 >> 5; |
| 188 | } |
| 189 | |
Yann Collet | 77c137b | 2017-09-14 15:12:57 -0700 | [diff] [blame] | 190 | /* DiB_shuffle() : |
| 191 | * shuffle a table of file names in a semi-random way |
| 192 | * It improves dictionary quality by reducing "locality" impact, so if sample set is very large, |
| 193 | * it will load random elements from it, instead of just the first ones. */ |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 194 | static void DiB_shuffle(const char** fileNamesTable, unsigned nbFiles) { |
Yann Collet | 77c137b | 2017-09-14 15:12:57 -0700 | [diff] [blame] | 195 | U32 seed = 0xFD2FB528; |
| 196 | unsigned i; |
Yann Collet | 42a02ab | 2018-08-15 14:35:38 -0700 | [diff] [blame] | 197 | assert(nbFiles >= 1); |
Yann Collet | 77c137b | 2017-09-14 15:12:57 -0700 | [diff] [blame] | 198 | for (i = nbFiles - 1; i > 0; --i) { |
| 199 | unsigned const j = DiB_rand(&seed) % (i + 1); |
| 200 | const char* const tmp = fileNamesTable[j]; |
| 201 | fileNamesTable[j] = fileNamesTable[i]; |
| 202 | fileNamesTable[i] = tmp; |
| 203 | } |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 206 | |
| 207 | /*-******************************************************** |
| 208 | * Dictionary training functions |
| 209 | **********************************************************/ |
| 210 | static size_t DiB_findMaxMem(unsigned long long requiredMem) |
| 211 | { |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 212 | size_t const step = 8 MB; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 213 | void* testmem = NULL; |
| 214 | |
| 215 | requiredMem = (((requiredMem >> 23) + 1) << 23); |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 216 | requiredMem += step; |
Yann Collet | 77c137b | 2017-09-14 15:12:57 -0700 | [diff] [blame] | 217 | if (requiredMem > g_maxMemory) requiredMem = g_maxMemory; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 218 | |
| 219 | while (!testmem) { |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 220 | testmem = malloc((size_t)requiredMem); |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 221 | requiredMem -= step; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | free(testmem); |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 225 | return (size_t)requiredMem; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | |
| 229 | static void DiB_fillNoise(void* buffer, size_t length) |
| 230 | { |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 231 | unsigned const prime1 = 2654435761U; |
| 232 | unsigned const prime2 = 2246822519U; |
| 233 | unsigned acc = prime1; |
Ed Maste | b81d7cc | 2019-08-15 21:17:06 -0400 | [diff] [blame] | 234 | size_t p=0; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 235 | |
| 236 | for (p=0; p<length; p++) { |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 237 | acc *= prime2; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 238 | ((unsigned char*)buffer)[p] = (unsigned char)(acc >> 21); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | |
| 243 | static void DiB_saveDict(const char* dictFileName, |
| 244 | const void* buff, size_t buffSize) |
| 245 | { |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 246 | FILE* const f = fopen(dictFileName, "wb"); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 247 | if (f==NULL) EXM_THROW(3, "cannot open %s ", dictFileName); |
| 248 | |
Yann Collet | f6ca09b | 2016-05-09 04:44:45 +0200 | [diff] [blame] | 249 | { size_t const n = fwrite(buff, 1, buffSize, f); |
| 250 | if (n!=buffSize) EXM_THROW(4, "%s : write error", dictFileName) } |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 251 | |
Yann Collet | f6ca09b | 2016-05-09 04:44:45 +0200 | [diff] [blame] | 252 | { size_t const n = (size_t)fclose(f); |
| 253 | if (n!=0) EXM_THROW(5, "%s : flush error", dictFileName) } |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 254 | } |
| 255 | |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 256 | typedef struct { |
stanjo74 | 52598d5 | 2021-10-04 17:47:52 -0700 | [diff] [blame] | 257 | S64 totalSizeToLoad; |
| 258 | int nbSamples; |
| 259 | int oneSampleTooLarge; |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 260 | } fileStats; |
| 261 | |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 262 | /*! DiB_fileStats() : |
| 263 | * Given a list of files, and a chunkSize (0 == no chunk, whole files) |
| 264 | * provides the amount of data to be loaded and the resulting nb of samples. |
| 265 | * This is useful primarily for allocation purpose => sample buffer, and sample sizes table. |
| 266 | */ |
stanjo74 | 52598d5 | 2021-10-04 17:47:52 -0700 | [diff] [blame] | 267 | static fileStats DiB_fileStats(const char** fileNamesTable, int nbFiles, size_t chunkSize, int displayLevel) |
Yann Collet | 1496c3d | 2016-12-18 11:58:23 +0100 | [diff] [blame] | 268 | { |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 269 | fileStats fs; |
stanjo74 | 52598d5 | 2021-10-04 17:47:52 -0700 | [diff] [blame] | 270 | int n; |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 271 | memset(&fs, 0, sizeof(fs)); |
stanjo74 | 52598d5 | 2021-10-04 17:47:52 -0700 | [diff] [blame] | 272 | |
Dimitris Apostolou | ebbd675 | 2021-11-13 10:04:04 +0200 | [diff] [blame^] | 273 | // We assume that if chunking is requested, the chunk size is < SAMPLESIZE_MAX |
stanjo74 | 52598d5 | 2021-10-04 17:47:52 -0700 | [diff] [blame] | 274 | assert( chunkSize <= SAMPLESIZE_MAX ); |
| 275 | |
Yann Collet | 1496c3d | 2016-12-18 11:58:23 +0100 | [diff] [blame] | 276 | for (n=0; n<nbFiles; n++) { |
stanjo74 | 52598d5 | 2021-10-04 17:47:52 -0700 | [diff] [blame] | 277 | S64 const fileSize = DiB_getFileSize(fileNamesTable[n]); |
| 278 | // TODO: is there a minimum sample size? What if the file is 1-byte? |
| 279 | if (fileSize == 0) { |
| 280 | DISPLAYLEVEL(3, "Sample file '%s' has zero size, skipping...\n", fileNamesTable[n]); |
| 281 | continue; |
| 282 | } |
| 283 | |
| 284 | /* the case where we are breaking up files in sample chunks */ |
| 285 | if (chunkSize > 0) |
| 286 | { |
| 287 | // TODO: is there a minimum sample size? Can we have a 1-byte sample? |
| 288 | fs.nbSamples += (int)((fileSize + chunkSize-1) / chunkSize); |
| 289 | fs.totalSizeToLoad += fileSize; |
| 290 | } |
| 291 | else { |
| 292 | /* the case where one file is one sample */ |
| 293 | if (fileSize > SAMPLESIZE_MAX) { |
| 294 | /* flag excessively large sample files */ |
| 295 | fs.oneSampleTooLarge |= (fileSize > 2*SAMPLESIZE_MAX); |
| 296 | |
| 297 | /* Limit to the first SAMPLESIZE_MAX (128kB) of the file */ |
| 298 | DISPLAYLEVEL(3, "Sample file '%s' is too large, limiting to %d KB", |
| 299 | fileNamesTable[n], SAMPLESIZE_MAX / (1 KB)); |
| 300 | } |
| 301 | fs.nbSamples += 1; |
| 302 | fs.totalSizeToLoad += MIN(fileSize, SAMPLESIZE_MAX); |
| 303 | } |
Yann Collet | 1496c3d | 2016-12-18 11:58:23 +0100 | [diff] [blame] | 304 | } |
stanjo74 | 52598d5 | 2021-10-04 17:47:52 -0700 | [diff] [blame] | 305 | DISPLAYLEVEL(4, "Found training data %d files, %d KB, %d samples\n", nbFiles, (int)(fs.totalSizeToLoad / (1 KB)), fs.nbSamples); |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 306 | return fs; |
Yann Collet | 1496c3d | 2016-12-18 11:58:23 +0100 | [diff] [blame] | 307 | } |
| 308 | |
stanjo74 | 52598d5 | 2021-10-04 17:47:52 -0700 | [diff] [blame] | 309 | int DiB_trainFromFiles(const char* dictFileName, size_t maxDictSize, |
| 310 | const char** fileNamesTable, int nbFiles, size_t chunkSize, |
Jennifer Liu | 9d6ed9d | 2018-08-23 12:06:20 -0700 | [diff] [blame] | 311 | ZDICT_legacy_params_t* params, ZDICT_cover_params_t* coverParams, |
| 312 | ZDICT_fastCover_params_t* fastCoverParams, int optimize) |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 313 | { |
stanjo74 | 52598d5 | 2021-10-04 17:47:52 -0700 | [diff] [blame] | 314 | fileStats fs; |
| 315 | size_t* sampleSizes; /* vector of sample sizes. Each sample can be up to SAMPLESIZE_MAX */ |
| 316 | int nbSamplesLoaded; /* nb of samples effectively loaded in srcBuffer */ |
| 317 | size_t loadedSize; /* total data loaded in srcBuffer for all samples */ |
| 318 | void* srcBuffer /* contiguous buffer with training data/samples */; |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 319 | void* const dictBuffer = malloc(maxDictSize); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 320 | int result = 0; |
| 321 | |
stanjo74 | 52598d5 | 2021-10-04 17:47:52 -0700 | [diff] [blame] | 322 | int const displayLevel = params ? params->zParams.notificationLevel : |
| 323 | coverParams ? coverParams->zParams.notificationLevel : |
| 324 | fastCoverParams ? fastCoverParams->zParams.notificationLevel : 0; |
| 325 | |
| 326 | /* Shuffle input files before we start assessing how much sample datA to load. |
| 327 | The purpose of the shuffle is to pick random samples when the sample |
| 328 | set is larger than what we can load in memory. */ |
| 329 | DISPLAYLEVEL(3, "Shuffling input files\n"); |
| 330 | DiB_shuffle(fileNamesTable, nbFiles); |
| 331 | |
| 332 | /* Figure out how much sample data to load with how many samples */ |
| 333 | fs = DiB_fileStats(fileNamesTable, nbFiles, chunkSize, displayLevel); |
| 334 | |
| 335 | { |
| 336 | int const memMult = params ? MEMMULT : |
| 337 | coverParams ? COVER_MEMMULT: |
| 338 | FASTCOVER_MEMMULT; |
| 339 | size_t const maxMem = DiB_findMaxMem(fs.totalSizeToLoad * memMult) / memMult; |
| 340 | /* Limit the size of the training data to the free memory */ |
| 341 | /* Limit the size of the training data to 2GB */ |
Dimitris Apostolou | ebbd675 | 2021-11-13 10:04:04 +0200 | [diff] [blame^] | 342 | /* TODO: there is opportunity to stop DiB_fileStats() early when the data limit is reached */ |
stanjo74 | 52598d5 | 2021-10-04 17:47:52 -0700 | [diff] [blame] | 343 | loadedSize = (size_t)MIN( MIN((S64)maxMem, fs.totalSizeToLoad), MAX_SAMPLES_SIZE ); |
| 344 | srcBuffer = malloc(loadedSize+NOISELENGTH); |
| 345 | sampleSizes = (size_t*)malloc(fs.nbSamples * sizeof(size_t)); |
| 346 | } |
| 347 | |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 348 | /* Checks */ |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 349 | if ((!sampleSizes) || (!srcBuffer) || (!dictBuffer)) |
Yann Collet | 77c137b | 2017-09-14 15:12:57 -0700 | [diff] [blame] | 350 | EXM_THROW(12, "not enough memory for DiB_trainFiles"); /* should not happen */ |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 351 | if (fs.oneSampleTooLarge) { |
| 352 | DISPLAYLEVEL(2, "! Warning : some sample(s) are very large \n"); |
| 353 | DISPLAYLEVEL(2, "! Note that dictionary is only useful for small samples. \n"); |
| 354 | DISPLAYLEVEL(2, "! As a consequence, only the first %u bytes of each sample are loaded \n", SAMPLESIZE_MAX); |
Yann Collet | 1496c3d | 2016-12-18 11:58:23 +0100 | [diff] [blame] | 355 | } |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 356 | if (fs.nbSamples < 5) { |
Yann Collet | 49d105c | 2016-08-18 15:02:11 +0200 | [diff] [blame] | 357 | DISPLAYLEVEL(2, "! Warning : nb of samples too low for proper processing ! \n"); |
| 358 | DISPLAYLEVEL(2, "! Please provide _one file per sample_. \n"); |
Yann Collet | 1722055 | 2017-09-15 16:23:50 -0700 | [diff] [blame] | 359 | DISPLAYLEVEL(2, "! Alternatively, split files into fixed-size blocks representative of samples, with -B# \n"); |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 360 | EXM_THROW(14, "nb of samples too low"); /* we now clearly forbid this case */ |
| 361 | } |
stanjo74 | 52598d5 | 2021-10-04 17:47:52 -0700 | [diff] [blame] | 362 | if (fs.totalSizeToLoad < (S64)maxDictSize * 8) { |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 363 | DISPLAYLEVEL(2, "! Warning : data size of samples too small for target dictionary size \n"); |
| 364 | DISPLAYLEVEL(2, "! Samples should be about 100x larger than target dictionary size \n"); |
Yann Collet | dd25a27 | 2016-07-27 12:35:29 +0200 | [diff] [blame] | 365 | } |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 366 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 367 | /* init */ |
stanjo74 | 52598d5 | 2021-10-04 17:47:52 -0700 | [diff] [blame] | 368 | if ((S64)loadedSize < fs.totalSizeToLoad) |
| 369 | DISPLAYLEVEL(1, "Training samples set too large (%u MB); training on %u MB only...\n", |
| 370 | (unsigned)(fs.totalSizeToLoad / (1 MB)), |
| 371 | (unsigned)(loadedSize / (1 MB))); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 372 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 373 | /* Load input buffer */ |
stanjo74 | 52598d5 | 2021-10-04 17:47:52 -0700 | [diff] [blame] | 374 | nbSamplesLoaded = DiB_loadFiles( |
| 375 | srcBuffer, &loadedSize, sampleSizes, fs.nbSamples, fileNamesTable, |
| 376 | nbFiles, chunkSize, displayLevel); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 377 | |
Yann Collet | 77c137b | 2017-09-14 15:12:57 -0700 | [diff] [blame] | 378 | { size_t dictSize; |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 379 | if (params) { |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 380 | DiB_fillNoise((char*)srcBuffer + loadedSize, NOISELENGTH); /* guard band, for end of buffer condition */ |
Yann Collet | 890d85b | 2021-01-06 16:19:42 -0800 | [diff] [blame] | 381 | dictSize = ZDICT_trainFromBuffer_legacy(dictBuffer, maxDictSize, |
stanjo74 | 52598d5 | 2021-10-04 17:47:52 -0700 | [diff] [blame] | 382 | srcBuffer, sampleSizes, nbSamplesLoaded, |
Yann Collet | 890d85b | 2021-01-06 16:19:42 -0800 | [diff] [blame] | 383 | *params); |
Jennifer Liu | 9d6ed9d | 2018-08-23 12:06:20 -0700 | [diff] [blame] | 384 | } else if (coverParams) { |
| 385 | if (optimize) { |
| 386 | dictSize = ZDICT_optimizeTrainFromBuffer_cover(dictBuffer, maxDictSize, |
stanjo74 | 52598d5 | 2021-10-04 17:47:52 -0700 | [diff] [blame] | 387 | srcBuffer, sampleSizes, nbSamplesLoaded, |
Jennifer Liu | 9d6ed9d | 2018-08-23 12:06:20 -0700 | [diff] [blame] | 388 | coverParams); |
| 389 | if (!ZDICT_isError(dictSize)) { |
| 390 | unsigned splitPercentage = (unsigned)(coverParams->splitPoint * 100); |
| 391 | DISPLAYLEVEL(2, "k=%u\nd=%u\nsteps=%u\nsplit=%u\n", coverParams->k, coverParams->d, |
| 392 | coverParams->steps, splitPercentage); |
| 393 | } |
| 394 | } else { |
| 395 | dictSize = ZDICT_trainFromBuffer_cover(dictBuffer, maxDictSize, srcBuffer, |
stanjo74 | 52598d5 | 2021-10-04 17:47:52 -0700 | [diff] [blame] | 396 | sampleSizes, nbSamplesLoaded, *coverParams); |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 397 | } |
| 398 | } else { |
Jennifer Liu | 9d6ed9d | 2018-08-23 12:06:20 -0700 | [diff] [blame] | 399 | assert(fastCoverParams != NULL); |
| 400 | if (optimize) { |
| 401 | dictSize = ZDICT_optimizeTrainFromBuffer_fastCover(dictBuffer, maxDictSize, |
stanjo74 | 52598d5 | 2021-10-04 17:47:52 -0700 | [diff] [blame] | 402 | srcBuffer, sampleSizes, nbSamplesLoaded, |
Jennifer Liu | 9d6ed9d | 2018-08-23 12:06:20 -0700 | [diff] [blame] | 403 | fastCoverParams); |
| 404 | if (!ZDICT_isError(dictSize)) { |
| 405 | unsigned splitPercentage = (unsigned)(fastCoverParams->splitPoint * 100); |
| 406 | DISPLAYLEVEL(2, "k=%u\nd=%u\nf=%u\nsteps=%u\nsplit=%u\naccel=%u\n", fastCoverParams->k, |
| 407 | fastCoverParams->d, fastCoverParams->f, fastCoverParams->steps, splitPercentage, |
| 408 | fastCoverParams->accel); |
| 409 | } |
| 410 | } else { |
| 411 | dictSize = ZDICT_trainFromBuffer_fastCover(dictBuffer, maxDictSize, srcBuffer, |
stanjo74 | 52598d5 | 2021-10-04 17:47:52 -0700 | [diff] [blame] | 412 | sampleSizes, nbSamplesLoaded, *fastCoverParams); |
Jennifer Liu | 9d6ed9d | 2018-08-23 12:06:20 -0700 | [diff] [blame] | 413 | } |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 414 | } |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 415 | if (ZDICT_isError(dictSize)) { |
| 416 | DISPLAYLEVEL(1, "dictionary training failed : %s \n", ZDICT_getErrorName(dictSize)); /* should not happen */ |
| 417 | result = 1; |
| 418 | goto _cleanup; |
| 419 | } |
| 420 | /* save dict */ |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 421 | DISPLAYLEVEL(2, "Save dictionary of size %u into file %s \n", (unsigned)dictSize, dictFileName); |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 422 | DiB_saveDict(dictFileName, dictBuffer, dictSize); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 423 | } |
| 424 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 425 | /* clean up */ |
| 426 | _cleanup: |
| 427 | free(srcBuffer); |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 428 | free(sampleSizes); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 429 | free(dictBuffer); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 430 | return result; |
| 431 | } |