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:
@@ -25,11 +25,11 @@
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <helper_cuda.h>
|
||||
#include <cuda.h>
|
||||
#include <cuda_runtime_api.h>
|
||||
#include <helper_cuda.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
cudaError_t setProp(CUmemAllocationProp *prop, bool UseCompressibleMemory)
|
||||
{
|
||||
@@ -38,9 +38,9 @@ cudaError_t setProp(CUmemAllocationProp *prop, bool UseCompressibleMemory)
|
||||
return cudaErrorMemoryAllocation;
|
||||
|
||||
memset(prop, 0, sizeof(CUmemAllocationProp));
|
||||
prop->type = CU_MEM_ALLOCATION_TYPE_PINNED;
|
||||
prop->type = CU_MEM_ALLOCATION_TYPE_PINNED;
|
||||
prop->location.type = CU_MEM_LOCATION_TYPE_DEVICE;
|
||||
prop->location.id = currentDevice;
|
||||
prop->location.id = currentDevice;
|
||||
|
||||
if (UseCompressibleMemory)
|
||||
prop->allocFlags.compressionType = CU_MEM_ALLOCATION_COMP_GENERIC;
|
||||
@@ -51,13 +51,12 @@ cudaError_t setProp(CUmemAllocationProp *prop, bool UseCompressibleMemory)
|
||||
cudaError_t allocateCompressible(void **adr, size_t size, bool UseCompressibleMemory)
|
||||
{
|
||||
CUmemAllocationProp prop = {};
|
||||
cudaError_t err = setProp(&prop, UseCompressibleMemory);
|
||||
cudaError_t err = setProp(&prop, UseCompressibleMemory);
|
||||
if (err != cudaSuccess)
|
||||
return err;
|
||||
|
||||
size_t granularity = 0;
|
||||
if (cuMemGetAllocationGranularity(&granularity, &prop,
|
||||
CU_MEM_ALLOC_GRANULARITY_MINIMUM) != CUDA_SUCCESS)
|
||||
if (cuMemGetAllocationGranularity(&granularity, &prop, CU_MEM_ALLOC_GRANULARITY_MINIMUM) != CUDA_SUCCESS)
|
||||
return cudaErrorMemoryAllocation;
|
||||
size = ((size - 1) / granularity + 1) * granularity;
|
||||
CUdeviceptr dptr;
|
||||
@@ -85,9 +84,9 @@ cudaError_t allocateCompressible(void **adr, size_t size, bool UseCompressibleMe
|
||||
return cudaErrorMemoryAllocation;
|
||||
|
||||
CUmemAccessDesc accessDescriptor;
|
||||
accessDescriptor.location.id = prop.location.id;
|
||||
accessDescriptor.location.id = prop.location.id;
|
||||
accessDescriptor.location.type = prop.location.type;
|
||||
accessDescriptor.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
|
||||
accessDescriptor.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
|
||||
|
||||
if (cuMemSetAccess(dptr, size, &accessDescriptor, 1) != CUDA_SUCCESS)
|
||||
return cudaErrorMemoryAllocation;
|
||||
@@ -99,20 +98,18 @@ cudaError_t allocateCompressible(void **adr, size_t size, bool UseCompressibleMe
|
||||
cudaError_t freeCompressible(void *ptr, size_t size, bool UseCompressibleMemory)
|
||||
{
|
||||
CUmemAllocationProp prop = {};
|
||||
cudaError_t err = setProp(&prop, UseCompressibleMemory);
|
||||
cudaError_t err = setProp(&prop, UseCompressibleMemory);
|
||||
if (err != cudaSuccess)
|
||||
return err;
|
||||
|
||||
size_t granularity = 0;
|
||||
if (cuMemGetAllocationGranularity(&granularity, &prop,
|
||||
CU_MEM_ALLOC_GRANULARITY_MINIMUM) != CUDA_SUCCESS)
|
||||
if (cuMemGetAllocationGranularity(&granularity, &prop, CU_MEM_ALLOC_GRANULARITY_MINIMUM) != CUDA_SUCCESS)
|
||||
return cudaErrorMemoryAllocation;
|
||||
size = ((size - 1) / granularity + 1) * granularity;
|
||||
|
||||
if (ptr == NULL)
|
||||
return cudaSuccess;
|
||||
if (cuMemUnmap((CUdeviceptr)ptr, size) != CUDA_SUCCESS ||
|
||||
cuMemAddressFree((CUdeviceptr)ptr, size) != CUDA_SUCCESS)
|
||||
if (cuMemUnmap((CUdeviceptr)ptr, size) != CUDA_SUCCESS || cuMemAddressFree((CUdeviceptr)ptr, size) != CUDA_SUCCESS)
|
||||
return cudaErrorInvalidValue;
|
||||
return cudaSuccess;
|
||||
}
|
||||
|
||||
@@ -27,53 +27,54 @@
|
||||
|
||||
//
|
||||
// This sample uses the compressible memory allocation if device supports it
|
||||
// and performs saxpy on it.
|
||||
// Compressible memory may give better performance if the data is amenable to
|
||||
// and performs saxpy on it.
|
||||
// Compressible memory may give better performance if the data is amenable to
|
||||
// compression.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <cuda.h>
|
||||
#include <stdio.h>
|
||||
#define CUDA_DRIVER_API
|
||||
#include "helper_cuda.h"
|
||||
#include "compMalloc.h"
|
||||
#include "helper_cuda.h"
|
||||
|
||||
__global__ void saxpy(const float a, const float4 *x, const float4 *y, float4 *z, const size_t n)
|
||||
{
|
||||
for (size_t i = blockIdx.x * blockDim.x + threadIdx.x; i < n; i += gridDim.x * blockDim.x)
|
||||
{
|
||||
for (size_t i = blockIdx.x * blockDim.x + threadIdx.x; i < n; i += gridDim.x * blockDim.x) {
|
||||
const float4 x4 = x[i];
|
||||
const float4 y4 = y[i];
|
||||
z[i] = make_float4(a * x4.x + y4.x, a * x4.y + y4.y,
|
||||
a * x4.z + y4.z, a * x4.w + y4.w);
|
||||
z[i] = make_float4(a * x4.x + y4.x, a * x4.y + y4.y, a * x4.z + y4.z, a * x4.w + y4.w);
|
||||
}
|
||||
}
|
||||
|
||||
__global__ void init(float4 *x, float4 *y, const float val, const size_t n)
|
||||
{
|
||||
const float4 val4 = make_float4(val, val, val, val);
|
||||
for (size_t i = blockIdx.x * blockDim.x + threadIdx.x; i < n; i += gridDim.x * blockDim.x)
|
||||
{
|
||||
for (size_t i = blockIdx.x * blockDim.x + threadIdx.x; i < n; i += gridDim.x * blockDim.x) {
|
||||
x[i] = y[i] = val4;
|
||||
}
|
||||
}
|
||||
|
||||
void launchSaxpy(const float a, float4 *x, float4 *y, float4 *z, const size_t n, const float init_val, const bool compressibleZbuf)
|
||||
void launchSaxpy(const float a,
|
||||
float4 *x,
|
||||
float4 *y,
|
||||
float4 *z,
|
||||
const size_t n,
|
||||
const float init_val,
|
||||
const bool compressibleZbuf)
|
||||
{
|
||||
cudaEvent_t start, stop;
|
||||
float ms;
|
||||
int blockSize;
|
||||
int minGridSize;
|
||||
dim3 threads, blocks;
|
||||
float ms;
|
||||
int blockSize;
|
||||
int minGridSize;
|
||||
dim3 threads, blocks;
|
||||
|
||||
if (!compressibleZbuf)
|
||||
{
|
||||
if (!compressibleZbuf) {
|
||||
// We are on config where compressible buffer can only be initialized through cudaMemcpy
|
||||
// hence, x & y buffers are allocated as compressible and initialized via cudaMemcpy
|
||||
// whereas z buffer is allocated as non-compressible.
|
||||
float4 *h_x = (float4 *) malloc(sizeof(float4) * n);
|
||||
float4 *h_y = (float4 *) malloc(sizeof(float4) * n);
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
float4 *h_x = (float4 *)malloc(sizeof(float4) * n);
|
||||
float4 *h_y = (float4 *)malloc(sizeof(float4) * n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
h_x[i].x = h_x[i].y = h_x[i].z = h_x[i].w = init_val;
|
||||
h_y[i].x = h_y[i].y = h_y[i].z = h_y[i].w = init_val;
|
||||
}
|
||||
@@ -82,15 +83,14 @@ void launchSaxpy(const float a, float4 *x, float4 *y, float4 *z, const size_t n,
|
||||
free(h_x);
|
||||
free(h_y);
|
||||
}
|
||||
else
|
||||
{
|
||||
checkCudaErrors(cudaOccupancyMaxPotentialBlockSize(&minGridSize, &blockSize, (void*)init));
|
||||
else {
|
||||
checkCudaErrors(cudaOccupancyMaxPotentialBlockSize(&minGridSize, &blockSize, (void *)init));
|
||||
threads = dim3(blockSize, 1, 1);
|
||||
blocks = dim3(minGridSize, 1, 1);
|
||||
init<<<blocks, threads>>>(x, y, init_val, n);
|
||||
}
|
||||
|
||||
checkCudaErrors(cudaOccupancyMaxPotentialBlockSize(&minGridSize, &blockSize, (void*)saxpy));
|
||||
checkCudaErrors(cudaOccupancyMaxPotentialBlockSize(&minGridSize, &blockSize, (void *)saxpy));
|
||||
threads = dim3(blockSize, 1, 1);
|
||||
blocks = dim3(minGridSize, 1, 1);
|
||||
|
||||
@@ -103,39 +103,39 @@ void launchSaxpy(const float a, float4 *x, float4 *y, float4 *z, const size_t n,
|
||||
checkCudaErrors(cudaEventElapsedTime(&ms, start, stop));
|
||||
|
||||
const size_t size = n * sizeof(float4);
|
||||
printf("Running saxpy with %d blocks x %d threads = %.3f ms %.3f TB/s\n", blocks.x, threads.x, ms, (size*3)/ms/1e9);
|
||||
printf("Running saxpy with %d blocks x %d threads = %.3f ms %.3f TB/s\n",
|
||||
blocks.x,
|
||||
threads.x,
|
||||
ms,
|
||||
(size * 3) / ms / 1e9);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
const size_t n = 10485760;
|
||||
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "help") ||
|
||||
checkCmdLineFlag(argc, (const char **)argv, "?")) {
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "help") || checkCmdLineFlag(argc, (const char **)argv, "?")) {
|
||||
printf("Usage -device=n (n >= 0 for deviceID)\n");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
findCudaDevice(argc, (const char**)argv);
|
||||
findCudaDevice(argc, (const char **)argv);
|
||||
CUdevice currentDevice;
|
||||
checkCudaErrors(cuCtxGetDevice(¤tDevice));
|
||||
|
||||
// Check that the selected device supports virtual memory management
|
||||
int vmm_supported = -1;
|
||||
checkCudaErrors(cuDeviceGetAttribute(&vmm_supported,
|
||||
CU_DEVICE_ATTRIBUTE_VIRTUAL_ADDRESS_MANAGEMENT_SUPPORTED,
|
||||
currentDevice));
|
||||
checkCudaErrors(
|
||||
cuDeviceGetAttribute(&vmm_supported, CU_DEVICE_ATTRIBUTE_VIRTUAL_ADDRESS_MANAGEMENT_SUPPORTED, currentDevice));
|
||||
if (vmm_supported == 0) {
|
||||
printf("Device %d doesn't support Virtual Memory Management, waiving the execution.\n", currentDevice);
|
||||
exit(EXIT_WAIVED);
|
||||
}
|
||||
|
||||
int isCompressionAvailable;
|
||||
checkCudaErrors(cuDeviceGetAttribute(&isCompressionAvailable,
|
||||
CU_DEVICE_ATTRIBUTE_GENERIC_COMPRESSION_SUPPORTED,
|
||||
currentDevice));
|
||||
if (isCompressionAvailable == 0)
|
||||
{
|
||||
checkCudaErrors(cuDeviceGetAttribute(
|
||||
&isCompressionAvailable, CU_DEVICE_ATTRIBUTE_GENERIC_COMPRESSION_SUPPORTED, currentDevice));
|
||||
if (isCompressionAvailable == 0) {
|
||||
printf("Device %d doesn't support Generic memory compression, waiving the execution.\n", currentDevice);
|
||||
exit(EXIT_WAIVED);
|
||||
}
|
||||
@@ -143,39 +143,33 @@ int main(int argc, char **argv)
|
||||
printf("Generic memory compression support is available\n");
|
||||
|
||||
int major, minor;
|
||||
checkCudaErrors(cuDeviceGetAttribute(&major,
|
||||
CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR,
|
||||
currentDevice));
|
||||
checkCudaErrors(cuDeviceGetAttribute(&minor,
|
||||
CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR,
|
||||
currentDevice));
|
||||
float4 *x, *y, *z;
|
||||
checkCudaErrors(cuDeviceGetAttribute(&major, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, currentDevice));
|
||||
checkCudaErrors(cuDeviceGetAttribute(&minor, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, currentDevice));
|
||||
float4 *x, *y, *z;
|
||||
const size_t size = n * sizeof(float4);
|
||||
|
||||
// Allocating compressible memory
|
||||
checkCudaErrors(allocateCompressible((void **)&x, size, true));
|
||||
checkCudaErrors(allocateCompressible((void **)&y, size, true));
|
||||
bool compressibleZbuf = 0;
|
||||
if ((major == 8 && minor == 0) || (major == 8 && minor == 6))
|
||||
{
|
||||
// On SM 8.0 and 8.6 GPUs compressible buffer can only be initialized
|
||||
if ((major == 8 && minor == 0) || (major == 8 && minor == 6)) {
|
||||
// On SM 8.0 and 8.6 GPUs compressible buffer can only be initialized
|
||||
// through cudaMemcpy.
|
||||
printf("allocating non-compressible Z buffer\n");
|
||||
checkCudaErrors(allocateCompressible((void **)&z, size, false));
|
||||
compressibleZbuf = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
checkCudaErrors(allocateCompressible((void **)&z, size, true));
|
||||
compressibleZbuf = 1;
|
||||
}
|
||||
|
||||
printf("Running saxpy on %zu bytes of Compressible memory\n", size);
|
||||
|
||||
const float a = 1.0f;
|
||||
const float a = 1.0f;
|
||||
const float init_val = 1.0f;
|
||||
launchSaxpy(a, x, y, z, n, init_val, compressibleZbuf);
|
||||
|
||||
|
||||
checkCudaErrors(freeCompressible(x, size, true));
|
||||
checkCudaErrors(freeCompressible(y, size, true));
|
||||
checkCudaErrors(freeCompressible(z, size, true));
|
||||
@@ -193,6 +187,6 @@ int main(int argc, char **argv)
|
||||
checkCudaErrors(freeCompressible(z, size, false));
|
||||
|
||||
printf("\nNOTE: The CUDA Samples are not meant for performance measurements. "
|
||||
"Results may vary when GPU Boost is enabled.\n");
|
||||
"Results may vary when GPU Boost is enabled.\n");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user