Apply consistent code formatting across the repo. Add clang-format and pre-commit hooks.

This commit is contained in:
Rob Armstrong
2025-03-27 10:30:07 -07:00
parent 2cd58fbc9a
commit ceab6e8bcc
782 changed files with 107230 additions and 106548 deletions

View File

@@ -30,19 +30,17 @@
* given set of input vector pairs
*/
#include <helper_cuda.h>
#include <helper_functions.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <helper_functions.h>
#include <helper_cuda.h>
#include <time.h>
///////////////////////////////////////////////////////////////////////////////
// Calculate scalar products of VectorN vectors of ElementN elements on CPU
///////////////////////////////////////////////////////////////////////////////
extern "C" void scalarProdCPU(float *h_C, float *h_A, float *h_B, int vectorN,
int elementN);
extern "C" void scalarProdCPU(float *h_C, float *h_A, float *h_B, int vectorN, int elementN);
///////////////////////////////////////////////////////////////////////////////
// Calculate scalar products of VectorN vectors of ElementN elements on GPU
@@ -53,9 +51,10 @@ extern "C" void scalarProdCPU(float *h_C, float *h_A, float *h_B, int vectorN,
// Helper function, returning uniformly distributed
// random float in [low, high] range
////////////////////////////////////////////////////////////////////////////////
float RandFloat(float low, float high) {
float t = (float)rand() / (float)RAND_MAX;
return (1.0f - t) * low + t * high;
float RandFloat(float low, float high)
{
float t = (float)rand() / (float)RAND_MAX;
return (1.0f - t) * low + t * high;
}
///////////////////////////////////////////////////////////////////////////////
@@ -71,98 +70,99 @@ const int ELEMENT_N = 4096;
// Total number of data elements
const int DATA_N = VECTOR_N * ELEMENT_N;
const int DATA_SZ = DATA_N * sizeof(float);
const int DATA_SZ = DATA_N * sizeof(float);
const int RESULT_SZ = VECTOR_N * sizeof(float);
///////////////////////////////////////////////////////////////////////////////
// Main program
///////////////////////////////////////////////////////////////////////////////
int main(int argc, char **argv) {
float *h_A, *h_B, *h_C_CPU, *h_C_GPU;
float *d_A, *d_B, *d_C;
double delta, ref, sum_delta, sum_ref, L1norm;
StopWatchInterface *hTimer = NULL;
int i;
int main(int argc, char **argv)
{
float *h_A, *h_B, *h_C_CPU, *h_C_GPU;
float *d_A, *d_B, *d_C;
double delta, ref, sum_delta, sum_ref, L1norm;
StopWatchInterface *hTimer = NULL;
int i;
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
findCudaDevice(argc, (const char **)argv);
// use command-line specified CUDA device, otherwise use device with highest
// Gflops/s
findCudaDevice(argc, (const char **)argv);
sdkCreateTimer(&hTimer);
sdkCreateTimer(&hTimer);
printf("Initializing data...\n");
printf("...allocating CPU memory.\n");
h_A = (float *)malloc(DATA_SZ);
h_B = (float *)malloc(DATA_SZ);
h_C_CPU = (float *)malloc(RESULT_SZ);
h_C_GPU = (float *)malloc(RESULT_SZ);
printf("Initializing data...\n");
printf("...allocating CPU memory.\n");
h_A = (float *)malloc(DATA_SZ);
h_B = (float *)malloc(DATA_SZ);
h_C_CPU = (float *)malloc(RESULT_SZ);
h_C_GPU = (float *)malloc(RESULT_SZ);
printf("...allocating GPU memory.\n");
checkCudaErrors(cudaMalloc((void **)&d_A, DATA_SZ));
checkCudaErrors(cudaMalloc((void **)&d_B, DATA_SZ));
checkCudaErrors(cudaMalloc((void **)&d_C, RESULT_SZ));
printf("...allocating GPU memory.\n");
checkCudaErrors(cudaMalloc((void **)&d_A, DATA_SZ));
checkCudaErrors(cudaMalloc((void **)&d_B, DATA_SZ));
checkCudaErrors(cudaMalloc((void **)&d_C, RESULT_SZ));
printf("...generating input data in CPU mem.\n");
srand(123);
printf("...generating input data in CPU mem.\n");
srand(123);
// Generating input data on CPU
for (i = 0; i < DATA_N; i++) {
h_A[i] = RandFloat(0.0f, 1.0f);
h_B[i] = RandFloat(0.0f, 1.0f);
}
// Generating input data on CPU
for (i = 0; i < DATA_N; i++) {
h_A[i] = RandFloat(0.0f, 1.0f);
h_B[i] = RandFloat(0.0f, 1.0f);
}
printf("...copying input data to GPU mem.\n");
// Copy options data to GPU memory for further processing
checkCudaErrors(cudaMemcpy(d_A, h_A, DATA_SZ, cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpy(d_B, h_B, DATA_SZ, cudaMemcpyHostToDevice));
printf("Data init done.\n");
printf("...copying input data to GPU mem.\n");
// Copy options data to GPU memory for further processing
checkCudaErrors(cudaMemcpy(d_A, h_A, DATA_SZ, cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpy(d_B, h_B, DATA_SZ, cudaMemcpyHostToDevice));
printf("Data init done.\n");
printf("Executing GPU kernel...\n");
checkCudaErrors(cudaDeviceSynchronize());
sdkResetTimer(&hTimer);
sdkStartTimer(&hTimer);
scalarProdGPU<<<128, 256>>>(d_C, d_A, d_B, VECTOR_N, ELEMENT_N);
getLastCudaError("scalarProdGPU() execution failed\n");
checkCudaErrors(cudaDeviceSynchronize());
sdkStopTimer(&hTimer);
printf("GPU time: %f msecs.\n", sdkGetTimerValue(&hTimer));
printf("Executing GPU kernel...\n");
checkCudaErrors(cudaDeviceSynchronize());
sdkResetTimer(&hTimer);
sdkStartTimer(&hTimer);
scalarProdGPU<<<128, 256>>>(d_C, d_A, d_B, VECTOR_N, ELEMENT_N);
getLastCudaError("scalarProdGPU() execution failed\n");
checkCudaErrors(cudaDeviceSynchronize());
sdkStopTimer(&hTimer);
printf("GPU time: %f msecs.\n", sdkGetTimerValue(&hTimer));
printf("Reading back GPU result...\n");
// Read back GPU results to compare them to CPU results
checkCudaErrors(cudaMemcpy(h_C_GPU, d_C, RESULT_SZ, cudaMemcpyDeviceToHost));
printf("Reading back GPU result...\n");
// Read back GPU results to compare them to CPU results
checkCudaErrors(cudaMemcpy(h_C_GPU, d_C, RESULT_SZ, cudaMemcpyDeviceToHost));
printf("Checking GPU results...\n");
printf("..running CPU scalar product calculation\n");
scalarProdCPU(h_C_CPU, h_A, h_B, VECTOR_N, ELEMENT_N);
printf("Checking GPU results...\n");
printf("..running CPU scalar product calculation\n");
scalarProdCPU(h_C_CPU, h_A, h_B, VECTOR_N, ELEMENT_N);
printf("...comparing the results\n");
// Calculate max absolute difference and L1 distance
// between CPU and GPU results
sum_delta = 0;
sum_ref = 0;
printf("...comparing the results\n");
// Calculate max absolute difference and L1 distance
// between CPU and GPU results
sum_delta = 0;
sum_ref = 0;
for (i = 0; i < VECTOR_N; i++) {
delta = fabs(h_C_GPU[i] - h_C_CPU[i]);
ref = h_C_CPU[i];
sum_delta += delta;
sum_ref += ref;
}
for (i = 0; i < VECTOR_N; i++) {
delta = fabs(h_C_GPU[i] - h_C_CPU[i]);
ref = h_C_CPU[i];
sum_delta += delta;
sum_ref += ref;
}
L1norm = sum_delta / sum_ref;
L1norm = sum_delta / sum_ref;
printf("Shutting down...\n");
checkCudaErrors(cudaFree(d_C));
checkCudaErrors(cudaFree(d_B));
checkCudaErrors(cudaFree(d_A));
free(h_C_GPU);
free(h_C_CPU);
free(h_B);
free(h_A);
sdkDeleteTimer(&hTimer);
printf("Shutting down...\n");
checkCudaErrors(cudaFree(d_C));
checkCudaErrors(cudaFree(d_B));
checkCudaErrors(cudaFree(d_A));
free(h_C_GPU);
free(h_C_CPU);
free(h_B);
free(h_A);
sdkDeleteTimer(&hTimer);
printf("L1 error: %E\n", L1norm);
printf((L1norm < 1e-6) ? "Test passed\n" : "Test failed!\n");
exit(L1norm < 1e-6 ? EXIT_SUCCESS : EXIT_FAILURE);
printf("L1 error: %E\n", L1norm);
printf((L1norm < 1e-6) ? "Test passed\n" : "Test failed!\n");
exit(L1norm < 1e-6 ? EXIT_SUCCESS : EXIT_FAILURE);
}

View File

@@ -29,17 +29,17 @@
// Calculate scalar products of VectorN vectors of ElementN elements on CPU.
// Straight accumulation in double precision.
////////////////////////////////////////////////////////////////////////////
extern "C" void scalarProdCPU(float *h_C, float *h_A, float *h_B, int vectorN,
int elementN) {
for (int vec = 0; vec < vectorN; vec++) {
int vectorBase = elementN * vec;
int vectorEnd = vectorBase + elementN;
extern "C" void scalarProdCPU(float *h_C, float *h_A, float *h_B, int vectorN, int elementN)
{
for (int vec = 0; vec < vectorN; vec++) {
int vectorBase = elementN * vec;
int vectorEnd = vectorBase + elementN;
double sum = 0;
double sum = 0;
for (int pos = vectorBase; pos < vectorEnd; pos++)
sum += h_A[pos] * h_B[pos];
for (int pos = vectorBase; pos < vectorEnd; pos++)
sum += h_A[pos] * h_B[pos];
h_C[vec] = (float)sum;
}
h_C[vec] = (float)sum;
}
}

View File

@@ -47,50 +47,51 @@ namespace cg = cooperative_groups;
// 2) ACCUM_N must be a power of two.
///////////////////////////////////////////////////////////////////////////////
#define ACCUM_N 1024
__global__ void scalarProdGPU(float *d_C, float *d_A, float *d_B, int vectorN,
int elementN) {
// Handle to thread block group
cg::thread_block cta = cg::this_thread_block();
// Accumulators cache
__shared__ float accumResult[ACCUM_N];
__global__ void scalarProdGPU(float *d_C, float *d_A, float *d_B, int vectorN, int elementN)
{
// Handle to thread block group
cg::thread_block cta = cg::this_thread_block();
// Accumulators cache
__shared__ float accumResult[ACCUM_N];
////////////////////////////////////////////////////////////////////////////
// Cycle through every pair of vectors,
// taking into account that vector counts can be different
// from total number of thread blocks
////////////////////////////////////////////////////////////////////////////
for (int vec = blockIdx.x; vec < vectorN; vec += gridDim.x) {
int vectorBase = IMUL(elementN, vec);
int vectorEnd = vectorBase + elementN;
////////////////////////////////////////////////////////////////////////////
// Cycle through every pair of vectors,
// taking into account that vector counts can be different
// from total number of thread blocks
////////////////////////////////////////////////////////////////////////////
for (int vec = blockIdx.x; vec < vectorN; vec += gridDim.x) {
int vectorBase = IMUL(elementN, vec);
int vectorEnd = vectorBase + elementN;
////////////////////////////////////////////////////////////////////////
// Each accumulator cycles through vectors with
// stride equal to number of total number of accumulators ACCUM_N
// At this stage ACCUM_N is only preferred be a multiple of warp size
// to meet memory coalescing alignment constraints.
////////////////////////////////////////////////////////////////////////
for (int iAccum = threadIdx.x; iAccum < ACCUM_N; iAccum += blockDim.x) {
float sum = 0;
////////////////////////////////////////////////////////////////////////
// Each accumulator cycles through vectors with
// stride equal to number of total number of accumulators ACCUM_N
// At this stage ACCUM_N is only preferred be a multiple of warp size
// to meet memory coalescing alignment constraints.
////////////////////////////////////////////////////////////////////////
for (int iAccum = threadIdx.x; iAccum < ACCUM_N; iAccum += blockDim.x) {
float sum = 0;
for (int pos = vectorBase + iAccum; pos < vectorEnd; pos += ACCUM_N)
sum += d_A[pos] * d_B[pos];
for (int pos = vectorBase + iAccum; pos < vectorEnd; pos += ACCUM_N)
sum += d_A[pos] * d_B[pos];
accumResult[iAccum] = sum;
accumResult[iAccum] = sum;
}
////////////////////////////////////////////////////////////////////////
// Perform tree-like reduction of accumulators' results.
// ACCUM_N has to be power of two at this stage
////////////////////////////////////////////////////////////////////////
for (int stride = ACCUM_N / 2; stride > 0; stride >>= 1) {
cg::sync(cta);
for (int iAccum = threadIdx.x; iAccum < stride; iAccum += blockDim.x)
accumResult[iAccum] += accumResult[stride + iAccum];
}
cg::sync(cta);
if (threadIdx.x == 0)
d_C[vec] = accumResult[0];
}
////////////////////////////////////////////////////////////////////////
// Perform tree-like reduction of accumulators' results.
// ACCUM_N has to be power of two at this stage
////////////////////////////////////////////////////////////////////////
for (int stride = ACCUM_N / 2; stride > 0; stride >>= 1) {
cg::sync(cta);
for (int iAccum = threadIdx.x; iAccum < stride; iAccum += blockDim.x)
accumResult[iAccum] += accumResult[stride + iAccum];
}
cg::sync(cta);
if (threadIdx.x == 0) d_C[vec] = accumResult[0];
}
}