Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 1 | /* |
Nick Terrell | 66e811d | 2021-01-04 17:53:52 -0500 | [diff] [blame] | 2 | * Copyright (c) 2016-2021, 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 |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 52 | |
| 53 | |
| 54 | /*-************************************* |
| 55 | * Console display |
| 56 | ***************************************/ |
| 57 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 58 | #define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); } |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 59 | |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 60 | static const U64 g_refreshRate = SEC_TO_MICRO / 6; |
| 61 | static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER; |
Yann Collet | f6ca09b | 2016-05-09 04:44:45 +0200 | [diff] [blame] | 62 | |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 63 | #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 Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 67 | |
| 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 Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 78 | DISPLAY("Error %i : ", error); \ |
| 79 | DISPLAY(__VA_ARGS__); \ |
| 80 | DISPLAY("\n"); \ |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 81 | exit(error); \ |
| 82 | } |
| 83 | |
| 84 | |
| 85 | /* ******************************************************** |
| 86 | * Helper functions |
| 87 | **********************************************************/ |
Sean Purcell | 42bac7f | 2017-04-13 15:35:05 -0700 | [diff] [blame] | 88 | #undef MIN |
| 89 | #define MIN(a,b) ((a) < (b) ? (a) : (b)) |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 90 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 91 | |
| 92 | /* ******************************************************** |
| 93 | * File related operations |
| 94 | **********************************************************/ |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 95 | /** DiB_loadFiles() : |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 96 | * 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 Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 104 | static unsigned DiB_loadFiles(void* buffer, size_t* bufferSizePtr, |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 105 | size_t* sampleSizes, unsigned sstSize, |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 106 | const char** fileNamesTable, unsigned nbFiles, size_t targetChunkSize, |
| 107 | unsigned displayLevel) |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 108 | { |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 109 | char* const buff = (char*)buffer; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 110 | size_t pos = 0; |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 111 | unsigned nbLoadedChunks = 0, fileIndex; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 112 | |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 113 | for (fileIndex=0; fileIndex<nbFiles; fileIndex++) { |
| 114 | const char* const fileName = fileNamesTable[fileIndex]; |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 115 | unsigned long long const fs64 = UTIL_getFileSize(fileName); |
Yann Collet | 18b7953 | 2017-10-17 16:14:25 -0700 | [diff] [blame] | 116 | unsigned long long remainingToLoad = (fs64 == UTIL_FILESIZE_UNKNOWN) ? 0 : fs64; |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 117 | U32 const nbChunks = targetChunkSize ? (U32)((fs64 + (targetChunkSize-1)) / targetChunkSize) : 1; |
| 118 | U64 const chunkSize = targetChunkSize ? MIN(targetChunkSize, fs64) : fs64; |
Yann Collet | 25a6048 | 2017-09-15 11:55:13 -0700 | [diff] [blame] | 119 | size_t const maxChunkSize = (size_t)MIN(chunkSize, SAMPLESIZE_MAX); |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 120 | 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 Collet | 25a6048 | 2017-09-15 11:55:13 -0700 | [diff] [blame] | 125 | size_t const toLoad = (size_t)MIN(maxChunkSize, remainingToLoad); |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 126 | 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 Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 130 | sampleSizes[nbLoadedChunks++] = toLoad; |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 131 | remainingToLoad -= targetChunkSize; |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 132 | if (nbLoadedChunks == sstSize) { /* no more space left in sampleSizes table */ |
| 133 | fileIndex = nbFiles; /* stop there */ |
| 134 | break; |
| 135 | } |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 136 | if (toLoad < targetChunkSize) { |
Yann Collet | a969423 | 2017-09-15 10:16:26 -0700 | [diff] [blame] | 137 | fseek(f, (long)(targetChunkSize - toLoad), SEEK_CUR); |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 138 | } } } |
| 139 | fclose(f); |
| 140 | } |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 141 | DISPLAYLEVEL(2, "\r%79s\r", ""); |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 142 | *bufferSizePtr = pos; |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 143 | DISPLAYLEVEL(4, "loaded : %u KB \n", (unsigned)(pos >> 10)) |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 144 | return nbLoadedChunks; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 145 | } |
| 146 | |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 147 | #define DiB_rotl32(x,r) ((x << r) | (x >> (32 - r))) |
| 148 | static 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 Collet | 77c137b | 2017-09-14 15:12:57 -0700 | [diff] [blame] | 160 | /* 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 Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 164 | static void DiB_shuffle(const char** fileNamesTable, unsigned nbFiles) { |
Yann Collet | 77c137b | 2017-09-14 15:12:57 -0700 | [diff] [blame] | 165 | U32 seed = 0xFD2FB528; |
| 166 | unsigned i; |
Yann Collet | 42a02ab | 2018-08-15 14:35:38 -0700 | [diff] [blame] | 167 | assert(nbFiles >= 1); |
Yann Collet | 77c137b | 2017-09-14 15:12:57 -0700 | [diff] [blame] | 168 | 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 Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 176 | |
| 177 | /*-******************************************************** |
| 178 | * Dictionary training functions |
| 179 | **********************************************************/ |
| 180 | static size_t DiB_findMaxMem(unsigned long long requiredMem) |
| 181 | { |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 182 | size_t const step = 8 MB; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 183 | void* testmem = NULL; |
| 184 | |
| 185 | requiredMem = (((requiredMem >> 23) + 1) << 23); |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 186 | requiredMem += step; |
Yann Collet | 77c137b | 2017-09-14 15:12:57 -0700 | [diff] [blame] | 187 | if (requiredMem > g_maxMemory) requiredMem = g_maxMemory; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 188 | |
| 189 | while (!testmem) { |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 190 | testmem = malloc((size_t)requiredMem); |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 191 | requiredMem -= step; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | free(testmem); |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 195 | return (size_t)requiredMem; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | |
| 199 | static void DiB_fillNoise(void* buffer, size_t length) |
| 200 | { |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 201 | unsigned const prime1 = 2654435761U; |
| 202 | unsigned const prime2 = 2246822519U; |
| 203 | unsigned acc = prime1; |
Ed Maste | b81d7cc | 2019-08-15 21:17:06 -0400 | [diff] [blame] | 204 | size_t p=0; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 205 | |
| 206 | for (p=0; p<length; p++) { |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 207 | acc *= prime2; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 208 | ((unsigned char*)buffer)[p] = (unsigned char)(acc >> 21); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | |
| 213 | static void DiB_saveDict(const char* dictFileName, |
| 214 | const void* buff, size_t buffSize) |
| 215 | { |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 216 | FILE* const f = fopen(dictFileName, "wb"); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 217 | if (f==NULL) EXM_THROW(3, "cannot open %s ", dictFileName); |
| 218 | |
Yann Collet | f6ca09b | 2016-05-09 04:44:45 +0200 | [diff] [blame] | 219 | { size_t const n = fwrite(buff, 1, buffSize, f); |
| 220 | if (n!=buffSize) EXM_THROW(4, "%s : write error", dictFileName) } |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 221 | |
Yann Collet | f6ca09b | 2016-05-09 04:44:45 +0200 | [diff] [blame] | 222 | { size_t const n = (size_t)fclose(f); |
| 223 | if (n!=0) EXM_THROW(5, "%s : flush error", dictFileName) } |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 227 | typedef struct { |
| 228 | U64 totalSizeToLoad; |
| 229 | unsigned oneSampleTooLarge; |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 230 | unsigned nbSamples; |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 231 | } fileStats; |
| 232 | |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 233 | /*! 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 Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 238 | static fileStats DiB_fileStats(const char** fileNamesTable, unsigned nbFiles, size_t chunkSize, unsigned displayLevel) |
Yann Collet | 1496c3d | 2016-12-18 11:58:23 +0100 | [diff] [blame] | 239 | { |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 240 | fileStats fs; |
Yann Collet | 1496c3d | 2016-12-18 11:58:23 +0100 | [diff] [blame] | 241 | unsigned n; |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 242 | memset(&fs, 0, sizeof(fs)); |
Yann Collet | 1496c3d | 2016-12-18 11:58:23 +0100 | [diff] [blame] | 243 | for (n=0; n<nbFiles; n++) { |
| 244 | U64 const fileSize = UTIL_getFileSize(fileNamesTable[n]); |
Yann Collet | 18b7953 | 2017-10-17 16:14:25 -0700 | [diff] [blame] | 245 | 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 Collet | 25a6048 | 2017-09-15 11:55:13 -0700 | [diff] [blame] | 248 | size_t const cappedChunkSize = (size_t)MIN(chunkToLoad, SAMPLESIZE_MAX); |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 249 | fs.totalSizeToLoad += cappedChunkSize * nbSamples; |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 250 | fs.oneSampleTooLarge |= (chunkSize > 2*SAMPLESIZE_MAX); |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 251 | fs.nbSamples += nbSamples; |
Yann Collet | 1496c3d | 2016-12-18 11:58:23 +0100 | [diff] [blame] | 252 | } |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 253 | DISPLAYLEVEL(4, "Preparing to load : %u KB \n", (unsigned)(fs.totalSizeToLoad >> 10)); |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 254 | return fs; |
Yann Collet | 1496c3d | 2016-12-18 11:58:23 +0100 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 258 | int DiB_trainFromFiles(const char* dictFileName, unsigned maxDictSize, |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 259 | const char** fileNamesTable, unsigned nbFiles, size_t chunkSize, |
Jennifer Liu | 9d6ed9d | 2018-08-23 12:06:20 -0700 | [diff] [blame] | 260 | ZDICT_legacy_params_t* params, ZDICT_cover_params_t* coverParams, |
| 261 | ZDICT_fastCover_params_t* fastCoverParams, int optimize) |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 262 | { |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 263 | unsigned const displayLevel = params ? params->zParams.notificationLevel : |
| 264 | coverParams ? coverParams->zParams.notificationLevel : |
Jennifer Liu | 9d6ed9d | 2018-08-23 12:06:20 -0700 | [diff] [blame] | 265 | fastCoverParams ? fastCoverParams->zParams.notificationLevel : |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 266 | 0; /* should never happen */ |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 267 | void* const dictBuffer = malloc(maxDictSize); |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 268 | fileStats const fs = DiB_fileStats(fileNamesTable, nbFiles, chunkSize, displayLevel); |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 269 | size_t* const sampleSizes = (size_t*)malloc(fs.nbSamples * sizeof(size_t)); |
Jennifer Liu | 9d6ed9d | 2018-08-23 12:06:20 -0700 | [diff] [blame] | 270 | size_t const memMult = params ? MEMMULT : |
| 271 | coverParams ? COVER_MEMMULT: |
| 272 | FASTCOVER_MEMMULT; |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 273 | 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 Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 276 | int result = 0; |
| 277 | |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 278 | /* Checks */ |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 279 | if ((!sampleSizes) || (!srcBuffer) || (!dictBuffer)) |
Yann Collet | 77c137b | 2017-09-14 15:12:57 -0700 | [diff] [blame] | 280 | EXM_THROW(12, "not enough memory for DiB_trainFiles"); /* should not happen */ |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 281 | 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 Collet | 1496c3d | 2016-12-18 11:58:23 +0100 | [diff] [blame] | 285 | } |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 286 | if (fs.nbSamples < 5) { |
Yann Collet | 49d105c | 2016-08-18 15:02:11 +0200 | [diff] [blame] | 287 | DISPLAYLEVEL(2, "! Warning : nb of samples too low for proper processing ! \n"); |
| 288 | DISPLAYLEVEL(2, "! Please provide _one file per sample_. \n"); |
Yann Collet | 1722055 | 2017-09-15 16:23:50 -0700 | [diff] [blame] | 289 | 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] | 290 | EXM_THROW(14, "nb of samples too low"); /* we now clearly forbid this case */ |
| 291 | } |
ihsinme | f37896d | 2020-12-13 12:08:31 +0300 | [diff] [blame] | 292 | if (fs.totalSizeToLoad < (unsigned long long)maxDictSize * 8) { |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 293 | 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 Collet | dd25a27 | 2016-07-27 12:35:29 +0200 | [diff] [blame] | 295 | } |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 296 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 297 | /* init */ |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 298 | if (loadedSize < fs.totalSizeToLoad) |
| 299 | DISPLAYLEVEL(1, "Not enough memory; training on %u MB only...\n", (unsigned)(loadedSize >> 20)); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 300 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 301 | /* Load input buffer */ |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 302 | DISPLAYLEVEL(3, "Shuffling input files\n"); |
| 303 | DiB_shuffle(fileNamesTable, nbFiles); |
Jennifer Liu | 9d6ed9d | 2018-08-23 12:06:20 -0700 | [diff] [blame] | 304 | |
Yann Collet | 42a02ab | 2018-08-15 14:35:38 -0700 | [diff] [blame] | 305 | DiB_loadFiles(srcBuffer, &loadedSize, sampleSizes, fs.nbSamples, fileNamesTable, nbFiles, chunkSize, displayLevel); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 306 | |
Yann Collet | 77c137b | 2017-09-14 15:12:57 -0700 | [diff] [blame] | 307 | { size_t dictSize; |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 308 | if (params) { |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 309 | 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] | 310 | dictSize = ZDICT_trainFromBuffer_legacy(dictBuffer, maxDictSize, |
| 311 | srcBuffer, sampleSizes, fs.nbSamples, |
| 312 | *params); |
Jennifer Liu | 9d6ed9d | 2018-08-23 12:06:20 -0700 | [diff] [blame] | 313 | } 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 Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 326 | } |
| 327 | } else { |
Jennifer Liu | 9d6ed9d | 2018-08-23 12:06:20 -0700 | [diff] [blame] | 328 | 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 Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 343 | } |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 344 | 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 Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 350 | 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] | 351 | DiB_saveDict(dictFileName, dictBuffer, dictSize); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 352 | } |
| 353 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 354 | /* clean up */ |
| 355 | _cleanup: |
| 356 | free(srcBuffer); |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 357 | free(sampleSizes); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 358 | free(dictBuffer); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 359 | return result; |
| 360 | } |