forked from rai-project/go-cupti
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.cpp
236 lines (202 loc) · 10.3 KB
/
utils.cpp
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#ifdef CUPTI_ENABLED
#include "utils.hpp"
#include <iostream>
#include <vector>
#include <cupti_target.h>
#include <cupti_profiler_target.h>
#include <cupti_callbacks.h>
#include <cupti_driver_cbid.h>
#include <nvperf_host.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include "Metric.h"
#include "Eval.h"
#define NVPW_API_CALL(apiFuncCall) \
do { \
NVPA_Status _status = apiFuncCall; \
if (_status != NVPA_STATUS_SUCCESS) { \
fprintf(stderr, "%s:%d: error: function %s failed with error %d.\n", \
__FILE__, __LINE__, #apiFuncCall, _status); \
exit(-1); \
} \
} while (0)
#define CUPTI_API_CALL(apiFuncCall) \
do { \
CUptiResult _status = apiFuncCall; \
if (_status != CUPTI_SUCCESS) { \
const char *errstr; \
cuptiGetResultString(_status, &errstr); \
fprintf(stderr, "%s:%d: error: function %s failed with error %s.\n", \
__FILE__, __LINE__, #apiFuncCall, errstr); \
exit(-1); \
} \
} while (0)
#define DRIVER_API_CALL(apiFuncCall) \
do { \
CUresult _status = apiFuncCall; \
if (_status != CUDA_SUCCESS) { \
fprintf(stderr, "%s:%d: error: function %s failed with error %d.\n", \
__FILE__, __LINE__, #apiFuncCall, _status); \
exit(-1); \
} \
} while (0)
#define RUNTIME_API_CALL(apiFuncCall) \
do { \
cudaError_t _status = apiFuncCall; \
if (_status != cudaSuccess) { \
fprintf(stderr, "%s:%d: error: function %s failed with error %s.\n", \
__FILE__, __LINE__, #apiFuncCall, cudaGetErrorString(_status));\
exit(-1); \
} \
} while (0)
struct ProfilingData_t {
int numRanges = 100000;
std::string chipName;
std::vector<std::string> metricNames;
CUpti_ProfilerRange profilerRange = CUPTI_AutoRange;
CUpti_ProfilerReplayMode profilerReplayMode = CUPTI_KernelReplay;
std::vector<uint8_t> counterDataImagePrefix;
std::vector<uint8_t> configImage;
std::vector<uint8_t> counterDataImage;
std::vector<uint8_t> counterDataScratchBuffer;
std::vector<double> metricData;
int numMetrics = 0;
}cur;
void enableProfiling() {
CUpti_Profiler_EnableProfiling_Params enableProfilingParams = { CUpti_Profiler_EnableProfiling_Params_STRUCT_SIZE };
CUPTI_API_CALL(cuptiProfilerEnableProfiling(&enableProfilingParams));
}
void disableProfiling() {
CUpti_Profiler_DisableProfiling_Params disableProfilingParams = { CUpti_Profiler_DisableProfiling_Params_STRUCT_SIZE };
CUPTI_API_CALL(cuptiProfilerDisableProfiling(&disableProfilingParams));
}
void beginSession() {
CUpti_Profiler_BeginSession_Params beginSessionParams = { CUpti_Profiler_BeginSession_Params_STRUCT_SIZE };
beginSessionParams.ctx = NULL;
beginSessionParams.counterDataImageSize = cur.counterDataImage.size();
beginSessionParams.pCounterDataImage = &cur.counterDataImage[0];
beginSessionParams.counterDataScratchBufferSize = cur.counterDataScratchBuffer.size();
beginSessionParams.pCounterDataScratchBuffer = &cur.counterDataScratchBuffer[0];
beginSessionParams.range = cur.profilerRange;
beginSessionParams.replayMode = cur.profilerReplayMode;
beginSessionParams.maxRangesPerPass = cur.numRanges;
beginSessionParams.maxLaunchesPerPass = cur.numRanges;
CUPTI_API_CALL(cuptiProfilerBeginSession(&beginSessionParams));
}
void setConfig() {
CUpti_Profiler_SetConfig_Params setConfigParams = { CUpti_Profiler_SetConfig_Params_STRUCT_SIZE };
setConfigParams.pConfig = &cur.configImage[0];
setConfigParams.configSize = cur.configImage.size();
setConfigParams.passIndex = 0;
CUPTI_API_CALL(cuptiProfilerSetConfig(&setConfigParams));
}
void createCounterDataImage(int numRanges,
std::vector<uint8_t>& counterDataImagePrefix,
std::vector<uint8_t>& counterDataScratchBuffer,
std::vector<uint8_t>& counterDataImage
) {
CUpti_Profiler_CounterDataImageOptions counterDataImageOptions;
counterDataImageOptions.pCounterDataPrefix = &counterDataImagePrefix[0];
counterDataImageOptions.counterDataPrefixSize = counterDataImagePrefix.size();
counterDataImageOptions.maxNumRanges = numRanges;
counterDataImageOptions.maxNumRangeTreeNodes = numRanges;
counterDataImageOptions.maxRangeNameLength = 64;
CUpti_Profiler_CounterDataImage_CalculateSize_Params calculateSizeParams = { CUpti_Profiler_CounterDataImage_CalculateSize_Params_STRUCT_SIZE };
calculateSizeParams.pOptions = &counterDataImageOptions;
calculateSizeParams.sizeofCounterDataImageOptions = CUpti_Profiler_CounterDataImageOptions_STRUCT_SIZE;
CUPTI_API_CALL(cuptiProfilerCounterDataImageCalculateSize(&calculateSizeParams));
CUpti_Profiler_CounterDataImage_Initialize_Params initializeParams = { CUpti_Profiler_CounterDataImage_Initialize_Params_STRUCT_SIZE };
initializeParams.sizeofCounterDataImageOptions = CUpti_Profiler_CounterDataImageOptions_STRUCT_SIZE;
initializeParams.pOptions = &counterDataImageOptions;
initializeParams.counterDataImageSize = calculateSizeParams.counterDataImageSize;
counterDataImage.resize(calculateSizeParams.counterDataImageSize);
initializeParams.pCounterDataImage = &counterDataImage[0];
CUPTI_API_CALL(cuptiProfilerCounterDataImageInitialize(&initializeParams));
CUpti_Profiler_CounterDataImage_CalculateScratchBufferSize_Params scratchBufferSizeParams = { CUpti_Profiler_CounterDataImage_CalculateScratchBufferSize_Params_STRUCT_SIZE };
scratchBufferSizeParams.counterDataImageSize = calculateSizeParams.counterDataImageSize;
scratchBufferSizeParams.pCounterDataImage = initializeParams.pCounterDataImage;
CUPTI_API_CALL(cuptiProfilerCounterDataImageCalculateScratchBufferSize(&scratchBufferSizeParams));
counterDataScratchBuffer.resize(scratchBufferSizeParams.counterDataScratchBufferSize);
CUpti_Profiler_CounterDataImage_InitializeScratchBuffer_Params initScratchBufferParams = { CUpti_Profiler_CounterDataImage_InitializeScratchBuffer_Params_STRUCT_SIZE };
initScratchBufferParams.counterDataImageSize = calculateSizeParams.counterDataImageSize;
initScratchBufferParams.pCounterDataImage = initializeParams.pCounterDataImage;
initScratchBufferParams.counterDataScratchBufferSize = scratchBufferSizeParams.counterDataScratchBufferSize;
initScratchBufferParams.pCounterDataScratchBuffer = &counterDataScratchBuffer[0];
CUPTI_API_CALL(cuptiProfilerCounterDataImageInitializeScratchBuffer(&initScratchBufferParams));
}
void setupProfiling() {
NVPW_InitializeHost_Params initializeHostParams = { NVPW_InitializeHost_Params_STRUCT_SIZE };
NVPW_API_CALL(NVPW_InitializeHost(&initializeHostParams));
if (cur.metricNames.size()) {
if (!NV::Metric::Config::GetConfigImage(cur.chipName, cur.metricNames, cur.configImage))
{
std::cout << "Failed to create configImage" << std::endl;
exit(-1);
}
if (!NV::Metric::Config::GetCounterDataPrefixImage(cur.chipName, cur.metricNames, cur.counterDataImagePrefix))
{
std::cout << "Failed to create counterDataImagePrefix" << std::endl;
exit(-1);
}
}
createCounterDataImage(cur.numRanges, cur.counterDataImagePrefix,
cur.counterDataScratchBuffer, cur.counterDataImage);
beginSession();
setConfig();
}
void stopProfiling() {
CUpti_Profiler_UnsetConfig_Params unsetConfigParams = { CUpti_Profiler_UnsetConfig_Params_STRUCT_SIZE };
CUPTI_API_CALL(cuptiProfilerUnsetConfig(&unsetConfigParams));
// CUpti_Profiler_DeInitialize_Params profilerDeInitializeParams = {CUpti_Profiler_DeInitialize_Params_STRUCT_SIZE};
CUpti_Profiler_EndSession_Params endSessionParams = { CUpti_Profiler_EndSession_Params_STRUCT_SIZE };
CUPTI_API_CALL(cuptiProfilerEndSession(&endSessionParams));
// cuptiProfilerDeInitialize(&profilerDeInitializeParams);
}
void onCallback(int start) {
if(start) {
}
else {
}
}
void startProfiling(char *goMetrics) {
if (goMetrics == NULL) {
return;
}
cur = ProfilingData_t();
char* metricName = strtok(goMetrics, ",");
while (metricName != NULL) {
cur.metricNames.push_back(metricName);
cur.numMetrics++;
metricName = strtok(NULL, ",");
}
CUpti_Profiler_Initialize_Params profilerInitializeParams = { CUpti_Profiler_Initialize_Params_STRUCT_SIZE };
CUPTI_API_CALL(cuptiProfilerInitialize(&profilerInitializeParams));
CUpti_Device_GetChipName_Params getChipNameParams = { CUpti_Device_GetChipName_Params_STRUCT_SIZE };
getChipNameParams.deviceIndex = 0;
CUPTI_API_CALL(cuptiDeviceGetChipName(&getChipNameParams));
cur.chipName = getChipNameParams.pChipName;
setupProfiling();
enableProfiling();
}
double* endProfiling(uint64_t* len) {
if(cur.numMetrics == 0) {
*len = 0;
return NULL;
}
disableProfiling();
stopProfiling();
cur.metricData = NV::Metric::Eval::GetMetricValues(cur.chipName, cur.counterDataImage, cur.metricNames);
*len = cur.metricData.size();
return cur.metricData.data();
}
bool getProfilingAPI(void) {
int ccMajor = 0;
CUdevice cuDevice;
DRIVER_API_CALL(cuDeviceGet(&cuDevice, 0));
DRIVER_API_CALL(cuDeviceGetAttribute(&ccMajor, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, cuDevice));
return ccMajor >= 7;
}
#endif // CUPTI_ENABLED