forked from JeanLucPons/Kangaroo
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPerfMain.cpp
More file actions
164 lines (140 loc) · 5.38 KB
/
Copy pathPerfMain.cpp
File metadata and controls
164 lines (140 loc) · 5.38 KB
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
#include "PerfHarness.h"
#include <limits>
#include <stdio.h>
#include <string>
#include <string.h>
using namespace std;
namespace {
int ParseInt(const char *name,const char *value) {
try {
return stoi(string(value));
} catch(...) {
throw PerfError(PERF_EXIT_INVALID_INPUT,string("Invalid ") + name + " value");
}
}
double ParseDouble(const char *name,const char *value) {
try {
return stod(string(value));
} catch(...) {
throw PerfError(PERF_EXIT_INVALID_INPUT,string("Invalid ") + name + " value");
}
}
uint32_t ParseUInt32(const char *name,const char *value) {
try {
unsigned long parsed = stoul(string(value),NULL,0);
if(parsed > numeric_limits<uint32_t>::max()) {
throw PerfError(PERF_EXIT_INVALID_INPUT,string("Invalid ") + name + " value");
}
return (uint32_t)parsed;
} catch(...) {
throw PerfError(PERF_EXIT_INVALID_INPUT,string("Invalid ") + name + " value");
}
}
void ParseGrid(const char *value,int *gridSizeX,int *gridSizeY) {
string text(value);
size_t comma = text.find(',');
if(comma == string::npos || comma == 0 || comma == text.length() - 1 || text.find(',',comma + 1) != string::npos) {
throw PerfError(PERF_EXIT_INVALID_INPUT,"Invalid --grid value, expected X,Y");
}
*gridSizeX = ParseInt("grid.x",text.substr(0,comma).c_str());
*gridSizeY = ParseInt("grid.y",text.substr(comma + 1).c_str());
if(*gridSizeX <= 0 || *gridSizeY <= 0) {
throw PerfError(PERF_EXIT_INVALID_INPUT,"Invalid --grid value, expected positive X,Y");
}
}
}
int main(int argc,char *argv[]) {
try {
PerfOptions options;
options.gpuId = 0;
options.gridSizeX = 0;
options.gridSizeY = 0;
options.gridSpecified = false;
options.gridSweepAuto = false;
options.warmupIterations = 5;
options.requestedIterations = 50;
options.requestedSeconds = 0.0;
options.useSeconds = false;
options.seed = 0x600DCAFEU;
bool hasIterations = false;
bool hasSeconds = false;
for(int i = 1; i < argc; i++) {
if(strcmp(argv[i],"--help") == 0 || strcmp(argv[i],"-h") == 0) {
PerfHarness::PrintUsage();
return PERF_EXIT_OK;
} else if(strcmp(argv[i],"--gpu-id") == 0) {
if(i >= argc - 1)
throw PerfError(PERF_EXIT_INVALID_INPUT,"--gpu-id requires a value");
i++;
options.gpuId = ParseInt("gpu-id",argv[i]);
} else if(strcmp(argv[i],"--grid") == 0) {
if(i >= argc - 1)
throw PerfError(PERF_EXIT_INVALID_INPUT,"--grid requires a value");
i++;
ParseGrid(argv[i],&options.gridSizeX,&options.gridSizeY);
options.gridSpecified = true;
} else if(strcmp(argv[i],"--grid-sweep") == 0) {
if(i >= argc - 1)
throw PerfError(PERF_EXIT_INVALID_INPUT,"--grid-sweep requires a value");
i++;
if(strcmp(argv[i],"auto") != 0)
throw PerfError(PERF_EXIT_INVALID_INPUT,"Unsupported --grid-sweep value, expected auto");
options.gridSweepAuto = true;
} else if(strcmp(argv[i],"--warmup") == 0) {
if(i >= argc - 1)
throw PerfError(PERF_EXIT_INVALID_INPUT,"--warmup requires a value");
i++;
options.warmupIterations = ParseInt("warmup",argv[i]);
} else if(strcmp(argv[i],"--iterations") == 0) {
if(i >= argc - 1)
throw PerfError(PERF_EXIT_INVALID_INPUT,"--iterations requires a value");
i++;
if(hasSeconds)
throw PerfError(PERF_EXIT_INVALID_INPUT,"--iterations and --seconds cannot be combined");
options.requestedIterations = ParseInt("iterations",argv[i]);
hasIterations = true;
} else if(strcmp(argv[i],"--seconds") == 0) {
if(i >= argc - 1)
throw PerfError(PERF_EXIT_INVALID_INPUT,"--seconds requires a value");
i++;
if(hasIterations)
throw PerfError(PERF_EXIT_INVALID_INPUT,"--iterations and --seconds cannot be combined");
options.requestedSeconds = ParseDouble("seconds",argv[i]);
options.useSeconds = true;
hasSeconds = true;
} else if(strcmp(argv[i],"--json-out") == 0) {
if(i >= argc - 1)
throw PerfError(PERF_EXIT_INVALID_INPUT,"--json-out requires a value");
i++;
options.jsonOutPath = string(argv[i]);
} else if(strcmp(argv[i],"--seed") == 0) {
if(i >= argc - 1)
throw PerfError(PERF_EXIT_INVALID_INPUT,"--seed requires a value");
i++;
options.seed = ParseUInt32("seed",argv[i]);
} else {
throw PerfError(PERF_EXIT_INVALID_INPUT,string("Unexpected argument: ") + argv[i]);
}
}
if(options.gpuId < 0)
throw PerfError(PERF_EXIT_INVALID_INPUT,"--gpu-id must be >= 0");
if(options.warmupIterations < 0)
throw PerfError(PERF_EXIT_INVALID_INPUT,"--warmup must be >= 0");
if(!options.useSeconds && options.requestedIterations <= 0)
throw PerfError(PERF_EXIT_INVALID_INPUT,"--iterations must be > 0");
if(options.useSeconds && options.requestedSeconds <= 0.0)
throw PerfError(PERF_EXIT_INVALID_INPUT,"--seconds must be > 0");
PerfHarness harness(options);
harness.Run();
return PERF_EXIT_OK;
} catch(const PerfError& e) {
fprintf(stderr,"%s\n",e.what());
return e.exitCode;
} catch(const std::exception& e) {
fprintf(stderr,"%s\n",e.what());
return PERF_EXIT_RUNTIME_ERROR;
} catch(...) {
fprintf(stderr,"Unexpected fatal error\n");
return PERF_EXIT_RUNTIME_ERROR;
}
}