【问题标题】:CUDA 5.0: checkCudaErrors fails to find correct "check" method if class has a "check" methodCUDA 5.0:如果类具有“检查”方法,则 checkCudaErrors 无法找到正确的“检查”方法
【发布时间】:2012-12-08 13:42:59
【问题描述】:

由于从 CUDA 示例中删除了 cutil.h 标头,因此引入了一些新标头,例如 helper_cuda.h、helper_functions.h。

我使用的主要关键字之一是 CUDA_CHECK_ERROR,我认为它被替换为 checkCudaErrors。

在我的大部分代码中,宏都可以编译并且运行良好。但是,当我在具有名为 check(..) 的函数的类中使用它时,checkCudaErrors 函数会给出编译错误。

这是一个例子:

#include <stdio.h>

#include <cuda_runtime.h>
#include <helper_cuda.h>
#include <helper_functions.h>

template<typename T>
class Trivial {

public:

    void check()
    {

    }

    void initialize() 
    {
        checkCudaErrors(cudaMalloc(NULL, 1));
    }

    T val;

};

int main(int argc, char **argv)
{

    Trivial<int> tt;

    tt.initialize();

    return 0;
}

以及编译结果:(用GCC 4.5编译也出现同样的错误!)

1>------ Build started: Project: ZERO_CHECK, Configuration: Release x64 ------
2>------ Build started: Project: massivecc, Configuration: Release x64 ------
2>  trivial_main.cpp
2>..\src\trivial_main.cpp(19): error C2660: 'Trivial<T>::check' : function does not     take 4 arguments
2>          with
2>          [
2>              T=int
2>          ]
2>          ..\src\trivial_main.cpp(18) : while compiling class template member         function 'void Trivial<T>::initialize(void)'
2>          with
2>          [
2>              T=int
2>          ]
2>          ..\src\trivial_main.cpp(29) : see reference to class template         instantiation 'Trivial<T>' being compiled
2>          with
2>          [
2>              T=int
2>          ]
3>------ Skipped Build: Project: ALL_BUILD, Configuration: Release x64 ------
3>Project not selected to build for this solution configuration 
========== Build: 1 succeeded, 1 failed, 1 up-to-date, 1 skipped ==========

删除模板参数时出现同样的错误。

【问题讨论】:

  • 我必须将 check(..) 函数定义复制到我的类的头文件中才能编译该类(我的真实代码,不是这个示例,但它是相似的)。
  • 也许您可以将您的解决方案添加为答案并接受它,以便将此问题从未回答的问题列表中删除。

标签: sdk cuda compiler-errors sample


【解决方案1】:

我必须将 check(..) 函数的定义从 helper_functions.h 复制到我的类头文件中才能编译该类。

#include <stdio.h>    
#include <cuda_runtime.h>
#include <helper_cuda.h>
#include <helper_functions.h>    
class Trivial {    
public:    
    template< typename T >
    bool check(T result, char const *const func, const char *const file, int const line)
    {
        if (result) {
            fprintf(stderr, "CUDA error at %s:%d code=%d(%s) \"%s\" \n",
            file, line, static_cast<unsigned int>(result), _cudaGetErrorEnum(result), func);
            return true;
        } else {
            return false;
        }
    }

    void check() {  }

    void initialize() 
    {
        checkCudaErrors(cudaMalloc(NULL, 1));
    }
};

int main(int argc, char **argv)
{
    Trivial tt;
    tt.initialize();    
    return 0;
}

所以,这主要解决了我的问题,我的代码编译成功了。

【讨论】:

    【解决方案2】:

    参考第 680 行 helper_cuda.h 中的源代码

    https://github.com/pathscale/nvidia_sdk_samples/blob/master/vectorAdd/common/inc/helper_cuda.h

    你发现 checkCudaErrors 声明了一个 #define checkCudaErrors(val) check ( (val), #val, FILE, LINE ),它接受一个参数并调用 check带有 3 个基于 config 的其他参数。请注意,它也在第 680 行中定义

    而在您的情况下,您定义了一个不带任何参数的检查。声明和定义不同的情况,多个定义。

    【讨论】:

      猜你喜欢
      • 2020-08-16
      • 1970-01-01
      • 2011-04-15
      • 1970-01-01
      • 1970-01-01
      • 2015-02-14
      • 1970-01-01
      • 2017-01-10
      • 1970-01-01
      相关资源
      最近更新 更多