Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 1 | /* |
Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. |
| 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 | |
Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 32 | #include "mem.h" /* read */ |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 33 | #include "error_private.h" |
inikep | 23a0889 | 2016-04-22 12:43:18 +0200 | [diff] [blame] | 34 | #include "dibio.h" |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 35 | |
| 36 | |
| 37 | /*-************************************* |
| 38 | * Constants |
| 39 | ***************************************/ |
| 40 | #define KB *(1 <<10) |
| 41 | #define MB *(1 <<20) |
| 42 | #define GB *(1U<<30) |
| 43 | |
Yann Collet | 1496c3d | 2016-12-18 11:58:23 +0100 | [diff] [blame] | 44 | #define SAMPLESIZE_MAX (128 KB) |
| 45 | #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] | 46 | #define COVER_MEMMULT 9 /* rough estimation : memory cost to analyze 1 byte of sample */ |
Yann Collet | 77c137b | 2017-09-14 15:12:57 -0700 | [diff] [blame] | 47 | 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] | 48 | |
| 49 | #define NOISELENGTH 32 |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 50 | |
| 51 | |
| 52 | /*-************************************* |
| 53 | * Console display |
| 54 | ***************************************/ |
| 55 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 56 | #define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); } |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 57 | |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 58 | static const U64 g_refreshRate = SEC_TO_MICRO / 6; |
| 59 | static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER; |
Yann Collet | f6ca09b | 2016-05-09 04:44:45 +0200 | [diff] [blame] | 60 | |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 61 | #define DISPLAYUPDATE(l, ...) { if (displayLevel>=l) { \ |
| 62 | if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || (displayLevel>=4)) \ |
| 63 | { g_displayClock = UTIL_getTime(); DISPLAY(__VA_ARGS__); \ |
| 64 | if (displayLevel>=4) fflush(stderr); } } } |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 65 | |
| 66 | /*-************************************* |
| 67 | * Exceptions |
| 68 | ***************************************/ |
| 69 | #ifndef DEBUG |
| 70 | # define DEBUG 0 |
| 71 | #endif |
| 72 | #define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__); |
| 73 | #define EXM_THROW(error, ...) \ |
| 74 | { \ |
| 75 | DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \ |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 76 | DISPLAY("Error %i : ", error); \ |
| 77 | DISPLAY(__VA_ARGS__); \ |
| 78 | DISPLAY("\n"); \ |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 79 | exit(error); \ |
| 80 | } |
| 81 | |
| 82 | |
| 83 | /* ******************************************************** |
| 84 | * Helper functions |
| 85 | **********************************************************/ |
| 86 | unsigned DiB_isError(size_t errorCode) { return ERR_isError(errorCode); } |
| 87 | |
| 88 | const char* DiB_getErrorName(size_t errorCode) { return ERR_getErrorName(errorCode); } |
| 89 | |
Sean Purcell | 42bac7f | 2017-04-13 15:35:05 -0700 | [diff] [blame] | 90 | #undef MIN |
| 91 | #define MIN(a,b) ((a) < (b) ? (a) : (b)) |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 92 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 93 | |
| 94 | /* ******************************************************** |
| 95 | * File related operations |
| 96 | **********************************************************/ |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 97 | /** DiB_loadFiles() : |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 98 | * load samples from files listed in fileNamesTable into buffer. |
| 99 | * works even if buffer is too small to load all samples. |
| 100 | * Also provides the size of each sample into sampleSizes table |
| 101 | * which must be sized correctly, using DiB_fileStats(). |
| 102 | * @return : nb of samples effectively loaded into `buffer` |
| 103 | * *bufferSizePtr is modified, it provides the amount data loaded within buffer. |
| 104 | * sampleSizes is filled with the size of each sample. |
| 105 | */ |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 106 | static unsigned DiB_loadFiles(void* buffer, size_t* bufferSizePtr, |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 107 | size_t* sampleSizes, unsigned sstSize, |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 108 | const char** fileNamesTable, unsigned nbFiles, size_t targetChunkSize, |
| 109 | unsigned displayLevel) |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 110 | { |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 111 | char* const buff = (char*)buffer; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 112 | size_t pos = 0; |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 113 | unsigned nbLoadedChunks = 0, fileIndex; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 114 | |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 115 | for (fileIndex=0; fileIndex<nbFiles; fileIndex++) { |
| 116 | const char* const fileName = fileNamesTable[fileIndex]; |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 117 | unsigned long long const fs64 = UTIL_getFileSize(fileName); |
Yann Collet | 18b7953 | 2017-10-17 16:14:25 -0700 | [diff] [blame] | 118 | unsigned long long remainingToLoad = (fs64 == UTIL_FILESIZE_UNKNOWN) ? 0 : fs64; |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 119 | U32 const nbChunks = targetChunkSize ? (U32)((fs64 + (targetChunkSize-1)) / targetChunkSize) : 1; |
| 120 | U64 const chunkSize = targetChunkSize ? MIN(targetChunkSize, fs64) : fs64; |
Yann Collet | 25a6048 | 2017-09-15 11:55:13 -0700 | [diff] [blame] | 121 | size_t const maxChunkSize = (size_t)MIN(chunkSize, SAMPLESIZE_MAX); |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 122 | U32 cnb; |
| 123 | FILE* const f = fopen(fileName, "rb"); |
| 124 | if (f==NULL) EXM_THROW(10, "zstd: dictBuilder: %s %s ", fileName, strerror(errno)); |
| 125 | DISPLAYUPDATE(2, "Loading %s... \r", fileName); |
| 126 | for (cnb=0; cnb<nbChunks; cnb++) { |
Yann Collet | 25a6048 | 2017-09-15 11:55:13 -0700 | [diff] [blame] | 127 | size_t const toLoad = (size_t)MIN(maxChunkSize, remainingToLoad); |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 128 | if (toLoad > *bufferSizePtr-pos) break; |
| 129 | { size_t const readSize = fread(buff+pos, 1, toLoad, f); |
| 130 | if (readSize != toLoad) EXM_THROW(11, "Pb reading %s", fileName); |
| 131 | pos += readSize; |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 132 | sampleSizes[nbLoadedChunks++] = toLoad; |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 133 | remainingToLoad -= targetChunkSize; |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 134 | if (nbLoadedChunks == sstSize) { /* no more space left in sampleSizes table */ |
| 135 | fileIndex = nbFiles; /* stop there */ |
| 136 | break; |
| 137 | } |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 138 | if (toLoad < targetChunkSize) { |
Yann Collet | a969423 | 2017-09-15 10:16:26 -0700 | [diff] [blame] | 139 | fseek(f, (long)(targetChunkSize - toLoad), SEEK_CUR); |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 140 | } } } |
| 141 | fclose(f); |
| 142 | } |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 143 | DISPLAYLEVEL(2, "\r%79s\r", ""); |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 144 | *bufferSizePtr = pos; |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 145 | DISPLAYLEVEL(4, "loaded : %u KB \n", (U32)(pos >> 10)) |
| 146 | return nbLoadedChunks; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 149 | #define DiB_rotl32(x,r) ((x << r) | (x >> (32 - r))) |
| 150 | static U32 DiB_rand(U32* src) |
| 151 | { |
| 152 | static const U32 prime1 = 2654435761U; |
| 153 | static const U32 prime2 = 2246822519U; |
| 154 | U32 rand32 = *src; |
| 155 | rand32 *= prime1; |
| 156 | rand32 ^= prime2; |
| 157 | rand32 = DiB_rotl32(rand32, 13); |
| 158 | *src = rand32; |
| 159 | return rand32 >> 5; |
| 160 | } |
| 161 | |
Yann Collet | 77c137b | 2017-09-14 15:12:57 -0700 | [diff] [blame] | 162 | /* DiB_shuffle() : |
| 163 | * shuffle a table of file names in a semi-random way |
| 164 | * It improves dictionary quality by reducing "locality" impact, so if sample set is very large, |
| 165 | * it will load random elements from it, instead of just the first ones. */ |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 166 | static void DiB_shuffle(const char** fileNamesTable, unsigned nbFiles) { |
Yann Collet | 77c137b | 2017-09-14 15:12:57 -0700 | [diff] [blame] | 167 | U32 seed = 0xFD2FB528; |
| 168 | unsigned i; |
Yann Collet | 42a02ab | 2018-08-15 14:35:38 -0700 | [diff] [blame] | 169 | assert(nbFiles >= 1); |
Yann Collet | 77c137b | 2017-09-14 15:12:57 -0700 | [diff] [blame] | 170 | for (i = nbFiles - 1; i > 0; --i) { |
| 171 | unsigned const j = DiB_rand(&seed) % (i + 1); |
| 172 | const char* const tmp = fileNamesTable[j]; |
| 173 | fileNamesTable[j] = fileNamesTable[i]; |
| 174 | fileNamesTable[i] = tmp; |
| 175 | } |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 178 | |
| 179 | /*-******************************************************** |
| 180 | * Dictionary training functions |
| 181 | **********************************************************/ |
| 182 | static size_t DiB_findMaxMem(unsigned long long requiredMem) |
| 183 | { |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 184 | size_t const step = 8 MB; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 185 | void* testmem = NULL; |
| 186 | |
| 187 | requiredMem = (((requiredMem >> 23) + 1) << 23); |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 188 | requiredMem += step; |
Yann Collet | 77c137b | 2017-09-14 15:12:57 -0700 | [diff] [blame] | 189 | if (requiredMem > g_maxMemory) requiredMem = g_maxMemory; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 190 | |
| 191 | while (!testmem) { |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 192 | testmem = malloc((size_t)requiredMem); |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 193 | requiredMem -= step; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | free(testmem); |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 197 | return (size_t)requiredMem; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | |
| 201 | static void DiB_fillNoise(void* buffer, size_t length) |
| 202 | { |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 203 | unsigned const prime1 = 2654435761U; |
| 204 | unsigned const prime2 = 2246822519U; |
| 205 | unsigned acc = prime1; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 206 | size_t p=0;; |
| 207 | |
| 208 | for (p=0; p<length; p++) { |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 209 | acc *= prime2; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 210 | ((unsigned char*)buffer)[p] = (unsigned char)(acc >> 21); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | |
| 215 | static void DiB_saveDict(const char* dictFileName, |
| 216 | const void* buff, size_t buffSize) |
| 217 | { |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 218 | FILE* const f = fopen(dictFileName, "wb"); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 219 | if (f==NULL) EXM_THROW(3, "cannot open %s ", dictFileName); |
| 220 | |
Yann Collet | f6ca09b | 2016-05-09 04:44:45 +0200 | [diff] [blame] | 221 | { size_t const n = fwrite(buff, 1, buffSize, f); |
| 222 | if (n!=buffSize) EXM_THROW(4, "%s : write error", dictFileName) } |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 223 | |
Yann Collet | f6ca09b | 2016-05-09 04:44:45 +0200 | [diff] [blame] | 224 | { size_t const n = (size_t)fclose(f); |
| 225 | if (n!=0) EXM_THROW(5, "%s : flush error", dictFileName) } |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 229 | typedef struct { |
| 230 | U64 totalSizeToLoad; |
| 231 | unsigned oneSampleTooLarge; |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 232 | unsigned nbSamples; |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 233 | } fileStats; |
| 234 | |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 235 | /*! DiB_fileStats() : |
| 236 | * Given a list of files, and a chunkSize (0 == no chunk, whole files) |
| 237 | * provides the amount of data to be loaded and the resulting nb of samples. |
| 238 | * This is useful primarily for allocation purpose => sample buffer, and sample sizes table. |
| 239 | */ |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 240 | 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] | 241 | { |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 242 | fileStats fs; |
Yann Collet | 1496c3d | 2016-12-18 11:58:23 +0100 | [diff] [blame] | 243 | unsigned n; |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 244 | memset(&fs, 0, sizeof(fs)); |
Yann Collet | 1496c3d | 2016-12-18 11:58:23 +0100 | [diff] [blame] | 245 | for (n=0; n<nbFiles; n++) { |
| 246 | U64 const fileSize = UTIL_getFileSize(fileNamesTable[n]); |
Yann Collet | 18b7953 | 2017-10-17 16:14:25 -0700 | [diff] [blame] | 247 | U64 const srcSize = (fileSize == UTIL_FILESIZE_UNKNOWN) ? 0 : fileSize; |
| 248 | U32 const nbSamples = (U32)(chunkSize ? (srcSize + (chunkSize-1)) / chunkSize : 1); |
| 249 | U64 const chunkToLoad = chunkSize ? MIN(chunkSize, srcSize) : srcSize; |
Yann Collet | 25a6048 | 2017-09-15 11:55:13 -0700 | [diff] [blame] | 250 | size_t const cappedChunkSize = (size_t)MIN(chunkToLoad, SAMPLESIZE_MAX); |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 251 | fs.totalSizeToLoad += cappedChunkSize * nbSamples; |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 252 | fs.oneSampleTooLarge |= (chunkSize > 2*SAMPLESIZE_MAX); |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 253 | fs.nbSamples += nbSamples; |
Yann Collet | 1496c3d | 2016-12-18 11:58:23 +0100 | [diff] [blame] | 254 | } |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 255 | DISPLAYLEVEL(4, "Preparing to load : %u KB \n", (U32)(fs.totalSizeToLoad >> 10)); |
| 256 | return fs; |
Yann Collet | 1496c3d | 2016-12-18 11:58:23 +0100 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | |
Nick Terrell | 5b7fd7c | 2017-06-26 21:07:14 -0700 | [diff] [blame] | 260 | /*! ZDICT_trainFromBuffer_unsafe_legacy() : |
Yann Collet | 6f3acba | 2016-02-12 20:19:48 +0100 | [diff] [blame] | 261 | Strictly Internal use only !! |
Nick Terrell | 5b7fd7c | 2017-06-26 21:07:14 -0700 | [diff] [blame] | 262 | Same as ZDICT_trainFromBuffer_legacy(), but does not control `samplesBuffer`. |
Yann Collet | 6f3acba | 2016-02-12 20:19:48 +0100 | [diff] [blame] | 263 | `samplesBuffer` must be followed by noisy guard band to avoid out-of-buffer reads. |
| 264 | @return : size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`) |
| 265 | or an error code. |
| 266 | */ |
Nick Terrell | 5b7fd7c | 2017-06-26 21:07:14 -0700 | [diff] [blame] | 267 | size_t ZDICT_trainFromBuffer_unsafe_legacy(void* dictBuffer, size_t dictBufferCapacity, |
| 268 | const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples, |
| 269 | ZDICT_legacy_params_t parameters); |
Yann Collet | 6f3acba | 2016-02-12 20:19:48 +0100 | [diff] [blame] | 270 | |
| 271 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 272 | int DiB_trainFromFiles(const char* dictFileName, unsigned maxDictSize, |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 273 | const char** fileNamesTable, unsigned nbFiles, size_t chunkSize, |
Nick Terrell | 5b7fd7c | 2017-06-26 21:07:14 -0700 | [diff] [blame] | 274 | ZDICT_legacy_params_t *params, ZDICT_cover_params_t *coverParams, |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 275 | int optimizeCover) |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 276 | { |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 277 | unsigned const displayLevel = params ? params->zParams.notificationLevel : |
| 278 | coverParams ? coverParams->zParams.notificationLevel : |
| 279 | 0; /* should never happen */ |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 280 | void* const dictBuffer = malloc(maxDictSize); |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 281 | fileStats const fs = DiB_fileStats(fileNamesTable, nbFiles, chunkSize, displayLevel); |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 282 | size_t* const sampleSizes = (size_t*)malloc(fs.nbSamples * sizeof(size_t)); |
Nick Terrell | c220d4c | 2017-01-09 16:49:04 -0800 | [diff] [blame] | 283 | size_t const memMult = params ? MEMMULT : COVER_MEMMULT; |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 284 | size_t const maxMem = DiB_findMaxMem(fs.totalSizeToLoad * memMult) / memMult; |
| 285 | size_t loadedSize = (size_t) MIN ((unsigned long long)maxMem, fs.totalSizeToLoad); |
| 286 | void* const srcBuffer = malloc(loadedSize+NOISELENGTH); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 287 | int result = 0; |
| 288 | |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 289 | /* Checks */ |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 290 | if ((!sampleSizes) || (!srcBuffer) || (!dictBuffer)) |
Yann Collet | 77c137b | 2017-09-14 15:12:57 -0700 | [diff] [blame] | 291 | EXM_THROW(12, "not enough memory for DiB_trainFiles"); /* should not happen */ |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 292 | if (fs.oneSampleTooLarge) { |
| 293 | DISPLAYLEVEL(2, "! Warning : some sample(s) are very large \n"); |
| 294 | DISPLAYLEVEL(2, "! Note that dictionary is only useful for small samples. \n"); |
| 295 | 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] | 296 | } |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 297 | if (fs.nbSamples < 5) { |
Yann Collet | 49d105c | 2016-08-18 15:02:11 +0200 | [diff] [blame] | 298 | DISPLAYLEVEL(2, "! Warning : nb of samples too low for proper processing ! \n"); |
| 299 | DISPLAYLEVEL(2, "! Please provide _one file per sample_. \n"); |
Yann Collet | 1722055 | 2017-09-15 16:23:50 -0700 | [diff] [blame] | 300 | 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] | 301 | EXM_THROW(14, "nb of samples too low"); /* we now clearly forbid this case */ |
| 302 | } |
| 303 | if (fs.totalSizeToLoad < (unsigned long long)(8 * maxDictSize)) { |
| 304 | DISPLAYLEVEL(2, "! Warning : data size of samples too small for target dictionary size \n"); |
| 305 | 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] | 306 | } |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 307 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 308 | /* init */ |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 309 | if (loadedSize < fs.totalSizeToLoad) |
| 310 | 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] | 311 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 312 | /* Load input buffer */ |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 313 | DISPLAYLEVEL(3, "Shuffling input files\n"); |
| 314 | DiB_shuffle(fileNamesTable, nbFiles); |
Yann Collet | 42a02ab | 2018-08-15 14:35:38 -0700 | [diff] [blame] | 315 | DiB_loadFiles(srcBuffer, &loadedSize, sampleSizes, fs.nbSamples, fileNamesTable, nbFiles, chunkSize, displayLevel); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 316 | |
Yann Collet | 77c137b | 2017-09-14 15:12:57 -0700 | [diff] [blame] | 317 | { size_t dictSize; |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 318 | if (params) { |
Yann Collet | 086b959 | 2017-09-14 16:45:10 -0700 | [diff] [blame] | 319 | DiB_fillNoise((char*)srcBuffer + loadedSize, NOISELENGTH); /* guard band, for end of buffer condition */ |
Nick Terrell | 5b7fd7c | 2017-06-26 21:07:14 -0700 | [diff] [blame] | 320 | dictSize = ZDICT_trainFromBuffer_unsafe_legacy(dictBuffer, maxDictSize, |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 321 | srcBuffer, sampleSizes, fs.nbSamples, |
Nick Terrell | 5b7fd7c | 2017-06-26 21:07:14 -0700 | [diff] [blame] | 322 | *params); |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 323 | } else if (optimizeCover) { |
Yann Collet | 42a02ab | 2018-08-15 14:35:38 -0700 | [diff] [blame] | 324 | assert(coverParams != NULL); |
Nick Terrell | 5b7fd7c | 2017-06-26 21:07:14 -0700 | [diff] [blame] | 325 | dictSize = ZDICT_optimizeTrainFromBuffer_cover(dictBuffer, maxDictSize, |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 326 | srcBuffer, sampleSizes, fs.nbSamples, |
Nick Terrell | 5b7fd7c | 2017-06-26 21:07:14 -0700 | [diff] [blame] | 327 | coverParams); |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 328 | if (!ZDICT_isError(dictSize)) { |
Jennifer Liu | 8afcb8e | 2018-07-01 19:59:37 -0700 | [diff] [blame] | 329 | unsigned splitPercentage = (unsigned)(coverParams->splitPoint * 100); |
| 330 | DISPLAYLEVEL(2, "k=%u\nd=%u\nsteps=%u\nsplit=%u\n", coverParams->k, coverParams->d, coverParams->steps, splitPercentage); |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 331 | } |
| 332 | } else { |
Yann Collet | 42a02ab | 2018-08-15 14:35:38 -0700 | [diff] [blame] | 333 | assert(coverParams != NULL); |
Yann Collet | 77c137b | 2017-09-14 15:12:57 -0700 | [diff] [blame] | 334 | dictSize = ZDICT_trainFromBuffer_cover(dictBuffer, maxDictSize, srcBuffer, |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 335 | sampleSizes, fs.nbSamples, *coverParams); |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 336 | } |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 337 | if (ZDICT_isError(dictSize)) { |
| 338 | DISPLAYLEVEL(1, "dictionary training failed : %s \n", ZDICT_getErrorName(dictSize)); /* should not happen */ |
| 339 | result = 1; |
| 340 | goto _cleanup; |
| 341 | } |
| 342 | /* save dict */ |
| 343 | DISPLAYLEVEL(2, "Save dictionary of size %u into file %s \n", (U32)dictSize, dictFileName); |
| 344 | DiB_saveDict(dictFileName, dictBuffer, dictSize); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 345 | } |
| 346 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 347 | /* clean up */ |
| 348 | _cleanup: |
| 349 | free(srcBuffer); |
Yann Collet | c68d17f | 2017-09-15 15:31:31 -0700 | [diff] [blame] | 350 | free(sampleSizes); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 351 | free(dictBuffer); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 352 | return result; |
| 353 | } |