-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommand_line.cu
337 lines (284 loc) · 10.3 KB
/
command_line.cu
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* Firepony
*
* Copyright (c) 2014-2015, NVIDIA CORPORATION
* Copyright (c) 2015, Nuno Subtil <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include "runtime_options.h"
namespace firepony {
struct runtime_options command_line_options;
static void usage(void)
{
fprintf(stderr, "\n");
fprintf(stderr, "usage: firepony <options> <input-file-name>\n");
fprintf(stderr, "\n");
fprintf(stderr, " -r, --reference <genome-file-name> Use <genome-file-name> as reference (required, fasta format)\n");
fprintf(stderr, " -s, --snp-database <dbsnp-file-name> Use <dbsnp-file-name> as a SNP database\n");
fprintf(stderr, " --no-reference-mmap\n");
fprintf(stderr, " --no-snp-database-mmap Do not attempt to use system shared memory for reference or dbSNP\n");
fprintf(stderr, " -d, --debug Enable debugging (*extremely* verbose)\n");
fprintf(stderr, " --disable-rounding Disable rounding on the output tables\n");
fprintf(stderr, " -b, --batch-size <n> Process input in batches of <n> reads\n");
fprintf(stderr, " --mmap Load reference/dbsnp from system shared memory if present\n");
fprintf(stderr, " -v, --verbose Verbose logging\n");
fprintf(stderr, " -o, --output <output-file-name> File to write tabulated output to (default is stdout)\n");
fprintf(stderr, " --gpu-only Use only the CUDA GPU-accelerated backend\n");
fprintf(stderr, " --cpu-only Use only the CPU backend\n");
fprintf(stderr, " --cpu-threads Number of CPU worker threads to run\n");
fprintf(stderr, "\n");
fprintf(stderr, " http://github.com/broadinstitute/firepony\n");
fprintf(stderr, " [email protected]\n");
fprintf(stderr, "\n");
exit(1);
}
// check environment variables for default arguments if present
static void parse_env_vars(void)
{
command_line_options.reference = getenv("FIREPONY_REFERENCE");
if (command_line_options.reference)
{
command_line_options.reference = strdup(command_line_options.reference);
}
command_line_options.snp_database = getenv("FIREPONY_DBSNP");
if (command_line_options.snp_database)
{
command_line_options.snp_database = strdup(command_line_options.snp_database);
}
char *backend = getenv("FIREPONY_BACKEND");
if (backend)
{
if (!strcmp(backend, "cuda"))
{
command_line_options.disable_all_backends();
command_line_options.enable_cuda = true;
}
if (!strcmp(backend, "cpu"))
{
command_line_options.disable_all_backends();
command_line_options.enable_tbb = true;
}
}
char *cpu_threads = getenv("FIREPONY_CPU_THREADS");
if (cpu_threads)
{
errno = 0;
command_line_options.cpu_threads = strtol(cpu_threads, NULL, 10);
if (errno != 0)
{
fprintf(stderr, "WARNING: error parsing FIREPONY_CPU_THREADS\n");
command_line_options.cpu_threads = -1;
}
}
}
void parse_command_line(int argc, char **argv)
{
static const char *options_short = "r:s:db:o:v";
static struct option options_long[] = {
{ "reference", required_argument, NULL, 'r' },
{ "snp-database", required_argument, NULL, 's' },
{ "no-reference-mmap", no_argument, NULL, 'k' },
{ "no-snp-database-mmap", no_argument, NULL, 'l' },
{ "debug", no_argument, NULL, 'd' },
{ "disable-rounding", no_argument, NULL, 'n' },
{ "batch-size", required_argument, NULL, 'b' },
{ "mmap", no_argument, NULL, 'm' },
{ "verbose", no_argument, NULL, 'v' },
{ "output", required_argument, NULL, 'o' },
{ "gpu-only", no_argument, NULL, 'g' },
{ "cpu-only", no_argument, NULL, 'c' },
{ "cpu-threads", required_argument, NULL, 't' },
{ 0 },
};
parse_env_vars();
int ch;
while((ch = getopt_long(argc, argv, options_short, options_long, NULL)) != -1)
{
switch(ch)
{
case 'r':
// --reference, -r
command_line_options.reference = strdup(optarg);
break;
case 's':
// --snp-database, -s
command_line_options.snp_database = strdup(optarg);
break;
case 'k':
// --no-reference-mmap
command_line_options.reference_use_mmap = false;
break;
case 'l':
// --no-snp-database-mmap
command_line_options.snp_database_use_mmap = false;
break;
case 'd':
// -d, --debug
command_line_options.debug = true;
break;
case 'n':
// --disable-rounding
command_line_options.disable_output_rounding = true;
break;
case 'b':
// -b, --batch-size
errno = 0;
command_line_options.batch_size = strtol(optarg, NULL, 10);
if (errno != 0)
{
fprintf(stderr, "error: invalid batch size\n");
usage();
}
break;
case 'm':
// --mmap
command_line_options.try_mmap = true;
break;
case 'v':
// -v, --verbose
command_line_options.verbose = true;
break;
case 'o':
// -o, --output
command_line_options.output = strdup(optarg);
break;
case 'g':
// --gpu-only
command_line_options.disable_all_backends();
command_line_options.enable_cuda = true;
break;
case 'c':
// --cpu-only
command_line_options.disable_all_backends();
command_line_options.enable_tbb = true;
break;
case 't':
// --cpu-threads
errno = 0;
command_line_options.cpu_threads = strtol(optarg, NULL, 10);
if (errno != 0)
{
fprintf(stderr, "error: invalid number of cpu threads\n");
usage();
}
break;
case '?':
case ':':
default:
usage();
break;
}
}
// check that we got required arguments
if (command_line_options.reference == nullptr)
{
fprintf(stderr, "error: missing reference file name\n\n");
usage();
}
if (command_line_options.snp_database == nullptr)
{
fprintf(stderr, "error: missing SNP database file name\n\n");
usage();
}
if (optind == argc)
{
fprintf(stderr, "error: missing input file name\n\n");
usage();
}
if (optind != argc - 1)
{
fprintf(stderr, "error: extraneous arguments\n\n");
usage();
}
command_line_options.input = argv[optind];
}
static void concat(std::string& out, const char *in)
{
if (out.size())
{
out = out + std::string(" ");
}
out = out + std::string(in);
}
std::string canonical_command_line(void)
{
std::string ret = "";
char buf[1024];
snprintf(buf, sizeof(buf), "-r %s", command_line_options.reference);
concat(ret, buf);
snprintf(buf, sizeof(buf), "-s %s", command_line_options.snp_database);
concat(ret, buf);
snprintf(buf, sizeof(buf), "-o %s", command_line_options.output ? command_line_options.output : "-");
concat(ret, buf);
if (command_line_options.batch_size != uint32(-1))
{
snprintf(buf, sizeof(buf), "--batch-size %d", command_line_options.batch_size);
concat(ret, buf);
}
if (command_line_options.debug)
{
concat(ret, "--debug");
}
if (command_line_options.disable_output_rounding)
{
concat(ret, "--disable-rounding");
}
if (!command_line_options.enable_cuda)
{
concat(ret, "--cpu-only");
}
if (!command_line_options.enable_tbb)
{
concat(ret, "--gpu-only");
}
if (command_line_options.cpu_threads != -1)
{
snprintf(buf, sizeof(buf), "--cpu-threads %d", command_line_options.cpu_threads);
concat(ret, buf);
}
if (command_line_options.try_mmap)
{
concat(ret, "--mmap");
if (!command_line_options.reference_use_mmap)
{
concat(ret, "--no-reference-mmap");
}
if (!command_line_options.snp_database_use_mmap)
{
concat(ret, "--no-snp-database-mmap");
}
}
if (command_line_options.verbose)
{
concat(ret, "--verbose");
}
return ret;
}
} // namespace firepony