博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c++ 模版编程,解析输入命令argv,argc
阅读量:4210 次
发布时间:2019-05-26

本文共 5241 字,大约阅读时间需要 17 分钟。

下面的代码是从 CUB中摘录出来的。

/** * Utility for parsing command line arguments */struct CommandLineArgs{    std::vector
keys; std::vector
values; std::vector
args; cudaDeviceProp deviceProp; float device_giga_bandwidth; size_t device_free_physmem; size_t device_total_physmem; /** * Constructor */ CommandLineArgs(int argc, char **argv) : keys(10), values(10) { using namespace std; // Initialize mersenne generator unsigned int mersenne_init[4]= {0x123, 0x234, 0x345, 0x456}; mersenne::init_by_array(mersenne_init, 4); for (int i = 1; i < argc; i++) { string arg = argv[i]; if ((arg[0] != '-') || (arg[1] != '-')) { args.push_back(arg); continue; } string::size_type pos; string key, val; if ((pos = arg.find('=')) == string::npos) { key = string(arg, 2, arg.length() - 2); val = ""; } else { key = string(arg, 2, pos - 2); val = string(arg, pos + 1, arg.length() - 1); } keys.push_back(key); values.push_back(val); } } /** * Checks whether a flag "--
" is present in the commandline */ bool CheckCmdLineFlag(const char* arg_name) { using namespace std; for (int i = 0; i < int(keys.size()); ++i) { if (keys[i] == string(arg_name)) return true; } return false; } /** * Returns number of naked (non-flag and non-key-value) commandline parameters */ template
int NumNakedArgs() { return args.size(); } /** * Returns the commandline parameter for a given index (not including flags) */ template
void GetCmdLineArgument(int index, T &val) { using namespace std; if (index < args.size()) { istringstream str_stream(args[index]); str_stream >> val; } } /** * Returns the value specified for a given commandline parameter --
=
*/ template
void GetCmdLineArgument(const char *arg_name, T &val) { using namespace std; for (int i = 0; i < int(keys.size()); ++i) { if (keys[i] == string(arg_name)) { istringstream str_stream(values[i]); str_stream >> val; } } } /** * Returns the values specified for a given commandline parameter --
=
,
* */ template
void GetCmdLineArguments(const char *arg_name, std::vector
&vals) { using namespace std; if (CheckCmdLineFlag(arg_name)) { // Clear any default values vals.clear(); // Recover from multi-value string for (int i = 0; i < keys.size(); ++i) { if (keys[i] == string(arg_name)) { string val_string(values[i]); istringstream str_stream(val_string); string::size_type old_pos = 0; string::size_type new_pos = 0; // Iterate comma-separated values T val; while ((new_pos = val_string.find(',', old_pos)) != string::npos) { if (new_pos != old_pos) { str_stream.width(new_pos - old_pos); str_stream >> val; vals.push_back(val); } // skip over comma str_stream.ignore(1); old_pos = new_pos + 1; } // Read last value str_stream >> val; vals.push_back(val); } } } } /** * The number of pairs parsed */ int ParsedArgc() { return (int) keys.size(); } /** * Initialize device */ cudaError_t DeviceInit(int dev = -1) { cudaError_t error = cudaSuccess; do { int deviceCount; error = CubDebug(cudaGetDeviceCount(&deviceCount)); if (error) break; if (deviceCount == 0) { fprintf(stderr, "No devices supporting CUDA.\n"); exit(1); } if (dev < 0) { GetCmdLineArgument("device", dev); } if ((dev > deviceCount - 1) || (dev < 0)) { dev = 0; } error = CubDebug(cudaSetDevice(dev)); if (error) break; CubDebugExit(cudaMemGetInfo(&device_free_physmem, &device_total_physmem)); int ptx_version; error = CubDebug(cub::PtxVersion(ptx_version)); if (error) break; error = CubDebug(cudaGetDeviceProperties(&deviceProp, dev)); if (error) break; if (deviceProp.major < 1) { fprintf(stderr, "Device does not support CUDA.\n"); exit(1); } device_giga_bandwidth = float(deviceProp.memoryBusWidth) * deviceProp.memoryClockRate * 2 / 8 / 1000 / 1000; if (!CheckCmdLineFlag("quiet")) { printf( "Using device %d: %s (PTX version %d, SM%d, %d SMs, " "%lld free / %lld total MB physmem, " "%.3f GB/s @ %d kHz mem clock, ECC %s)\n", dev, deviceProp.name, ptx_version, deviceProp.major * 100 + deviceProp.minor * 10, deviceProp.multiProcessorCount, (unsigned long long) device_free_physmem / 1024 / 1024, (unsigned long long) device_total_physmem / 1024 / 1024, device_giga_bandwidth, deviceProp.memoryClockRate, (deviceProp.ECCEnabled) ? "on" : "off"); fflush(stdout); } } while (0); return error; }};

使用:

int mian(int argc,char *argv){    int num_items = 150;    // Initialize command line    CommandLineArgs args(argc, argv);    g_verbose = args.CheckCmdLineFlag("v");    args.GetCmdLineArgument("n", num_items);    // Print usage    if (args.CheckCmdLineFlag("help"))    {        printf("%s "               "[--n= "               "[--device=
] " "[--v] " "\n", argv[0]); exit(0); }return 0;}

转载地址:http://quwmi.baihongyu.com/

你可能感兴趣的文章
Unicode,ANSI,UTF-8的故事
查看>>
ANSI、Unicode、UTF-8、DBCS等字符集及相关数据类型、函数的区别
查看>>
Linux学习记录--vim与vi常用命令
查看>>
Linux学习记录--shell介绍
查看>>
Linux学习记录--shell变量
查看>>
Linux学习记录--命名别名与历史命令
查看>>
Linux学习记录--数据流重定向
查看>>
Linux学习记录--管道命令
查看>>
Linux学习记录--正则表达式与其应用
查看>>
Linux学习记录--文件特殊权限
查看>>
Linux学习记录--ACL权限控制
查看>>
Linux学习记录--文件权限相关汇总
查看>>
Linux学习记录--shell script
查看>>
Linux学习记录--工作管理与进程管理
查看>>
Linux学习记录--服务
查看>>
Linux学习记录--日志系统
查看>>
Linux学习记录--启动流程
查看>>
Linux学习记录--Boot Loader
查看>>
Linux学习记录--开机挂载错误
查看>>
Linux学习记录--程序编译与函数库
查看>>