用户
 找回密码
 立即注册
发表于 2021-3-1 15:08:15
94391
本帖最后由 yjy 于 2021-3-1 15:09 编辑

I have an example of Cuda, but the latest CMakelists won't write it.Could someone please help me, if anyone, I would be very grateful?


The CUDA code is as follows:

  1. #include "cuda_runtime.h"
  2. #include "device_launch_parameters.h"
  3. #include <stdio.h>
  4. #include <memory.h>
  5. #include <time.h>
  6. #include "helper_timer.h"


  7. __global__ void increment_kernel(int *g_data, int inc_value)
  8. {
  9.     int idx = blockIdx.x*blockDim.x + threadIdx.x;
  10.     g_data[idx] = g_data[idx] + inc_value;


  11. }

  12. int correct_output(int *data, const int n, const int x)
  13. {
  14.     for (int i = 0; i < n; i++)
  15.         if (data[i] != x)
  16.             return 0;
  17. }
  18. int main(int argc, char *argv[])
  19. {
  20.     cudaDeviceProp deviceProps;
  21.     cudaGetDeviceProperties(&deviceProps, 0);
  22.     printf("CUDA device [%s]\n", deviceProps.name);
  23.     int n = 16 * 1024 * 1024;
  24.     int nbytes = n*sizeof(int);
  25.     int value = 26;
  26.     int *a = 0;
  27.     cudaMallocHost((void**)&a, nbytes);
  28.     memset(a, 0, nbytes);
  29.     int *d_a = 0;
  30.     cudaMalloc((void**)&d_a, nbytes);
  31.     cudaMemset(d_a, 255, nbytes);
  32.     dim3 threads = dim3(512, 1);
  33.     dim3 blocks = dim3(n / threads.x, 1);

  34.     cudaEvent_t startGPU, stopGPU;
  35.     cudaEventCreate(&startGPU);
  36.     cudaEventCreate(&stopGPU);

  37.     StopWatchInterface *timer = NULL;
  38.     sdkCreateTimer(&timer);
  39.     sdkResetTimer(&timer);

  40.     cudaThreadSynchronize();

  41.     float gpu_time = 0.0f;


  42.     sdkStartTimer(&timer);
  43.     cudaEventRecord(startGPU, 0);
  44.     cudaMemcpyAsync(d_a, a, nbytes, cudaMemcpyHostToDevice, 0);
  45.     increment_kernel<<<blocks, threads, 0, 0>>>(d_a, value);
  46.     cudaMemcpyAsync(a, d_a, nbytes, cudaMemcpyDeviceToHost, 0);
  47.     cudaEventRecord(stopGPU, 0);
  48.     sdkStopTimer(&timer);

  49.     unsigned long int counter = 0;

  50.     while (cudaEventQuery(stopGPU) == cudaErrorNotReady)
  51.     {
  52.         counter++;
  53.     }

  54.     cudaEventElapsedTime(&gpu_time, startGPU, stopGPU);
  55.     printf("time spent executing by the GPU:%.2f\n", gpu_time);
  56.     printf("time spent by CPU in CUDA calls:%.8f\n", sdkGetTimerValue(&timer));
  57.     printf("GPU execute %d iteration while waiting forGPU to finish\n", counter);

  58.     printf("-------------------------------------------------------------------------\n");
  59.     if (correct_output(a, n, value))
  60.         printf("TEST PASSED\n");
  61.     else
  62.         printf("Test FAILED\n");

  63.     cudaEventDestroy(startGPU);
  64.     cudaEventDestroy(stopGPU);
  65.     cudaFreeHost(a);
  66.     cudaFree(d_a);
  67.     getchar();
  68.     cudaThreadExit();
  69.     return 0;
  70. }
复制代码

使用道具 举报 回复
发表于 2021-3-1 15:15:44
希望大佬可以帮助以下。我这只有着一个.cu文件,想用cmakelists的方式跑通一下,但是不会写。网上很多教程都是过时的,比如什么cuda_add_execute()这些命令早在cmake3.9之后就没有了。希望大佬能帮助,非常感谢!
使用道具 举报 回复 支持 反对
发新帖
您需要登录后才可以回帖 登录 | 立即注册