~ubuntu-branches/ubuntu/raring/sphinxtrain/raring-proposed

« back to all changes in this revision

Viewing changes to src/libs/libio/s3gau_full_io.c

  • Committer: Package Import Robot
  • Author(s): Samuel Thibault
  • Date: 2013-01-02 04:10:21 UTC
  • Revision ID: package-import@ubuntu.com-20130102041021-ynsizmz33fx02hea
Tags: upstream-1.0.8
ImportĀ upstreamĀ versionĀ 1.0.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- c-basic-offset: 4 -*- */
 
2
/* ====================================================================
 
3
 * Copyright (c) 1996-2000 Carnegie Mellon University.  All rights 
 
4
 * reserved.
 
5
 *
 
6
 * Redistribution and use in source and binary forms, with or without
 
7
 * modification, are permitted provided that the following conditions
 
8
 * are met:
 
9
 *
 
10
 * 1. Redistributions of source code must retain the above copyright
 
11
 *    notice, this list of conditions and the following disclaimer. 
 
12
 *
 
13
 * 2. Redistributions in binary form must reproduce the above copyright
 
14
 *    notice, this list of conditions and the following disclaimer in
 
15
 *    the documentation and/or other materials provided with the
 
16
 *    distribution.
 
17
 *
 
18
 * This work was supported in part by funding from the Defense Advanced 
 
19
 * Research Projects Agency and the National Science Foundation of the 
 
20
 * United States of America, and the CMU Sphinx Speech Consortium.
 
21
 *
 
22
 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 
 
23
 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 
24
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
25
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
 
26
 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
27
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 
28
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 
29
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 
30
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 
31
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 
32
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
33
 *
 
34
 * ====================================================================
 
35
 *
 
36
 */
 
37
/*********************************************************************
 
38
 *
 
39
 * File: s3gau_full_io.c
 
40
 * 
 
41
 * Description: 
 
42
 *     Gaussian full covariance matrix file I/O
 
43
 * Author: 
 
44
 *     Eric Thayer (eht@cs.cmu.edu)
 
45
 *     David Huggins-Daines (dhuggins@cs.cmu.edu)
 
46
 *********************************************************************/
 
47
 
 
48
#include <s3/s3gau_io.h>
 
49
#include <s3/s3io.h>
 
50
 
 
51
#include <sphinxbase/matrix.h>
 
52
#include <sphinxbase/ckd_alloc.h>
 
53
#include <s3/s3.h>
 
54
 
 
55
#include <assert.h>
 
56
#include <string.h>
 
57
 
 
58
int
 
59
s3gau_read_maybe_full(const char *fn,
 
60
                      vector_t *****out,
 
61
                      uint32 *out_n_mgau,
 
62
                      uint32 *out_n_feat,
 
63
                      uint32 *out_n_density,
 
64
                      uint32 **out_veclen,
 
65
                      uint32 need_full)
 
