CUTLASS
CUDA Templates for Linear Algebra Subroutines and Solvers
command_line.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (c) 2011-2019, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are not permitted.
6  *
7  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
8  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
10  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
11  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
12  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
13  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
14  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
16  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  ******************************************************************************/
19 
20 #pragma once
21 
27 #include <iostream>
28 #include <limits>
29 #include <sstream>
30 #include <string>
31 #include <vector>
32 
33 #include <cuda_runtime.h>
34 
35 namespace cutlass {
36 
37 /******************************************************************************
38  * command_line
39  ******************************************************************************/
40 
44 struct CommandLine {
45  std::vector<std::string> keys;
46  std::vector<std::string> values;
47  std::vector<std::string> args;
48 
52  CommandLine(int argc, const char** argv) {
53  using namespace std;
54 
55  for (int i = 1; i < argc; i++) {
56  string arg = argv[i];
57 
58  if ((arg[0] != '-') || (arg[1] != '-')) {
59  args.push_back(arg);
60  continue;
61  }
62 
63  string::size_type pos;
64  string key, val;
65  if ((pos = arg.find('=')) == string::npos) {
66  key = string(arg, 2, arg.length() - 2);
67  val = "";
68  } else {
69  key = string(arg, 2, pos - 2);
70  val = string(arg, pos + 1, arg.length() - 1);
71  }
72 
73  keys.push_back(key);
74  values.push_back(val);
75  }
76  }
77 
81  bool check_cmd_line_flag(const char* arg_name) const {
82  using namespace std;
83 
84  for (int i = 0; i < int(keys.size()); ++i) {
85  if (keys[i] == string(arg_name)) return true;
86  }
87  return false;
88  }
89 
93  template <typename value_t>
94  int num_naked_args() const {
95  return args.size();
96  }
97 
101  template <typename value_t>
102  void get_cmd_line_argument(int index, value_t& val) const {
103  using namespace std;
104  if (index < args.size()) {
105  istringstream str_stream(args[index]);
106  str_stream >> val;
107  }
108  }
109 
113  void get_cmd_line_argument(const char* arg_name, bool& val, bool _default = true) const {
114  val = _default;
115  if (check_cmd_line_flag(arg_name)) {
116  std::string value;
117  get_cmd_line_argument(arg_name, value);
118 
119  val = !(value == "0" || value == "false");
120  }
121  }
122 
126  template <typename value_t>
127  void get_cmd_line_argument(const char* arg_name,
128  value_t& val,
129  value_t const& _default = value_t()) const {
130  using namespace std;
131 
132  val = _default;
133 
134  for (int i = 0; i < int(keys.size()); ++i) {
135  if (keys[i] == string(arg_name)) {
136  istringstream str_stream(values[i]);
137  str_stream >> val;
138  }
139  }
140  }
141 
145  template <typename value_t>
146  void get_cmd_line_arguments(const char* arg_name,
147  std::vector<value_t>& vals,
148  char sep = ',') const {
149  using namespace std;
150 
151  if (check_cmd_line_flag(arg_name)) {
152  // Clear any default values
153  vals.clear();
154 
155  // Recover from multi-value string
156  for (int i = 0; i < keys.size(); ++i) {
157  if (keys[i] == string(arg_name)) {
158  string val_string(values[i]);
159  separate_string(val_string, vals, sep);
160  }
161  }
162  }
163  }
164 
169  void get_cmd_line_argument_pairs(const char* arg_name,
170  std::vector<std::pair<std::string, std::string> >& tokens,
171  char delim = ',',
172  char sep = ':') const {
173  if (check_cmd_line_flag(arg_name)) {
174  std::string value;
175  get_cmd_line_argument(arg_name, value);
176 
177  tokenize(tokens, value, delim, sep);
178  }
179  }
180 
185  void get_cmd_line_argument_ranges(const char* arg_name,
186  std::vector<std::vector<std::string> >& vals,
187  char delim = ',',
188  char sep = ':') const {
189  std::vector<std::string> ranges;
190  get_cmd_line_arguments(arg_name, ranges, delim);
191 
192  for (std::vector<std::string>::const_iterator range = ranges.begin();
193  range != ranges.end(); ++range) {
194 
195  std::vector<std::string> range_vals;
196  separate_string(*range, range_vals, sep);
197  vals.push_back(range_vals);
198  }
199  }
200 
204  int parsed_argc() const { return (int)keys.size(); }
205 
206  //-------------------------------------------------------------------------
207  // Utility functions
208  //-------------------------------------------------------------------------
209 
211  static void tokenize(std::vector<std::pair<std::string, std::string> >& tokens,
212  std::string const& str,
213  char delim = ',',
214  char sep = ':') {
215  // Home-built to avoid Boost dependency
216  size_t s_idx = 0;
217  size_t d_idx = std::string::npos;
218  while (s_idx < str.size()) {
219  d_idx = str.find_first_of(delim, s_idx);
220 
221  size_t end_idx = (d_idx != std::string::npos ? d_idx : str.size());
222  size_t sep_idx = str.find_first_of(sep, s_idx);
223  size_t offset = 1;
224  if (sep_idx == std::string::npos || sep_idx >= end_idx) {
225  sep_idx = end_idx;
226  offset = 0;
227  }
228 
229  std::pair<std::string, std::string> item(
230  str.substr(s_idx, sep_idx - s_idx),
231  str.substr(sep_idx + offset, end_idx - sep_idx - offset));
232 
233  tokens.push_back(item);
234  s_idx = end_idx + 1;
235  }
236  }
237 
239  static void tokenize(std::vector<std::string>& tokens,
240  std::string const& str,
241  char delim = ',',
242  char sep = ':') {
243  typedef std::vector<std::pair<std::string, std::string> > TokenVector;
244  typedef TokenVector::const_iterator token_iterator;
245 
246  std::vector<std::pair<std::string, std::string> > token_pairs;
247  tokenize(token_pairs, str, delim, sep);
248  for (token_iterator tok = token_pairs.begin(); tok != token_pairs.end(); ++tok) {
249  tokens.push_back(tok->first);
250  }
251  }
252 
253  template <typename value_t>
254  static void separate_string(std::string const& str,
255  std::vector<value_t>& vals,
256  char sep = ',') {
257  std::istringstream str_stream(str);
258  std::string::size_type old_pos = 0;
259  std::string::size_type new_pos = 0;
260 
261  // Iterate <sep>-delimited values
262  value_t val;
263  while ((new_pos = str.find(sep, old_pos)) != std::string::npos) {
264  if (new_pos != old_pos) {
265  str_stream.width(new_pos - old_pos);
266  str_stream >> val;
267  vals.push_back(val);
268  }
269 
270  // skip over delimiter
271  str_stream.ignore(1);
272  old_pos = new_pos + 1;
273  }
274 
275  // Read last value
276  str_stream >> val;
277  vals.push_back(val);
278  }
279 };
280 
281 } // namespace cutlass
Definition: aligned_buffer.h:35
void get_cmd_line_argument(const char *arg_name, value_t &val, value_t const &_default=value_t()) const
Definition: command_line.h:127
void get_cmd_line_argument_pairs(const char *arg_name, std::vector< std::pair< std::string, std::string > > &tokens, char delim= ',', char sep= ':') const
Definition: command_line.h:169
STL namespace.
void get_cmd_line_arguments(const char *arg_name, std::vector< value_t > &vals, char sep= ',') const
Definition: command_line.h:146
static void tokenize(std::vector< std::string > &tokens, std::string const &str, char delim= ',', char sep= ':')
Tokenizes a comma-delimited list of string pairs delimited by &#39;:&#39;.
Definition: command_line.h:239
bool check_cmd_line_flag(const char *arg_name) const
Definition: command_line.h:81
std::vector< std::string > keys
Definition: command_line.h:45
CUTLASS_HOST_DEVICE T arg(complex< T > const &z)
Returns the magnitude of the complex number.
Definition: complex.h:319
static void tokenize(std::vector< std::pair< std::string, std::string > > &tokens, std::string const &str, char delim= ',', char sep= ':')
Tokenizes a comma-delimited list of string pairs delimited by &#39;:&#39;.
Definition: command_line.h:211
void get_cmd_line_argument(int index, value_t &val) const
Definition: command_line.h:102
void get_cmd_line_argument(const char *arg_name, bool &val, bool _default=true) const
Definition: command_line.h:113
int num_naked_args() const
Definition: command_line.h:94
void get_cmd_line_argument_ranges(const char *arg_name, std::vector< std::vector< std::string > > &vals, char delim= ',', char sep= ':') const
Definition: command_line.h:185
std::vector< std::string > values
Definition: command_line.h:46
CommandLine(int argc, const char **argv)
Definition: command_line.h:52
std::vector< std::string > args
Definition: command_line.h:47
Definition: command_line.h:44
static void separate_string(std::string const &str, std::vector< value_t > &vals, char sep= ',')
Definition: command_line.h:254
int parsed_argc() const
Definition: command_line.h:204