如何集成CUDA .cu代码与C ++应用程序 [英] How to integrate CUDA .cu code with C++ app

查看:690
本文介绍了如何集成CUDA .cu代码与C ++应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此信息与我之前的信息非常相似:如何将CUDA代码分成多个文件恐怕我对我实际要求的错误做出了错误,以至于不能在那里尝试和纠正它。



我将此代码放在CUDA SDK的cppIntegration示例之外。



我想要一个main.cpp与我的 main()函数,并调用一个单独的。 cu文件在GPU上做一些工作。在单独的.cu文件中,我希望它能够调用驻留在另一个.cu文件中的内核代码。我设想三个文件:Main.cpp,KernelWrapper.cu和MyKernel.cu。 Main.cpp用于测试目的不在于调用位于KernelWrapper.cu中的函数 RunTest() RunTest()方法执行位于MyKernel.cu中的 TestDevice(int * deviceArray) >

Main.cpp:

  #include< iostream> 

void RunTest(); // forward declaration ???

int main(int argc,char ** argv)
{
RunTest();
std :: cout<< blah \\\
;

return 0;
}

KernelWrapper.cu:

  #include< iostream> 

__global__ void TestDevice(int * deviceArray); // forward declaration

void RunTest()
{
int * hostArray;
int * deviceArray;
const int arrayLength = 16;
const unsigned int memSize = sizeof(int)* arrayLength;

hostArray =(int *)malloc(memSize);
cudaMalloc((void **)& deviceArray,memSize);

std :: cout<< before device\\\
;
for(int i = 0; i {
hostArray [i] = i + 1;
std :: cout<< hostArray [i]< \\\
;
}
std :: cout<< \\\
;

cudaMemcpy(deviceArray,hostArray,memSize,cudaMemcpyHostToDevice);
TestDevice<<< 4,4>> (deviceArray);
cudaMemcpy(hostArray,deviceArray,memSize,cudaMemcpyDeviceToHost);

std :: cout<< after device\\\
;
for(int i = 0; i {
std :: cout< hostArray [i]< \\\
;
}

cudaFree(deviceArray);
free(hostArray);

std :: cout<< Done \\\
;
}

MyKernel.cu:

  #ifndef _MY_KERNEL_ 
#define _MY_KERNEL_

__global__ void TestDevice(int * deviceArray)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
deviceArray [idx] = deviceArray [idx] * deviceArray [idx];
}

#endif

/ p>

  1> ------构建开始:项目:CUDASandbox,配置:调试x64 ------ 
1>使用CUDA Build Rule ...编译
1>C:\CUDA\bin64\\\
vcc.exe-arch sm_10 -ccbinC:\Program Files(x86)\ Microsoft Visual Studio 9.0 \VC\bin-Xcompiler/ EHsc / W3 / nologo / O2 / Zi / MT-maxrregcount = 32 --compile -ox64 \Debug\KernelWrapper.cu.obj d:\Stuff\Programming\Visual Studio 2008\Projects\CUDASandbox\CUDASandbox\KernelWrapper.cu
1> KernelWrapper.cu
1> tmpxft_00001ab8_00000000-3_KernelWrapper.cudafe1.gpu
1> tmpxft_00001ab8_00000000-8_KernelWrapper.cudafe2.gpu
1> tmpxft_00001ab8_00000000-3_KernelWrapper.cudafe1.cpp
1> tmpxft_00001ab8_00000000-12_KernelWrapper.ii
1>使用CUDA Build Rule ...编译。
1>C:\CUDA\bin64 \\\
vcc.exe-arch sm_10 -ccbinC:\Program Files(x86)\ Microsoft Visual Studio 9.0 \VC\bin -Xcompiler/ EHsc / W3 / nologo / O2 / Zi / MT-maxrregcount = 32 --compile -ox64 \Debug\MyKernel.cu.objd:\Stuff\Programming\Visual Studio 2008\Projects\CUDASandbox\CUDASandbox\MyKernel.cu
1> MyKernel.cu
1> tmpxft_000017b0_00000000-3_MyKernel.cudafe1.gpu
1> tmpxft_000017b0_00000000-8_MyKernel.cudafe2 .gpu
1> tmpxft_000017b0_00000000-3_MyKernel.cudafe1.cpp
1> tmpxft_000017b0_00000000-12_MyKernel.ii
1>正在编译...
1> Main.cpp
1> ;将清单编译为资源...
1> Microsoft(R)Windows资源编译器版本6.1.6723.1
1>版权所有(C)Microsoft Corporation。版权所有。
1>链接...
1> msvcprtd.lib(MSVCP90D.dll):错误LNK2005:public:void __cdecl std :: basic_ios< char,struct std :: char_traits< char> :: setstate(int,bool)(?setstate @?$ basic_ios @ DU?$ char_traits @ D @ std @@@ std @@ QEAAXH_N @ Z)已经在KernelWrapper.cu.obj中定义
1> msvcprtd .lib(MSVCP90D.dll):错误LNK2005:已在KernelWrapper.cu.obj $ b中定义的public:__int64 __cdecl std :: ios_base :: width(__ int64)(?width @ ios_base @ std @@ QEAA_J_J @ Z) $ b 1> msvcprtd.lib(MSVCP90D.dll):error LNK2005:public:__int64 __cdecl std :: basic_streambuf 1> msvcprtd.lib(MSVCP90D.dll)中定义的:?sputn @?$ basic_streambuf @ DU?$ char_traits @ D @ std @@@ std @@ QEAA_JPEBD_J @ Z):error LNK2005 :public:static bool __cdecl std :: char_traits< char> :: eq_int_type(int const&,int const&)(?eq_int_type @?$ char_traits @ D @ std @@ SA_NAEBH0 @ Z) .cu.obj
1> msvcprtd.lib(MSVCP90D.dll):error LNK2005:public:static int __cdecl std :: char_traits< char> :: eof(void)(?eof @?$ char_traits @ D @ std @@ SAHXZ)已经在KernelWrapper.cu.obj中定义
1> msvcprtd.lib(MSVCP90D.dll):error LNK2005:public:int __cdecl std :: basic_streambuf 1> msvcprtd .lib(MSVCP90D.dll):error LNK2005:public:class std :: basic_streambuf 1> msvcprtd.lib(MSVCP90D.dll):error LNK2005:public:char __cdecl std :: basic_ios< char,struct std :: char_traits< char> > :: fill(void)const(?fill @?$ basic_ios @ DU?$ char_traits @ D @ std @@@ std @@ QEBADXZ) lib(MSVCP90D.dll):错误LNK2005:已经在KernelWrapper.cu.obj中定义的public:int __cdecl std :: ios_base :: flags(void)const(?flags @ ios_base @ std @@ QEBAHXZ)
1> msvcprtd.lib(MSVCP90D.dll):错误LNK2005:已经在KernelWrapper.cu.obj中定义的public:__int64 __cdecl std :: ios_base :: width(void)const(?width @ ios_base @ std @@ QEBA_JXZ)
1> msvcprtd.lib(MSVCP90D.dll):error LNK2005:public:static unsigned __int64 __cdecl std :: char_traits< char> :: length(char const *)(?length @?$ char_traits @ D @ std @@ SA_KPEBD @ Z)已经在KernelWrapper.cu.obj中定义
1> msvcprtd.lib(MSVCP90D.dll):error LNK2005:public:class std :: basic_ostream< char,struct std :: char_traits< ; char> > & __cdecl std :: basic_ostream< char,struct std :: char_traits< char>已经在KernelWrapper.cu.obj中定义的> :: flush(void)(?flush @?$ basic_ostream @ DU?$ char_traits @ D @ std @@@ std @@ QEAAAEAV12 @ XZ)
1> msvcprtd .lib(MSVCP90D.dll):error LNK2005:public:class std :: basic_ostream< char,struct std :: char_traits< char> > * __cdecl std :: basic_ios< char,struct std :: char_traits< char> > :: tie(void)const(?tie @?$ basic_ios @ DU?$ char_traits @ D @ std @@@ std @@ QEBAPEAV?$ basic_ostream @ DU?$ char_traits @ D @ std @@@ @ XZ)已经在KernelWrapper.cu.obj中定义了
1> msvcprtd.lib(MSVCP90D.dll):error LNK2005:public:bool __cdecl std :: ios_base :: good(void)const(?good @ ios_base已经在KernelWrapper.cu.obj中定义的@ std @@ QEBA_NXZ)
1> msvcprtd.lib(MSVCP90D.dll):error LNK2005:public:void __cdecl std :: basic_ostream< char,struct std :: char_traits& char> > :: _ Osfx(void)(?_Osfx @?$ basic_ostream @ DU?$ char_traits @ D @ std @@@@@@@@@@@@@@ std @@ QEAAXXZ)
1> msvcprtd.lib (MSVCP90D.dll):错误LNK2005:已在libcpmt.lib(uncaught.obj)中定义的bool __cdecl std :: uncaught_exception(void)(?uncaught_exception @ std @@ YA_NXZ)
1> msvcprtd.lib MSVCP90D.dll):error LNK2005:public:void __cdecl std :: basic_streambuf< char,struct std :: char_traits< char>已经在KernelWrapper.cu.obj中定义的> :: _ Lock(void)(?_Lock @?$ basic_streambuf @ DU?$ char_traits @ D @ std @@@ std @@ QEAAXXZ)
1> msvcprtd.lib (MSVCP90D.dll):error LNK2005:public:void __cdecl std :: basic_streambuf< char,struct std :: char_traits< char> > :: _ Unlock(void)(?_Unlock @?$ basic_streambuf @ DU?$ char_traits @ D @ std @@@ std @@ QEAAXXZ) (typ_info.obj)中定义的错误LNK2005:private:__cdecl type_info :: type_info(class type_info const&)(?? 0type_info @@ AEAA @ AEBV0 @@ Z) b $ b 1> MSVCRTD.lib(ti_inst.obj):error LNK2005:private:class type_info&已经在LIBCMT.lib(typinfo.obj)中定义的__cdecl type_info :: operator =(class type_info const&)(?? 4type_info @@ AEAAAEAV0 @ AEBV0 @@ Z)
1> LINK:warning LNK4098:defaultlib 'MSVCRTD'与使用其他库冲突;使用/ NODEFAULTLIB:库
1> D:\Stuff\Programming\Visual Studio 2008 \Projects\CUDASandbox\x64\Debug\CUDASandbox。 exe:致命错误LNK1169:找到一个或多个定义的符号
1>构建日志保存在file:// d:\Stuff\Programming\Visual Studio 2008\Projects\CUDASandbox\ CUDASandbox\x64\Debug\BuildLog.htm
1> CUDASandbox - 21错误,1警告
========== Build:0成功,1失败,0最新,0跳过==========