66
{
 
67
    FILE *fp;
 
68
    const char *do_chk;
 
69
    const char *ver;
 
70
    uint32 n_mgau, n_feat, n_density;
 
71
    uint32 *veclen, maxveclen;
 
72
    uint32 blk, i, j, k, l, r, n;
 
73
    uint32 chksum = 0;
 
74
    uint32 sv_chksum, ignore = 0;
 
75
    float32 *raw;
 
76
    vector_t ****o;
 
77
    uint32 swap;
 
78
 
 
79
    fp = s3open(fn, "rb", &swap);
 
80
    if (fp == NULL)
 
81
        return S3_ERROR;
 
82
 
 
83
    /* check version id */
 
84
    ver = s3get_gvn_fattr("version");
 
85
    if (ver) {
 
86
        if (strcmp(ver, GAU_FILE_VERSION) != 0) {
 
87
            E_FATAL("Version mismatch for %s, file ver: %s != reader ver: %s\n",
 
88
                    fn, ver, GAU_FILE_VERSION);
 
89
        }
 
90
    }
 
91
    else {
 
92
        E_FATAL("No version attribute for %s\n", fn);
 
93
    }
 
94
    
 
95
    /* if do_chk is non-NULL, there is a checksum after the data in the file */
 
96
    do_chk = s3get_gvn_fattr("chksum0");
 
97
 
 
98
    if (do_chk && !strcmp(do_chk, "no")) {
 
99
        do_chk = NULL;
 
100
    }
 
101
    
 
102
    if (s3read(&n_mgau, sizeof(uint32), 1, fp, swap, &chksum) != 1) {
 
103
        goto error;
 
104
    }
 
105
 
 
106
    if (s3read(&n_feat, sizeof(uint32), 1, fp, swap, &chksum) != 1) {
 
107
        goto error;
 
108
    }
 
109
 
 
110
    if (s3read(&n_density, sizeof(uint32), 1, fp, swap, &chksum) != 1) {
 
111
        goto error;
 
112
    }
 
113
 
 
114
    veclen = ckd_calloc(n_feat, sizeof(uint32));
 
115
    if (s3read(veclen, sizeof(uint32), n_feat, fp, swap, &chksum) != n_feat) {
 
116
        goto error;
 
117
    }
 
118
 
 
119
    if (s3read_1d((void **)&raw, sizeof(float32), &n, fp, swap, &chksum) != S3_SUCCESS) {
 
120
        ckd_free(veclen);
 
121
 
 
122
        goto error;
 
123
    }
 
124
 
 
125
    for (i = 0, blk = 0, maxveclen = 0; i < n_feat; i++) {
 
126
        blk += veclen[i] * veclen[i];
 
127
        if (veclen[i] > maxveclen) maxveclen = veclen[i];
 
128
    }
 
129
    if (n != n_mgau * n_density * blk) {
 
130
        if (need_full)
 
131
             E_ERROR("Failed to read full covariance file %s (expected %d values, got %d)\n",
 
132
                     fn, n_mgau * n_density * blk, n);
 
133
        goto error;
 
134
    }
 
135
 
 
136
    o = (vector_t ****)ckd_calloc_4d(n_mgau, n_feat, n_density,
 
137
                                     maxveclen, sizeof(vector_t));
 
138
 
 
139
    for (i = 0, r = 0; i < n_mgau; i++) {
 
140
        for (j = 0; j < n_feat; j++) {
 
141
            for (k = 0; k < n_density; k++) {
 
142
                for (l = 0; l < veclen[j]; l++) {
 
143
                    o[i][j][k][l] = &raw[r];
 
144
 
 
145
                    r += veclen[j];
 
146
                }
 
147
            }
 
148
        }
 
149
    }
 
150
 
 
151
    if (do_chk) {
 
152
        /* See if the checksum in the file matches that which
 
153
           was computed from the read data */
 
154
 
 
155
        if (s3read(&sv_chksum, sizeof(uint32), 1, fp, swap, &ignore) != 1) {
 
156
            goto error;
 
157
        }
 
158
 
 
159
        if (sv_chksum != chksum) {
 
160
            E_FATAL("Checksum error; read corrupt data.\n");
 
161
        }
 
162
    }
 
163
 
 
164
 
 
165
    *out = o;
 
166
    *out_n_mgau = n_mgau;
 
167
    *out_n_feat = n_feat;
 
168
    *out_n_density = n_density;
 
169
    *out_veclen = veclen;
 
170
 
 
171
    s3close(fp);
 
172
 
 
173
    E_INFO("Read %s [%ux%ux%u array of full matrices]\n",
 
174
           fn, n_mgau, n_feat, n_density);
 
175
 
 
176
    return S3_SUCCESS;
 
177
 
 
178
error:
 
179
    if (fp) s3close(fp);
 
180
 
 
181
    return S3_ERROR;
 
182
}
 
183
 
 
184
int
 
185
s3gau_read_full(const char *fn,
 
186
                vector_t *****out,
 
187
                uint32 *out_n_mgau,
 
188
                uint32 *out_n_feat,
 
189
                uint32 *out_n_density,
 
190
                uint32 **out_veclen)
 
191
{
 
192
    return s3gau_read_maybe_full(fn, out, out_n_mgau, out_n_feat, 
 
193
                                 out_n_density, out_veclen, TRUE);
 
194
}
 
195
 
 
196
int
 
197
s3gau_write_full(const char *fn,
 
198
                 const vector_t ****out,
 
199
                 uint32 n_mgau,
 
200
                 uint32 n_feat,
 
201
                 uint32 n_density,
 
202
                 const uint32 *veclen)
 
