File: arith_dynamic_test.c

package info (click to toggle)
htscodecs 1.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,640 kB
  • sloc: ansic: 15,651; javascript: 2,907; makefile: 330; sh: 184
file content (302 lines) | stat: -rw-r--r-- 8,946 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* Arithmetic coder tests */
/*
 * Copyright (c) 2019,2020 Genome Research Ltd.
 * Author(s): James Bonfield
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following
 *       disclaimer in the documentation and/or other materials provided
 *       with the distribution.
 *
 *    3. Neither the names Genome Research Ltd and Wellcome Trust Sanger
 *       Institute nor the names of its contributors may be used to endorse
 *       or promote products derived from this software without specific
 *       prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH
 * LTD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "config.h"

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <sys/time.h>
#include <limits.h>
#include <fcntl.h>

#include "htscodecs/arith_dynamic.h"

#ifndef BLK_SIZE
// Divisible by 4 for X4
#  define BLK_SIZE 1039*251*4
#endif

// Room to allow for expanded BLK_SIZE on worst case compression.
#define BLK_SIZE2 ((105LL*BLK_SIZE)/100)

static unsigned char *in_buf;

// Max 4GB
static unsigned char *load(FILE *infp, uint32_t *lenp) {
    unsigned char *data = NULL;
    uint32_t dsize = 0;
    uint32_t dcurr = 0;
    signed int len;

    do {
        if (dsize - dcurr < BLK_SIZE) {
            dsize = dsize ? dsize * 2 : BLK_SIZE;
            data = realloc(data, dsize);
        }

        len = fread(data + dcurr, 1, BLK_SIZE, infp);
        if (len > 0)
            dcurr += len;
    } while (len > 0);

    if (len == -1) {
        perror("fread");
    }

    *lenp = dcurr;
    return data;
}

int main(int argc, char **argv) {
    int opt, order = 0;
    int decode = 0, test = 0;
    FILE *infp = stdin, *outfp = stdout;
    struct timeval tv1, tv2, tv3, tv4;
    size_t bytes = 0, raw = 0;

    in_buf = malloc(BLK_SIZE2+257*257*3);

#ifdef _WIN32
        _setmode(_fileno(stdin),  _O_BINARY);
        _setmode(_fileno(stdout), _O_BINARY);
#endif

    extern char *optarg;
    extern int optind;

    while ((opt = getopt(argc, argv, "o:dtr")) != -1) {
        switch (opt) {
        case 'o': {
            char *optend;
            order = strtol(optarg, &optend, 0);
            if (*optend == '.')
                order += atoi(optend+1)<<8;
            break;
        }

        case 'd':
            decode = 1;
            break;
            
        case 't':
            test = 1;
            break;

        case 'r':
            raw = 1;
            break;
        }
    }

    //order = order ? 1 : 0; // Only support O(0) and O(1)

    if (optind < argc) {
        if (!(infp = fopen(argv[optind], "rb"))) {
            perror(argv[optind]);
            return 1;
        }
        optind++;
    }

    if (optind < argc) {
        if (!(outfp = fopen(argv[optind], "wb"))) {
            perror(argv[optind]);
            return 1;
        }
        optind++;
    }

    gettimeofday(&tv1, NULL);

    if (test) {
        size_t len, in_sz = 0, out_sz = 0;
        typedef struct {
            unsigned char *blk;
            uint32_t sz;
        } blocks;
        blocks *b = NULL, *bc = NULL, *bu = NULL;
        int nb = 0, i;
        
        while ((len = fread(in_buf, 1, BLK_SIZE, infp)) != 0) {
            // inefficient, but it'll do for testing
            b = realloc(b, (nb+1)*sizeof(*b));
            bu = realloc(bu, (nb+1)*sizeof(*bu));
            bc = realloc(bc, (nb+1)*sizeof(*bc));
            b[nb].blk = malloc(len);
            b[nb].sz = len;
            memcpy(b[nb].blk, in_buf, len);
            bc[nb].sz = arith_compress_bound(BLK_SIZE, order);
            bc[nb].blk = malloc(bc[nb].sz);
            bu[nb].sz = len;
            bu[nb].blk = malloc(BLK_SIZE);
            nb++;
            in_sz += len;
        }
        fprintf(stderr, "Testing %d blocks\n", nb);

#ifndef NTRIALS
#define NTRIALS 10
#endif
        int trials = NTRIALS;
        while (trials--) {
            // Warmup
            for (i = 0; i < nb; i++) memset(bc[i].blk, 0, bc[i].sz);

            gettimeofday(&tv1, NULL);

            out_sz = 0;
            for (i = 0; i < nb; i++) {
                unsigned int csz = bc[i].sz;
                bc[i].blk = arith_compress_to(b[i].blk, b[i].sz, bc[i].blk, &csz, order);
                assert(csz <= bc[i].sz);
                out_sz += 5 + csz;
            }

            gettimeofday(&tv2, NULL);
            
            // Warmup
            for (i = 0; i < nb; i++) memset(bu[i].blk, 0, BLK_SIZE);

            gettimeofday(&tv3, NULL);

            for (i = 0; i < nb; i++)
                bu[i].blk = arith_uncompress_to(bc[i].blk, bc[i].sz, bu[i].blk, &bu[i].sz);

            gettimeofday(&tv4, NULL);

            for (i = 0; i < nb; i++) {
                if (b[i].sz != bu[i].sz || memcmp(b[i].blk, bu[i].blk, b[i].sz))
                    fprintf(stderr, "Mismatch in block %d, sz %d/%d\n", i, b[i].sz, bu[i].sz);
                //free(bc[i].blk);
                //free(bu[i].blk);
            }

            fprintf(stderr, "%5.1f MB/s enc, %5.1f MB/s dec\t %ld bytes -> %ld bytes\n",
                    (double)in_sz / ((long)(tv2.tv_sec - tv1.tv_sec)*1000000 +
                                     tv2.tv_usec - tv1.tv_usec),
                    (double)in_sz / ((long)(tv4.tv_sec - tv3.tv_sec)*1000000 +
                                     tv4.tv_usec - tv3.tv_usec),
                    (long)in_sz, (long)out_sz);
        }

        exit(0);
        
    }

    if (raw) {
        // One naked / raw block, to match the specification
        uint32_t in_size, out_size;
        unsigned char *in = load(infp, &in_size), *out;
        if (!in) exit(1);

        if (decode) {
            if (!(out = arith_uncompress(in, in_size, &out_size)))
                exit(1);

            fwrite(out, 1, out_size, outfp);
            bytes = out_size;
        } else {
            if (!(out = arith_compress(in, in_size, &out_size, order)))
                exit(1);

            fwrite(out, 1, out_size, outfp);
            bytes += in_size;
        }

        free(in);
        free(out);
    } else {
        // Block based, to permit arbitrarily large data sets.
        if (decode) {
            for (;;) {
                uint32_t in_size, out_size;
                unsigned char *out;

                if (4 != fread(&in_size, 1, 4, infp))
                    break;
                if (in_size > BLK_SIZE)
                    exit(1);

                if (in_size != fread(in_buf, 1, in_size, infp)) {
                    fprintf(stderr, "Truncated input\n");
                    exit(1);
                }
                out = arith_uncompress(in_buf, in_size, &out_size);
                if (!out)
                    exit(1);

                fwrite(out, 1, out_size, outfp);
                fflush(outfp);
                free(out);

                bytes += out_size;
            }
        } else {
            int loop = 0;
            for (;;loop++) {
                uint32_t in_size, out_size;
                unsigned char *out;

                in_size = fread(in_buf, 1, BLK_SIZE, infp);
                if (loop && in_size <= 0)
                    break;

                if (in_size < 4)
                    order &= ~1;

                out = arith_compress(in_buf, in_size, &out_size, order);

                fwrite(&out_size, 1, 4, outfp);
                fwrite(out, 1, out_size, outfp);
                free(out);

                bytes += in_size;
            }
        }
    }

    gettimeofday(&tv2, NULL);

    fprintf(stderr, "Took %ld microseconds, %5.1f MB/s\n",
            (long)(tv2.tv_sec - tv1.tv_sec)*1000000 +
            tv2.tv_usec - tv1.tv_usec,
            (double)bytes / ((long)(tv2.tv_sec - tv1.tv_sec)*1000000 +
                             tv2.tv_usec - tv1.tv_usec));

    free(in_buf);
    return 0;
}