问题描述
由于从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()
{
}
$ b b void initialize()
{
checkCudaErrors(cudaMalloc(NULL,1));
}
T val;
};
int main(int argc,char ** argv)
{
Trivial< int> tt;
tt.initialize();
return 0;
}
和编译结果:(使用GCC编译时会出现同样的错误4.5 also!)
1> ------构建开始:项目:ZERO_CHECK, ---
$
2> ------构建开始:项目:hugecc,配置:版本x64 ------
2> trivial_main.cpp
2> .. \src\trivial_main.cpp(19):error C2660:'Trivial< T> :: check':函数不接受4个参数
2> with
2> [
2> T = int
2> ]
2> .. \src\trivial_main.cpp(18):当编译类模板成员函数'void Trivial< T> :: initialize(void)'
2> with
2> [
2> T = int
2> ]
2> .. \src\trivial_main.cpp(29):参见类模板实例化'Trivial< T>'被编译
2> with
2> [
2> T = int
2> ]
3> ------跳过生成:项目:ALL_BUILD,配置:版本x64 ------
3>未选择项目以构建此解决方案配置
= ========= Build:1 succeeded,1 failed,1 up-to-date,1 skipped ==========
解决方案pre>
p>我不得不复制检查(..)函数的定义从helper_functions.h到我的类的头,以能够编译类。
#include< stdio.h>
#include #include< helper_cuda.h>
#include< helper_functions.h>
class Trivial {
public:
template<类型名T>
bool check(T result,char const * const func,const char * const file,int const line)
{
if(result){
fprintf(stderr,CUDA %s的错误:%d代码=%d(%s)\%s\\\\
,
文件,行,static_cast< unsigned int>(结果),_cudaGetErrorEnum func);
return true;
} else {
return false;
}
}
void check(){}
void initialize()
{
checkCudaErrors(cudaMalloc ,1));
}
};
int main(int argc,char ** argv)
{
Trivial tt;
tt.initialize();
return 0;
}
所以,这主要解决了我的问题, >
As the cutil.h header is removed from CUDA Samples, some new headers are introduced like helper_cuda.h, helper_functions.h.
One of the main keywords that is used by me was CUDA_CHECK_ERROR, and I think it is replaced with checkCudaErrors.
In most of my code the macro compiles and works well. However when I use it in a class which has a function named check(..), checkCudaErrors function gives compile errors.
Here is an example:
#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; }
and the result of compilation: (the same error is seen when compiled with GCC 4.5 also!)
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 ==========
The same error is taken when I removed the template parameter.
解决方案I had to copy the check(..) function's definition from helper_functions.h into my class's header to be able to compile the class.
#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; }
So, this mainly solved my problem and my code compiled successfully.
这篇关于CUDA 5.0:checkCudaErrors无法找到正确的“检查”方法,如果类具有“检查”方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!