예제 #1
0
int main(int argc, char **argv)
{
	int c, bdev = -1;
	unsigned i, ncache_devices = 0, nbacking_devices = 0;
	char *cache_devices[argc];
	char *backing_devices[argc];

	unsigned block_size = 0, bucket_size = 1024;
	int writeback = 0, discard = 0, wipe_bcache = 0;
	unsigned cache_replacement_policy = 0;
	uint64_t data_offset = BDEV_DATA_START_DEFAULT;
	uuid_t set_uuid;

	uuid_generate(set_uuid);

	struct option opts[] = {
		{ "cache",		0, NULL,	'C' },
		{ "bdev",		0, NULL,	'B' },
		{ "bucket",		1, NULL,	'b' },
		{ "block",		1, NULL,	'w' },
		{ "writeback",		0, &writeback,	1 },
		{ "wipe-bcache",	0, &wipe_bcache,	1 },
		{ "discard",		0, &discard,	1 },
		{ "cache_replacement_policy", 1, NULL, 'p' },
		{ "data_offset",	1, NULL,	'o' },
		{ "cset-uuid",		1, NULL,	'u' },
		{ "help",		0, NULL,	'h' },
		{ NULL,			0, NULL,	0 },
	};

	while ((c = getopt_long(argc, argv,
				"-hCBU:w:b:",
				opts, NULL)) != -1)
		switch (c) {
		case 'C':
			bdev = 0;
			break;
		case 'B':
			bdev = 1;
			break;
		case 'b':
			bucket_size = hatoi_validate(optarg, "bucket size");
			break;
		case 'w':
			block_size = hatoi_validate(optarg, "block size");
			break;
#if 0
		case 'U':
			if (uuid_parse(optarg, sb.uuid)) {
				fprintf(stderr, "Bad uuid\n");
				exit(EXIT_FAILURE);
			}
			break;
#endif
		case 'p':
			cache_replacement_policy = read_string_list(optarg,
						    cache_replacement_policies);
			break;
		case 'o':
			data_offset = atoll(optarg);
			if (data_offset < BDEV_DATA_START_DEFAULT) {
				fprintf(stderr, "Bad data offset; minimum %d sectors\n",
				       BDEV_DATA_START_DEFAULT);
				exit(EXIT_FAILURE);
			}
			break;
		case 'u':
			if (uuid_parse(optarg, set_uuid)) {
				fprintf(stderr, "Bad uuid\n");
				exit(EXIT_FAILURE);
			}
			break;
		case 'h':
			usage();
			break;
		case 1:
			if (bdev == -1) {
				fprintf(stderr, "Please specify -C or -B\n");
				exit(EXIT_FAILURE);
			}

			if (bdev)
				backing_devices[nbacking_devices++] = optarg;
			else
				cache_devices[ncache_devices++] = optarg;
			break;
		}

	if (!ncache_devices && !nbacking_devices) {
		fprintf(stderr, "Please supply a device\n");
		usage();
	}

	if (bucket_size < block_size) {
		fprintf(stderr, "Bucket size cannot be smaller than block size\n");
		exit(EXIT_FAILURE);
	}

	if (!block_size) {
		for (i = 0; i < ncache_devices; i++)
			block_size = max(block_size,
					 get_blocksize(cache_devices[i]));

		for (i = 0; i < nbacking_devices; i++)
			block_size = max(block_size,
					 get_blocksize(backing_devices[i]));
	}

	for (i = 0; i < ncache_devices; i++)
		write_sb(cache_devices[i], block_size, bucket_size,
			 writeback, discard, wipe_bcache,
			 cache_replacement_policy,
			 data_offset, set_uuid, false);

	for (i = 0; i < nbacking_devices; i++)
		write_sb(backing_devices[i], block_size, bucket_size,
			 writeback, discard, wipe_bcache,
			 cache_replacement_policy,
			 data_offset, set_uuid, true);

	return 0;
}
void main_loop(const char** argv)
    {
    //  Process command line arguments.
    map<string, string> params;
    process_cmd_line(argv, params);
    bool verbose = get_bool_param(params, "verbose");

    //  Load feature files for templates.
    //  Get template label from matrix name.
    vector<string> templateLabelList;
    vector<matrix<double> > templateMatList;
    ifstream templateStrm(
        get_required_string_param(params, "template_file").c_str());
    while (templateStrm.peek() != EOF)
        {
        templateMatList.push_back(matrix<double>());
        string labelStr = read_float_matrix(templateStrm,
            templateMatList.back());
        templateLabelList.push_back(labelStr);
        }
    templateStrm.close();
    if (templateMatList.empty())
        throw runtime_error("No templates supplied.");

    //  Load correct label for each feature file, if present.
    vector<string> featLabelList;
    string labelFile = get_string_param(params, "feat_label_list");
    if (!labelFile.empty())
        read_string_list(labelFile, featLabelList);

    //  The main loop.
    ifstream featStrm(
        get_required_string_param(params, "feat_file").c_str());
    matrix<double> feats;
    unsigned templCnt = templateLabelList.size();
    unsigned uttCnt = 0;
    unsigned correctCnt = 0;
    while (featStrm.peek() != EOF)
        {
        int uttIdx = uttCnt++;
        string idStr = read_float_matrix(featStrm, feats);

        //  Find closest template.
        int bestTempl = -1;
        double bestScore = DBL_MAX;
        for (unsigned templIdx = 0; templIdx < templCnt; ++templIdx)
            {
            if (feats.size2() != templateMatList[templIdx].size2())
                throw runtime_error("Mismatch in test/template feature dim.");
            double curScore = compute_distance(feats,
                templateMatList[templIdx]);
            if (verbose)
                cout << format("  %s: %.3f") % templateLabelList[templIdx] %
                    curScore << endl;
            if (curScore < bestScore)
                bestScore = curScore, bestTempl = templIdx;
            }
        if (bestTempl < 0)
            throw runtime_error("No alignments found in DTW.");

        string hypLabel = (bestTempl >= 0) ? templateLabelList[bestTempl] : "";
        if (!featLabelList.empty())
            {
            //  If have reference labels, print ref and hyp classes.
            if (uttIdx >= (int) featLabelList.size())
                throw runtime_error("Mismatch in number of utterances "
                    "and labels.");
            string refLabel = featLabelList[uttIdx];
            cout << format("Reference: %s, Hyp: %s, Correct: %d") %
                refLabel % hypLabel % (hypLabel == refLabel) << endl;
            if (hypLabel == refLabel)
                ++correctCnt;
            }
        else
            //  If don't have reference labels, just print hyp class.
            cout << hypLabel << " (" << idStr << ")" << endl;
        }
    featStrm.close();
    if (!featLabelList.empty())
        {
        //  If have reference labels, print accuracy.
        unsigned errCnt = uttCnt - correctCnt;
        cout << format("Accuracy: %.2f%% (%d/%d), Error rate: %.2f%% (%d/%d)")
            % (100.0 * correctCnt / uttCnt) % correctCnt % uttCnt %
            (100.0 * errCnt / uttCnt) % errCnt % uttCnt << endl;
        }
    }