如何在Visual Studio中为CUDA项目启用单独的编译

How to enable separate compilation for CUDA project in Visual Studio

本文关键字:启用 项目 单独 编译 CUDA 中为 Visual Studio      更新时间:2023-10-16

我是 CUDA 的新手。 我正在尝试编写一个应用程序,其中我从另一个内核函数调用一个内核函数。但是我在构建应用程序时收到错误">设备全局函数启动内核需要单独的编译模式"。 这是我的完整代码。任何帮助将不胜感激。

#include<iostream>
#include<curand.h>
#include<cuda.h>
#include <curand_kernel.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
__device__ int *vectorData;
__device__ void initializeArray(int elementCount)
{
for (int i = 0; i < elementCount; i++)
{
vectorData[i] = 1;
}
}
__global__ void AddOneToEachElement(int elementCount)
{
for (int i = 0; i < elementCount; i++)
{
vectorData[i] = vectorData[i]+1;
}
}
__global__ void addKernel(int *numberOfElements)
{
vectorData = (int*)malloc(sizeof(int));
initializeArray(*numberOfElements);
int gridSize = ceil((*numberOfElements) / 1024) + 1;
AddOneToEachElement << <gridSize, 1024 >> > (*numberOfElements);
cudaDeviceSynchronize();
free(vectorData);
}
int main()
{
int numberOfElements = 1;
int *device_numberOfElements;
cudaMalloc((int**)&device_numberOfElements, sizeof(int));
cout << "Enter the Number of elements" << endl;
cin >> numberOfElements;
cudaMemcpy(device_numberOfElements, &(numberOfElements), sizeof(int), cudaMemcpyHostToDevice);
addKernel << <1, 1 >> > (device_numberOfElements);
cudaFree(device_numberOfElements);
return 0;
}

使用以下链接中提供的信息解决了该问题 在Visual Studio中使用CUDA动态并行性

这是我从上述链接中获得的完整信息:

从 CUDA 5.0 开始,CUDA 支持对计算能力为 3.5 或更高的 GPU 使用动态并行性。动态并行性允许直接从其他内核启动内核,并在这些应用程序中实现进一步的加速,这些应用程序可以从运行时直接在 GPU 上更好地处理计算工作负载中受益;在许多情况下,动态并行性避免了 CPU/GPU 交互,从而有利于递归等机制。 若要在 Visual Studio 2010 或 Visual Studio 2013 中使用动态并行性,请执行以下操作:

  • 查看 ->属性页
  • 配置属性 -> CUDA C/C++ -> 通用 -> 生成可重定位的设备代码 -> 是 (-rdc=true(
  • 配置属性 -> CUDA C/C++ ->设备 ->代码生成 -> compute_35,sm_35
  • 配置属性 -> 链接器 -> 输入 -> 其他依赖项 -> cudadevrt.lib