我在Windows Vista 7 64位上运行Visual Studio 2008



编辑:



将代码生成设置为/ MT所产生的构建日志

  1> CUDASandbox,配置:Debug x64 ------ 
1>使用CUDA Build Rule ...编译
1>C:\CUDA\bin64\\\\ -ccbinC:\Program Files(x86)\Microsoft Visual Studio 9.0 \VC\bin-Xcompiler/ EHsc / W3 / nologo / O2 / Zi / MT-maxrregcount = 32 --compile -o x64 \Debug\MyKernel.cu.objd:\Stuff\Programming\Visual Studio 2008\Projects\CUDASandbox\CUDASandbox\MyKernel.cu
1> MyKernel。 cu
1> tmpxft_000019e4_00000000-3_MyKernel.cudafe1.gpu
1> tmpxft_000019e4_00000000-8_MyKernel.cudafe2.gpu
1> tmpxft_000019e4_00000000-3_MyKernel.cudafe1.cpp
1> tmpxft_000019e4_00000000-12_MyKernel。 ii
1>使用CUDA Build Rule ...编译
1>C:\CUDA\bin64\\\
vcc.exe-arch sm_10 -ccbinC:\Program Files(x86 )\Microsoft Visual Studio 9.0 \VC\bin-Xcompiler/ EHsc / W3 / nologo / O2 / Zi / MT-maxrregcount = 32 --compile -ox64 \Debug\KernelWrapper.cu。 objd:\Stuff\Programming\Visual Studio 2008\Projects\CUDASandbox\CUDASandbox\KernelWrapper.cu
1> KernelWrapper.cu
1> tmpxft_0000078c_00000000-3_KernelWrapper。 cudafe1.gpu
1> tmpxft_0000078c_00000000-8_KernelWrapper.cudafe2.gpu
1> tmpxft_0000078c_00000000-3_KernelWrapper.cudafe1.cpp
1> tmpxft_0000078c_00000000-12_KernelWrapper.ii
1>正在编译...
1> Main.cpp
1>链接...
1> LINK:警告LNK4098:defaultlib'LIBCMT'与其他库的使用冲突;使用/ NODEFAULTLIB:库
1> libcpmtd.lib(cout.obj):error LNK2019:未解析的外部符号_CrtDbgReportW在函数public:char const& __cdecl std :: _ St​​ring_const_iterator< char,struct std :: char_traits& ; char>,class std :: allocator< char>> :: operator *(void)const(?? D?$ _ St​​ring_const_iterator @ DU?$ char_traits @ D @ std @@ V?$ allocator @ D @ @ std @@ QEBAAEBDXZ)
1> libcpmtd.lib(stdthrow.obj):error LNK2001:未解析的外部符号_CrtDbgReportW
1> libcpmtd.lib(xmbtowc.obj):error LNK2001:未解析的外部符号_CrtDbgReportW
1> libcpmtd.lib(xdebug.obj):error LNK2019:未解析的外部符号_malloc_dbg在函数void * __cdecl operator new(unsigned __int64,struct std :: _ DebugHeapTag_t const&,char *,int) (?? 2 @ YAPEAX_KAEBU_DebugHeapTag_t @ std @@ PEADH @ Z)
1> libcpmtd.lib(xmbtowc.obj):error LNK2001:未解析的外部符号_malloc_dbg
1> libcpmtd.lib(xdebug.obj) :error LNK2019:在函数void __cdecl operator delete(void *,struct std :: _ DebugHeapTag_t const&,char *,int)中引用的未解析的外部符号_free_dbg(?? 3 @ YAXPEAXAEBU_DebugHeapTag_t @ std @@ PEADH @ Z)
1> libcpmtd.lib(xmbtowc.obj):error LNK2001:未解析的外部符号_free_dbg
1> libcpmtd.lib(_tolower.obj):error LNK2019:未解析的外部符号_calloc_dbg在函数_Getctype中引用
1> D:\Stuff\Programming\Visual Studio 2008\Projects\CUDASandbox\x64\Debug\CUDASandbox.exe:致命错误LNK1120:4未解析的外部
1>构建日志保存在file:// d:\Stuff\Programming\Visual Studio 2008 \Projects\CUDASandbox\CUDASandbox\x64\Debug\BuildLog.htm
1> CUDASandbox - 9错误,1警告
========== Build:0成功,1失败,0最新,0跳过========= =


解决方案

看起来你的两个不同版本的C运行时。 CUDA运行时使用静态链接的C运行时,您的应用程序使用动态加载的C运行时(通常是Visual Studio中的默认设置)。



将代码生成更改为静态使用加载C运行时以匹配CUDA运行时:




  • 右键单击项目并选择属性,然后在C / C ++ - >代码生成更改运行时库到/ MT



您可以使用/ MTd进行调试,在这种情况下,您需要在CUDA构建规则 - >混合CUDA / C ++选项。



查看此之前的帖子,一步一步地提供更多提示。


This post closely resembles my earlier post: How to separate CUDA code into multiple files I am afraid I made such a blunder of what I was actually asking that it will be too confusing to try and correct it there.

I am basing this code loosely off the cppIntegration example from the CUDA SDK.

I want to have a main.cpp with my main() function and have it call to a separate .cu file to do some work on the GPU. In the separate .cu file I want it to be able to call to my kernel code that resides in another .cu file. I envision three files: Main.cpp, KernelWrapper.cu, and MyKernel.cu. Main.cpp for purposes of testing does nothing except call a function RunTest() that is located in KernelWrapper.cu. the RunTest() method executes the TestDevice(int *deviceArray) kernel that is located in MyKernel.cu

Main.cpp:

#include <iostream>

void RunTest(); //forward declaration???

int main( int argc, char** argv) 
{
    RunTest();
    std::cout << "blah\n";

    return 0;
}

KernelWrapper.cu:

#include <iostream>

__global__ void TestDevice(int *deviceArray); //forward declaration

void RunTest()
{
    int* hostArray;
    int* deviceArray;
    const int arrayLength = 16;
    const unsigned int memSize = sizeof(int) * arrayLength;

    hostArray = (int*)malloc(memSize);
    cudaMalloc((void**) &deviceArray, memSize);

    std::cout << "Before device\n";
    for(int i=0;i<arrayLength;i++)
    {
        hostArray[i] = i+1;
        std::cout << hostArray[i] << "\n";
    }
    std::cout << "\n";

    cudaMemcpy(deviceArray, hostArray, memSize, cudaMemcpyHostToDevice);
    TestDevice <<< 4, 4 >>> (deviceArray);
    cudaMemcpy(hostArray, deviceArray, memSize, cudaMemcpyDeviceToHost);

    std::cout << "After device\n";
    for(int i=0;i<arrayLength;i++)
    {
        std::cout << hostArray[i] << "\n";
    }

    cudaFree(deviceArray);
    free(hostArray);

    std::cout << "Done\n";
}

MyKernel.cu:

#ifndef _MY_KERNEL_
#define _MY_KERNEL_

__global__ void TestDevice(int *deviceArray)
{
    int idx = blockIdx.x*blockDim.x + threadIdx.x;
    deviceArray[idx] = deviceArray[idx]*deviceArray[idx];
}

#endif

Which yields these errors:

1>------ Build started: Project: CUDASandbox, Configuration: Debug x64 ------
1>Compiling with CUDA Build Rule...
1>"C:\CUDA\bin64\nvcc.exe"    -arch sm_10 -ccbin "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin"    -Xcompiler "/EHsc /W3 /nologo /O2 /Zi   /MT  "  -maxrregcount=32  --compile -o "x64\Debug\KernelWrapper.cu.obj" "d:\Stuff\Programming\Visual Studio 2008\Projects\CUDASandbox\CUDASandbox\KernelWrapper.cu" 
1>KernelWrapper.cu
1>tmpxft_00001ab8_00000000-3_KernelWrapper.cudafe1.gpu
1>tmpxft_00001ab8_00000000-8_KernelWrapper.cudafe2.gpu
1>tmpxft_00001ab8_00000000-3_KernelWrapper.cudafe1.cpp
1>tmpxft_00001ab8_00000000-12_KernelWrapper.ii
1>Compiling with CUDA Build Rule...
1>"C:\CUDA\bin64\nvcc.exe"    -arch sm_10 -ccbin "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin"    -Xcompiler "/EHsc /W3 /nologo /O2 /Zi   /MT  "  -maxrregcount=32  --compile -o "x64\Debug\MyKernel.cu.obj" "d:\Stuff\Programming\Visual Studio 2008\Projects\CUDASandbox\CUDASandbox\MyKernel.cu" 
1>MyKernel.cu
1>tmpxft_000017b0_00000000-3_MyKernel.cudafe1.gpu
1>tmpxft_000017b0_00000000-8_MyKernel.cudafe2.gpu
1>tmpxft_000017b0_00000000-3_MyKernel.cudafe1.cpp
1>tmpxft_000017b0_00000000-12_MyKernel.ii
1>Compiling...
1>Main.cpp
1>Compiling manifest to resources...
1>Microsoft (R) Windows (R) Resource Compiler Version 6.1.6723.1
1>Copyright (C) Microsoft Corporation.  All rights reserved.
1>Linking...
1>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: void __cdecl std::basic_ios<char,struct std::char_traits<char> >::setstate(int,bool)" (?setstate@?$basic_ios@DU?$char_traits@D@std@@@std@@QEAAXH_N@Z) already defined in KernelWrapper.cu.obj
1>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: __int64 __cdecl std::ios_base::width(__int64)" (?width@ios_base@std@@QEAA_J_J@Z) already defined in KernelWrapper.cu.obj
1>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: __int64 __cdecl std::basic_streambuf<char,struct std::char_traits<char> >::sputn(char const *,__int64)" (?sputn@?$basic_streambuf@DU?$char_traits@D@std@@@std@@QEAA_JPEBD_J@Z) already defined in KernelWrapper.cu.obj
1>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: static bool __cdecl std::char_traits<char>::eq_int_type(int const &,int const &)" (?eq_int_type@?$char_traits@D@std@@SA_NAEBH0@Z) already defined in KernelWrapper.cu.obj
1>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: static int __cdecl std::char_traits<char>::eof(void)" (?eof@?$char_traits@D@std@@SAHXZ) already defined in KernelWrapper.cu.obj
1>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: int __cdecl std::basic_streambuf<char,struct std::char_traits<char> >::sputc(char)" (?sputc@?$basic_streambuf@DU?$char_traits@D@std@@@std@@QEAAHD@Z) already defined in KernelWrapper.cu.obj
1>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: class std::basic_streambuf<char,struct std::char_traits<char> > * __cdecl std::basic_ios<char,struct std::char_traits<char> >::rdbuf(void)const " (?rdbuf@?$basic_ios@DU?$char_traits@D@std@@@std@@QEBAPEAV?$basic_streambuf@DU?$char_traits@D@std@@@2@XZ) already defined in KernelWrapper.cu.obj
1>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: char __cdecl std::basic_ios<char,struct std::char_traits<char> >::fill(void)const " (?fill@?$basic_ios@DU?$char_traits@D@std@@@std@@QEBADXZ) already defined in KernelWrapper.cu.obj
1>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: int __cdecl std::ios_base::flags(void)const " (?flags@ios_base@std@@QEBAHXZ) already defined in KernelWrapper.cu.obj
1>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: __int64 __cdecl std::ios_base::width(void)const " (?width@ios_base@std@@QEBA_JXZ) already defined in KernelWrapper.cu.obj
1>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: static unsigned __int64 __cdecl std::char_traits<char>::length(char const *)" (?length@?$char_traits@D@std@@SA_KPEBD@Z) already defined in KernelWrapper.cu.obj
1>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl std::basic_ostream<char,struct std::char_traits<char> >::flush(void)" (?flush@?$basic_ostream@DU?$char_traits@D@std@@@std@@QEAAAEAV12@XZ) already defined in KernelWrapper.cu.obj
1>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: class std::basic_ostream<char,struct std::char_traits<char> > * __cdecl std::basic_ios<char,struct std::char_traits<char> >::tie(void)const " (?tie@?$basic_ios@DU?$char_traits@D@std@@@std@@QEBAPEAV?$basic_ostream@DU?$char_traits@D@std@@@2@XZ) already defined in KernelWrapper.cu.obj
1>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: bool __cdecl std::ios_base::good(void)const " (?good@ios_base@std@@QEBA_NXZ) already defined in KernelWrapper.cu.obj
1>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: void __cdecl std::basic_ostream<char,struct std::char_traits<char> >::_Osfx(void)" (?_Osfx@?$basic_ostream@DU?$char_traits@D@std@@@std@@QEAAXXZ) already defined in KernelWrapper.cu.obj
1>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "bool __cdecl std::uncaught_exception(void)" (?uncaught_exception@std@@YA_NXZ) already defined in libcpmt.lib(uncaught.obj)
1>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: void __cdecl std::basic_streambuf<char,struct std::char_traits<char> >::_Lock(void)" (?_Lock@?$basic_streambuf@DU?$char_traits@D@std@@@std@@QEAAXXZ) already defined in KernelWrapper.cu.obj
1>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: void __cdecl std::basic_streambuf<char,struct std::char_traits<char> >::_Unlock(void)" (?_Unlock@?$basic_streambuf@DU?$char_traits@D@std@@@std@@QEAAXXZ) already defined in KernelWrapper.cu.obj
1>MSVCRTD.lib(ti_inst.obj) : error LNK2005: "private: __cdecl type_info::type_info(class type_info const &)" (??0type_info@@AEAA@AEBV0@@Z) already defined in LIBCMT.lib(typinfo.obj)
1>MSVCRTD.lib(ti_inst.obj) : error LNK2005: "private: class type_info & __cdecl type_info::operator=(class type_info const &)" (??4type_info@@AEAAAEAV0@AEBV0@@Z) already defined in LIBCMT.lib(typinfo.obj)
1>LINK : warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; use /NODEFAULTLIB:library
1>D:\Stuff\Programming\Visual Studio 2008\Projects\CUDASandbox\x64\Debug\CUDASandbox.exe : fatal error LNK1169: one or more multiply defined symbols found
1>Build log was saved at "file://d:\Stuff\Programming\Visual Studio 2008\Projects\CUDASandbox\CUDASandbox\x64\Debug\BuildLog.htm"
1>CUDASandbox - 21 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I am running Visual Studio 2008 on Windows Vista 7 64 bit