203
{
 
204
    FILE *fp = NULL;
 
205
    uint32 chksum = 0;
 
206
    uint32 blk, i, ignore = 0;
 
207
 
 
208
    s3clr_fattr();
 
209
    s3add_fattr("version", GAU_FILE_VERSION, TRUE);
 
210
    s3add_fattr("chksum0", "yes", TRUE);
 
211
 
 
212
    fp = s3open(fn, "wb", NULL);
 
213
    if (fp == NULL)
 
214
        return S3_ERROR;
 
215
 
 
216
    if (s3write(&n_mgau, sizeof(uint32), 1, fp, &chksum) != 1) {
 
217
        goto error;
 
218
    }
 
219
 
 
220
    if (s3write(&n_feat, sizeof(uint32), 1, fp, &chksum) != 1) {
 
221
        goto error;
 
222
    }
 
223
 
 
224
    if (s3write(&n_density, sizeof(uint32), 1, fp, &chksum) != 1) {
 
225
        goto error;
 
226
    }
 
227
 
 
228
    if (s3write(veclen, sizeof(uint32), n_feat, fp, &chksum) != n_feat) {
 
229
        goto error;
 
230
    }
 
231
 
 
232
    for (blk = 0, i = 0; i < n_feat; i++)
 
233
        blk += veclen[i] * veclen[i];
 
234
 
 
235
    if (s3write_1d(out[0][0][0][0], sizeof(float32),
 
236
                   n_mgau*n_density*blk, fp, &chksum) != S3_SUCCESS) {
 
237
        goto error;
 
238
    }
 
239
 
 
240
    if (s3write(&chksum, sizeof(uint32), 1, fp, &ignore) != 1) {
 
241
        goto error;
 
242
    }
 
243
 
 
244
    s3close(fp);
 
245
 
 
246
    E_INFO("Wrote %s [%ux%ux%u array of full matrices]\n",
 
247
           fn, n_mgau, n_feat, n_density);
 
248
 
 
249
    return S3_SUCCESS;
 
250
 
 
251
error:
 
252
    if (fp) s3close(fp);
 
253
    return S3_ERROR;
 
254
}
 
255
 
 
256
int
 
257
s3gaucnt_read_full(const char *fn,
 
258
                   vector_t ****out_wt_mean,
 
259
                   vector_t *****out_wt_var,
 
260
                   int32 *out_pass2var,
 
261
                   float32 ****out_dnom,
 
262
                   uint32 *out_n_cb,
 
263
                   uint32 *out_n_feat,
 
264
                   uint32 *out_n_density,
 
265
                   uint32 **out_veclen)
 
