【问题标题】:Static Assert for NVCC and Compiler BugNVCC 和编译器错误的静态断言
【发布时间】:2011-10-01 11:14:01
【问题描述】:

在用于编译时设置的结构中为 NVCC 编译器提供静态断言的最佳方法是:

以下工作主要但有时 NVCC 会产生废话错误消息,即使应该编译也不会编译!

template<int A, int B>
struct Settings{

   static const int a = A;
   static const int b = B;
   STATIC_ASSERT(a  == 15);
}
typedef Settings<15,5> set1; // Comment this out and it works....

template<int A, int B>
struct Settings2{

   static const int a = A;
   static const int b = B;
   STATIC_ASSERT(a % b == 0);
}
typedef Settings<10,5> set2;

静态断言不起作用,我不知道,但有一个 CUDA 编译器错误告诉我,当我编译它时会抛出 STATIC_ASSERT(a == 15);即使它应该编译,因为上面的代码是正确的,如果我注释掉(A)那么它会突然起作用, 我使用 Thrust 的 STATIC_ASSERT,它基本上取自 Boost:

    #define JOIN( X, Y ) DO_JOIN( X, Y )
#define DO_JOIN( X, Y ) DO_JOIN2(X,Y)
#define DO_JOIN2( X, Y ) X##Y

namespace staticassert {

   // HP aCC cannot deal with missing names for template value parameters
   template <bool x> struct STATIC_ASSERTION_FAILURE;

   template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; };

   // HP aCC cannot deal with missing names for template value parameters
   template<int x> struct static_assert_test{};

};

// XXX nvcc 2.3 can't handle STATIC_ASSERT

#if defined(__CUDACC__) && (CUDA_VERSION > 100)
#error your version number of cuda is not 2 digits!
#endif

#if defined(__CUDACC__) /* && (CUDA_VERSION < 30)*/


#define STATIC_ASSERT( B ) typedef staticassert::static_assert_test<sizeof(staticassert::STATIC_ASSERTION_FAILURE< (bool)( (B) ) >) > JOIN(thrust_static_assert_typedef_, __LINE__)
#define STATIC_ASSERT2(B,COMMENT) STATIC_ASSERT(B)


#else
#define STATIC_ASSERT2(B,COMMENT) \
   typedef staticassert::static_assert_test< \
      sizeof(staticassert::STATIC_ASSERTION_FAILURE< (bool)( (B) ) >)>\
         JOIN(thrust_static_assert_typedef_, JOIN(__LINE__, COMMENT ))

#define STATIC_ASSERT( B ) \
   typedef staticassert::static_assert_test<sizeof(staticassert::STATIC_ASSERTION_FAILURE< (bool)( (B) ) >) > JOIN(thrust_static_assert_typedef_, __LINE__)

#endif // NVCC 2.3

有人遇到过同样的问题吗?

感谢任何cmets!

【问题讨论】:

  • 我在我的 CUDA 源代码中使用了 boost 的静态断言(使用 nvcc 3.2)。当断言失败时,错误消息不是那么好,但我没有遇到不应该的断言失败。

标签: cuda nvcc


【解决方案1】:

在每个struct 定义后添加缺少的分号后,您的代码编译时不会出现警告或错误。系统详情:

harrism$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2010 NVIDIA Corporation
Built on Thu_Nov_11_15:26:50_PST_2010
Cuda compilation tools, release 3.2, V0.2.1221

harrism$ g++ --version
i686-apple-darwin10-g++-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666) (dot 3)

【讨论】:

    猜你喜欢
    • 2022-10-15
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多