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:
@@ -31,10 +31,10 @@
|
||||
*/
|
||||
|
||||
// includes, system
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// includes CUDA
|
||||
#include <cuda_runtime.h>
|
||||
@@ -47,34 +47,34 @@
|
||||
// declaration, forward
|
||||
void runTest(int argc, char **argv);
|
||||
|
||||
extern "C" void computeGold(float *reference, float *idata,
|
||||
const unsigned int len);
|
||||
extern "C" void computeGold(float *reference, float *idata, const unsigned int len);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//! Simple test kernel for device functionality
|
||||
//! @param g_idata input data in global memory
|
||||
//! @param g_odata output data in global memory
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
__global__ void testKernel(float *g_idata, float *g_odata) {
|
||||
// shared memory
|
||||
// the size is determined by the host application
|
||||
extern __shared__ float sdata[];
|
||||
__global__ void testKernel(float *g_idata, float *g_odata)
|
||||
{
|
||||
// shared memory
|
||||
// the size is determined by the host application
|
||||
extern __shared__ float sdata[];
|
||||
|
||||
// access thread id
|
||||
const unsigned int tid = threadIdx.x;
|
||||
// access number of threads in this block
|
||||
const unsigned int num_threads = blockDim.x;
|
||||
// access thread id
|
||||
const unsigned int tid = threadIdx.x;
|
||||
// access number of threads in this block
|
||||
const unsigned int num_threads = blockDim.x;
|
||||
|
||||
// read in input data from global memory
|
||||
sdata[tid] = g_idata[tid];
|
||||
__syncthreads();
|
||||
// read in input data from global memory
|
||||
sdata[tid] = g_idata[tid];
|
||||
__syncthreads();
|
||||
|
||||
// perform some computations
|
||||
sdata[tid] = (float)num_threads * sdata[tid];
|
||||
__syncthreads();
|
||||
// perform some computations
|
||||
sdata[tid] = (float)num_threads * sdata[tid];
|
||||
__syncthreads();
|
||||
|
||||
// write data to global memory
|
||||
g_odata[tid] = sdata[tid];
|
||||
// write data to global memory
|
||||
g_odata[tid] = sdata[tid];
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -85,81 +85,81 @@ int main(int argc, char **argv) { runTest(argc, argv); }
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//! Run a simple test for CUDA
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void runTest(int argc, char **argv) {
|
||||
bool bTestResult = true;
|
||||
void runTest(int argc, char **argv)
|
||||
{
|
||||
bool bTestResult = true;
|
||||
|
||||
printf("%s Starting...\n\n", argv[0]);
|
||||
printf("%s Starting...\n\n", argv[0]);
|
||||
|
||||
// use command-line specified CUDA device, otherwise use device with highest
|
||||
// Gflops/s
|
||||
int devID = findCudaDevice(argc, (const char **)argv);
|
||||
// use command-line specified CUDA device, otherwise use device with highest
|
||||
// Gflops/s
|
||||
int devID = findCudaDevice(argc, (const char **)argv);
|
||||
|
||||
StopWatchInterface *timer = 0;
|
||||
sdkCreateTimer(&timer);
|
||||
sdkStartTimer(&timer);
|
||||
StopWatchInterface *timer = 0;
|
||||
sdkCreateTimer(&timer);
|
||||
sdkStartTimer(&timer);
|
||||
|
||||
unsigned int num_threads = 32;
|
||||
unsigned int mem_size = sizeof(float) * num_threads;
|
||||
unsigned int num_threads = 32;
|
||||
unsigned int mem_size = sizeof(float) * num_threads;
|
||||
|
||||
// allocate host memory
|
||||
float *h_idata = (float *)malloc(mem_size);
|
||||
// allocate host memory
|
||||
float *h_idata = (float *)malloc(mem_size);
|
||||
|
||||
// initalize the memory
|
||||
for (unsigned int i = 0; i < num_threads; ++i) {
|
||||
h_idata[i] = (float)i;
|
||||
}
|
||||
// initalize the memory
|
||||
for (unsigned int i = 0; i < num_threads; ++i) {
|
||||
h_idata[i] = (float)i;
|
||||
}
|
||||
|
||||
// allocate device memory
|
||||
float *d_idata;
|
||||
checkCudaErrors(cudaMalloc((void **)&d_idata, mem_size));
|
||||
// copy host memory to device
|
||||
checkCudaErrors(
|
||||
cudaMemcpy(d_idata, h_idata, mem_size, cudaMemcpyHostToDevice));
|
||||
// allocate device memory
|
||||
float *d_idata;
|
||||
checkCudaErrors(cudaMalloc((void **)&d_idata, mem_size));
|
||||
// copy host memory to device
|
||||
checkCudaErrors(cudaMemcpy(d_idata, h_idata, mem_size, cudaMemcpyHostToDevice));
|
||||
|
||||
// allocate device memory for result
|
||||
float *d_odata;
|
||||
checkCudaErrors(cudaMalloc((void **)&d_odata, mem_size));
|
||||
// allocate device memory for result
|
||||
float *d_odata;
|
||||
checkCudaErrors(cudaMalloc((void **)&d_odata, mem_size));
|
||||
|
||||
// setup execution parameters
|
||||
dim3 grid(1, 1, 1);
|
||||
dim3 threads(num_threads, 1, 1);
|
||||
// setup execution parameters
|
||||
dim3 grid(1, 1, 1);
|
||||
dim3 threads(num_threads, 1, 1);
|
||||
|
||||
// execute the kernel
|
||||
testKernel<<<grid, threads, mem_size>>>(d_idata, d_odata);
|
||||
// execute the kernel
|
||||
testKernel<<<grid, threads, mem_size>>>(d_idata, d_odata);
|
||||
|
||||
// check if kernel execution generated and error
|
||||
getLastCudaError("Kernel execution failed");
|
||||
// check if kernel execution generated and error
|
||||
getLastCudaError("Kernel execution failed");
|
||||
|
||||
// allocate mem for the result on host side
|
||||
float *h_odata = (float *)malloc(mem_size);
|
||||
// copy result from device to host
|
||||
checkCudaErrors(cudaMemcpy(h_odata, d_odata, sizeof(float) * num_threads,
|
||||
cudaMemcpyDeviceToHost));
|
||||
// allocate mem for the result on host side
|
||||
float *h_odata = (float *)malloc(mem_size);
|
||||
// copy result from device to host
|
||||
checkCudaErrors(cudaMemcpy(h_odata, d_odata, sizeof(float) * num_threads, cudaMemcpyDeviceToHost));
|
||||
|
||||
sdkStopTimer(&timer);
|
||||
printf("Processing time: %f (ms)\n", sdkGetTimerValue(&timer));
|
||||
sdkDeleteTimer(&timer);
|
||||
sdkStopTimer(&timer);
|
||||
printf("Processing time: %f (ms)\n", sdkGetTimerValue(&timer));
|
||||
sdkDeleteTimer(&timer);
|
||||
|
||||
// compute reference solution
|
||||
float *reference = (float *)malloc(mem_size);
|
||||
computeGold(reference, h_idata, num_threads);
|
||||
// compute reference solution
|
||||
float *reference = (float *)malloc(mem_size);
|
||||
computeGold(reference, h_idata, num_threads);
|
||||
|
||||
// check result
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "regression")) {
|
||||
// write file for regression test
|
||||
sdkWriteFile("./data/regression.dat", h_odata, num_threads, 0.0f, false);
|
||||
} else {
|
||||
// custom output handling when no regression test running
|
||||
// in this case check if the result is equivalent to the expected solution
|
||||
bTestResult = compareData(reference, h_odata, num_threads, 0.0f, 0.0f);
|
||||
}
|
||||
// check result
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "regression")) {
|
||||
// write file for regression test
|
||||
sdkWriteFile("./data/regression.dat", h_odata, num_threads, 0.0f, false);
|
||||
}
|
||||
else {
|
||||
// custom output handling when no regression test running
|
||||
// in this case check if the result is equivalent to the expected solution
|
||||
bTestResult = compareData(reference, h_odata, num_threads, 0.0f, 0.0f);
|
||||
}
|
||||
|
||||
// cleanup memory
|
||||
free(h_idata);
|
||||
free(h_odata);
|
||||
free(reference);
|
||||
checkCudaErrors(cudaFree(d_idata));
|
||||
checkCudaErrors(cudaFree(d_odata));
|
||||
// cleanup memory
|
||||
free(h_idata);
|
||||
free(h_odata);
|
||||
free(reference);
|
||||
checkCudaErrors(cudaFree(d_idata));
|
||||
checkCudaErrors(cudaFree(d_odata));
|
||||
|
||||
exit(bTestResult ? EXIT_SUCCESS : EXIT_FAILURE);
|
||||
exit(bTestResult ? EXIT_SUCCESS : EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -26,8 +26,7 @@
|
||||
*/
|
||||
|
||||
// export C interface
|
||||
extern "C" void computeGold(float *reference, float *idata,
|
||||
const unsigned int len);
|
||||
extern "C" void computeGold(float *reference, float *idata, const unsigned int len);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//! Compute reference data set
|
||||
@@ -36,10 +35,11 @@ extern "C" void computeGold(float *reference, float *idata,
|
||||
//! @param idata input data as provided to device
|
||||
//! @param len number of elements in reference / idata
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void computeGold(float *reference, float *idata, const unsigned int len) {
|
||||
const float f_len = static_cast<float>(len);
|
||||
void computeGold(float *reference, float *idata, const unsigned int len)
|
||||
{
|
||||
const float f_len = static_cast<float>(len);
|
||||
|
||||
for (unsigned int i = 0; i < len; ++i) {
|
||||
reference[i] = idata[i] * f_len;
|
||||
}
|
||||
for (unsigned int i = 0; i < len; ++i) {
|
||||
reference[i] = idata[i] * f_len;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user