简体   繁体   English

CUDA和VS2010问题

[英]CUDA & VS2010 problem

I have scoured the internets looking for an answer to this one, but couldn't find any. 我搜寻了互联网,以寻找答案,但找不到任何答案。 I've installed the CUDA 3.2 SDK (and, just now, CUDA 4.0 RC) and everything seems to work fine after long hours of fooling around with include directories, NSight, and all the rest. 我已经安装了CUDA 3.2 SDK(以及现在的CUDA 4.0 RC),经过漫长的漫长时间浏览包括目录,NSight和所有其他内容,一切似乎都可以正常工作。 Well, except this one thing: it keeps highlighting the <<< >>> operator as a mistake. 好吧,除了这一件事:它一直高亮显示<<< >>>运算符是一个错误。 Only on VS2010--not on VS2008. 仅在VS2010上-不在VS2008上。

On VS2010 I also get several warnings of the following sort: 在VS2010上,我还收到以下几种警告:

C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xdebug(109): warning C4251: 'std::_String_val<_Ty,_Alloc>::_Alval' : class 'std::_DebugHeapAllocator<_Ty>' needs to have dll-interface to be used by clients of class 'std::_String_val<_Ty,_Alloc>'

Update: If I try and include an entry point in a .cpp file that calls a CUDA kernel, instead of writing main() in a .cu file as I was doing, the operator is actually flagged as an error, besides highlighting it! 更新:如果我尝试在调用CUDA内核的.cpp文件中包含一个入口点,而不是像我.cu那样在.cu文件中写入main() ,则除了突出显示该操作符之外,该操作符实际上还被标记为错误! The same thing happens with VS2008. VS2008也发生同样的事情。

Anyone know how this can be fixed? 有谁知道该如何解决?

Update 2: Here is the code. 更新2:这是代码。 The main.cpp file: main.cpp文件:

#include "kernel.cu"

int main()
{
    doStuff();
    return 0;
}

and the .cu file: .cu文件:

#include <iostream>
#include "cuda.h"
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <cutil_inline.h>
#include <time.h>

using namespace std;

#define N 16

__global__ void MatAdd(float A[N][N], float B[N][N], float C[N][N])

{
    int i = blockIdx.x * blockDim.x + threadIdx.x;
int j = blockIdx.y * blockDim.y + threadIdx.y;

if (i < N && j < N)
    C[i][j] = A[i][j] + B[i][j];
}

int doStuff()
{
    dim3 threadsPerBlock(8, 8);
    dim3 numBlocks(N / threadsPerBlock.x, N / threadsPerBlock.y);

    float A[N][N], B[N][N], C[N][N];

    for (int i = 0; i < N; ++i)
        for (int j = 0; j < N; ++j)
        {
            A[i][j] = 0;
            B[i][j] = 0;
            C[i][j] = 0;
        }

    clock_t start = clock();
    MatAdd<<<numBlocks, threadsPerBlock>>>(A, B, C);
    clock_t end = clock();

    cout << "Took " << float(end - start) << "ms to work out." << endl;
    cin.get();

    return 0;
}

Update 3: Alright, I was (idiotically) including the CUDA code in the .cpp file, so of course it couldn't compile. 更新3:好吧,我是(白痴),包括在该CUDA代码.cpp文件,所以它当然无法编译。 Now I have CUDA 4.0 up and running on VS2010, but I still get several warnings of the kind explained above. 现在,我已经在VS2010上启动并运行了CUDA 4.0,但是仍然收到上述几种警告。

You cannot do this... 你不可以做这个...

#include "kernel.cu" 

Now you're asking the Visual Studio CPP compiler to compile the .CU file as though it was a header. 现在,您要Visual Studio CPP编译器编译.CU文件,就好像它是一个头文件一样。 You need to have a header file that declares doStuff() and include the header not the definition. 您需要有一个声明doStuff()的头文件,并包含该头而不是定义。

The following might be helpful. 以下内容可能会有所帮助。

http://www.ademiller.com/blogs/tech/2010/12/using-cudathrust-with-the-parallel-patterns-library/ http://www.ademiller.com/blogs/tech/2010/12/using-cudathrust-with-the-parallel-patterns-library/

http://blog.cuvilib.com/2011/02/24/how-to-run-cuda-in-visual-studio-2010/ http://blog.cuvilib.com/2011/02/24/how-to-run-cuda-in-visual-studio-2010/

Typically I set this up as two projects. 通常,我将其设置为两个项目。 One project that compiles against the the 2008 CPP compiler for .CU and another that uses the 2010 compiler to get all the C++0x features. 一个项目针对.CU的2008 CPP编译器进行编译,而另一个项目则使用2010编译器进行编译以获取所有C ++ 0x功能。

The warnings your getting can be fixed by exporting the appropriate templates. 您可以通过导出适当的模板来解决收到的警告。 Something like this but you'll have to write a specific one for each of the warning types. 像这样,但是您必须为每种警告类型编写一个特定的警告。

#if defined(__CUDACC__)
#define DECLSPECIFIER  __declspec(dllexport)
#define EXPIMP_TEMPLATE

#else
#define DECLSPECIFIER  __declspec(dllimport)
#define EXPIMP_TEMPLATE extern
#endif

EXPIMP_TEMPLATE template class DECLSPECIFIER thrust::device_vector<unsigned long>;

See: 看到:

http://support.microsoft.com/default.aspx?scid=KB;EN-US;168958 and http://msdn.microsoft.com/en-us/library/esew7y1w.aspx http://support.microsoft.com/default.aspx?scid=KB;EN-US;168958http://msdn.microsoft.com/en-us/library/esew7y1w.aspx

I've written a step-by-step guide to setting up VS 2010 and CUDA 4.0 here 我已经在此处编写了设置VS 2010和CUDA 4.0的分步指南

http://www.ademiller.com/blogs/tech/2011/03/using-cuda-and-thrust-with-visual-studio-2010/ http://www.ademiller.com/blogs/tech/2011/03/using-cuda-and-thrust-with-visual-studio-2010/

BTW: A better way of timing CUDA code is with the event API. 顺便说一句:计时CUDA代码的更好方法是使用事件API。

cudaEvent_t start, stop; 
float time;
cudaEventCreate(&start);
cudaEventCreate(&stop); 
cudaEventRecord( start, 0 ); 
kernel<<<grid,threads>>> ( d_odata, d_idata, size_x, size_y, NUM_REPS); 
cudaEventRecord( stop, 0 ); 
cudaEventSynchronize( stop ); 
cudaEventElapsedTime( &time, start, stop );
cudaEventDestroy( start );
cudaEventDestroy( stop );

I was including the .cu file directly. 我直接包含了.cu文件。 Of course, that's pretty much including the CUDA code in the .cpp file, and hence the error! 当然,这几乎包括.cpp文件中的CUDA代码,因此会出现错误!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM