[英]CUDA code compile on Linux but not in Windows ( Visual Studio 2012)
[英]CUDA 8.0 Visual Studio 2012 linker options when using Relocatable Device Code
我有一個與此相似的基於類的C ++代碼,並遵循了用於聲明該類的__device__
方法的結構。 我也轉到了Project Properties-> CUDA C/C++ -> Common -> Generate Relocatable Device Code
,並將其更改為YES,但是在這里它說鏈接選項需要修改,我不理解如何從VS選項中進行操作。 這些信息 。 我該如何使其鏈接?
**刪除原始帖子中的VC Studio設置不相關**
測試代碼為:
kernel.cu
#include "Test.cuh"
// CUDA kernel
__global__ void myKernel(Test* t) {
// Call device function
t->device_function();
}
// Entry point
int main()
{
// Create object (host and device copies)
Test* t = new Test();
Test* t_dev = NULL;
cudaMalloc(&t_dev, sizeof(Test));
// Copy object
cudaMemcpy(t_dev,t,sizeof(Test),cudaMemcpyHostToDevice);
// Call kernel passing pointer to device copy of the object
myKernel<<<1,1>>>(t_dev);
}
Test.cuh
class Test
{
public:
Test(void);
~Test(void);
__device__ void device_function();
};
Test.cu
#include "Test.cuh"
Test::Test(void){}
Test::~Test(void){}
__device__ void Test::device_function() {
// Do something
}
編輯
添加編譯日志,因為我認為它可能無法正確編譯設備實現。
1> C:\C++ Projects\CudaTest\CudaTest>"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin\nvcc.exe" -ccbin "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include" --keep-dir Release -maxrregcount=0 --machine 32 --compile -DWIN32 -DNDEBUG -D_CONSOLE -D_MBCS -Xcompiler "/EHsc /W3 /nologo /O2 /Zi /MD " -o Release\kernel.cu.obj "C:\C++ Projects\CudaTest\CudaTest\kernel.cu" -clean
1> kernel.cu
1> Compiling CUDA source file kernel.cu...
1>
1> C:\C++ Projects\CudaTest\CudaTest>"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin\nvcc.exe" -gencode=arch=compute_20,code=\"sm_20,compute_20\" --use-local-env --cl-version 2012 -ccbin "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin" -rdc=true -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include" --keep-dir Release -maxrregcount=0 --machine 32 --compile -cudart static -DWIN32 -DNDEBUG -D_CONSOLE -D_MBCS -Xcompiler "/EHsc /W3 /nologo /O2 /Zi /MD " -o Release\kernel.cu.obj "C:\C++ Projects\CudaTest\CudaTest\kernel.cu"
1> kernel.cu
1> Test.cu
1>
1> C:\C++ Projects\CudaTest\CudaTest>"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin\nvcc.exe" -dlink -o Release\CudaTest.device-link.obj -Xcompiler "/EHsc /W3 /nologo /O2 /Zi /MD " -L"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\lib\Win32" cudart.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib -gencode=arch=compute_20,code=sm_20 --machine 32 Release\kernel.cu.obj
1> CUDALINK : nvlink error : Undefined reference to '_ZN4Test15device_functionEv' in 'Release/kernel.cu.obj'`
好的,因此帶有CUDA插件的VS有兩個不同的Item Type
,它們的目錄分類不同,分別是CUDA C/C++ source/header file
和C/C++ source/header
並且您不能簡單地通過重命名*.cpp
將它們轉換為另一個*.cpp
到*.cu
,這是我本來試圖強迫它將類編譯成可以鏈接到的對象的方法。
通過在解決方案資源管理器中單擊Test.cuh
右鍵,然后Test.cuh
Add Item -> CUDA source/header file
, Test.cuh
分別用與Test.cu
和Test.cuh
完全相同的源代碼重新添加了文件Test.cpp
和Test.h
它將kernel.cu
和 Test.cu/h
為兩個對象,這兩個對象都在鏈接階段提供,問題消失了。
感謝@talonmies幫助我指出正確的方向。
編輯
正如我在評論中提到的,如果仍然需要告訴VS它們現在是不同的Item Type
,則可以簡單地重命名文件。 最初,我通過編輯*.vcxproj
文件來告訴CUDA編譯器來編譯重命名的文件,從而做到了這一點。 這是通過更改<ItemGroup>
定義來完成的,如下所示:
<ItemGroup>
<CudaCompile Include="main.cu" />
<CudaCompile Include="RenamedSrcFile.cu" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="RenamedHeaderFile.cuh" />
<ClInclude Include="myOtherHeader.h" />
</ItemGroup>
此行<CudaCompile Include="RenamedSrcFile.cu" />
是關鍵,並且在CUDA編譯階段包括此文件。 常規C ++文件出現在組<ClCompile>
盡管在此示例中未給出。
如羅伯特(Robert)的評論所示,您永遠不需要直接編輯*.vcxproj
文件,因為VS將根據您在VS中選擇的選項為您管理文件。 通過像我之前所做的那樣(重新)添加文件,VS可以解決此交換問題。
EDIT2
根據Drop在評論中的建議,您可以通過在解決方案資源管理器中右鍵單擊文件來實現此交換而無需重新添加文件,並且在Properties->General
您可以將Item Type
選擇為CUDA C/C++
而不是C/C++
將其正確分類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.