266
{
 
267
    uint32 rd_chksum = 0;
 
268
    uint32 sv_chksum;
 
269
    uint32 ignore;
 
270
    char *ver;
 
271
    char *do_chk;
 
272
    FILE *fp;
 
273
    uint32 swap;
 
274
 
 
275
    uint32 has_means;
 
276
    uint32 has_vars;
 
277
    uint32 pass2var;
 
278
    uint32 n_cb;
 
279
    uint32 n_feat;
 
280
    uint32 n_density;
 
281
    uint32 *veclen;
 
282
    float32 *buf;
 
283
    float32 ***dnom;
 
284
    uint32 n, i, b_i, j, k, l, d1, d2, d3;
 
285
    vector_t ***wt_mean = NULL;
 
286
    vector_t ****wt_var = NULL;
 
287
 
 
288
    fp = s3open(fn, "rb", &swap);
 
289
    if (fp == NULL)
 
290
        return S3_ERROR;
 
291
 
 
292
    /* check version id */
 
293
    ver = s3get_gvn_fattr("version");
 
294
    if (ver) {
 
295
        if (strcmp(ver, GAUCNT_FILE_VERSION) != 0) {
 
296
            E_FATAL("Version mismatch for %s, file ver: %s != reader ver: %s\n",
 
297
                    fn, ver, GAUCNT_FILE_VERSION);
 
298
        }
 
299
    }
 
300
    else {
 
301
        E_FATAL("No version attribute for %s\n", fn);
 
302
    }
 
303
    
 
304
    /* if do_chk is non-NULL, there is a checksum after the data in the file */
 
305
    do_chk = s3get_gvn_fattr("chksum0");
 
306
 
 
307
    if (s3read((void *)&has_means, sizeof(uint32), 1, fp, swap, &rd_chksum) != 1) {
 
308
        return S3_ERROR;
 
309
    }
 
310
 
 
311
    if (s3read((void *)&has_vars, sizeof(uint32), 1, fp, swap, &rd_chksum) != 1) {
 
312
        return S3_ERROR;
 
313
    }
 
314
 
 
315
    if (s3read((void *)&pass2var, sizeof(uint32), 1, fp, swap, &rd_chksum) != 1) {
 
316
        return S3_ERROR;
 
317
    }
 
318
 
 
319
    if (s3read((void *)&n_cb, sizeof(uint32), 1, fp, swap, &rd_chksum) != 1) {
 
320
        return S3_ERROR;
 
321
    }
 
322
 
 
323
    if (s3read((void *)&n_density, sizeof(uint32), 1, fp, swap, &rd_chksum) != 1) {
 
324
        return S3_ERROR;
 
325
    }
 
326
 
 
327
    if (s3read_1d((void **)&veclen, sizeof(uint32), &n_feat, fp, swap, &rd_chksum) != S3_SUCCESS) {
 
328
        return S3_ERROR;
 
329
    }
 
330
 
 
331
    if (has_means) {
 
332
        if (s3read_1d((void *)&buf, sizeof(float32), &n, fp, swap, &rd_chksum) != S3_SUCCESS) {
 
333
            return S3_ERROR;
 
334
        }
 
335
        
 
336
        wt_mean = (vector_t ***)ckd_calloc_3d(n_cb, n_feat, n_density, sizeof(vector_t));
 
337
 
 
338
        for (i = 0, b_i = 0; i < n_cb; i++) {
 
339
            for (j = 0; j < n_feat; j++) {
 
340
                for (k = 0; k < n_density; k++) {
 
341
                    wt_mean[i][j][k] = &buf[b_i];
 
342
 
 
343
                    b_i += veclen[j];
 
344
                }
 
345
            }
 
346
        }
 
347
    }
 
348
 
 
349
    if (has_vars) {
 
350
        uint32 blk, maxveclen;
 
351
 
 
352
        for (i = 0, blk = 0, maxveclen = 0; i < n_feat; i++) {
 
353
            blk += veclen[i];
 
354
            if (veclen[i] > maxveclen) maxveclen = veclen[i];
 
355
        }
 
356
 
 
357
        if (s3read_1d((void *)&buf, sizeof(float32), &n, fp, swap, &rd_chksum) != S3_SUCCESS) {
 
358
            return S3_ERROR;
 
359
        }
 
360
        assert(n == n_cb * n_density * blk * blk);
 
361
        
 
362
        wt_var = (vector_t ****)ckd_calloc_4d(n_cb, n_feat, n_density,
 
363
                                              maxveclen, sizeof(vector_t));
 
364
 
 
365
        for (i = 0, b_i = 0; i < n_cb; i++) {
 
366
            for (j = 0; j < n_feat; j++) {
 
367
                for (k = 0; k < n_density; k++) {
 
368
                    for (l = 0; l < veclen[j]; l++) {
 
369
                        wt_var[i][j][k][l] = &buf[b_i];
 
370
 
 
371
                        b_i += veclen[j];
 
372
                    }
 
373
                }
 
374
            }
 
375
        }
 
376
    }
 
377
 
 
378
    if (s3read_3d((void ****)&dnom, sizeof(float32), &d1, &d2, &d3, fp, swap, &rd_chksum) != S3_SUCCESS) {
 
379
        return S3_ERROR;
 
380
    }
 
381
 
 
382
    assert(d1 == n_cb);
 
383
    assert(d2 == n_feat);
 
384
    assert(d3 == n_density);
 
385
 
 
386
    if (do_chk) {
 
387
        /* See if the checksum in the file matches that which
 
388
           was computed from the read data */
 
389
        
 
390
        if (s3read(&sv_chksum, sizeof(uint32), 1, fp, swap, &ignore) != 1) {
 
391
            s3close(fp);
 
392
            return S3_ERROR;
 
393
        }
 
394
        
 
395
        if (sv_chksum != rd_chksum) {
 
396
            E_FATAL("Checksum error; read corrupt data.\n");
 
397
        }
 
398
    }
 
399
    
 
400
    s3close(fp);
 
401
 
 
402
    *out_wt_mean = wt_mean;
 
403
    *out_wt_var = wt_var;
 
404
    *out_pass2var = pass2var;
 
405
    *out_dnom = dnom;
 
406
    *out_n_cb = n_cb;
 
407
    *out_n_feat = n_feat;
 
408
    *out_n_density = n_density;
 
409
    *out_veclen = veclen;
 
410
 
 
411
    E_INFO("Read %s%s%s%s [%ux%ux%u vector arrays]\n",
 
412
           fn,
 
413
           (has_means ? " with means" : ""),
 
414
           (has_vars ? " with vars" : ""),
 
415
           (has_vars && pass2var ? " (2pass)" : ""),
 
416
           n_cb, n_feat, n_density);
 
417
 
 
418
    return S3_SUCCESS;
 
419
}
 
420
 
 
421
int
 
