2016-02-12 01:31:57 +00:00
|
|
|
/*
|
|
|
|
dibio - I/O API for dictionary builder
|
|
|
|
Copyright (C) Yann Collet 2016
|
|
|
|
|
|
|
|
GPL v2 License
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
|
|
|
You can contact the author at :
|
|
|
|
- zstd homepage : http://www.zstd.net/
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*-*************************************
|
|
|
|
* Includes
|
|
|
|
***************************************/
|
2016-05-05 11:58:56 +00:00
|
|
|
#include "util.h" /* Compiler options, UTIL_GetFileSize, UTIL_getTotalFileSize */
|
2016-02-12 01:31:57 +00:00
|
|
|
#include <stdlib.h> /* malloc, free */
|
|
|
|
#include <string.h> /* memset */
|
|
|
|
#include <stdio.h> /* fprintf, fopen, ftello64 */
|
2016-05-10 12:22:55 +00:00
|
|
|
#include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
|
2016-02-12 01:31:57 +00:00
|
|
|
|
|
|
|
#include "mem.h" /* read */
|
|
|
|
#include "error_private.h"
|
2016-04-22 10:43:18 +00:00
|
|
|
#include "dibio.h"
|
2016-02-12 01:31:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*-*************************************
|
|
|
|
* Constants
|
|
|
|
***************************************/
|
|
|
|
#define KB *(1 <<10)
|
|
|
|
#define MB *(1 <<20)
|
|
|
|
#define GB *(1U<<30)
|
|
|
|
|
|
|
|
#define DICTLISTSIZE 10000
|
|
|
|
#define MEMMULT 11
|
|
|
|
static const size_t maxMemory = (sizeof(size_t) == 4) ? (2 GB - 64 MB) : ((size_t)(512 MB) << sizeof(size_t));
|
|
|
|
|
|
|
|
#define NOISELENGTH 32
|
|
|
|
#define PRIME1 2654435761U
|
|
|
|
#define PRIME2 2246822519U
|
|
|
|
|
|
|
|
|
|
|
|
/*-*************************************
|
|
|
|
* Console display
|
|
|
|
***************************************/
|
|
|
|
#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
|
|
|
|
#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
|
|
|
|
static unsigned g_displayLevel = 0; /* 0 : no display; 1: errors; 2: default; 4: full information */
|
|
|
|
|
2016-05-09 02:44:45 +00:00
|
|
|
#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
|
|
|
|
if ((DIB_GetMilliSpan(g_time) > refreshRate) || (g_displayLevel>=4)) \
|
|
|
|
{ g_time = clock(); DISPLAY(__VA_ARGS__); \
|
|
|
|
if (g_displayLevel>=4) fflush(stdout); } }
|
|
|
|
static const unsigned refreshRate = 150;
|
|
|
|
static clock_t g_time = 0;
|
|
|
|
|
|
|
|
static unsigned DIB_GetMilliSpan(clock_t nPrevious)
|
|
|
|
{
|
|
|
|
clock_t const nCurrent = clock();
|
|
|
|
return (unsigned)(((nCurrent - nPrevious) * 1000) / CLOCKS_PER_SEC);
|
|
|
|
}
|
|
|
|
|
2016-02-12 01:31:57 +00:00
|
|
|
|
|
|
|
/*-*************************************
|
|
|
|
* Exceptions
|
|
|
|
***************************************/
|
|
|
|
#ifndef DEBUG
|
|
|
|
# define DEBUG 0
|
|
|
|
#endif
|
|
|
|
#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__);
|
|
|
|
#define EXM_THROW(error, ...) \
|
|
|
|
{ \
|
|
|
|
DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
|
|
|
|
DISPLAYLEVEL(1, "Error %i : ", error); \
|
|
|
|
DISPLAYLEVEL(1, __VA_ARGS__); \
|
|
|
|
DISPLAYLEVEL(1, "\n"); \
|
|
|
|
exit(error); \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ********************************************************
|
|
|
|
* Helper functions
|
|
|
|
**********************************************************/
|
|
|
|
unsigned DiB_isError(size_t errorCode) { return ERR_isError(errorCode); }
|
|
|
|
|
|
|
|
const char* DiB_getErrorName(size_t errorCode) { return ERR_getErrorName(errorCode); }
|
|
|
|
|
|
|
|
|
|
|
|
/* ********************************************************
|
|
|
|
* File related operations
|
|
|
|
**********************************************************/
|
2016-05-30 19:18:52 +00:00
|
|
|
/** DiB_loadFiles() :
|
|
|
|
* @return : nb of files effectively loaded into `buffer` */
|
|
|
|
static unsigned DiB_loadFiles(void* buffer, size_t bufferSize,
|
|
|
|
size_t* fileSizes,
|
|
|
|
const char** fileNamesTable, unsigned nbFiles)
|
2016-02-12 01:31:57 +00:00
|
|
|
{
|
2016-05-30 19:18:52 +00:00
|
|
|
char* const buff = (char*)buffer;
|
2016-02-12 01:31:57 +00:00
|
|
|
size_t pos = 0;
|
|
|
|
unsigned n;
|
|
|
|
|
|
|
|
for (n=0; n<nbFiles; n++) {
|
2016-05-30 19:18:52 +00:00
|
|
|
unsigned long long const fs64 = UTIL_getFileSize(fileNamesTable[n]);
|
|
|
|
size_t const fileSize = (size_t)(fs64 > bufferSize-pos ? 0 : fs64);
|
|
|
|
FILE* const f = fopen(fileNamesTable[n], "rb");
|
2016-02-12 01:31:57 +00:00
|
|
|
if (f==NULL) EXM_THROW(10, "impossible to open file %s", fileNamesTable[n]);
|
2016-05-09 02:44:45 +00:00
|
|
|
DISPLAYUPDATE(2, "Loading %s... \r", fileNamesTable[n]);
|
2016-05-30 19:18:52 +00:00
|
|
|
{ size_t const readSize = fread(buff+pos, 1, fileSize, f);
|
|
|
|
if (readSize != fileSize) EXM_THROW(11, "could not read %s", fileNamesTable[n]);
|
|
|
|
pos += readSize; }
|
|
|
|
fileSizes[n] = fileSize;
|
2016-02-12 01:31:57 +00:00
|
|
|
fclose(f);
|
2016-05-30 19:18:52 +00:00
|
|
|
if (fileSize == 0) break; /* stop there, not enough memory to load all files */
|
2016-02-12 01:31:57 +00:00
|
|
|
}
|
2016-05-30 19:18:52 +00:00
|
|
|
return n;
|
2016-02-12 01:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*-********************************************************
|
|
|
|
* Dictionary training functions
|
|
|
|
**********************************************************/
|
|
|
|
static size_t DiB_findMaxMem(unsigned long long requiredMem)
|
|
|
|
{
|
2016-05-30 19:18:52 +00:00
|
|
|
size_t const step = 8 MB;
|
2016-02-12 01:31:57 +00:00
|
|
|
void* testmem = NULL;
|
|
|
|
|
|
|
|
requiredMem = (((requiredMem >> 23) + 1) << 23);
|
|
|
|
requiredMem += 2 * step;
|
|
|
|
if (requiredMem > maxMemory) requiredMem = maxMemory;
|
|
|
|
|
|
|
|
while (!testmem) {
|
|
|
|
requiredMem -= step;
|
|
|
|
testmem = malloc((size_t)requiredMem);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(testmem);
|
|
|
|
return (size_t)(requiredMem - step);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void DiB_fillNoise(void* buffer, size_t length)
|
|
|
|
{
|
|
|
|
unsigned acc = PRIME1;
|
|
|
|
size_t p=0;;
|
|
|
|
|
|
|
|
for (p=0; p<length; p++) {
|
|
|
|
acc *= PRIME2;
|
|
|
|
((unsigned char*)buffer)[p] = (unsigned char)(acc >> 21);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void DiB_saveDict(const char* dictFileName,
|
|
|
|
const void* buff, size_t buffSize)
|
|
|
|
{
|
2016-05-30 19:18:52 +00:00
|
|
|
FILE* const f = fopen(dictFileName, "wb");
|
2016-02-12 01:31:57 +00:00
|
|
|
if (f==NULL) EXM_THROW(3, "cannot open %s ", dictFileName);
|
|
|
|
|
2016-05-09 02:44:45 +00:00
|
|
|
{ size_t const n = fwrite(buff, 1, buffSize, f);
|
|
|
|
if (n!=buffSize) EXM_THROW(4, "%s : write error", dictFileName) }
|
2016-02-12 01:31:57 +00:00
|
|
|
|
2016-05-09 02:44:45 +00:00
|
|
|
{ size_t const n = (size_t)fclose(f);
|
|
|
|
if (n!=0) EXM_THROW(5, "%s : flush error", dictFileName) }
|
2016-02-12 01:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-12 19:19:48 +00:00
|
|
|
/*! ZDICT_trainFromBuffer_unsafe() :
|
|
|
|
Strictly Internal use only !!
|
|
|
|
Same as ZDICT_trainFromBuffer_advanced(), but does not control `samplesBuffer`.
|
|
|
|
`samplesBuffer` must be followed by noisy guard band to avoid out-of-buffer reads.
|
|
|
|
@return : size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
|
|
|
|
or an error code.
|
|
|
|
*/
|
|
|
|
size_t ZDICT_trainFromBuffer_unsafe(void* dictBuffer, size_t dictBufferCapacity,
|
|
|
|
const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples,
|
|
|
|
ZDICT_params_t parameters);
|
|
|
|
|
|
|
|
|
2016-05-30 19:18:52 +00:00
|
|
|
#define MIN(a,b) ((a)<(b)?(a):(b))
|
2016-02-12 01:31:57 +00:00
|
|
|
int DiB_trainFromFiles(const char* dictFileName, unsigned maxDictSize,
|
|
|
|
const char** fileNamesTable, unsigned nbFiles,
|
|
|
|
ZDICT_params_t params)
|
|
|
|
{
|
2016-05-30 19:18:52 +00:00
|
|
|
void* const dictBuffer = malloc(maxDictSize);
|
|
|
|
size_t* const fileSizes = (size_t*)malloc(nbFiles * sizeof(size_t));
|
|
|
|
unsigned long long const totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, nbFiles);
|
|
|
|
size_t const maxMem = DiB_findMaxMem(totalSizeToLoad * MEMMULT) / MEMMULT;
|
|
|
|
size_t const benchedSize = MIN (maxMem, (size_t)totalSizeToLoad);
|
|
|
|
void* const srcBuffer = malloc(benchedSize+NOISELENGTH);
|
2016-02-12 01:31:57 +00:00
|
|
|
int result = 0;
|
|
|
|
|
2016-05-30 19:18:52 +00:00
|
|
|
/* Checks */
|
|
|
|
if ((!fileSizes) || (!srcBuffer) || (!dictBuffer)) EXM_THROW(12, "not enough memory for DiB_trainFiles"); /* should not happen */
|
|
|
|
|
2016-02-12 01:31:57 +00:00
|
|
|
/* init */
|
2016-02-12 19:19:48 +00:00
|
|
|
g_displayLevel = params.notificationLevel;
|
2016-02-12 01:31:57 +00:00
|
|
|
if (benchedSize < totalSizeToLoad)
|
|
|
|
DISPLAYLEVEL(1, "Not enough memory; training on %u MB only...\n", (unsigned)(benchedSize >> 20));
|
|
|
|
|
|
|
|
/* Load input buffer */
|
2016-05-30 19:18:52 +00:00
|
|
|
nbFiles = DiB_loadFiles(srcBuffer, benchedSize, fileSizes, fileNamesTable, nbFiles);
|
2016-02-12 01:31:57 +00:00
|
|
|
DiB_fillNoise((char*)srcBuffer + benchedSize, NOISELENGTH); /* guard band, for end of buffer condition */
|
|
|
|
|
2016-05-30 19:18:52 +00:00
|
|
|
{ size_t const dictSize = ZDICT_trainFromBuffer_unsafe(dictBuffer, maxDictSize,
|
|
|
|
srcBuffer, fileSizes, nbFiles,
|
|
|
|
params);
|
|
|
|
if (ZDICT_isError(dictSize)) {
|
|
|
|
DISPLAYLEVEL(1, "dictionary training failed : %s \n", ZDICT_getErrorName(dictSize)); /* should not happen */
|
|
|
|
result = 1;
|
|
|
|
goto _cleanup;
|
|
|
|
}
|
|
|
|
/* save dict */
|
|
|
|
DISPLAYLEVEL(2, "Save dictionary of size %u into file %s \n", (U32)dictSize, dictFileName);
|
|
|
|
DiB_saveDict(dictFileName, dictBuffer, dictSize);
|
2016-02-12 01:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* clean up */
|
|
|
|
_cleanup:
|
|
|
|
free(srcBuffer);
|
|
|
|
free(dictBuffer);
|
|
|
|
free(fileSizes);
|
|
|
|
return result;
|
|
|
|
}
|