【问题标题】:How to enable separate compilation for CUDA project in Visual Studio如何在 Visual Studio 中为 CUDA 项目启用单独编译
【发布时间】:2020-04-10 10:35:08
【问题描述】:

我是 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;
}

【问题讨论】:

标签: c++ visual-studio compilation cuda kernel


【解决方案1】:

使用以下链接Using CUDA dynamic parallelism in Visual Studio 上提供的信息解决了问题

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

从 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

【讨论】:

  • 回答您自己的问题并将答案的来源添加为链接是完全可以的。但是答案必须包含所有相关信息才能回答问题,而无需点击链接。原因是这个外部链接可能随时都无法访问,这会使这个答案变得毫无用处。
  • 谢谢。感谢您的建议。
猜你喜欢
  • 2011-01-04
  • 1970-01-01
  • 2011-04-28
  • 1970-01-01
  • 1970-01-01
  • 2011-04-26
  • 2017-01-10
  • 2012-01-06
  • 2017-05-09
相关资源
最近更新 更多