-
Notifications
You must be signed in to change notification settings - Fork 1
/
appArgs.hpp
179 lines (145 loc) · 5.78 KB
/
appArgs.hpp
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
#ifndef _APP_ARGS_HPP_
#define _APP_ARGS_HPP_
#include <iostream>
#include <stdexcept>
#include <vector>
#include <getopt.h>
static std::vector<int> get_int_sequence(std::string seqStr)
{
std::vector<int> lengths;
std::size_t pos = 0;
std::size_t new_pos;
new_pos = seqStr.find(',', pos);
while(new_pos != std::string::npos)
{
std::string sliceStr = seqStr.substr(pos, new_pos - pos);
int len = std::stoi(sliceStr);
lengths.push_back(len);
pos = new_pos + 1;
new_pos = seqStr.find(',', pos);
};
std::string sliceStr = seqStr.substr(pos);
int len = std::stoi(sliceStr);
lengths.push_back(len);
return (lengths);
}
static const struct option long_options[] = {
{"dimLengths", required_argument, NULL, 'D'},
{"reduceDims", required_argument, NULL, 'R'},
{"reduceOp", required_argument, NULL, 'O'},
{"nanPropa", no_argument, NULL, 'N'},
{"indices", no_argument, NULL, 'I'},
{"help", no_argument, NULL, 'H'},
{"verify", no_argument, NULL, 'V'},
{0, 0, 0, 0} };
class AppArgs
{
private:
int option_index;
void show_usage()
{
std::cout << "Arguments: " << std::endl << std::endl;
std::cout << "--dimLengths <xxx> ---the lengths of the input tensor dimensions" << std::endl << std::endl;
std::cout << "--reduceDims <xxx> ---the indexes of the dimensions to reduce" << std::endl << std::endl;
std::cout << "--reduceOp <xxx> ---the id of the reduce operation (0 for add, 1 for mul, 2 for min, ...)" << std::endl << std::endl;
std::cout << "--nanPropa ---enable nanPropagation" << std::endl << std::endl;
std::cout << "--indices ---enable the reduce indices" << std::endl << std::endl;
std::cout << "--verify ---verify the device computed result by comparing to the host computed result" << std::endl << std::endl;
std::cout << "--help ---show the above information" << std::endl << std::endl;
};
public:
AppArgs() = default;
int processArgs(int argc, char *argv[])
{
unsigned int ch;
while (1) {
ch = getopt_long (argc, argv, "", long_options, &option_index);
if ( static_cast<int>(ch) == -1 )
break;
switch (ch) {
case 'D':
inLengths = get_int_sequence(optarg);
break;
case 'R':
toReduceDims = get_int_sequence(optarg);
break;
case 'O':
#ifdef CUDA_CUDNN_APP
op = static_cast<cudnnReduceTensorOp_t>(op);
#else
op = static_cast<miopenReduceTensorOp_t>(op);
#endif
break;
case 'N':
#ifdef CUDA_CUDNN_APP
nanPropaOpt = CUDNN_PROPAGATE_NAN;
#else
nanPropaOpt = MIOPEN_PROPAGATE_NAN;
#endif
break;
case 'I':
#ifdef CUDA_CUDNN_APP
indicesOpt = CUDNN_REDUCE_TENSOR_FLATTENED_INDICES;
#else
indicesOpt = MIOPEN_REDUCE_TENSOR_FLATTENED_INDICES;
#endif
break;
case 'V':
doVerify = true;
break;
case 'H':
show_usage();
return(-1);
default:
std::cerr << "Wrong arguments format!" << std::endl;
show_usage();
throw std::runtime_error("Wrong command-line format!");
}
}
if ( inLengths.empty() || toReduceDims.empty() || toReduceDims.size() > inLengths.size() )
throw std::runtime_error("Wrong command-line format/parameters!");
for(const auto & dimIndex : toReduceDims)
if ( dimIndex >= inLengths.size() )
throw std::runtime_error("Wrong command-line parameters(toReduceDims not correct)!");
outLengths = inLengths;
// set the lengths of the dimensions to be reduced to 1 to represent the output Tensor
for(int i = 0; i < toReduceDims.size(); i++)
outLengths[toReduceDims[i]] = 1;
inStrides.resize(inLengths.size());
inStrides[inStrides.size()-1] = 1;
for(int i=inStrides.size()-2; i >= 0; i--)
inStrides[i] = inStrides[i+1] * inLengths[i+1];
outStrides.resize(outLengths.size());
outStrides[outStrides.size()-1] = 1;
for(int i=inStrides.size()-2; i >= 0; i--)
outStrides[i] = outStrides[i+1] * outLengths[i+1];
for(int i = 0; i < inLengths.size(); i++)
if(inLengths[i] == outLengths[i])
invariantDims.push_back(i);
return(0);
};
protected:
#ifdef CUDA_CUDNN_APP
cudnnReduceTensorOp_t op = CUDNN_REDUCE_TENSOR_ADD;
cudnnDataType_t dataType = CUDNN_DATA_FLOAT;
cudnnDataType_t compType = CUDNN_DATA_FLOAT;
cudnnNanPropagation_t nanPropaOpt = CUDNN_NOT_PROPAGATE_NAN;
cudnnReduceTensorIndices_t indicesOpt = CUDNN_REDUCE_TENSOR_NO_INDICES;
cudnnIndicesType_t indicesType = CUDNN_32BIT_INDICES;
#else
miopenReduceTensorOp_t op = MIOPEN_REDUCE_TENSOR_ADD;
miopenDataType_t dataType = miopenFloat;
miopenDataType_t compType = miopenFloat;
miopenNanPropagation_t nanPropaOpt = MIOPEN_NOT_PROPAGATE_NAN;
miopenReduceTensorIndices_t indicesOpt = MIOPEN_REDUCE_TENSOR_NO_INDICES;
miopenIndicesType_t indicesType = MIOPEN_32BIT_INDICES;
#endif
std::vector<int> inLengths;
std::vector<int> inStrides;
std::vector<int> outLengths;
std::vector<int> outStrides;
std::vector<int> invariantDims;
std::vector<int> toReduceDims;
bool doVerify = false;
};
#endif