[英]Using Template Classes with CUDA keywords in generic classes
我創建了一個包裝內核功能的模板類。 這樣,當任何其他類需要使用內核時,它都可以簡單地調用該類函數,而不必擔心CUDA術語和功能。 由於該類是模板類,因此所有定義和聲明都包含在同一文件中,如下所示。
Scanner.h
#include <iostream>
#include "ScanKernals.h"
class Scanner {
public :
template<typename T>
void ScanDevice(T * h_in, int size);
template<typename T>
void ScanHost();
};
template<typename T>
void Scanner::ScanHost()
{
}
template<typename T>
void Scanner::ScanDevice(T * h_in, int size)
{
T * d_in;
cudaMalloc(&d_in, size * sizeof(T));
cudaMemcpy(d_in , h_in, size * sizeof(T), cudaMemcpyHostToDevice);
// runs kernel that is required to calculate the scan
}
ScanKernals.h
template<typename T>
__global__
void scan(T * d_in, T* d_out, int size)
{
// keranel code
}
然后在主函數中使用上面的類,如下所示
main.cpp中
#include <iostream>
#include "Scanner.h"
int main()
{
Scanner scanner;
return 0;
}
但是,當我編譯代碼時,編譯器會給出錯誤,但無法識別CUDA特定的關鍵字。
如果我將Scanner類的定義分離到單獨的.cpp文件,則不會生成此錯誤,但是由於無法在兩個單獨的文件中聲明和定義模板類 ,因此這不是一個選擇
我在這里錯過了什么嗎,有解決方法嗎?
如果您的代碼路徑包含將CUDA語法包含到C ++編譯中的代碼,則必須執行以下兩項操作:
.cu
的文件。 nvcc使用文件擴展名確定給定文件的編譯軌跡,如果文件擴展名為.cpp
,則代碼將直接傳遞給主機編譯器,並且編譯將失敗。 您顯然沒有做這些事情之一或可能做不到。
nvcc在文件上的默認行為不是您所期望的,因為@talonmies帶下划線。 但是,您可能需要嘗試以下編譯器命令行選項:
--x {c|c++|cu} (-x)
Explicitly specify the language for the input files, rather than letting
the compiler choose a default based on the file name suffix.
Allowed values for this option: 'c','c++','cu'.
明確指定輸入代碼為cuda,即使以.cpp后綴命名。 這是一個示例main.cpp
文件:
__global__ void f() {}
沒有標志:
/usr/local/cuda-7.5/bin/nvcc -c main.cpp
main.cpp:1:1: error: ‘__global__’ does not name a type
__global__ void f() {}
^
帶有標志(無錯誤):
/usr/local/cuda-7.5/bin/nvcc -c main.cpp -x cu
文件命名自由似乎很安全。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.