簡體   English   中英

在CUDA設備代碼和主機代碼中創建模板類的對象時,無法解析的外部函數

[英]Unresolved extern function while creating objects of template class in CUDA device code and host code

我在文件template.cutemplate.cuh中定義了一個類模板 我通過使用hostdevice關鍵字將構造函數和析構函數標記為可調用的設備和主機。

template.cuh

#pragma once

#include "cuda_runtime.h"

template<class T>
class Foo
{
public:

    __host__ __device__
    Foo();

    __host__ __device__
    ~Foo();
};

template.cu

#include "template.cuh"

template<class T>
__host__ __device__
Foo<T>::Foo()
{

}

template<class T>
__host__ __device__
Foo<T>::~Foo()
{

}

// Instantiating template of type int
template
class Foo<int> ;

我的主要功能是在包含template.cuh標頭的Kernel.cu文件中。 我只是實例化主機和設備代碼中int類型的Foo對象。

#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include "template.cuh"

__global__ void addKernel(int *c, const int *a, const int *b)
{
    Foo<int> f;

    int i = threadIdx.x;
    c[i] = a[i] + b[i];
}

int main()
{
    Foo<int> t;
    return 0;
}

在NVIDIA CUDA 6.5運行時類型的Visual Studio C ++項目中編譯上述代碼文件時,出現以下日志,導致出現未解決的外部函數錯誤:

1>  c:\Users\admin\documents\visual studio 2013\Projects\Test\Testtemplates>"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.5\bin\nvcc.exe" -gencode=arch=compute_20,code=\"sm_20,compute_20\" --use-local-env --cl-version 2013 -ccbin "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin"  -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.5\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.5\include"  -G   --keep-dir Debug -maxrregcount=0  --machine 32 --compile -cudart static  -g   -DWIN32 -D_DEBUG -D_CONSOLE -D_MBCS -Xcompiler "/EHsc /W3 /nologo /Od /Zi /RTC1 /MDd  " -o Debug\kernel.cu.obj "c:\Users\admin\documents\visual studio 2013\Projects\Test\Testtemplates\kernel.cu"     
1>  ptxas fatal   : Unresolved extern function '_ZN3FooIiEC1Ev'    
1>  kernel.cu

我在這里做錯了什么?

出現此錯誤的原因是您沒有使用設備代碼鏈接。 看一下本文: CUDA C ++設備代碼的單獨編譯和鏈接

我只是用您的代碼嘗試了以下方法,它對我有用。 注意其他標志-dc

nvcc template.cu kernel.cu -dc
nvcc template.o kernel.o -o kernel

我沒有直接使用Visual Studio的經驗,我更喜歡使用CMake涵蓋為VS生成正確的設置。

以下CMakeLists.txt文件在Linux和gcc上對我有用,您可以在Windows和VS上嘗試一下,然后將生成的項目設置與您使用的設置進行比較。

PROJECT(kernel)
FIND_PACKAGE(CUDA REQUIRED)

SET(CUDA_SEPARABLE_COMPILATION ON)
CUDA_ADD_EXECUTABLE(kernel template.cuh template.cu kernel.cu)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM