5

这篇文章与我之前的文章非常相似:如何将 CUDA 代码分离为多个文件 恐怕我对我实际要求的内容犯了这样的错误,以至于尝试在那里纠正它会太混乱。

我将此代码松散地基于 CUDA SDK 中的 cppIntegration 示例。

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

主要.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

这会产生这些错误:

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 ==========

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

编辑:

Tom,这是将代码生成设置为 /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 ==========
4

1 回答 1

6

看起来您在 C 运行时的两个不同版本之间存在冲突。CUDA 运行时使用静态链接的 C 运行时,而您的应用程序使用动态加载的 C 运行时(通常是 Visual Studio 中的默认值)。

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

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

您可以使用 /MTd 进行调试,在这种情况下,您需要在 CUDA Build Rule -> Hybrid CUDA/C++ Options 中进行镜像。

有关更多提示的分步操作,请参阅此较早的帖子。

于 2010-01-20T11:39:04.610 回答