422
s3gaucnt_write_full(const char *fn,
 
423
                    vector_t ***wt_mean,
 
424
                    vector_t ****wt_var,
 
425
                    int32 pass2var,
 
426
                    float32 ***dnom,
 
427
                    uint32 n_cb,
 
428
                    uint32 n_feat,
 
429
                    uint32 n_density,
 
430
                    const uint32 *veclen)
 
431
{
 
432
    FILE *fp;
 
433
    uint32 chksum = 0;
 
434
    uint32 ignore = 0;
 
435
    uint32 n_elem, blk, j, has_means, has_vars;
 
436
 
 
437
    s3clr_fattr();
 
438
    s3add_fattr("version", GAUCNT_FILE_VERSION, TRUE);
 
439
    s3add_fattr("chksum0", "yes", TRUE);
 
440
 
 
441
    fp = s3open(fn, "wb", NULL);
 
442
    if (fp == NULL)
 
443
        return S3_ERROR;
 
444
 
 
445
 
 
446
    if (wt_mean != NULL)
 
447
        has_means = TRUE;
 
448
    else
 
449
        has_means = FALSE;
 
450
    if (s3write((void *)&has_means, sizeof(uint32), 1, fp, &chksum) != 1) {
 
451
        return S3_ERROR;
 
452
    }
 
453
 
 
454
    if (wt_var != NULL)
 
455
        has_vars = TRUE;
 
456
    else
 
457
        has_vars = FALSE;
 
458
    if (s3write((void *)&has_vars, sizeof(uint32), 1, fp, &chksum) != 1) {
 
459
        return S3_ERROR;
 
460
    }
 
461
    if (s3write((void *)&pass2var, sizeof(uint32), 1, fp, &chksum) != 1) {
 
462
        return S3_ERROR;
 
463
    }
 
464
    if (s3write((void *)&n_cb, sizeof(uint32), 1, fp, &chksum) != 1) {
 
465
        return S3_ERROR;
 
466
    }
 
467
    if (s3write((void *)&n_density, sizeof(uint32), 1, fp, &chksum) != 1) {
 
468
        return S3_ERROR;
 
469
    }
 
470
 
 
471
    if (s3write_1d((void *)veclen, sizeof(uint32), n_feat, fp, &chksum) != S3_SUCCESS) {
 
472
        return S3_ERROR;
 
473
    }
 
474
    
 
475
    for (j = 0, blk = 0; j < n_feat; j++)
 
476
        blk += veclen[j];
 
477
 
 
478
    n_elem = n_cb * n_density * blk;
 
479
 
 
480
    if (has_means) {
 
481
        band_nz_1d(wt_mean[0][0][0], n_elem, MIN_POS_FLOAT32);
 
482
        
 
483
        if (s3write_1d((void *)wt_mean[0][0][0], sizeof(float32), n_elem, fp, &chksum) != S3_SUCCESS)
 
484
            return S3_ERROR;
 
485
    }
 
486
 
 
487
    if (has_vars) {
 
488
        /* Don't floor full variances!!! */
 
489
        if (s3write_1d((void *)wt_var[0][0][0][0], sizeof(float32),
 
490
                       n_elem * blk, fp, &chksum) != S3_SUCCESS)
 
491
            return S3_ERROR;
 
492
    }
 
493
 
 
494
    /* floor all non-zero entries to this value to make sure
 
495
       that results are compatible between machines */
 
496
    floor_nz_3d(dnom, n_cb, n_feat, n_density, MIN_POS_FLOAT32);
 
497
 
 
498
    if (s3write_3d((void ***)dnom, sizeof(float32),
 
499
                   n_cb, n_feat, n_density, fp, &chksum) != S3_SUCCESS) {
 
500
        return S3_ERROR;
 
501
    }
 
502
 
 
503
    if (s3write(&chksum, sizeof(uint32), 1, fp, &ignore) != 1) {
 
504
        s3close(fp);
 
505
        
 
506
        return S3_ERROR;
 
507
    }
 
508
        
 
509
    s3close(fp);
 
510
 
 
511
    E_INFO("Wrote %s%s%s%s [%ux%ux%u vector/matrix arrays]\n",
 
512
           fn,
 
513
           (has_means ? " with means" : ""),
 
514
           (has_vars ? " with full vars" : ""),
 
515
           (has_vars && pass2var ? " (2pass)" : ""),
 
516
           n_cb, n_feat, n_density);
 
517
 
 
518
    return S3_SUCCESS;
 
519
}
 
520
/*
 
521
 * Log record.  Maintained by RCS.
 
522
 *
 
523
 * $Log$
 
524
 * 
 
525
 *
 
526
 */
 
527
 
 
528
 
 
529