2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
#include <stdio.h>
// includes CUDA Runtime
#include <cuda_runtime.h>
#include <cuda_profiler_api.h>
// includes, project
#include <helper_cuda.h>
#include <helper_functions.h> // helper utility functions
__global__ void increment_kernel(int *g_data, int inc_value) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
g_data[idx] = g_data[idx] + inc_value;
}
bool correct_output(int *data, const int n, const int x) {
for (int i = 0; i < n; i++)
if (data[i] != x) {
printf("Error! data[%d] = %d, ref = %dn", i, data[i], x);
return false;
}
return true;
}
int main(int argc, char *argv[]) {
int devID;
cudaDeviceProp deviceProps;
printf("[%s] - Starting...n", argv[0]);
// This will pick the best possible CUDA capable device
devID = findCudaDevice(argc, (const char **)argv);
// get device name
checkCudaErrors(cudaGetDeviceProperties(&deviceProps, devID));
printf("CUDA device [%s]n", deviceProps.name);
int n = 16 * 1024 * 1024;
int nbytes = n * sizeof(int);
int value = 26;
// allocate host memory
int *a = 0;
checkCudaErrors(cudaMallocHost((void **)&a, nbytes));
memset(a, 0, nbytes);
// allocate device memory
int *d_a = 0;
checkCudaErrors(cudaMalloc((void **)&d_a, nbytes));
checkCudaErrors(cudaMemset(d_a, 255, nbytes));
// set kernel launch configuration
dim3 threads = dim3(512, 1);
dim3 blocks = dim3(n / threads.x, 1);
// create cuda event handles
cudaEvent_t start, stop;
checkCudaErrors(cudaEventCreate(&start));
checkCudaErrors(cudaEventCreate(&stop));
StopWatchInterface *timer = NULL;
sdkCreateTimer(&timer);
sdkResetTimer(&timer);
checkCudaErrors(cudaDeviceSynchronize());
float gpu_time = 0.0f;
// asynchronously issue work to the GPU (all to stream 0)
checkCudaErrors(cudaProfilerStart());
sdkStartTimer(&timer);
cudaEventRecord(start, 0);
cudaMemcpyAsync(d_a, a, nbytes, cudaMemcpyHostToDevice, 0);
increment_kernel<<<blocks, threads, 0, 0>>>(d_a, value);
cudaMemcpyAsync(a, d_a, nbytes, cudaMemcpyDeviceToHost, 0);
cudaEventRecord(stop, 0);
sdkStopTimer(&timer);
checkCudaErrors(cudaProfilerStop());
// have CPU do some work while waiting for stage 1 to finish
unsigned long int counter = 0;
while (cudaEventQuery(stop) == cudaErrorNotReady) {
counter++;
}
checkCudaErrors(cudaEventElapsedTime(&gpu_time, start, stop));
// print the cpu and gpu times
printf("time spent executing by the GPU: %.2fn", gpu_time);
printf("time spent by CPU in CUDA calls: %.2fn", sdkGetTimerValue(&timer));
printf("CPU executed %lu iterations while waiting for GPU to finishn",
counter);
// check the output for correctness
bool bFinalResults = correct_output(a, n, value);
// release resources
checkCudaErrors(cudaEventDestroy(start));
checkCudaErrors(cudaEventDestroy(stop));
checkCudaErrors(cudaFreeHost(a));
checkCudaErrors(cudaFree(d_a));
exit(bFinalResults ? EXIT_SUCCESS : EXIT_FAILURE);
}
设备初始化:findCudaDevice函数用于选择最佳的CUDA设备,并返回设备ID。
devID = findCudaDevice(argc, (const char **)argv);
获取设备属性:cudaGetDeviceProperties函数获取指定设备的属性,这些属性包括设备名称等信息。
checkCudaErrors(cudaGetDeviceProperties(&deviceProps, devID));
内存分配:使用cudaMallocHost分配CPU上可访问的页锁定内存,cudaMalloc分配设备上的内存。
int *a = 0;
checkCudaErrors(cudaMallocHost((void **)&a, nbytes));
设置线程块和网格:这里将线程块大小设置为512个线程,网格大小根据数据大小动态计算。
dim3 threads = dim3(512, 1);
dim3 blocks = dim3(n / threads.x, 1);
创建CUDA事件和计时器:CUDA事件用于记录时间,计时器用于测量CPU执行时间。
cudaEvent_t start, stop;
checkCudaErrors(cudaEventCreate(&start));
checkCudaErrors(cudaEventCreate(&stop));
CUDA流处理:使用cudaMemcpyAsync进行异步内存拷贝,<<<blocks, threads>>>语法启动并发执行的CUDA内核函数increment_kernel。
cudaMemcpyAsync(d_a, a, nbytes, cudaMemcpyHostToDevice, 0);
increment_kernel<<<blocks, threads, 0, 0>>>(d_a, value);
cudaMemcpyAsync(a, d_a, nbytes, cudaMemcpyDeviceToHost, 0);
计时和等待:cudaEventRecord记录事件,用于计算GPU执行时间。通过cudaEventQuery(stop)等待GPU操作完成。
cudaEventRecord(start, 0);
// ...
cudaEventRecord(stop, 0);
结果验证:使用correct_output函数验证GPU计算结果的正确性。
bool bFinalResults = correct_output(a, n, value);
资源释放:释放分配的内存和CUDA事件。
checkCudaErrors(cudaEventDestroy(start));
checkCudaErrors(cudaEventDestroy(stop));
checkCudaErrors(cudaFreeHost(a));
checkCudaErrors(cudaFree(d_a));
CUDA内核函数 increment_kernel :
这个简单的CUDA内核函数用于将数组中的每个元素增加一个指定的值inc_value。blockIdx.x和threadIdx.x用于计算每个线程的全局索引idx,然后执行加法操作。
__global__ void increment_kernel(int *g_data, int inc_value) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
g_data[idx] = g_data[idx] + inc_value;
}
其他辅助函数
checkCudaErrors:检查CUDA函数调用是否出错。
sdkCreateTimer和sdkResetTimer:用于创建和重置计时器。
sdkStartTimer和sdkStopTimer:用于启动和停止计时器并记录CPU执行时间。