Example #1
0
/*
 * Dispatches the given algorithm to compute the sum of the values in
 * the array.  If the algorithm name is unknown then the usage string
 * is printed and the program exits.
 *
 * Returns 0 on success and 1 on failure.
 */
static unsigned int do_sum(FILE * outfile, const char *const alg,
			   double ary[], unsigned int n)
{
    double result = -1;
    double start_time, end_time;
    unsigned int err;

    start_time = get_current_seconds();

    if (strcmp(alg, seq_str) == 0)
	err = do_seq_sum(ary, n, &result);
    else if (strcmp(alg, sort_str) == 0)
	err = do_sorted_sum(ary, n, &result);
    else if (strcmp(alg, min2_scan_str) == 0)
	err = do_min_two_sum(ary, n, &result);
    else if (strcmp(alg, heap_str) == 0)
	err = do_heap_sum(ary, n, &result);
    else
	usage();

    if (!err) {
	end_time = get_current_seconds();
	fprintf(stderr, "%f\n", end_time - start_time);
	fprintf(outfile, "%f\n", result);
    }

    return err;
}
Example #2
0
/*
 * Loads the tokens from the files and calls the output_diff() routine
 * to actually output the differences.
 */
static int align_files(char *f0, char *f1, compute_alignment c){
    int err = 0;
    char *ary0, *ary1;
    unsigned int n0, n1;
    double start, end;

    printf("Diffing %s and %s.\n", f0, f1);

    err = load_token_arrays(f0, f1, &ary0, &n0, &ary1, &n1);
    if (err) return 1;

    printf("%s contains %u characters.\n", f0, n0);
    printf("%s contains %u characters.\n", f1, n1);

    start = get_current_seconds();

    if (c != NULL) err = c(ary0,n0,ary1,n1);
    else err = 1;

    end = get_current_seconds();
    fprintf(stdout, "time: %f seconds\n", end - start);

    free(ary0);
    free(ary1);

    return err;
}
Example #3
0
/* Read the user's input and call the search. */
static int input_and_search(FILE * infile, struct node nodes[],
			    unsigned int nnodes)
{
    int err = 0;
    unsigned int s, t;
    unsigned int cost = 0;
    double start, end;

    while (fscanf(infile, "%u %u", &s, &t) == 2) {
	s = s - 1; /* avoiding 1-indexing */
	t = t - 1;
	if (s >= nnodes) {
	    fprintf(stderr, "Start node is invalid\n");
	    continue;
	}
	if (t >= nnodes) {
	    fprintf(stderr, "Target node is invalid\n");
	    continue;
	}
	printf("finding a route from %d to %d\n", s, t);
	start = get_current_seconds();
	err = djikstra(stdout, nodes, nnodes, s, t, &cost);
	end = get_current_seconds();
	if (err)
	    break;
	printf("cost: %u\n", cost);
	printf("time: %f seconds\n", end - start);
    }

    return err;
}
Example #4
0
int main(int argc, const char *const argv[])
{
    int err, ret = EXIT_FAILURE;
    FILE *f, *infile = stdin;
    double start, end;
    unsigned int nnodes;
    struct node *nodes;

    if (argc != 3)
	usage();

    f = fopen(argv[1], "r");
    if (!f) {
	perror("Error opening data file");
	return EXIT_FAILURE;
    }
    if (strcmp(argv[2], "-") != 0) {
	infile = fopen(argv[2], "r");
	if (!infile) {
	    perror("Error opening data file");
	    fclose(f);
	    return EXIT_FAILURE;
	}
    }

    start = get_current_seconds();
    err = load_map(f, &nodes, &nnodes);
    if (err)
	goto out_map;
    end = get_current_seconds();
    fclose(f);
    if (err)
	return EXIT_FAILURE;
    printf("Loaded %d nodes in %f seconds\n", nnodes, end - start);
    printf("Using %d MB\n", peak_memory_usage());

    err = input_and_search(infile, nodes, nnodes);
    if (err)
	goto out;

    printf("Peak memory usage %d MB\n", peak_memory_usage());

    ret = EXIT_SUCCESS;
  out:
    free_map(nodes, nnodes);
  out_map:
    if (infile != stderr)
	fclose(infile);
    return ret;
}
Example #5
0
/*
 * Reads the values, begins the timer, calls the sorting algorithm,
 * stops the timer and outputs the values.  The time taken is printed
 * to standard error.
 */
static unsigned int do_sort(const char *const algorithm, FILE * infile,
			    FILE * outfile)
{
    int err = 0;
    double start, end;
    unsigned long n, nbits;
    unsigned long *ary;

    ary = get_values(infile, &n, &nbits);
    if (!ary)
	return 1;

    start = get_current_seconds();

    if (strcmp(algorithm, counting_sort_str) == 0) {
	err = do_counting_sort(ary, n, nbits);

    } else if (strcmp(algorithm, radix_sort_str) == 0) {
	err = do_radix_sort(ary, n, nbits);

    } else if (strcmp(algorithm, quick_sort_str) == 0) {
	err = do_quicksort(ary, n, nbits);

    } else if (strcmp(algorithm, insertion_sort_str) == 0) {
	err = do_insertion_sort(ary, n, nbits);

    } else if (strcmp(algorithm, system_quick_sort_str) == 0) {
	err = do_system_quicksort(ary, n, nbits);

    } else {
	fprintf(stderr, "Impossible\n");
	exit(EXIT_FAILURE);
    }

    end = get_current_seconds();

    output_from_array(outfile, ary, n);
    fprintf(stderr, "%f\n", end - start);

    free(ary);

    return err;
}