Back to Cutlass

CUTLASS: command_line.h Source File

docs/command__line_8h_source.html

4.4.215.9 KB
Original Source

| | 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

52CommandLine(int argc, const char** argv) {

53using namespace std;

54

55for (int i = 1; i < argc; i++) {

56string arg = argv[i];

57

58if ((arg[0] != '-') || (arg[1] != '-')) {

59 args.push_back(arg);

60continue;

61 }

62

63 string::size_type pos;

64string key, val;

65if ((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

81bool check_cmd_line_flag(const char* arg_name) const {

82using namespace std;

83

84for (int i = 0; i < int(keys.size()); ++i) {

85if (keys[i] == string(arg_name)) return true;

86 }

87return false;

88 }

89

93template <typename value_t>

94int num_naked_args() const {

95return args.size();

96 }

97

101template <typename value_t>

102void get_cmd_line_argument(int index, value_t& val) const {

103using namespace std;

104if (index < args.size()) {

105 istringstream str_stream(args[index]);

106 str_stream >> val;

107 }

108 }

109

113void get_cmd_line_argument(const char* arg_name, bool& val, bool _default = true) const {

114 val = _default;

115if (check_cmd_line_flag(arg_name)) {

116 std::string value;

117get_cmd_line_argument(arg_name, value);

118

119 val = !(value == "0" || value == "false");

120 }

121 }

122

126template <typename value_t>

127void get_cmd_line_argument(const char* arg_name,

128 value_t& val,

129 value_t const& _default = value_t()) const {

130using namespace std;

131

132 val = _default;

133

134for (int i = 0; i < int(keys.size()); ++i) {

135if (keys[i] == string(arg_name)) {

136 istringstream str_stream(values[i]);

137 str_stream >> val;

138 }

139 }

140 }

141

145template <typename value_t>

146void get_cmd_line_arguments(const char* arg_name,

147 std::vector<value_t>& vals,

148char sep = ',') const {

149using namespace std;

150

151if (check_cmd_line_flag(arg_name)) {

152// Clear any default values

153 vals.clear();

154

155// Recover from multi-value string

156for (int i = 0; i < keys.size(); ++i) {

157if (keys[i] == string(arg_name)) {

158string val_string(values[i]);

159separate_string(val_string, vals, sep);

160 }

161 }

162 }

163 }

164

169void get_cmd_line_argument_pairs(const char* arg_name,

170 std::vector<std::pair<std::string, std::string> >& tokens,

171char delim = ',',

172char sep = ':') const {

173if (check_cmd_line_flag(arg_name)) {

174 std::string value;

175get_cmd_line_argument(arg_name, value);

176

177tokenize(tokens, value, delim, sep);

178 }

179 }

180

185void get_cmd_line_argument_ranges(const char* arg_name,

186 std::vector<std::vector<std::string> >& vals,

187char delim = ',',

188char sep = ':') const {

189 std::vector<std::string> ranges;

190get_cmd_line_arguments(arg_name, ranges, delim);

191

192for (std::vector<std::string>::const_iterator range = ranges.begin();

193 range != ranges.end(); ++range) {

194

195 std::vector<std::string> range_vals;

196separate_string(*range, range_vals, sep);

197 vals.push_back(range_vals);

198 }

199 }

200

204int parsed_argc() const { return (int)keys.size(); }

205

206//-------------------------------------------------------------------------

207// Utility functions

208//-------------------------------------------------------------------------

209

211static void tokenize(std::vector<std::pair<std::string, std::string> >& tokens,

212 std::string const& str,

213char delim = ',',

214char sep = ':') {

215// Home-built to avoid Boost dependency

216size_t s_idx = 0;

217size_t d_idx = std::string::npos;

218while (s_idx < str.size()) {

219 d_idx = str.find_first_of(delim, s_idx);

220

221size_t end_idx = (d_idx != std::string::npos ? d_idx : str.size());

222size_t sep_idx = str.find_first_of(sep, s_idx);

223size_t offset = 1;

224if (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

239static void tokenize(std::vector<std::string>& tokens,

240 std::string const& str,

241char delim = ',',

242char sep = ':') {

243typedef std::vector<std::pair<std::string, std::string> > TokenVector;

244typedef TokenVector::const_iterator token_iterator;

245

246 std::vector<std::pair<std::string, std::string> > token_pairs;

247tokenize(token_pairs, str, delim, sep);

248for (token_iterator tok = token_pairs.begin(); tok != token_pairs.end(); ++tok) {

249 tokens.push_back(tok->first);

250 }

251 }

252

253template <typename value_t>

254static void separate_string(std::string const& str,

255 std::vector<value_t>& vals,

256char 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;

263while ((new_pos = str.find(sep, old_pos)) != std::string::npos) {

264if (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

cutlass

Definition: aligned_buffer.h:35

cutlass::CommandLine::get_cmd_line_argument

void get_cmd_line_argument(const char *arg_name, value_t &val, value_t const &_default=value_t()) const

Definition: command_line.h:127

cutlass::CommandLine::get_cmd_line_argument_pairs

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

std

STL namespace.

cutlass::CommandLine::get_cmd_line_arguments

void get_cmd_line_arguments(const char *arg_name, std::vector< value_t > &vals, char sep= ',') const

Definition: command_line.h:146

cutlass::CommandLine::tokenize

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 ':'.

Definition: command_line.h:239

cutlass::CommandLine::check_cmd_line_flag

bool check_cmd_line_flag(const char *arg_name) const

Definition: command_line.h:81

cutlass::CommandLine::keys

std::vector< std::string > keys

Definition: command_line.h:45

cutlass::arg

CUTLASS_HOST_DEVICE T arg(complex< T > const &z)

Returns the magnitude of the complex number.

Definition: complex.h:319

cutlass::CommandLine::tokenize

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 ':'.

Definition: command_line.h:211

cutlass::CommandLine::get_cmd_line_argument

void get_cmd_line_argument(int index, value_t &val) const

Definition: command_line.h:102

cutlass::CommandLine::get_cmd_line_argument

void get_cmd_line_argument(const char *arg_name, bool &val, bool _default=true) const

Definition: command_line.h:113

cutlass::CommandLine::num_naked_args

int num_naked_args() const

Definition: command_line.h:94

cutlass::CommandLine::get_cmd_line_argument_ranges

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

cutlass::CommandLine::values

std::vector< std::string > values

Definition: command_line.h:46

cutlass::CommandLine::CommandLine

CommandLine(int argc, const char **argv)

Definition: command_line.h:52

cutlass::CommandLine::args

std::vector< std::string > args

Definition: command_line.h:47

cutlass::CommandLine

Definition: command_line.h:44

cutlass::CommandLine::separate_string

static void separate_string(std::string const &str, std::vector< value_t > &vals, char sep= ',')

Definition: command_line.h:254

cutlass::CommandLine::parsed_argc

int parsed_argc() const

Definition: command_line.h:204


Generated by 1.8.11