Exemplo n.º 1
0
        /*! ZDICT_optimizeTrainFromBuffer_fastCover():
         * The same requirements as above hold for all the parameters except `parameters`.
         * This function tries many parameter combinations (specifically, k and d combinations)
         * and picks the best parameters. `*parameters` is filled with the best parameters found,
         * dictionary constructed with those parameters is stored in `dictBuffer`.
         * All of the parameters d, k, steps, f, and accel are optional.
         * If d is non-zero then we don't check multiple values of d, otherwise we check d = {6, 8}.
         * if steps is zero it defaults to its default value.
         * If k is non-zero then we don't check multiple values of k, otherwise we check steps values in [50, 2000].
         * If f is zero, default value of 20 is used.
         * If accel is zero, default value of 1 is used.
         *
         * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
         *          or an error code, which can be tested with ZDICT_isError().
         *          On success `*parameters` contains the parameters selected.
         *          See ZDICT_trainFromBuffer() for details on failure modes.
         * Note: ZDICT_optimizeTrainFromBuffer_fastCover() requires about 6 * 2^f bytes of memory for each thread.
         */
        public static nuint ZDICT_optimizeTrainFromBuffer_fastCover(void *dictBuffer, nuint dictBufferCapacity, void *samplesBuffer, nuint *samplesSizes, uint nbSamples, ZDICT_fastCover_params_t *parameters)
        {
            ZDICT_cover_params_t coverParams;
            FASTCOVER_accel_t    accelParams;
            uint         nbThreads    = parameters->nbThreads;
            double       splitPoint   = parameters->splitPoint <= 0.0 ? 0.75 : parameters->splitPoint;
            uint         kMinD        = (uint)(parameters->d == 0 ? 6 : parameters->d);
            uint         kMaxD        = (uint)(parameters->d == 0 ? 8 : parameters->d);
            uint         kMinK        = (uint)(parameters->k == 0 ? 50 : parameters->k);
            uint         kMaxK        = (uint)(parameters->k == 0 ? 2000 : parameters->k);
            uint         kSteps       = (uint)(parameters->steps == 0 ? 40 : parameters->steps);
            uint         kStepSize    = (((kMaxK - kMinK) / kSteps) > (1) ? ((kMaxK - kMinK) / kSteps) : (1));
            uint         kIterations  = (1 + (kMaxD - kMinD) / 2) * (1 + (kMaxK - kMinK) / kStepSize);
            uint         f            = (uint)(parameters->f == 0 ? 20 : parameters->f);
            uint         accel        = (uint)(parameters->accel == 0 ? 1 : parameters->accel);
            uint         shrinkDict   = 0;
            int          displayLevel = (int)parameters->zParams.notificationLevel;
            uint         iteration    = 1;
            uint         d;
            uint         k;
            COVER_best_s best;
            int          warned = 0;

            if (splitPoint <= 0 || splitPoint > 1)
            {
                if (displayLevel >= 1)
                {
                    ;
                }

                return(unchecked ((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)));
            }

            if (accel == 0 || accel > 10)
            {
                if (displayLevel >= 1)
                {
                    ;
                }

                return(unchecked ((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)));
            }

            if (kMinK < kMaxD || kMaxK < kMinK)
            {
                if (displayLevel >= 1)
                {
                    ;
                }

                return(unchecked ((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)));
            }

            if (nbSamples == 0)
            {
                if (displayLevel >= 1)
                {
                    ;
                }

                return(unchecked ((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong)));
            }

            if (dictBufferCapacity < 256)
            {
                if (displayLevel >= 1)
                {
                    ;
                }

                return(unchecked ((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)));
            }

            if (nbThreads > 1)
            {
                throw new NotImplementedException("Multiple threads are not supported");
            }

            COVER_best_init(&best);
            memset((void *)&coverParams, 0, (nuint)(sizeof(ZDICT_cover_params_t)));
            FASTCOVER_convertToCoverParams(*parameters, &coverParams);
            accelParams    = FASTCOVER_defaultAccelParameters[accel];
            g_displayLevel = displayLevel == 0 ? 0 : displayLevel - 1;
            if (displayLevel >= 2)
            {
                ;
            }

            for (d = kMinD; d <= kMaxD; d += 2)
            {
                FASTCOVER_ctx_t ctx;

                if (displayLevel >= 3)
                {
                    ;
                }


                {
                    nuint initVal = FASTCOVER_ctx_init(&ctx, samplesBuffer, samplesSizes, nbSamples, d, splitPoint, f, accelParams);

                    if ((ERR_isError(initVal)) != 0)
                    {
                        if (displayLevel >= 1)
                        {
                            ;
                        }

                        COVER_best_destroy(&best);
                        return(initVal);
                    }
                }

                if (warned == 0)
                {
                    COVER_warnOnSmallCorpus(dictBufferCapacity, ctx.nbDmers, displayLevel);
                    warned = 1;
                }

                for (k = kMinK; k <= kMaxK; k += kStepSize)
                {
                    FASTCOVER_tryParameters_data_s *data = (FASTCOVER_tryParameters_data_s *)(malloc((nuint)(sizeof(FASTCOVER_tryParameters_data_s))));

                    if (displayLevel >= 3)
                    {
                        ;
                    }

                    if (data == null)
                    {
                        if (displayLevel >= 1)
                        {
                            ;
                        }

                        COVER_best_destroy(&best);
                        FASTCOVER_ctx_destroy(&ctx);
                        return(unchecked ((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation)));
                    }

                    data->ctx  = &ctx;
                    data->best = &best;
                    data->dictBufferCapacity    = dictBufferCapacity;
                    data->parameters            = coverParams;
                    data->parameters.k          = k;
                    data->parameters.d          = d;
                    data->parameters.splitPoint = splitPoint;
                    data->parameters.steps      = kSteps;
                    data->parameters.shrinkDict = shrinkDict;
                    data->parameters.zParams.notificationLevel = (uint)g_displayLevel;
                    if ((FASTCOVER_checkParameters(data->parameters, dictBufferCapacity, data->ctx->f, accel)) == 0)
                    {
                        free((void *)data);
                        continue;
                    }

                    COVER_best_start(&best);
                    FASTCOVER_tryParameters((void *)data);

                    ++iteration;
                }

                COVER_best_wait(&best);
                FASTCOVER_ctx_destroy(&ctx);
            }

            if (displayLevel >= 2)
            {
                ;
            }


            {
                nuint dictSize = best.dictSize;

                if ((ERR_isError(best.compressedSize)) != 0)
                {
                    nuint compressedSize = best.compressedSize;

                    COVER_best_destroy(&best);
                    return(compressedSize);
                }

                FASTCOVER_convertToFastCoverParams(best.parameters, parameters, f, accel);
                memcpy(dictBuffer, best.dict, dictSize);
                COVER_best_destroy(&best);
                return(dictSize);
            }
        }
Exemplo n.º 2
0
 private static void FASTCOVER_convertToFastCoverParams(ZDICT_cover_params_t coverParams, ZDICT_fastCover_params_t *fastCoverParams, uint f, uint accel)
 {
     fastCoverParams->k          = coverParams.k;
     fastCoverParams->d          = coverParams.d;
     fastCoverParams->steps      = coverParams.steps;
     fastCoverParams->nbThreads  = coverParams.nbThreads;
     fastCoverParams->splitPoint = coverParams.splitPoint;
     fastCoverParams->f          = f;
     fastCoverParams->accel      = accel;
     fastCoverParams->zParams    = coverParams.zParams;
     fastCoverParams->shrinkDict = coverParams.shrinkDict;
 }