使用Qt编写CUDA程序

  • Post author:
  • Post category:其他


本文基于的情况是,Qt,CUDA和VS已经安装完成且能够正常运行的情况

  1. 创建一个空的Qt项目
  2. 创建一个.cu文件,本文创建的为kernel.cu

    内容如下
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size);
__global__ void addKernel(int *c, const int *a, const int *b)
{
    int i = threadIdx.x;
    c[i] = a[i] + b[i];
}
extern "C"
void run()
{
    const int arraySize = 5;
    const int a[arraySize] = { 1, 2, 3, 4, 5 };
    const int b[arraySize] = { 10, 20, 30, 40, 50 };
    int c[arraySize] = { 0 };
    // Add vectors in parallel.
    cudaError_t cudaStatus = addWithCuda(c, a, b, arraySize);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "addWithCuda failed!");
        return;
    }
    printf("{1,2,3,4,5} + {10,20,30,40,50} = {%d,%d,%d,%d,%d}\n",
           c[0], c[1], c[2], c[3], c[4]);
    // cudaDeviceReset must be called before exiting in order for profiling and
    // tracing tools such as Nsight and Visual Profiler to show complete traces.
    cudaStatus = cudaDeviceReset();
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaDeviceReset failed!");
        return;
    }
//    return 0;
}
// Helper function for using CUDA to add vectors in parallel.
cudaError_t addWithCuda(int 



版权声明:本文为u014683187原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。