Path: blob/master/Utilities/cmzstd/lib/dictBuilder/fastcover.c
3156 views
/*1* Copyright (c) Meta Platforms, Inc. and affiliates.2* All rights reserved.3*4* This source code is licensed under both the BSD-style license (found in the5* LICENSE file in the root directory of this source tree) and the GPLv2 (found6* in the COPYING file in the root directory of this source tree).7* You may select, at your option, one of the above-listed licenses.8*/910/*-*************************************11* Dependencies12***************************************/13#include <stdio.h> /* fprintf */14#include <stdlib.h> /* malloc, free, qsort */15#include <string.h> /* memset */16#include <time.h> /* clock */1718#ifndef ZDICT_STATIC_LINKING_ONLY19# define ZDICT_STATIC_LINKING_ONLY20#endif2122#include "../common/mem.h" /* read */23#include "../common/pool.h"24#include "../common/threading.h"25#include "../common/zstd_internal.h" /* includes zstd.h */26#include "../compress/zstd_compress_internal.h" /* ZSTD_hash*() */27#include "../zdict.h"28#include "cover.h"293031/*-*************************************32* Constants33***************************************/34/**35* There are 32bit indexes used to ref samples, so limit samples size to 4GB36* on 64bit builds.37* For 32bit builds we choose 1 GB.38* Most 32bit platforms have 2GB user-mode addressable space and we allocate a large39* contiguous buffer, so 1GB is already a high limit.40*/41#define FASTCOVER_MAX_SAMPLES_SIZE (sizeof(size_t) == 8 ? ((unsigned)-1) : ((unsigned)1 GB))42#define FASTCOVER_MAX_F 3143#define FASTCOVER_MAX_ACCEL 1044#define FASTCOVER_DEFAULT_SPLITPOINT 0.7545#define DEFAULT_F 2046#define DEFAULT_ACCEL 1474849/*-*************************************50* Console display51***************************************/52#ifndef LOCALDISPLAYLEVEL53static int g_displayLevel = 0;54#endif55#undef DISPLAY56#define DISPLAY(...) \57{ \58fprintf(stderr, __VA_ARGS__); \59fflush(stderr); \60}61#undef LOCALDISPLAYLEVEL62#define LOCALDISPLAYLEVEL(displayLevel, l, ...) \63if (displayLevel >= l) { \64DISPLAY(__VA_ARGS__); \65} /* 0 : no display; 1: errors; 2: default; 3: details; 4: debug */66#undef DISPLAYLEVEL67#define DISPLAYLEVEL(l, ...) LOCALDISPLAYLEVEL(g_displayLevel, l, __VA_ARGS__)6869#ifndef LOCALDISPLAYUPDATE70static const clock_t g_refreshRate = CLOCKS_PER_SEC * 15 / 100;71static clock_t g_time = 0;72#endif73#undef LOCALDISPLAYUPDATE74#define LOCALDISPLAYUPDATE(displayLevel, l, ...) \75if (displayLevel >= l) { \76if ((clock() - g_time > g_refreshRate) || (displayLevel >= 4)) { \77g_time = clock(); \78DISPLAY(__VA_ARGS__); \79} \80}81#undef DISPLAYUPDATE82#define DISPLAYUPDATE(l, ...) LOCALDISPLAYUPDATE(g_displayLevel, l, __VA_ARGS__)838485/*-*************************************86* Hash Functions87***************************************/88/**89* Hash the d-byte value pointed to by p and mod 2^f into the frequency vector90*/91static size_t FASTCOVER_hashPtrToIndex(const void* p, U32 f, unsigned d) {92if (d == 6) {93return ZSTD_hash6Ptr(p, f);94}95return ZSTD_hash8Ptr(p, f);96}979899/*-*************************************100* Acceleration101***************************************/102typedef struct {103unsigned finalize; /* Percentage of training samples used for ZDICT_finalizeDictionary */104unsigned skip; /* Number of dmer skipped between each dmer counted in computeFrequency */105} FASTCOVER_accel_t;106107108static const FASTCOVER_accel_t FASTCOVER_defaultAccelParameters[FASTCOVER_MAX_ACCEL+1] = {109{ 100, 0 }, /* accel = 0, should not happen because accel = 0 defaults to accel = 1 */110{ 100, 0 }, /* accel = 1 */111{ 50, 1 }, /* accel = 2 */112{ 34, 2 }, /* accel = 3 */113{ 25, 3 }, /* accel = 4 */114{ 20, 4 }, /* accel = 5 */115{ 17, 5 }, /* accel = 6 */116{ 14, 6 }, /* accel = 7 */117{ 13, 7 }, /* accel = 8 */118{ 11, 8 }, /* accel = 9 */119{ 10, 9 }, /* accel = 10 */120};121122123/*-*************************************124* Context125***************************************/126typedef struct {127const BYTE *samples;128size_t *offsets;129const size_t *samplesSizes;130size_t nbSamples;131size_t nbTrainSamples;132size_t nbTestSamples;133size_t nbDmers;134U32 *freqs;135unsigned d;136unsigned f;137FASTCOVER_accel_t accelParams;138} FASTCOVER_ctx_t;139140141/*-*************************************142* Helper functions143***************************************/144/**145* Selects the best segment in an epoch.146* Segments of are scored according to the function:147*148* Let F(d) be the frequency of all dmers with hash value d.149* Let S_i be hash value of the dmer at position i of segment S which has length k.150*151* Score(S) = F(S_1) + F(S_2) + ... + F(S_{k-d+1})152*153* Once the dmer with hash value d is in the dictionary we set F(d) = 0.154*/155static COVER_segment_t FASTCOVER_selectSegment(const FASTCOVER_ctx_t *ctx,156U32 *freqs, U32 begin, U32 end,157ZDICT_cover_params_t parameters,158U16* segmentFreqs) {159/* Constants */160const U32 k = parameters.k;161const U32 d = parameters.d;162const U32 f = ctx->f;163const U32 dmersInK = k - d + 1;164165/* Try each segment (activeSegment) and save the best (bestSegment) */166COVER_segment_t bestSegment = {0, 0, 0};167COVER_segment_t activeSegment;168169/* Reset the activeDmers in the segment */170/* The activeSegment starts at the beginning of the epoch. */171activeSegment.begin = begin;172activeSegment.end = begin;173activeSegment.score = 0;174175/* Slide the activeSegment through the whole epoch.176* Save the best segment in bestSegment.177*/178while (activeSegment.end < end) {179/* Get hash value of current dmer */180const size_t idx = FASTCOVER_hashPtrToIndex(ctx->samples + activeSegment.end, f, d);181182/* Add frequency of this index to score if this is the first occurrence of index in active segment */183if (segmentFreqs[idx] == 0) {184activeSegment.score += freqs[idx];185}186/* Increment end of segment and segmentFreqs*/187activeSegment.end += 1;188segmentFreqs[idx] += 1;189/* If the window is now too large, drop the first position */190if (activeSegment.end - activeSegment.begin == dmersInK + 1) {191/* Get hash value of the dmer to be eliminated from active segment */192const size_t delIndex = FASTCOVER_hashPtrToIndex(ctx->samples + activeSegment.begin, f, d);193segmentFreqs[delIndex] -= 1;194/* Subtract frequency of this index from score if this is the last occurrence of this index in active segment */195if (segmentFreqs[delIndex] == 0) {196activeSegment.score -= freqs[delIndex];197}198/* Increment start of segment */199activeSegment.begin += 1;200}201202/* If this segment is the best so far save it */203if (activeSegment.score > bestSegment.score) {204bestSegment = activeSegment;205}206}207208/* Zero out rest of segmentFreqs array */209while (activeSegment.begin < end) {210const size_t delIndex = FASTCOVER_hashPtrToIndex(ctx->samples + activeSegment.begin, f, d);211segmentFreqs[delIndex] -= 1;212activeSegment.begin += 1;213}214215{216/* Zero the frequency of hash value of each dmer covered by the chosen segment. */217U32 pos;218for (pos = bestSegment.begin; pos != bestSegment.end; ++pos) {219const size_t i = FASTCOVER_hashPtrToIndex(ctx->samples + pos, f, d);220freqs[i] = 0;221}222}223224return bestSegment;225}226227228static int FASTCOVER_checkParameters(ZDICT_cover_params_t parameters,229size_t maxDictSize, unsigned f,230unsigned accel) {231/* k, d, and f are required parameters */232if (parameters.d == 0 || parameters.k == 0) {233return 0;234}235/* d has to be 6 or 8 */236if (parameters.d != 6 && parameters.d != 8) {237return 0;238}239/* k <= maxDictSize */240if (parameters.k > maxDictSize) {241return 0;242}243/* d <= k */244if (parameters.d > parameters.k) {245return 0;246}247/* 0 < f <= FASTCOVER_MAX_F*/248if (f > FASTCOVER_MAX_F || f == 0) {249return 0;250}251/* 0 < splitPoint <= 1 */252if (parameters.splitPoint <= 0 || parameters.splitPoint > 1) {253return 0;254}255/* 0 < accel <= 10 */256if (accel > 10 || accel == 0) {257return 0;258}259return 1;260}261262263/**264* Clean up a context initialized with `FASTCOVER_ctx_init()`.265*/266static void267FASTCOVER_ctx_destroy(FASTCOVER_ctx_t* ctx)268{269if (!ctx) return;270271free(ctx->freqs);272ctx->freqs = NULL;273274free(ctx->offsets);275ctx->offsets = NULL;276}277278279/**280* Calculate for frequency of hash value of each dmer in ctx->samples281*/282static void283FASTCOVER_computeFrequency(U32* freqs, const FASTCOVER_ctx_t* ctx)284{285const unsigned f = ctx->f;286const unsigned d = ctx->d;287const unsigned skip = ctx->accelParams.skip;288const unsigned readLength = MAX(d, 8);289size_t i;290assert(ctx->nbTrainSamples >= 5);291assert(ctx->nbTrainSamples <= ctx->nbSamples);292for (i = 0; i < ctx->nbTrainSamples; i++) {293size_t start = ctx->offsets[i]; /* start of current dmer */294size_t const currSampleEnd = ctx->offsets[i+1];295while (start + readLength <= currSampleEnd) {296const size_t dmerIndex = FASTCOVER_hashPtrToIndex(ctx->samples + start, f, d);297freqs[dmerIndex]++;298start = start + skip + 1;299}300}301}302303304/**305* Prepare a context for dictionary building.306* The context is only dependent on the parameter `d` and can be used multiple307* times.308* Returns 0 on success or error code on error.309* The context must be destroyed with `FASTCOVER_ctx_destroy()`.310*/311static size_t312FASTCOVER_ctx_init(FASTCOVER_ctx_t* ctx,313const void* samplesBuffer,314const size_t* samplesSizes, unsigned nbSamples,315unsigned d, double splitPoint, unsigned f,316FASTCOVER_accel_t accelParams)317{318const BYTE* const samples = (const BYTE*)samplesBuffer;319const size_t totalSamplesSize = COVER_sum(samplesSizes, nbSamples);320/* Split samples into testing and training sets */321const unsigned nbTrainSamples = splitPoint < 1.0 ? (unsigned)((double)nbSamples * splitPoint) : nbSamples;322const unsigned nbTestSamples = splitPoint < 1.0 ? nbSamples - nbTrainSamples : nbSamples;323const size_t trainingSamplesSize = splitPoint < 1.0 ? COVER_sum(samplesSizes, nbTrainSamples) : totalSamplesSize;324const size_t testSamplesSize = splitPoint < 1.0 ? COVER_sum(samplesSizes + nbTrainSamples, nbTestSamples) : totalSamplesSize;325326/* Checks */327if (totalSamplesSize < MAX(d, sizeof(U64)) ||328totalSamplesSize >= (size_t)FASTCOVER_MAX_SAMPLES_SIZE) {329DISPLAYLEVEL(1, "Total samples size is too large (%u MB), maximum size is %u MB\n",330(unsigned)(totalSamplesSize >> 20), (FASTCOVER_MAX_SAMPLES_SIZE >> 20));331return ERROR(srcSize_wrong);332}333334/* Check if there are at least 5 training samples */335if (nbTrainSamples < 5) {336DISPLAYLEVEL(1, "Total number of training samples is %u and is invalid\n", nbTrainSamples);337return ERROR(srcSize_wrong);338}339340/* Check if there's testing sample */341if (nbTestSamples < 1) {342DISPLAYLEVEL(1, "Total number of testing samples is %u and is invalid.\n", nbTestSamples);343return ERROR(srcSize_wrong);344}345346/* Zero the context */347memset(ctx, 0, sizeof(*ctx));348DISPLAYLEVEL(2, "Training on %u samples of total size %u\n", nbTrainSamples,349(unsigned)trainingSamplesSize);350DISPLAYLEVEL(2, "Testing on %u samples of total size %u\n", nbTestSamples,351(unsigned)testSamplesSize);352353ctx->samples = samples;354ctx->samplesSizes = samplesSizes;355ctx->nbSamples = nbSamples;356ctx->nbTrainSamples = nbTrainSamples;357ctx->nbTestSamples = nbTestSamples;358ctx->nbDmers = trainingSamplesSize - MAX(d, sizeof(U64)) + 1;359ctx->d = d;360ctx->f = f;361ctx->accelParams = accelParams;362363/* The offsets of each file */364ctx->offsets = (size_t*)calloc((nbSamples + 1), sizeof(size_t));365if (ctx->offsets == NULL) {366DISPLAYLEVEL(1, "Failed to allocate scratch buffers \n");367FASTCOVER_ctx_destroy(ctx);368return ERROR(memory_allocation);369}370371/* Fill offsets from the samplesSizes */372{ U32 i;373ctx->offsets[0] = 0;374assert(nbSamples >= 5);375for (i = 1; i <= nbSamples; ++i) {376ctx->offsets[i] = ctx->offsets[i - 1] + samplesSizes[i - 1];377}378}379380/* Initialize frequency array of size 2^f */381ctx->freqs = (U32*)calloc(((U64)1 << f), sizeof(U32));382if (ctx->freqs == NULL) {383DISPLAYLEVEL(1, "Failed to allocate frequency table \n");384FASTCOVER_ctx_destroy(ctx);385return ERROR(memory_allocation);386}387388DISPLAYLEVEL(2, "Computing frequencies\n");389FASTCOVER_computeFrequency(ctx->freqs, ctx);390391return 0;392}393394395/**396* Given the prepared context build the dictionary.397*/398static size_t399FASTCOVER_buildDictionary(const FASTCOVER_ctx_t* ctx,400U32* freqs,401void* dictBuffer, size_t dictBufferCapacity,402ZDICT_cover_params_t parameters,403U16* segmentFreqs)404{405BYTE *const dict = (BYTE *)dictBuffer;406size_t tail = dictBufferCapacity;407/* Divide the data into epochs. We will select one segment from each epoch. */408const COVER_epoch_info_t epochs = COVER_computeEpochs(409(U32)dictBufferCapacity, (U32)ctx->nbDmers, parameters.k, 1);410const size_t maxZeroScoreRun = 10;411size_t zeroScoreRun = 0;412size_t epoch;413DISPLAYLEVEL(2, "Breaking content into %u epochs of size %u\n",414(U32)epochs.num, (U32)epochs.size);415/* Loop through the epochs until there are no more segments or the dictionary416* is full.417*/418for (epoch = 0; tail > 0; epoch = (epoch + 1) % epochs.num) {419const U32 epochBegin = (U32)(epoch * epochs.size);420const U32 epochEnd = epochBegin + epochs.size;421size_t segmentSize;422/* Select a segment */423COVER_segment_t segment = FASTCOVER_selectSegment(424ctx, freqs, epochBegin, epochEnd, parameters, segmentFreqs);425426/* If the segment covers no dmers, then we are out of content.427* There may be new content in other epochs, for continue for some time.428*/429if (segment.score == 0) {430if (++zeroScoreRun >= maxZeroScoreRun) {431break;432}433continue;434}435zeroScoreRun = 0;436437/* Trim the segment if necessary and if it is too small then we are done */438segmentSize = MIN(segment.end - segment.begin + parameters.d - 1, tail);439if (segmentSize < parameters.d) {440break;441}442443/* We fill the dictionary from the back to allow the best segments to be444* referenced with the smallest offsets.445*/446tail -= segmentSize;447memcpy(dict + tail, ctx->samples + segment.begin, segmentSize);448DISPLAYUPDATE(4492, "\r%u%% ",450(unsigned)(((dictBufferCapacity - tail) * 100) / dictBufferCapacity));451}452DISPLAYLEVEL(2, "\r%79s\r", "");453return tail;454}455456/**457* Parameters for FASTCOVER_tryParameters().458*/459typedef struct FASTCOVER_tryParameters_data_s {460const FASTCOVER_ctx_t* ctx;461COVER_best_t* best;462size_t dictBufferCapacity;463ZDICT_cover_params_t parameters;464} FASTCOVER_tryParameters_data_t;465466467/**468* Tries a set of parameters and updates the COVER_best_t with the results.469* This function is thread safe if zstd is compiled with multithreaded support.470* It takes its parameters as an *OWNING* opaque pointer to support threading.471*/472static void FASTCOVER_tryParameters(void* opaque)473{474/* Save parameters as local variables */475FASTCOVER_tryParameters_data_t *const data = (FASTCOVER_tryParameters_data_t*)opaque;476const FASTCOVER_ctx_t *const ctx = data->ctx;477const ZDICT_cover_params_t parameters = data->parameters;478size_t dictBufferCapacity = data->dictBufferCapacity;479size_t totalCompressedSize = ERROR(GENERIC);480/* Initialize array to keep track of frequency of dmer within activeSegment */481U16* segmentFreqs = (U16*)calloc(((U64)1 << ctx->f), sizeof(U16));482/* Allocate space for hash table, dict, and freqs */483BYTE *const dict = (BYTE*)malloc(dictBufferCapacity);484COVER_dictSelection_t selection = COVER_dictSelectionError(ERROR(GENERIC));485U32* freqs = (U32*) malloc(((U64)1 << ctx->f) * sizeof(U32));486if (!segmentFreqs || !dict || !freqs) {487DISPLAYLEVEL(1, "Failed to allocate buffers: out of memory\n");488goto _cleanup;489}490/* Copy the frequencies because we need to modify them */491memcpy(freqs, ctx->freqs, ((U64)1 << ctx->f) * sizeof(U32));492/* Build the dictionary */493{ const size_t tail = FASTCOVER_buildDictionary(ctx, freqs, dict, dictBufferCapacity,494parameters, segmentFreqs);495496const unsigned nbFinalizeSamples = (unsigned)(ctx->nbTrainSamples * ctx->accelParams.finalize / 100);497selection = COVER_selectDict(dict + tail, dictBufferCapacity, dictBufferCapacity - tail,498ctx->samples, ctx->samplesSizes, nbFinalizeSamples, ctx->nbTrainSamples, ctx->nbSamples, parameters, ctx->offsets,499totalCompressedSize);500501if (COVER_dictSelectionIsError(selection)) {502DISPLAYLEVEL(1, "Failed to select dictionary\n");503goto _cleanup;504}505}506_cleanup:507free(dict);508COVER_best_finish(data->best, parameters, selection);509free(data);510free(segmentFreqs);511COVER_dictSelectionFree(selection);512free(freqs);513}514515516static void517FASTCOVER_convertToCoverParams(ZDICT_fastCover_params_t fastCoverParams,518ZDICT_cover_params_t* coverParams)519{520coverParams->k = fastCoverParams.k;521coverParams->d = fastCoverParams.d;522coverParams->steps = fastCoverParams.steps;523coverParams->nbThreads = fastCoverParams.nbThreads;524coverParams->splitPoint = fastCoverParams.splitPoint;525coverParams->zParams = fastCoverParams.zParams;526coverParams->shrinkDict = fastCoverParams.shrinkDict;527}528529530static void531FASTCOVER_convertToFastCoverParams(ZDICT_cover_params_t coverParams,532ZDICT_fastCover_params_t* fastCoverParams,533unsigned f, unsigned accel)534{535fastCoverParams->k = coverParams.k;536fastCoverParams->d = coverParams.d;537fastCoverParams->steps = coverParams.steps;538fastCoverParams->nbThreads = coverParams.nbThreads;539fastCoverParams->splitPoint = coverParams.splitPoint;540fastCoverParams->f = f;541fastCoverParams->accel = accel;542fastCoverParams->zParams = coverParams.zParams;543fastCoverParams->shrinkDict = coverParams.shrinkDict;544}545546547ZDICTLIB_API size_t548ZDICT_trainFromBuffer_fastCover(void* dictBuffer, size_t dictBufferCapacity,549const void* samplesBuffer,550const size_t* samplesSizes, unsigned nbSamples,551ZDICT_fastCover_params_t parameters)552{553BYTE* const dict = (BYTE*)dictBuffer;554FASTCOVER_ctx_t ctx;555ZDICT_cover_params_t coverParams;556FASTCOVER_accel_t accelParams;557/* Initialize global data */558g_displayLevel = (int)parameters.zParams.notificationLevel;559/* Assign splitPoint and f if not provided */560parameters.splitPoint = 1.0;561parameters.f = parameters.f == 0 ? DEFAULT_F : parameters.f;562parameters.accel = parameters.accel == 0 ? DEFAULT_ACCEL : parameters.accel;563/* Convert to cover parameter */564memset(&coverParams, 0 , sizeof(coverParams));565FASTCOVER_convertToCoverParams(parameters, &coverParams);566/* Checks */567if (!FASTCOVER_checkParameters(coverParams, dictBufferCapacity, parameters.f,568parameters.accel)) {569DISPLAYLEVEL(1, "FASTCOVER parameters incorrect\n");570return ERROR(parameter_outOfBound);571}572if (nbSamples == 0) {573DISPLAYLEVEL(1, "FASTCOVER must have at least one input file\n");574return ERROR(srcSize_wrong);575}576if (dictBufferCapacity < ZDICT_DICTSIZE_MIN) {577DISPLAYLEVEL(1, "dictBufferCapacity must be at least %u\n",578ZDICT_DICTSIZE_MIN);579return ERROR(dstSize_tooSmall);580}581/* Assign corresponding FASTCOVER_accel_t to accelParams*/582accelParams = FASTCOVER_defaultAccelParameters[parameters.accel];583/* Initialize context */584{585size_t const initVal = FASTCOVER_ctx_init(&ctx, samplesBuffer, samplesSizes, nbSamples,586coverParams.d, parameters.splitPoint, parameters.f,587accelParams);588if (ZSTD_isError(initVal)) {589DISPLAYLEVEL(1, "Failed to initialize context\n");590return initVal;591}592}593COVER_warnOnSmallCorpus(dictBufferCapacity, ctx.nbDmers, g_displayLevel);594/* Build the dictionary */595DISPLAYLEVEL(2, "Building dictionary\n");596{597/* Initialize array to keep track of frequency of dmer within activeSegment */598U16* segmentFreqs = (U16 *)calloc(((U64)1 << parameters.f), sizeof(U16));599const size_t tail = FASTCOVER_buildDictionary(&ctx, ctx.freqs, dictBuffer,600dictBufferCapacity, coverParams, segmentFreqs);601const unsigned nbFinalizeSamples = (unsigned)(ctx.nbTrainSamples * ctx.accelParams.finalize / 100);602const size_t dictionarySize = ZDICT_finalizeDictionary(603dict, dictBufferCapacity, dict + tail, dictBufferCapacity - tail,604samplesBuffer, samplesSizes, nbFinalizeSamples, coverParams.zParams);605if (!ZSTD_isError(dictionarySize)) {606DISPLAYLEVEL(2, "Constructed dictionary of size %u\n",607(unsigned)dictionarySize);608}609FASTCOVER_ctx_destroy(&ctx);610free(segmentFreqs);611return dictionarySize;612}613}614615616ZDICTLIB_API size_t617ZDICT_optimizeTrainFromBuffer_fastCover(618void* dictBuffer, size_t dictBufferCapacity,619const void* samplesBuffer,620const size_t* samplesSizes, unsigned nbSamples,621ZDICT_fastCover_params_t* parameters)622{623ZDICT_cover_params_t coverParams;624FASTCOVER_accel_t accelParams;625/* constants */626const unsigned nbThreads = parameters->nbThreads;627const double splitPoint =628parameters->splitPoint <= 0.0 ? FASTCOVER_DEFAULT_SPLITPOINT : parameters->splitPoint;629const unsigned kMinD = parameters->d == 0 ? 6 : parameters->d;630const unsigned kMaxD = parameters->d == 0 ? 8 : parameters->d;631const unsigned kMinK = parameters->k == 0 ? 50 : parameters->k;632const unsigned kMaxK = parameters->k == 0 ? 2000 : parameters->k;633const unsigned kSteps = parameters->steps == 0 ? 40 : parameters->steps;634const unsigned kStepSize = MAX((kMaxK - kMinK) / kSteps, 1);635const unsigned kIterations =636(1 + (kMaxD - kMinD) / 2) * (1 + (kMaxK - kMinK) / kStepSize);637const unsigned f = parameters->f == 0 ? DEFAULT_F : parameters->f;638const unsigned accel = parameters->accel == 0 ? DEFAULT_ACCEL : parameters->accel;639const unsigned shrinkDict = 0;640/* Local variables */641const int displayLevel = (int)parameters->zParams.notificationLevel;642unsigned iteration = 1;643unsigned d;644unsigned k;645COVER_best_t best;646POOL_ctx *pool = NULL;647int warned = 0;648/* Checks */649if (splitPoint <= 0 || splitPoint > 1) {650LOCALDISPLAYLEVEL(displayLevel, 1, "Incorrect splitPoint\n");651return ERROR(parameter_outOfBound);652}653if (accel == 0 || accel > FASTCOVER_MAX_ACCEL) {654LOCALDISPLAYLEVEL(displayLevel, 1, "Incorrect accel\n");655return ERROR(parameter_outOfBound);656}657if (kMinK < kMaxD || kMaxK < kMinK) {658LOCALDISPLAYLEVEL(displayLevel, 1, "Incorrect k\n");659return ERROR(parameter_outOfBound);660}661if (nbSamples == 0) {662LOCALDISPLAYLEVEL(displayLevel, 1, "FASTCOVER must have at least one input file\n");663return ERROR(srcSize_wrong);664}665if (dictBufferCapacity < ZDICT_DICTSIZE_MIN) {666LOCALDISPLAYLEVEL(displayLevel, 1, "dictBufferCapacity must be at least %u\n",667ZDICT_DICTSIZE_MIN);668return ERROR(dstSize_tooSmall);669}670if (nbThreads > 1) {671pool = POOL_create(nbThreads, 1);672if (!pool) {673return ERROR(memory_allocation);674}675}676/* Initialization */677COVER_best_init(&best);678memset(&coverParams, 0 , sizeof(coverParams));679FASTCOVER_convertToCoverParams(*parameters, &coverParams);680accelParams = FASTCOVER_defaultAccelParameters[accel];681/* Turn down global display level to clean up display at level 2 and below */682g_displayLevel = displayLevel == 0 ? 0 : displayLevel - 1;683/* Loop through d first because each new value needs a new context */684LOCALDISPLAYLEVEL(displayLevel, 2, "Trying %u different sets of parameters\n",685kIterations);686for (d = kMinD; d <= kMaxD; d += 2) {687/* Initialize the context for this value of d */688FASTCOVER_ctx_t ctx;689LOCALDISPLAYLEVEL(displayLevel, 3, "d=%u\n", d);690{691size_t const initVal = FASTCOVER_ctx_init(&ctx, samplesBuffer, samplesSizes, nbSamples, d, splitPoint, f, accelParams);692if (ZSTD_isError(initVal)) {693LOCALDISPLAYLEVEL(displayLevel, 1, "Failed to initialize context\n");694COVER_best_destroy(&best);695POOL_free(pool);696return initVal;697}698}699if (!warned) {700COVER_warnOnSmallCorpus(dictBufferCapacity, ctx.nbDmers, displayLevel);701warned = 1;702}703/* Loop through k reusing the same context */704for (k = kMinK; k <= kMaxK; k += kStepSize) {705/* Prepare the arguments */706FASTCOVER_tryParameters_data_t *data = (FASTCOVER_tryParameters_data_t *)malloc(707sizeof(FASTCOVER_tryParameters_data_t));708LOCALDISPLAYLEVEL(displayLevel, 3, "k=%u\n", k);709if (!data) {710LOCALDISPLAYLEVEL(displayLevel, 1, "Failed to allocate parameters\n");711COVER_best_destroy(&best);712FASTCOVER_ctx_destroy(&ctx);713POOL_free(pool);714return ERROR(memory_allocation);715}716data->ctx = &ctx;717data->best = &best;718data->dictBufferCapacity = dictBufferCapacity;719data->parameters = coverParams;720data->parameters.k = k;721data->parameters.d = d;722data->parameters.splitPoint = splitPoint;723data->parameters.steps = kSteps;724data->parameters.shrinkDict = shrinkDict;725data->parameters.zParams.notificationLevel = (unsigned)g_displayLevel;726/* Check the parameters */727if (!FASTCOVER_checkParameters(data->parameters, dictBufferCapacity,728data->ctx->f, accel)) {729DISPLAYLEVEL(1, "FASTCOVER parameters incorrect\n");730free(data);731continue;732}733/* Call the function and pass ownership of data to it */734COVER_best_start(&best);735if (pool) {736POOL_add(pool, &FASTCOVER_tryParameters, data);737} else {738FASTCOVER_tryParameters(data);739}740/* Print status */741LOCALDISPLAYUPDATE(displayLevel, 2, "\r%u%% ",742(unsigned)((iteration * 100) / kIterations));743++iteration;744}745COVER_best_wait(&best);746FASTCOVER_ctx_destroy(&ctx);747}748LOCALDISPLAYLEVEL(displayLevel, 2, "\r%79s\r", "");749/* Fill the output buffer and parameters with output of the best parameters */750{751const size_t dictSize = best.dictSize;752if (ZSTD_isError(best.compressedSize)) {753const size_t compressedSize = best.compressedSize;754COVER_best_destroy(&best);755POOL_free(pool);756return compressedSize;757}758FASTCOVER_convertToFastCoverParams(best.parameters, parameters, f, accel);759memcpy(dictBuffer, best.dict, dictSize);760COVER_best_destroy(&best);761POOL_free(pool);762return dictSize;763}764765}766767768