如何在 Visual Studio 中为 CUDA 项目启用单独编译
How to enable separate compilation for CUDA project in Visual Studio
我是 CUDA 新手。
我正在尝试编写一个应用程序,在其中我从另一个内核函数调用一个内核函数。但是我收到错误消息“内核从 device 或 global 函数启动需要单独的编译模式”而构建应用程序。
这是我的完整代码。任何帮助将不胜感激。
#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;
}
使用以下可用信息解决了问题 link Using CUDA dynamic parallelism in Visual Studio
这是我从上面提到的link获得的完整信息:
从 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
我是 CUDA 新手。 我正在尝试编写一个应用程序,在其中我从另一个内核函数调用一个内核函数。但是我收到错误消息“内核从 device 或 global 函数启动需要单独的编译模式”而构建应用程序。 这是我的完整代码。任何帮助将不胜感激。
#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;
}
使用以下可用信息解决了问题 link Using CUDA dynamic parallelism in Visual Studio
这是我从上面提到的link获得的完整信息:
从 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