File size: 10,751 Bytes
6be3106 |
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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
/*
* Copyright 2020 NVIDIA Corporation. All rights reserved
*
* Sample CUPTI app to print the trace of CUDA graphs and correlate
* the graph node launch to the node creation API using CUPTI callbacks.
*
*/
#include <stdio.h>
#include <cuda.h>
#include <cupti.h>
#include <map>
#include <stdlib.h>
#ifndef EXIT_WAIVED
#define EXIT_WAIVED 2
#endif
#define CUPTI_CALL(call) \
do { \
CUptiResult _status = call; \
if (_status != CUPTI_SUCCESS) { \
const char *errstr; \
cuptiGetResultString(_status, &errstr); \
fprintf(stderr, "%s:%d: error: function %s failed with error %s.\n", \
__FILE__, __LINE__, #call, errstr); \
exit(EXIT_FAILURE); \
} \
} while (0)
#define BUF_SIZE (32 * 1024)
#define ALIGN_SIZE (8)
#define ALIGN_BUFFER(buffer, align) \
(((uintptr_t) (buffer) & ((align)-1)) ? ((buffer) + (align) - ((uintptr_t) (buffer) & ((align)-1))) : (buffer))
// Timestamp at trace initialization time. Used to normalized other
// timestamps
static uint64_t startTimestamp;
typedef struct {
const char *funcName;
uint32_t correlationId;
} ApiData;
typedef std::map<uint64_t, ApiData> nodeIdApiDataMap;
nodeIdApiDataMap nodeIdCorrelationMap;
static const char *
getMemcpyKindString(CUpti_ActivityMemcpyKind kind)
{
switch (kind) {
case CUPTI_ACTIVITY_MEMCPY_KIND_HTOD:
return "HtoD";
case CUPTI_ACTIVITY_MEMCPY_KIND_DTOH:
return "DtoH";
case CUPTI_ACTIVITY_MEMCPY_KIND_HTOA:
return "HtoA";
case CUPTI_ACTIVITY_MEMCPY_KIND_ATOH:
return "AtoH";
case CUPTI_ACTIVITY_MEMCPY_KIND_ATOA:
return "AtoA";
case CUPTI_ACTIVITY_MEMCPY_KIND_ATOD:
return "AtoD";
case CUPTI_ACTIVITY_MEMCPY_KIND_DTOA:
return "DtoA";
case CUPTI_ACTIVITY_MEMCPY_KIND_DTOD:
return "DtoD";
case CUPTI_ACTIVITY_MEMCPY_KIND_HTOH:
return "HtoH";
default:
break;
}
return "<unknown>";
}
static void
printActivity(CUpti_Activity *record)
{
switch (record->kind)
{
case CUPTI_ACTIVITY_KIND_MEMCPY:
{
CUpti_ActivityMemcpy5 *memcpy = (CUpti_ActivityMemcpy5 *) record;
printf("MEMCPY %s [ %llu - %llu ] device %u, context %u, stream %u, size %llu, correlation %u, graph ID %u, graph node ID %llu\n",
getMemcpyKindString((CUpti_ActivityMemcpyKind)memcpy->copyKind),
(unsigned long long) (memcpy->start - startTimestamp),
(unsigned long long) (memcpy->end - startTimestamp),
memcpy->deviceId, memcpy->contextId, memcpy->streamId,
(unsigned long long)memcpy->bytes, memcpy->correlationId,
memcpy->graphId, (unsigned long long)memcpy->graphNodeId);
// Retrieve the information of the API used to create the node
nodeIdApiDataMap::iterator it = nodeIdCorrelationMap.find(memcpy->graphNodeId);
if (it != nodeIdCorrelationMap.end()) {
printf("Graph node was created using API %s with correlationId %u\n", it->second.funcName, it->second.correlationId);
}
break;
}
case CUPTI_ACTIVITY_KIND_MEMSET:
{
CUpti_ActivityMemset4 *memset = (CUpti_ActivityMemset4 *) record;
printf("MEMSET value=%u [ %llu - %llu ] device %u, context %u, stream %u, correlation %u, graph ID %u, graph node ID %llu\n",
memset->value,
(unsigned long long) (memset->start - startTimestamp),
(unsigned long long) (memset->end - startTimestamp),
memset->deviceId, memset->contextId, memset->streamId,
memset->correlationId, memset->graphId, (unsigned long long)memset->graphNodeId);
break;
}
case CUPTI_ACTIVITY_KIND_KERNEL:
case CUPTI_ACTIVITY_KIND_CONCURRENT_KERNEL:
{
const char* kindString = (record->kind == CUPTI_ACTIVITY_KIND_KERNEL) ? "KERNEL" : "CONC KERNEL";
CUpti_ActivityKernel8 *kernel = (CUpti_ActivityKernel8 *) record;
printf("%s \"%s\" [ %llu - %llu ] device %u, context %u, stream %u, correlation %u\n",
kindString,
kernel->name,
(unsigned long long) (kernel->start - startTimestamp),
(unsigned long long) (kernel->end - startTimestamp),
kernel->deviceId, kernel->contextId, kernel->streamId,
kernel->correlationId);
printf(" grid [%u,%u,%u], block [%u,%u,%u], shared memory (static %u, dynamic %u), graph ID %u, graph node ID %llu\n",
kernel->gridX, kernel->gridY, kernel->gridZ,
kernel->blockX, kernel->blockY, kernel->blockZ,
kernel->staticSharedMemory, kernel->dynamicSharedMemory,
kernel->graphId, (unsigned long long)kernel->graphNodeId);
// Retrieve the information of the API used to create the node
nodeIdApiDataMap::iterator it = nodeIdCorrelationMap.find(kernel->graphNodeId);
if (it != nodeIdCorrelationMap.end()) {
printf("Graph node was created using API %s with correlationId %u\n", it->second.funcName, it->second.correlationId);
}
break;
}
case CUPTI_ACTIVITY_KIND_RUNTIME:
{
CUpti_ActivityAPI *api = (CUpti_ActivityAPI *) record;
const char* apiName;
CUPTI_CALL(cuptiGetCallbackName(CUPTI_CB_DOMAIN_RUNTIME_API, api->cbid, &apiName));
printf("RUNTIME cbid=%u [ %llu - %llu ] process %u, thread %u, correlation %u, Name %s\n",
api->cbid,
(unsigned long long) (api->start - startTimestamp),
(unsigned long long) (api->end - startTimestamp),
api->processId, api->threadId, api->correlationId, apiName);
break;
}
default:
printf(" <unknown>\n");
break;
}
}
void CUPTIAPI bufferRequested(uint8_t **buffer, size_t *size, size_t *maxNumRecords)
{
uint8_t *bfr = (uint8_t *) malloc(BUF_SIZE + ALIGN_SIZE);
if (bfr == NULL) {
printf("Error: out of memory\n");
exit(EXIT_FAILURE);
}
*size = BUF_SIZE;
*buffer = ALIGN_BUFFER(bfr, ALIGN_SIZE);
*maxNumRecords = 0;
}
void CUPTIAPI bufferCompleted(CUcontext ctx, uint32_t streamId, uint8_t *buffer, size_t size, size_t validSize)
{
CUptiResult status;
CUpti_Activity *record = NULL;
if (validSize > 0) {
do {
status = cuptiActivityGetNextRecord(buffer, validSize, &record);
if (status == CUPTI_SUCCESS) {
printActivity(record);
}
else if (status == CUPTI_ERROR_MAX_LIMIT_REACHED)
break;
else {
CUPTI_CALL(status);
}
} while (1);
// report any records dropped from the queue
size_t dropped;
CUPTI_CALL(cuptiActivityGetNumDroppedRecords(ctx, streamId, &dropped));
if (dropped != 0) {
printf("Dropped %u activity records\n", (unsigned int) dropped);
}
}
free(buffer);
}
void CUPTIAPI
callbackHandler(void *userdata, CUpti_CallbackDomain domain,
CUpti_CallbackId cbid, const CUpti_CallbackData *cbInfo)
{
static const char* funcName;
static uint32_t correlationId;
// Check last error
CUPTI_CALL(cuptiGetLastError());
switch (domain) {
case CUPTI_CB_DOMAIN_RESOURCE:
{
CUpti_ResourceData *resourceData = (CUpti_ResourceData *)cbInfo;
switch (cbid)
{
case CUPTI_CBID_RESOURCE_GRAPHNODE_CREATED:
{
// Do not store info for the nodes that are created during graph instantiate
if (!strncmp(funcName, "cudaGraphInstantiate", strlen("cudaGraphInstantiate"))) {
break;
}
CUpti_GraphData *cbData = (CUpti_GraphData *) resourceData->resourceDescriptor;
uint64_t nodeId;
// Query the graph node ID and store the API correlation id and function name
CUPTI_CALL(cuptiGetGraphNodeId(cbData->node, &nodeId));
ApiData apiData;
apiData.correlationId = correlationId;
apiData.funcName = funcName;
nodeIdCorrelationMap[nodeId] = apiData;
break;
}
case CUPTI_CBID_RESOURCE_GRAPHNODE_CLONED:
{
CUpti_GraphData *cbData = (CUpti_GraphData *) resourceData->resourceDescriptor;
uint64_t nodeId, originalNodeId;
// Overwrite the map entry with node ID of the cloned graph node
CUPTI_CALL(cuptiGetGraphNodeId(cbData->originalNode, &originalNodeId));
nodeIdApiDataMap::iterator it = nodeIdCorrelationMap.find(originalNodeId);
if (it != nodeIdCorrelationMap.end()) {
CUPTI_CALL(cuptiGetGraphNodeId(cbData->node, &nodeId));
ApiData apiData = it->second;
nodeIdCorrelationMap.erase(it);
nodeIdCorrelationMap[nodeId] = apiData;
}
break;
}
default:
break;
}
}
break;
case CUPTI_CB_DOMAIN_RUNTIME_API:
{
if (cbInfo->callbackSite == CUPTI_API_ENTER) {
correlationId = cbInfo->correlationId;
funcName = cbInfo->functionName;
}
break;
}
default:
break;
}
}
void
initTrace()
{
CUpti_SubscriberHandle subscriber;
// Enable activity record kinds.
CUPTI_CALL(cuptiActivityEnable(CUPTI_ACTIVITY_KIND_RUNTIME));
CUPTI_CALL(cuptiActivityEnable(CUPTI_ACTIVITY_KIND_MEMCPY));
CUPTI_CALL(cuptiActivityEnable(CUPTI_ACTIVITY_KIND_MEMSET));
CUPTI_CALL(cuptiActivityEnable(CUPTI_ACTIVITY_KIND_CONCURRENT_KERNEL));
// Register callbacks for buffer requests and for buffers completed by CUPTI.
CUPTI_CALL(cuptiActivityRegisterCallbacks(bufferRequested, bufferCompleted));
CUPTI_CALL(cuptiSubscribe(&subscriber, (CUpti_CallbackFunc)callbackHandler , NULL));
// Enable callbacks for CUDA graph
CUPTI_CALL(cuptiEnableCallback(1, subscriber, CUPTI_CB_DOMAIN_RESOURCE, CUPTI_CBID_RESOURCE_GRAPHNODE_CREATED));
CUPTI_CALL(cuptiEnableCallback(1, subscriber, CUPTI_CB_DOMAIN_RESOURCE, CUPTI_CBID_RESOURCE_GRAPHNODE_CLONED));
CUPTI_CALL(cuptiEnableDomain(1, subscriber, CUPTI_CB_DOMAIN_RUNTIME_API));
CUPTI_CALL(cuptiGetTimestamp(&startTimestamp));
}
void finiTrace()
{
// Force flush any remaining activity buffers before termination of the application
CUPTI_CALL(cuptiActivityFlushAll(1));
}
|