Edit:

Tom, here is the build log that results from setting the code generation to /MT

1>------ Build started: Project: CUDASandbox, Configuration: Debug x64 ------
1>Compiling with CUDA Build Rule...
1>"C:\CUDA\bin64\nvcc.exe"    -arch sm_10 -ccbin "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin"    -Xcompiler "/EHsc /W3 /nologo /O2 /Zi   /MT  "  -maxrregcount=32  --compile -o "x64\Debug\MyKernel.cu.obj" "d:\Stuff\Programming\Visual Studio 2008\Projects\CUDASandbox\CUDASandbox\MyKernel.cu" 
1>MyKernel.cu
1>tmpxft_000019e4_00000000-3_MyKernel.cudafe1.gpu
1>tmpxft_000019e4_00000000-8_MyKernel.cudafe2.gpu
1>tmpxft_000019e4_00000000-3_MyKernel.cudafe1.cpp
1>tmpxft_000019e4_00000000-12_MyKernel.ii
1>Compiling with CUDA Build Rule...
1>"C:\CUDA\bin64\nvcc.exe"    -arch sm_10 -ccbin "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin"    -Xcompiler "/EHsc /W3 /nologo /O2 /Zi   /MT  "  -maxrregcount=32  --compile -o "x64\Debug\KernelWrapper.cu.obj" "d:\Stuff\Programming\Visual Studio 2008\Projects\CUDASandbox\CUDASandbox\KernelWrapper.cu" 
1>KernelWrapper.cu
1>tmpxft_0000078c_00000000-3_KernelWrapper.cudafe1.gpu
1>tmpxft_0000078c_00000000-8_KernelWrapper.cudafe2.gpu
1>tmpxft_0000078c_00000000-3_KernelWrapper.cudafe1.cpp
1>tmpxft_0000078c_00000000-12_KernelWrapper.ii
1>Compiling...
1>Main.cpp
1>Linking...
1>LINK : warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs; use /NODEFAULTLIB:library
1>libcpmtd.lib(cout.obj) : error LNK2019: unresolved external symbol _CrtDbgReportW referenced in function "public: char const & __cdecl std::_String_const_iterator<char,struct std::char_traits<char>,class std::allocator<char> >::operator*(void)const " (??D?$_String_const_iterator@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEBAAEBDXZ)
1>libcpmtd.lib(stdthrow.obj) : error LNK2001: unresolved external symbol _CrtDbgReportW
1>libcpmtd.lib(xmbtowc.obj) : error LNK2001: unresolved external symbol _CrtDbgReportW
1>libcpmtd.lib(xdebug.obj) : error LNK2019: unresolved external symbol _malloc_dbg referenced in function "void * __cdecl operator new(unsigned __int64,struct std::_DebugHeapTag_t const &,char *,int)" (??2@YAPEAX_KAEBU_DebugHeapTag_t@std@@PEADH@Z)
1>libcpmtd.lib(xmbtowc.obj) : error LNK2001: unresolved external symbol _malloc_dbg
1>libcpmtd.lib(xdebug.obj) : error LNK2019: unresolved external symbol _free_dbg referenced in function "void __cdecl operator delete(void *,struct std::_DebugHeapTag_t const &,char *,int)" (??3@YAXPEAXAEBU_DebugHeapTag_t@std@@PEADH@Z)
1>libcpmtd.lib(xmbtowc.obj) : error LNK2001: unresolved external symbol _free_dbg
1>libcpmtd.lib(_tolower.obj) : error LNK2019: unresolved external symbol _calloc_dbg referenced in function _Getctype
1>D:\Stuff\Programming\Visual Studio 2008\Projects\CUDASandbox\x64\Debug\CUDASandbox.exe : fatal error LNK1120: 4 unresolved externals
1>Build log was saved at "file://d:\Stuff\Programming\Visual Studio 2008\Projects\CUDASandbox\CUDASandbox\x64\Debug\BuildLog.htm"
1>CUDASandbox - 9 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

解决方案

It looks like you have a conflict between two different versions of the C runtime. The CUDA runtime uses the statically linked C runtime, and your application uses the dynamically loaded C runtime (typically the default in Visual Studio).

Change the code generation to use statically loaded C runtime to match the CUDA runtime:

  • Right click on the project and choose Properties, then in C/C++ -> Code Generation change the Runtime Library to /MT

You could use /MTd for debug, in which case you will need to mirror this in CUDA Build Rule -> Hybrid CUDA/C++ Options.

See this earlier post for a step-by-step with more tips.

这篇关于如何集成CUDA .cu代码与C ++应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