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
|
/* SPDX-License-Identifier: LGPL-2.1-only */
/* Copyright (C) 2020 Gediminas Jakutis */
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <rin/diagnostic.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, 0, 0, NULL, NULL, mode_normal, array, cached};
static int parseargs(int argc, char **argv, struct settings * settings);
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 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) {
file_in.type = stream_randread;
file_in.n = settings.to;
} else {
file_in.name = settings.filein;
}
load_io_functions(&settings, &file_in);
file_out.name = settings.fileout ? settings.fileout : settings.filein;
try_s((ret = stream_open(&file_in)), out);
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);
if (settings.access == cached) {
try_s((ret = cache_create(&file_in)), out);
try_s((ret = cache_populate(&file_in)), out);
switch (settings.opmode) {
case mode_fetch:
if (settings.format == array) {
try_s((ret = cache_block_copy(&file_in, &file_out)), out);
} else { /* settings.format == list */
try_s((ret = cache_list_copy(&file_in, &file_out)), out);
}
break;
case mode_generate:
try_s((ret = cache_transfer(&file_in, &file_out)), out);
break;
case mode_normal:
/* 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 */
/* TODO */
}
stream_close(&file_in);
stream_close(&file_out);
stream_close(&file_tmp);
while (0) {
out:
stream_close(&file_in);
file_out.type = stream_invalid; /* in case of error-exit, just close, don't link */
stream_close(&file_out);
stream_close(&file_tmp);
}
early_out:
return ret;
}
static int parseargs(int argc, char **argv, struct settings * settings)
{
struct settings s = {0};
ssize_t i;
int ret = 0;
if (argc < 2) {
goto err;
}
for (i = 1; i < argc - 1; ++i) {
if (!(strcmp(argv[i], "--sort"))) {
s.opmode = mode_normal;
} else if (!(strcmp(argv[i], "--fetch"))) {
s.opmode = mode_fetch;
} else if (!(strcmp(argv[i], "--generate"))) {
s.opmode = mode_generate;
} else if (!(strcmp(argv[i], "--array"))) {
s.format = array;
} else if (!(strcmp(argv[i], "--list"))) {
s.format = list;
} else if (!(strcmp(argv[i], "--cache"))) {
s.access = cached;
} else if (!(strcmp(argv[i], "--no-cache"))) {
s.access = direct;
} else if (!(strncmp(argv[i], "--position=", 11))) {
if (strlen(argv[i]) > 11) {
s.ss = strtoul(argv[i] + 11, NULL, 10);
} else {
goto err;
}
} else if (!(strncmp(argv[i], "--num=", 6))) {
if (strlen(argv[i]) > 6) {
s.to = strtoul(argv[i] + 6, NULL, 10);
} else {
goto err;
}
} else if (!(strncmp(argv[i], "--out=", 6))) {
if (strlen(argv[i]) > 6) {
s.fileout = argv[i] + 6;
} else {
goto err;
}
} else {
goto err;
}
}
if (!strncmp(argv[i], "--", 2)) {
goto err;
} else {
s.filein = argv[i];
}
if (!s.fileout) {
s.fileout = s.filein;
}
if (s.opmode == mode_generate) {
/* we always generate in-memory for PERFORMANCE */
s.access = cached;
} else if (s.opmode == mode_fetch) {
s.to = s.ss + s.to;
}
s.stride = s.format == list ? sizeof(struct entry_l) : sizeof(struct entry);
*settings = s;
while (0) {
err:
ret = EINVAL;
printhelp(*argv);
}
return ret;
}
static int load_io_functions(struct settings const * const s, struct stream * const in)
{
int ret = 0;
if (in->type == stream_out) {
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 */
}
} else if (in->type == stream_in) { /* reading streams do not support dumping */
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 */
}
} else if (in->type == stream_randread) { /* data generation streams do not support dumping nor any cache I/O beyond initial data generation */
if (s->format == array) {
in->get_next_element_direct = gen_get_array;
in->place_next_element_cache = cached_put_array;
} else { /* if (s->format == list */
in->get_next_element_direct = gen_get_list;
in->place_next_element_cache = cached_put_list;
}
} else {
ret = EINVAL;
}
return ret;
}
static void printhelp(const char * const name)
{
printf( "This is a mergesort program and such\n"
"\n"
"usage:\t%s [OPTION]... FILE"
"\n"
"Options:\n"
" --sort sort mode (default)\n"
" --fetch do not sort, fetch element(s) instead\n"
" --generate generate an input dataset\n"
" --array use an array data format (default)\n"
" --list use a linked list data format\n"
" --[no]-cache cache data in memory (default) or process data in-place\n"
" --position=<num> fetch element from position <num>\n"
" --num=<num> <num> of elements to fetch/generate\n"
" --out=<name> write output to <name> file\n"
"\n"
"In case more than one same option is provided, the last one take precedence\n"
"\n",
name);
return;
}
static struct entry_l *stub_getnext(struct stream * const restrict in)
{
struct entry_l *ret = NULL;
(void) in;
rin_warn("stub!");
return ret;
}
static int stub_put(struct stream * const restrict in, struct entry_l const * const data)
{
int ret = ENOSYS;
(void) in;
(void) data;
rin_warn("stub!");
return ret;
}
static int stub_split(struct stream * const in, struct stream * const a, struct stream * const b)
{
int ret = ENOSYS;
(void) in;
(void) a;
(void) b;
rin_warn("stub!");
return ret;
}
|