-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
137 lines (104 loc) · 3.46 KB
/
util.c
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
/*
* Author: Chris Wailes <[email protected]>
* Project: Parallel Linear Program Solver
* Date: 2011/10/26
* Description: Utility functions.
*/
// Standard Includes
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
// Project Includes
#include "util.h"
// Forward Declarations
static inline bool check_option(char* opt, char* lng, char* srt);
static inline void init_config(void);
static inline void print_help(void);
static inline void print_help_item(char* lng, char* srt, char* help);
// Globals
extern config_t cfg;
// Functions
void get_config(int argc, char** argv) {
int index;
// Make sure that a filename is provided.
if (argc < 2) {
print_help();
exit(0);
}
// Initialize configuration.
init_config();
for (index = 1; index < argc; ++index) {
// Default Options
if (check_option(argv[index], "--verbose", "-v")) {
cfg.verbose = TRUE;
} else if (check_option(argv[index], "--help", "-h")) {
print_help();
exit(0);
// Project Options
} else if (check_option(argv[index], "--parallel", "-p")) {
if (strncmp(argv[index + 1], "auto", 4) == 0) {
cfg.pmode = AUTO;
} else if (strncmp(argv[index + 1], "omp", 2) == 0) {
cfg.pmode = OMP;
} else if (strncmp(argv[index + 1], "pthreads", 8) == 0) {
cfg.pmode = PTHREADS;
}
++index;
} else if (check_option(argv[index], "--limit", "-l")) {
cfg.simplex_limit = atoi(argv[++index]);
} else if (check_option(argv[index], "--mathprog", "-m")) {
cfg.mathprog_filename = argv[index + 1];
++index;
} else if (check_option(argv[index], "--rule", "-r")) {
if (!strcmp(argv[++index], "blands")) {
cfg.blands = TRUE;
} else if (!strcmp(argv[index], "profys")) {
cfg.profys = TRUE;
} else if (!strcmp(argv[index], "none")) {
cfg.profys = FALSE;
}
} else if (check_option(argv[index], "--very-verbose", "-vv")) {
cfg.verbose = cfg.vv = TRUE;
}
}
if (access(argv[argc - 1], R_OK) != 0) {
printf("Can't read file %s\n", argv[argc - 1]);
exit(-1);
} else {
cfg.filename = argv[argc - 1];
}
}
static inline bool check_option(char* opt, char* lng, char* srt) {
return ((strcmp(opt, lng) == 0) || (strcmp(opt, srt) == 0));
}
static inline void init_config(void) {
cfg.blands = FALSE;
cfg.init_done = FALSE;
cfg.mathprog_filename = 0;
cfg.method = GS;
cfg.ncpus = sysconf(_SC_NPROCESSORS_ONLN);
cfg.pmode = NONE;
cfg.profys = TRUE;
cfg.simplex_limit = 0;
cfg.verbose = FALSE;
cfg.vv = FALSE;
}
static inline void print_help(void) {
printf("Usage: ./superlp [options] <filename>\n\n");
printf("Options:\n");
//Help for Project options.
print_help_item("parallel", "p", "Specifies parallelism mode. Must be mp, pthreads, gpu, or none.");
print_help_item("rule", "r", "Specifies the termination rule to use. Choices are: none, blands, or profy. Default is none.");
print_help_item("limit", "l", "Set the maximum number of iterations to perform during simplex.");
// Help for Debug outputs.
print_help_item("mathprog", "m", "Outputs mathprog compatible file. Must specify filename.");
// Help for Default options.
print_help_item("verbose", "v", "Be verbose.");
print_help_item("very_verbose", "vv", "Be VERY verbose. Implies verbose.");
print_help_item("help", "h", "Print this message.");
}
static inline void print_help_item(char* lng, char* srt, char* help) {
printf("\t--%s, -%s\t%s\n", lng, srt, help);
}