mirror of
https://github.com/NVIDIA/cuda-samples.git
synced 2026-10-25 22:28:17 +08:00
Apply consistent code formatting across the repo. Add clang-format and pre-commit hooks.
This commit is contained in:
@@ -33,375 +33,358 @@
|
||||
#include <helper_cuda.h>
|
||||
#include <helper_functions.h>
|
||||
|
||||
#define NUM_GRAPHS 8
|
||||
#define NUM_GRAPHS 8
|
||||
#define THREADS_PER_BLOCK 512
|
||||
|
||||
void printMemoryFootprint(int device) {
|
||||
size_t footprint;
|
||||
checkCudaErrors(cudaDeviceGetGraphMemAttribute(
|
||||
device, (cudaGraphMemAttributeType)0, &footprint));
|
||||
printf(" FOOTPRINT: %lu bytes\n", footprint);
|
||||
void printMemoryFootprint(int device)
|
||||
{
|
||||
size_t footprint;
|
||||
checkCudaErrors(cudaDeviceGetGraphMemAttribute(device, (cudaGraphMemAttributeType)0, &footprint));
|
||||
printf(" FOOTPRINT: %lu bytes\n", footprint);
|
||||
}
|
||||
|
||||
void prepareAllocParams(cudaMemAllocNodeParams *allocParams, size_t bytes,
|
||||
int device) {
|
||||
memset(allocParams, 0, sizeof(*allocParams));
|
||||
void prepareAllocParams(cudaMemAllocNodeParams *allocParams, size_t bytes, int device)
|
||||
{
|
||||
memset(allocParams, 0, sizeof(*allocParams));
|
||||
|
||||
allocParams->bytesize = bytes;
|
||||
allocParams->poolProps.allocType = cudaMemAllocationTypePinned;
|
||||
allocParams->poolProps.location.id = device;
|
||||
allocParams->poolProps.location.type = cudaMemLocationTypeDevice;
|
||||
allocParams->bytesize = bytes;
|
||||
allocParams->poolProps.allocType = cudaMemAllocationTypePinned;
|
||||
allocParams->poolProps.location.id = device;
|
||||
allocParams->poolProps.location.type = cudaMemLocationTypeDevice;
|
||||
}
|
||||
|
||||
void createVirtAddrReuseGraph(cudaGraphExec_t *graphExec, size_t bytes,
|
||||
int device) {
|
||||
cudaGraph_t graph;
|
||||
cudaGraphNode_t allocNodeA, allocNodeB, freeNodeA, freeNodeB;
|
||||
cudaMemAllocNodeParams allocParams;
|
||||
float *d_a, *d_b;
|
||||
void createVirtAddrReuseGraph(cudaGraphExec_t *graphExec, size_t bytes, int device)
|
||||
{
|
||||
cudaGraph_t graph;
|
||||
cudaGraphNode_t allocNodeA, allocNodeB, freeNodeA, freeNodeB;
|
||||
cudaMemAllocNodeParams allocParams;
|
||||
float *d_a, *d_b;
|
||||
|
||||
checkCudaErrors(cudaGraphCreate(&graph, 0));
|
||||
prepareAllocParams(&allocParams, bytes, device);
|
||||
checkCudaErrors(cudaGraphCreate(&graph, 0));
|
||||
prepareAllocParams(&allocParams, bytes, device);
|
||||
|
||||
checkCudaErrors(
|
||||
cudaGraphAddMemAllocNode(&allocNodeA, graph, NULL, 0, &allocParams));
|
||||
d_a = (float *)allocParams.dptr;
|
||||
checkCudaErrors(
|
||||
cudaGraphAddMemFreeNode(&freeNodeA, graph, &allocNodeA, 1, (void *)d_a));
|
||||
checkCudaErrors(cudaGraphAddMemAllocNode(&allocNodeA, graph, NULL, 0, &allocParams));
|
||||
d_a = (float *)allocParams.dptr;
|
||||
checkCudaErrors(cudaGraphAddMemFreeNode(&freeNodeA, graph, &allocNodeA, 1, (void *)d_a));
|
||||
|
||||
// The dependency between the allocation of d_b and the free of d_a allows d_b
|
||||
// to reuse the same VA.
|
||||
checkCudaErrors(cudaGraphAddMemAllocNode(&allocNodeB, graph, &freeNodeA, 1,
|
||||
&allocParams));
|
||||
d_b = (float *)allocParams.dptr;
|
||||
// The dependency between the allocation of d_b and the free of d_a allows d_b
|
||||
// to reuse the same VA.
|
||||
checkCudaErrors(cudaGraphAddMemAllocNode(&allocNodeB, graph, &freeNodeA, 1, &allocParams));
|
||||
d_b = (float *)allocParams.dptr;
|
||||
|
||||
if (d_a == d_b) {
|
||||
printf("Check confirms that d_a and d_b share a virtual address.\n");
|
||||
} else {
|
||||
printf("Check shows that d_a and d_b DO NOT share a virtual address.\n");
|
||||
}
|
||||
if (d_a == d_b) {
|
||||
printf("Check confirms that d_a and d_b share a virtual address.\n");
|
||||
}
|
||||
else {
|
||||
printf("Check shows that d_a and d_b DO NOT share a virtual address.\n");
|
||||
}
|
||||
|
||||
checkCudaErrors(
|
||||
cudaGraphAddMemFreeNode(&freeNodeB, graph, &allocNodeB, 1, (void *)d_b));
|
||||
checkCudaErrors(cudaGraphAddMemFreeNode(&freeNodeB, graph, &allocNodeB, 1, (void *)d_b));
|
||||
|
||||
checkCudaErrors(cudaGraphInstantiate(graphExec, graph, NULL, NULL, 0));
|
||||
checkCudaErrors(cudaGraphDestroy(graph));
|
||||
checkCudaErrors(cudaGraphInstantiate(graphExec, graph, NULL, NULL, 0));
|
||||
checkCudaErrors(cudaGraphDestroy(graph));
|
||||
}
|
||||
|
||||
void virtualAddressReuseSingleGraph(size_t bytes, int device) {
|
||||
cudaStream_t stream;
|
||||
cudaGraphExec_t graphExec;
|
||||
void virtualAddressReuseSingleGraph(size_t bytes, int device)
|
||||
{
|
||||
cudaStream_t stream;
|
||||
cudaGraphExec_t graphExec;
|
||||
|
||||
printf("================================\n");
|
||||
printf("Running virtual address reuse example.\n");
|
||||
printf(
|
||||
"Sequential allocations & frees within a single graph enable CUDA to "
|
||||
"reuse virtual addresses.\n\n");
|
||||
printf("================================\n");
|
||||
printf("Running virtual address reuse example.\n");
|
||||
printf("Sequential allocations & frees within a single graph enable CUDA to "
|
||||
"reuse virtual addresses.\n\n");
|
||||
|
||||
createVirtAddrReuseGraph(&graphExec, bytes, device);
|
||||
checkCudaErrors(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking));
|
||||
createVirtAddrReuseGraph(&graphExec, bytes, device);
|
||||
checkCudaErrors(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking));
|
||||
|
||||
checkCudaErrors(cudaGraphLaunch(graphExec, stream));
|
||||
checkCudaErrors(cudaStreamSynchronize(stream));
|
||||
printMemoryFootprint(device);
|
||||
checkCudaErrors(cudaGraphLaunch(graphExec, stream));
|
||||
checkCudaErrors(cudaStreamSynchronize(stream));
|
||||
printMemoryFootprint(device);
|
||||
|
||||
checkCudaErrors(cudaGraphExecDestroy(graphExec));
|
||||
checkCudaErrors(cudaStreamDestroy(stream));
|
||||
checkCudaErrors(cudaGraphExecDestroy(graphExec));
|
||||
checkCudaErrors(cudaStreamDestroy(stream));
|
||||
}
|
||||
|
||||
// This is a kernel that does no real work but runs at least for a specified
|
||||
// number of clocks
|
||||
__global__ void clockBlock(clock_t clock_count) {
|
||||
unsigned int start_clock = (unsigned int)clock();
|
||||
__global__ void clockBlock(clock_t clock_count)
|
||||
{
|
||||
unsigned int start_clock = (unsigned int)clock();
|
||||
|
||||
clock_t clock_offset = 0;
|
||||
clock_t clock_offset = 0;
|
||||
|
||||
while (clock_offset < clock_count) {
|
||||
unsigned int end_clock = (unsigned int)clock();
|
||||
while (clock_offset < clock_count) {
|
||||
unsigned int end_clock = (unsigned int)clock();
|
||||
|
||||
// The code below should work like
|
||||
// this (thanks to modular arithmetics):
|
||||
//
|
||||
// clock_offset = (clock_t) (end_clock > start_clock ?
|
||||
// end_clock - start_clock :
|
||||
// end_clock + (0xffffffffu - start_clock));
|
||||
//
|
||||
// Indeed, let m = 2^32 then
|
||||
// end - start = end + m - start (mod m).
|
||||
// The code below should work like
|
||||
// this (thanks to modular arithmetics):
|
||||
//
|
||||
// clock_offset = (clock_t) (end_clock > start_clock ?
|
||||
// end_clock - start_clock :
|
||||
// end_clock + (0xffffffffu - start_clock));
|
||||
//
|
||||
// Indeed, let m = 2^32 then
|
||||
// end - start = end + m - start (mod m).
|
||||
|
||||
clock_offset = (clock_t)(end_clock - start_clock);
|
||||
}
|
||||
clock_offset = (clock_t)(end_clock - start_clock);
|
||||
}
|
||||
}
|
||||
|
||||
// A pointer to the allocated device buffer is returned in dPtr so the caller
|
||||
// can compare virtual addresses. The kernel node is added to increase the
|
||||
// graph's runtime.
|
||||
void createSimpleAllocFreeGraph(cudaGraphExec_t *graphExec, float **dPtr,
|
||||
size_t bytes, int device) {
|
||||
cudaGraph_t graph;
|
||||
cudaGraphNode_t allocNodeA, freeNodeA, blockDeviceNode;
|
||||
cudaMemAllocNodeParams allocParams;
|
||||
cudaKernelNodeParams blockDeviceNodeParams = {0};
|
||||
int numElements = bytes / sizeof(float);
|
||||
float kernelTime = 5; // time for each thread to run in microseconds
|
||||
void createSimpleAllocFreeGraph(cudaGraphExec_t *graphExec, float **dPtr, size_t bytes, int device)
|
||||
{
|
||||
cudaGraph_t graph;
|
||||
cudaGraphNode_t allocNodeA, freeNodeA, blockDeviceNode;
|
||||
cudaMemAllocNodeParams allocParams;
|
||||
cudaKernelNodeParams blockDeviceNodeParams = {0};
|
||||
int numElements = bytes / sizeof(float);
|
||||
float kernelTime = 5; // time for each thread to run in microseconds
|
||||
|
||||
checkCudaErrors(cudaGraphCreate(&graph, 0));
|
||||
prepareAllocParams(&allocParams, bytes, device);
|
||||
checkCudaErrors(cudaGraphCreate(&graph, 0));
|
||||
prepareAllocParams(&allocParams, bytes, device);
|
||||
|
||||
checkCudaErrors(
|
||||
cudaGraphAddMemAllocNode(&allocNodeA, graph, NULL, 0, &allocParams));
|
||||
*dPtr = (float *)allocParams.dptr;
|
||||
checkCudaErrors(cudaGraphAddMemAllocNode(&allocNodeA, graph, NULL, 0, &allocParams));
|
||||
*dPtr = (float *)allocParams.dptr;
|
||||
|
||||
cudaDeviceProp deviceProp;
|
||||
checkCudaErrors(cudaGetDeviceProperties(&deviceProp, device));
|
||||
clock_t time_clocks = (clock_t)((kernelTime / 1000.0) * deviceProp.clockRate);
|
||||
cudaDeviceProp deviceProp;
|
||||
checkCudaErrors(cudaGetDeviceProperties(&deviceProp, device));
|
||||
clock_t time_clocks = (clock_t)((kernelTime / 1000.0) * deviceProp.clockRate);
|
||||
|
||||
void *blockDeviceArgs[1] = {(void *)&time_clocks};
|
||||
void *blockDeviceArgs[1] = {(void *)&time_clocks};
|
||||
|
||||
size_t numBlocks = numElements / (size_t)THREADS_PER_BLOCK;
|
||||
blockDeviceNodeParams.gridDim = dim3(numBlocks, 1, 1);
|
||||
blockDeviceNodeParams.blockDim = dim3(THREADS_PER_BLOCK, 1, 1);
|
||||
blockDeviceNodeParams.sharedMemBytes = 0;
|
||||
blockDeviceNodeParams.extra = NULL;
|
||||
blockDeviceNodeParams.func = (void *)clockBlock;
|
||||
blockDeviceNodeParams.kernelParams = (void **)blockDeviceArgs;
|
||||
checkCudaErrors(cudaGraphAddKernelNode(&blockDeviceNode, graph, &allocNodeA,
|
||||
1, &blockDeviceNodeParams));
|
||||
size_t numBlocks = numElements / (size_t)THREADS_PER_BLOCK;
|
||||
blockDeviceNodeParams.gridDim = dim3(numBlocks, 1, 1);
|
||||
blockDeviceNodeParams.blockDim = dim3(THREADS_PER_BLOCK, 1, 1);
|
||||
blockDeviceNodeParams.sharedMemBytes = 0;
|
||||
blockDeviceNodeParams.extra = NULL;
|
||||
blockDeviceNodeParams.func = (void *)clockBlock;
|
||||
blockDeviceNodeParams.kernelParams = (void **)blockDeviceArgs;
|
||||
checkCudaErrors(cudaGraphAddKernelNode(&blockDeviceNode, graph, &allocNodeA, 1, &blockDeviceNodeParams));
|
||||
|
||||
checkCudaErrors(cudaGraphAddMemFreeNode(&freeNodeA, graph, &blockDeviceNode,
|
||||
1, (void *)*dPtr));
|
||||
checkCudaErrors(cudaGraphAddMemFreeNode(&freeNodeA, graph, &blockDeviceNode, 1, (void *)*dPtr));
|
||||
|
||||
checkCudaErrors(cudaGraphInstantiate(graphExec, graph, NULL, NULL, 0));
|
||||
checkCudaErrors(cudaGraphDestroy(graph));
|
||||
checkCudaErrors(cudaGraphInstantiate(graphExec, graph, NULL, NULL, 0));
|
||||
checkCudaErrors(cudaGraphDestroy(graph));
|
||||
}
|
||||
|
||||
void physicalMemoryReuseSingleStream(size_t bytes, int device) {
|
||||
cudaStream_t stream;
|
||||
cudaGraphExec_t graphExecs[NUM_GRAPHS];
|
||||
float *dPtrs[NUM_GRAPHS];
|
||||
bool virtualAddrDiffer = true;
|
||||
void physicalMemoryReuseSingleStream(size_t bytes, int device)
|
||||
{
|
||||
cudaStream_t stream;
|
||||
cudaGraphExec_t graphExecs[NUM_GRAPHS];
|
||||
float *dPtrs[NUM_GRAPHS];
|
||||
bool virtualAddrDiffer = true;
|
||||
|
||||
printf("================================\n");
|
||||
printf("Running physical memory reuse example.\n");
|
||||
printf(
|
||||
"CUDA reuses the same physical memory for allocations from separate "
|
||||
"graphs when the allocation lifetimes don't overlap.\n\n");
|
||||
printf("================================\n");
|
||||
printf("Running physical memory reuse example.\n");
|
||||
printf("CUDA reuses the same physical memory for allocations from separate "
|
||||
"graphs when the allocation lifetimes don't overlap.\n\n");
|
||||
|
||||
for (int i = 0; i < NUM_GRAPHS; i++) {
|
||||
createSimpleAllocFreeGraph(&graphExecs[i], &dPtrs[i], bytes, device);
|
||||
}
|
||||
|
||||
printf("Creating the graph execs does not reserve any physical memory.\n");
|
||||
printMemoryFootprint(device);
|
||||
|
||||
checkCudaErrors(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking));
|
||||
|
||||
checkCudaErrors(cudaGraphLaunch(graphExecs[0], stream));
|
||||
printf("\nThe first graph launched reserves the memory it needs.\n");
|
||||
printMemoryFootprint(device);
|
||||
|
||||
checkCudaErrors(cudaGraphLaunch(graphExecs[0], stream));
|
||||
printf(
|
||||
"A subsequent launch of the same graph in the same stream reuses the "
|
||||
"same physical memory. ");
|
||||
printf("Thus the memory footprint does not grow here.\n");
|
||||
printMemoryFootprint(device);
|
||||
|
||||
printf(
|
||||
"\nSubsequent launches of other graphs in the same stream also reuse the "
|
||||
"physical memory. ");
|
||||
printf("Thus the memory footprint does not grow here.\n");
|
||||
for (int i = 1; i < NUM_GRAPHS; i++) {
|
||||
checkCudaErrors(cudaGraphLaunch(graphExecs[i], stream));
|
||||
printf("%02d: ", i);
|
||||
printMemoryFootprint(device);
|
||||
}
|
||||
|
||||
checkCudaErrors(cudaStreamSynchronize(stream));
|
||||
|
||||
for (int i = 0; i < NUM_GRAPHS; i++) {
|
||||
for (int j = i + 1; j < NUM_GRAPHS; j++) {
|
||||
if (dPtrs[i] == dPtrs[j]) {
|
||||
virtualAddrDiffer = false;
|
||||
printf("Error: Graph exec %d and %d have the same virtual address!\n",
|
||||
i - 1, i);
|
||||
}
|
||||
for (int i = 0; i < NUM_GRAPHS; i++) {
|
||||
createSimpleAllocFreeGraph(&graphExecs[i], &dPtrs[i], bytes, device);
|
||||
}
|
||||
checkCudaErrors(cudaGraphExecDestroy(graphExecs[i]));
|
||||
}
|
||||
if (virtualAddrDiffer) {
|
||||
printf("\nCheck confirms all graphs use a different virtual address.\n");
|
||||
} else {
|
||||
printf(
|
||||
"\nAll graphs do NOT use different virtual addresses. Exiting test.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
checkCudaErrors(cudaStreamDestroy(stream));
|
||||
}
|
||||
|
||||
void simultaneousStreams(size_t bytes, int device) {
|
||||
cudaStream_t streams[NUM_GRAPHS];
|
||||
cudaGraphExec_t graphExecs[NUM_GRAPHS];
|
||||
float *dPtrs[NUM_GRAPHS];
|
||||
|
||||
printf("================================\n");
|
||||
printf("Running simultaneous streams example.\n");
|
||||
printf("Graphs that can run concurrently need separate physical memory. ");
|
||||
printf(
|
||||
"In this example, each graph launched in a separate stream increases the "
|
||||
"total memory footprint.\n\n");
|
||||
|
||||
printf(
|
||||
"When launching a new graph, CUDA may reuse physical memory from a graph "
|
||||
"whose execution has already ");
|
||||
printf(
|
||||
"finished -- even if the new graph is being launched in a different "
|
||||
"stream from the completed graph. ");
|
||||
printf(
|
||||
"Therefore, a kernel node is added to the graphs to increase "
|
||||
"runtime.\n\n");
|
||||
|
||||
for (int i = 0; i < NUM_GRAPHS; i++) {
|
||||
createSimpleAllocFreeGraph(&graphExecs[i], &dPtrs[i], bytes, device);
|
||||
checkCudaErrors(
|
||||
cudaStreamCreateWithFlags(&streams[i], cudaStreamNonBlocking));
|
||||
}
|
||||
|
||||
printf("Initial footprint:\n");
|
||||
printMemoryFootprint(device);
|
||||
|
||||
printf(
|
||||
"\nEach graph launch in a seperate stream grows the memory footprint:\n");
|
||||
for (int i = 1; i < NUM_GRAPHS; i++) {
|
||||
checkCudaErrors(cudaGraphLaunch(graphExecs[i], streams[i]));
|
||||
printf("%02d: ", i);
|
||||
printf("Creating the graph execs does not reserve any physical memory.\n");
|
||||
printMemoryFootprint(device);
|
||||
}
|
||||
|
||||
for (int i = 0; i < NUM_GRAPHS; i++) {
|
||||
checkCudaErrors(cudaStreamSynchronize(streams[i]));
|
||||
checkCudaErrors(cudaGraphExecDestroy(graphExecs[i]));
|
||||
checkCudaErrors(cudaStreamDestroy(streams[i]));
|
||||
}
|
||||
}
|
||||
checkCudaErrors(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking));
|
||||
|
||||
void createSimpleAllocNoFreeGraph(cudaGraphExec_t *graphExec, float **dPtr,
|
||||
size_t bytes, int device) {
|
||||
cudaGraph_t graph;
|
||||
cudaGraphNode_t allocNodeA;
|
||||
cudaMemAllocNodeParams allocParams;
|
||||
|
||||
checkCudaErrors(cudaGraphCreate(&graph, 0));
|
||||
prepareAllocParams(&allocParams, bytes, device);
|
||||
|
||||
checkCudaErrors(
|
||||
cudaGraphAddMemAllocNode(&allocNodeA, graph, NULL, 0, &allocParams));
|
||||
*dPtr = (float *)allocParams.dptr;
|
||||
|
||||
checkCudaErrors(cudaGraphInstantiate(graphExec, graph, NULL, NULL, 0));
|
||||
checkCudaErrors(cudaGraphDestroy(graph));
|
||||
}
|
||||
|
||||
void unfreedAllocations(size_t bytes, int device) {
|
||||
cudaStream_t stream;
|
||||
cudaGraphExec_t graphExecs[NUM_GRAPHS];
|
||||
float *dPtrs[NUM_GRAPHS];
|
||||
|
||||
printf("================================\n");
|
||||
printf("Running unfreed streams example.\n");
|
||||
printf(
|
||||
"CUDA cannot reuse phyiscal memory from graphs which do not free their "
|
||||
"allocations.\n\n");
|
||||
|
||||
for (int i = 0; i < NUM_GRAPHS; i++) {
|
||||
createSimpleAllocNoFreeGraph(&graphExecs[i], &dPtrs[i], bytes, device);
|
||||
}
|
||||
|
||||
checkCudaErrors(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking));
|
||||
|
||||
printf(
|
||||
"Despite being launched in the same stream, each graph launch grows the "
|
||||
"memory footprint. ");
|
||||
printf(
|
||||
"Since the allocation is not freed, CUDA keeps the memory valid for "
|
||||
"use.\n");
|
||||
for (int i = 0; i < NUM_GRAPHS; i++) {
|
||||
checkCudaErrors(cudaGraphLaunch(graphExecs[i], stream));
|
||||
printf("%02d: ", i);
|
||||
checkCudaErrors(cudaGraphLaunch(graphExecs[0], stream));
|
||||
printf("\nThe first graph launched reserves the memory it needs.\n");
|
||||
printMemoryFootprint(device);
|
||||
}
|
||||
|
||||
checkCudaErrors(cudaStreamSynchronize(stream));
|
||||
checkCudaErrors(cudaGraphLaunch(graphExecs[0], stream));
|
||||
printf("A subsequent launch of the same graph in the same stream reuses the "
|
||||
"same physical memory. ");
|
||||
printf("Thus the memory footprint does not grow here.\n");
|
||||
printMemoryFootprint(device);
|
||||
|
||||
checkCudaErrors(cudaDeviceGraphMemTrim(device));
|
||||
printf(
|
||||
"\nTrimming does not impact the memory footprint since the un-freed "
|
||||
"allocations are still holding onto the memory.\n");
|
||||
printMemoryFootprint(device);
|
||||
printf("\nSubsequent launches of other graphs in the same stream also reuse the "
|
||||
"physical memory. ");
|
||||
printf("Thus the memory footprint does not grow here.\n");
|
||||
for (int i = 1; i < NUM_GRAPHS; i++) {
|
||||
checkCudaErrors(cudaGraphLaunch(graphExecs[i], stream));
|
||||
printf("%02d: ", i);
|
||||
printMemoryFootprint(device);
|
||||
}
|
||||
|
||||
for (int i = 0; i < NUM_GRAPHS; i++) {
|
||||
checkCudaErrors(cudaFree(dPtrs[i]));
|
||||
}
|
||||
printf("\nFreeing the allocations does not shrink the footprint.\n");
|
||||
printMemoryFootprint(device);
|
||||
checkCudaErrors(cudaStreamSynchronize(stream));
|
||||
|
||||
checkCudaErrors(cudaDeviceGraphMemTrim(device));
|
||||
printf(
|
||||
"\nSince the allocations are now freed, trimming does reduce the "
|
||||
"footprint even when the graph execs are not yet destroyed.\n");
|
||||
printMemoryFootprint(device);
|
||||
for (int i = 0; i < NUM_GRAPHS; i++) {
|
||||
for (int j = i + 1; j < NUM_GRAPHS; j++) {
|
||||
if (dPtrs[i] == dPtrs[j]) {
|
||||
virtualAddrDiffer = false;
|
||||
printf("Error: Graph exec %d and %d have the same virtual address!\n", i - 1, i);
|
||||
}
|
||||
}
|
||||
checkCudaErrors(cudaGraphExecDestroy(graphExecs[i]));
|
||||
}
|
||||
if (virtualAddrDiffer) {
|
||||
printf("\nCheck confirms all graphs use a different virtual address.\n");
|
||||
}
|
||||
else {
|
||||
printf("\nAll graphs do NOT use different virtual addresses. Exiting test.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
for (int i = 0; i < NUM_GRAPHS; i++) {
|
||||
checkCudaErrors(cudaGraphExecDestroy(graphExecs[i]));
|
||||
}
|
||||
checkCudaErrors(cudaStreamDestroy(stream));
|
||||
checkCudaErrors(cudaStreamDestroy(stream));
|
||||
}
|
||||
|
||||
void cleanupMemory(int device) {
|
||||
checkCudaErrors(cudaDeviceGraphMemTrim(device));
|
||||
printf("\nCleaning up example by trimming device memory.\n");
|
||||
printMemoryFootprint(device);
|
||||
printf("\n");
|
||||
void simultaneousStreams(size_t bytes, int device)
|
||||
{
|
||||
cudaStream_t streams[NUM_GRAPHS];
|
||||
cudaGraphExec_t graphExecs[NUM_GRAPHS];
|
||||
float *dPtrs[NUM_GRAPHS];
|
||||
|
||||
printf("================================\n");
|
||||
printf("Running simultaneous streams example.\n");
|
||||
printf("Graphs that can run concurrently need separate physical memory. ");
|
||||
printf("In this example, each graph launched in a separate stream increases the "
|
||||
"total memory footprint.\n\n");
|
||||
|
||||
printf("When launching a new graph, CUDA may reuse physical memory from a graph "
|
||||
"whose execution has already ");
|
||||
printf("finished -- even if the new graph is being launched in a different "
|
||||
"stream from the completed graph. ");
|
||||
printf("Therefore, a kernel node is added to the graphs to increase "
|
||||
"runtime.\n\n");
|
||||
|
||||
for (int i = 0; i < NUM_GRAPHS; i++) {
|
||||
createSimpleAllocFreeGraph(&graphExecs[i], &dPtrs[i], bytes, device);
|
||||
checkCudaErrors(cudaStreamCreateWithFlags(&streams[i], cudaStreamNonBlocking));
|
||||
}
|
||||
|
||||
printf("Initial footprint:\n");
|
||||
printMemoryFootprint(device);
|
||||
|
||||
printf("\nEach graph launch in a seperate stream grows the memory footprint:\n");
|
||||
for (int i = 1; i < NUM_GRAPHS; i++) {
|
||||
checkCudaErrors(cudaGraphLaunch(graphExecs[i], streams[i]));
|
||||
printf("%02d: ", i);
|
||||
printMemoryFootprint(device);
|
||||
}
|
||||
|
||||
for (int i = 0; i < NUM_GRAPHS; i++) {
|
||||
checkCudaErrors(cudaStreamSynchronize(streams[i]));
|
||||
checkCudaErrors(cudaGraphExecDestroy(graphExecs[i]));
|
||||
checkCudaErrors(cudaStreamDestroy(streams[i]));
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
size_t bytes = 64 * 1024 * 1024;
|
||||
int device = findCudaDevice(argc, (const char **)argv);
|
||||
void createSimpleAllocNoFreeGraph(cudaGraphExec_t *graphExec, float **dPtr, size_t bytes, int device)
|
||||
{
|
||||
cudaGraph_t graph;
|
||||
cudaGraphNode_t allocNodeA;
|
||||
cudaMemAllocNodeParams allocParams;
|
||||
|
||||
int driverVersion = 0;
|
||||
int deviceSupportsMemoryPools = 0;
|
||||
checkCudaErrors(cudaGraphCreate(&graph, 0));
|
||||
prepareAllocParams(&allocParams, bytes, device);
|
||||
|
||||
cudaDriverGetVersion(&driverVersion);
|
||||
printf("Driver version is: %d.%d\n", driverVersion / 1000,
|
||||
(driverVersion % 100) / 10);
|
||||
checkCudaErrors(cudaGraphAddMemAllocNode(&allocNodeA, graph, NULL, 0, &allocParams));
|
||||
*dPtr = (float *)allocParams.dptr;
|
||||
|
||||
if (driverVersion < 11040) {
|
||||
printf("Waiving execution as driver does not support Graph Memory Nodes\n");
|
||||
exit(EXIT_WAIVED);
|
||||
}
|
||||
|
||||
cudaDeviceGetAttribute(&deviceSupportsMemoryPools,
|
||||
cudaDevAttrMemoryPoolsSupported, device);
|
||||
if (!deviceSupportsMemoryPools) {
|
||||
printf("Waiving execution as device does not support Memory Pools\n");
|
||||
exit(EXIT_WAIVED);
|
||||
} else {
|
||||
printf("Running sample.\n");
|
||||
}
|
||||
|
||||
virtualAddressReuseSingleGraph(bytes, device);
|
||||
cleanupMemory(device);
|
||||
|
||||
physicalMemoryReuseSingleStream(bytes, device);
|
||||
cleanupMemory(device);
|
||||
|
||||
simultaneousStreams(bytes, device);
|
||||
cleanupMemory(device);
|
||||
|
||||
unfreedAllocations(bytes, device);
|
||||
cleanupMemory(device);
|
||||
|
||||
printf("================================\n");
|
||||
printf("Sample complete.\n");
|
||||
checkCudaErrors(cudaGraphInstantiate(graphExec, graph, NULL, NULL, 0));
|
||||
checkCudaErrors(cudaGraphDestroy(graph));
|
||||
}
|
||||
|
||||
void unfreedAllocations(size_t bytes, int device)
|
||||
{
|
||||
cudaStream_t stream;
|
||||
cudaGraphExec_t graphExecs[NUM_GRAPHS];
|
||||
float *dPtrs[NUM_GRAPHS];
|
||||
|
||||
printf("================================\n");
|
||||
printf("Running unfreed streams example.\n");
|
||||
printf("CUDA cannot reuse phyiscal memory from graphs which do not free their "
|
||||
"allocations.\n\n");
|
||||
|
||||
for (int i = 0; i < NUM_GRAPHS; i++) {
|
||||
createSimpleAllocNoFreeGraph(&graphExecs[i], &dPtrs[i], bytes, device);
|
||||
}
|
||||
|
||||
checkCudaErrors(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking));
|
||||
|
||||
printf("Despite being launched in the same stream, each graph launch grows the "
|
||||
"memory footprint. ");
|
||||
printf("Since the allocation is not freed, CUDA keeps the memory valid for "
|
||||
"use.\n");
|
||||
for (int i = 0; i < NUM_GRAPHS; i++) {
|
||||
checkCudaErrors(cudaGraphLaunch(graphExecs[i], stream));
|
||||
printf("%02d: ", i);
|
||||
printMemoryFootprint(device);
|
||||
}
|
||||
|
||||
checkCudaErrors(cudaStreamSynchronize(stream));
|
||||
|
||||
checkCudaErrors(cudaDeviceGraphMemTrim(device));
|
||||
printf("\nTrimming does not impact the memory footprint since the un-freed "
|
||||
"allocations are still holding onto the memory.\n");
|
||||
printMemoryFootprint(device);
|
||||
|
||||
for (int i = 0; i < NUM_GRAPHS; i++) {
|
||||
checkCudaErrors(cudaFree(dPtrs[i]));
|
||||
}
|
||||
printf("\nFreeing the allocations does not shrink the footprint.\n");
|
||||
printMemoryFootprint(device);
|
||||
|
||||
checkCudaErrors(cudaDeviceGraphMemTrim(device));
|
||||
printf("\nSince the allocations are now freed, trimming does reduce the "
|
||||
"footprint even when the graph execs are not yet destroyed.\n");
|
||||
printMemoryFootprint(device);
|
||||
|
||||
for (int i = 0; i < NUM_GRAPHS; i++) {
|
||||
checkCudaErrors(cudaGraphExecDestroy(graphExecs[i]));
|
||||
}
|
||||
checkCudaErrors(cudaStreamDestroy(stream));
|
||||
}
|
||||
|
||||
void cleanupMemory(int device)
|
||||
{
|
||||
checkCudaErrors(cudaDeviceGraphMemTrim(device));
|
||||
printf("\nCleaning up example by trimming device memory.\n");
|
||||
printMemoryFootprint(device);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
size_t bytes = 64 * 1024 * 1024;
|
||||
int device = findCudaDevice(argc, (const char **)argv);
|
||||
|
||||
int driverVersion = 0;
|
||||
int deviceSupportsMemoryPools = 0;
|
||||
|
||||
cudaDriverGetVersion(&driverVersion);
|
||||
printf("Driver version is: %d.%d\n", driverVersion / 1000, (driverVersion % 100) / 10);
|
||||
|
||||
if (driverVersion < 11040) {
|
||||
printf("Waiving execution as driver does not support Graph Memory Nodes\n");
|
||||
exit(EXIT_WAIVED);
|
||||
}
|
||||
|
||||
cudaDeviceGetAttribute(&deviceSupportsMemoryPools, cudaDevAttrMemoryPoolsSupported, device);
|
||||
if (!deviceSupportsMemoryPools) {
|
||||
printf("Waiving execution as device does not support Memory Pools\n");
|
||||
exit(EXIT_WAIVED);
|
||||
}
|
||||
else {
|
||||
printf("Running sample.\n");
|
||||
}
|
||||
|
||||
virtualAddressReuseSingleGraph(bytes, device);
|
||||
cleanupMemory(device);
|
||||
|
||||
physicalMemoryReuseSingleStream(bytes, device);
|
||||
cleanupMemory(device);
|
||||
|
||||
simultaneousStreams(bytes, device);
|
||||
cleanupMemory(device);
|
||||
|
||||
unfreedAllocations(bytes, device);
|
||||
cleanupMemory(device);
|
||||
|
||||
printf("================================\n");
|
||||
printf("Sample complete.\n");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user