diff options
author | 2021-02-21 13:16:54 +0200 | |
---|---|---|
committer | 2021-02-21 13:19:06 +0200 | |
commit | 29712a5098842ea3930ec00ddd1c0b9d264ba9b5 (patch) | |
tree | 3744b445acb971342be18fa6d2c9c4c20a470e1d /src/main.c | |
parent | 26ab990a747ab675bcff32a40734dcb61468f652 (diff) | |
download | algos-ld1-29712a5098842ea3930ec00ddd1c0b9d264ba9b5.tar.gz algos-ld1-29712a5098842ea3930ec00ddd1c0b9d264ba9b5.tar.bz2 algos-ld1-29712a5098842ea3930ec00ddd1c0b9d264ba9b5.zip |
in-memory array sorting: GET!
lists need a bit more work and after that, in-file should shortly
follow.
Signed-off-by: Gediminas Jakutis <gediminas@varciai.lt>
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 74 |
1 files changed, 47 insertions, 27 deletions
@@ -8,32 +8,48 @@ #include <stdio.h> #include <string.h> #include <rin/diagnostic.h> -#include "io.h" +#include <rin/time.h> +#include "stream.h" #include "defs.h" #include "cache.h" #include "datagen.h" #include "mergesort.h" -static struct settings settings = {0}; +static struct settings settings = {0, 0, 0, NULL, NULL, mode_normal, array, cached}; static int parseargs(int argc, char **argv, struct settings * settings); -int load_io_functions(struct settings const * const s, struct stream * const in); -void printhelp(const char * const name); -struct entry_l *stub_getnext(struct stream * const restrict in); +static int load_io_functions(struct settings const * const s, struct stream * const in); +static void printhelp(const char * const name); +static struct entry_l *stub_getnext(struct stream * const restrict in); static int stub_put(struct stream * const restrict in, struct entry_l const * const data); static int stub_split(struct stream * const in, struct stream * const a, struct stream * const b); -static int stub_flush(struct stream * const in); -/* these need to go AFTER the stub declarations */ -static struct stream file_in = {stream_blank, .type = stream_in}; -static struct stream file_out = {stream_blank, .type = stream_out}; -static struct stream file_tmp = {stream_blank, .type = stream_outlite}; +static const struct stream stream_blank = { + .n = 0, .type = stream_invalid, .fd = -1, .settings = &settings, + .name = NULL, .index = 0, .pnode = NULL, .cnode = NULL, .cache = NULL, + .get_next_element_direct = stub_getnext, + .get_next_element_cache = stub_getnext, + .place_next_element_direct = stub_put, + .place_next_element_cache = stub_put, .split = stub_split }; + +static struct stream file_in = stream_blank; +static struct stream file_out = stream_blank; +static struct stream file_tmp; + +static struct rin_bench_result bongholio; int main(int argc, char **argv) { int ret = 0; rin_diag_init(); + rin_diag_format(rin_diag_info, "C:mn"); + rin_diag_channel_set_enabled_state(rin_diag_err, 0); + + file_in.type = stream_in; + file_out.type = stream_out; + file_tmp.type = stream_outlite; + try_s((ret = parseargs(argc, argv, &settings)), early_out); if (settings.opmode == mode_generate) { @@ -47,7 +63,7 @@ int main(int argc, char **argv) file_out.name = settings.fileout ? settings.fileout : settings.filein; try_s((ret = stream_open(&file_in)), out); - file_out.n = file_in.n - settings.ss; + file_out.n = settings.opmode == mode_normal ? file_in.n : file_in.n - settings.ss; try_s((ret = stream_open(&file_out)), out); load_io_functions(&settings, &file_out); @@ -68,8 +84,18 @@ int main(int argc, char **argv) try_s((ret = cache_transfer(&file_in, &file_out)), out); break; case mode_normal: - try_s((ret = cache_create(&file_out)), out); - try_s((ret = merge_sort(&file_in, &file_out)), out); + + /* BENCHMARK STARTS HERE */ + rin_bench_start(); + try_s((ret = merge_sort(&file_in, &file_tmp)), out); + rin_bench_stop(&bongholio); + /* BENCHMARK ENDS HERE */ + rin_info("wall: %lus%6luµs", bongholio.wall.tv_sec, bongholio.wall.tv_nsec / 1000); + rin_info("system: %lus%6luµs", bongholio.system.tv_sec, bongholio.system.tv_usec); + rin_info("user: %lus%6luµs", bongholio.user.tv_sec, bongholio.user.tv_usec); + rin_info("total: %lus%6luµs", bongholio.total.tv_sec, bongholio.total.tv_usec); + + try_s((ret = cache_transfer(&file_tmp, &file_out)), out); } } else { /* uncached */ @@ -78,6 +104,7 @@ int main(int argc, char **argv) stream_close(&file_in); stream_close(&file_out); + stream_close(&file_tmp); while (0) { out: @@ -170,7 +197,7 @@ err: return ret; } -int load_io_functions(struct settings const * const s, struct stream * const in) +static int load_io_functions(struct settings const * const s, struct stream * const in) { int ret = 0; @@ -178,6 +205,8 @@ int load_io_functions(struct settings const * const s, struct stream * const in) if (s->format == array) { in->get_next_element_cache = cached_get_array; in->place_next_element_cache = cached_put_array; + in->split = cache_block_split; + in->rewind = cache_rewind; } else { /* if (s->format == list */ /* TODO */ } @@ -185,6 +214,8 @@ int load_io_functions(struct settings const * const s, struct stream * const in) if (s->format == array) { in->get_next_element_cache = cached_get_array; in->place_next_element_cache = cached_put_array; + in->split = cache_block_split; + in->rewind = cache_rewind; } else { /* if (s->format == list */ /* TODO */ } @@ -203,7 +234,7 @@ int load_io_functions(struct settings const * const s, struct stream * const in) return ret; } -void printhelp(const char * const name) +static void printhelp(const char * const name) { printf( "This is a mergesort program and such\n" "\n" @@ -226,7 +257,7 @@ void printhelp(const char * const name) return; } -struct entry_l *stub_getnext(struct stream * const restrict in) +static struct entry_l *stub_getnext(struct stream * const restrict in) { struct entry_l *ret = NULL; @@ -261,14 +292,3 @@ static int stub_split(struct stream * const in, struct stream * const a, struct return ret; } - -static int stub_flush(struct stream * const in) -{ - int ret = ENOSYS; - - (void) in; - - rin_warn("stub!"); - - return ret; -} |