更新:标题具有误导性。最初我可以通过在下面的代码中展开block
循环来使错误消失。现在,即使是简单的代码更改也会让它消失。 (参见下面的代码示例)。
背景:
对12x12矩阵进行Cholesky分解的CUDA C内核实现会产生一个非常大的CUDA内核(280个代码行,大量循环)。
我通过简化设置(下面的代码)重现了错误。用
调用NVCC(CUDA 4.2)nvcc -arch sm_20 -o main main.cu
在Linux上的Fermi架构上执行:
kernel call: unspecified launch failure
内核主体包含条件预处理器块#if 1
,#else
,#endif
。我插入它可以轻松地在工作版本和非工作版本之间切换。编译unspecified launch failure
中的第一个替代结果。而第二种选择运行良好。
棘手的部分是,实际执行的代码在任何一种情况下应该是相同的。 (hasOrderedRep是真的!!)
即使#if 1
语句保持不变,仍然可以使错误消失。因此必须展开block
循环。这就是标题的起源地。
#include "cuda.h"
#include "stdio.h"
#include <iostream>
#include <string>
using namespace std;
/////////////////////////////////////
//
// First some basic types I need
// Implementation of a templated
// scalar and complex number
template<class T> class RScalar
{
public:
__device__ RScalar() {}
__device__ ~RScalar() {}
template<class T1> __device__
RScalar(const RScalar<T1>& rhs) : F(rhs.elem()) {}
template<class T1> __device__
RScalar(const T1& rhs) : F(rhs) {}
template<class T1> __device__ inline
RScalar& operator=(const RScalar<T1>& rhs) {
elem() = rhs.elem();
return *this;
}
public:
__device__ T& elem() {return F;}
__device__ const T& elem() const {return F;}
private:
T F;
};
template<class T> class RComplex
{
public:
__device__ RComplex() {}
__device__ ~RComplex() {}
template<class T1, class T2> __device__
RComplex(const RScalar<T1>& _re, const RScalar<T2>& _im):
re(_re.elem()), im(_im.elem()) {}
template<class T1, class T2> __device__
RComplex(const T1& _re, const T2& _im): re(_re), im(_im) {}
template<class T1> __device__
RComplex(const T1& _re): re(_re), im() {}
template<class T1>
__device__ inline
RComplex& operator*=(const RScalar<T1>& rhs)
{
real() *= rhs.elem();
imag() *= rhs.elem();
return *this;
}
template<class T1>
__device__ inline
RComplex& operator-=(const RComplex<T1>& rhs)
{
real() -= rhs.real();
imag() -= rhs.imag();
return *this;
}
template<class T1> __device__ inline
RComplex& operator/=(const RComplex<T1>& rhs)
{
RComplex<T> d;
d = *this / rhs;
real() = d.real();
imag() = d.imag();
return *this;
}
public:
__device__ T& real() {return re;}
__device__ const T& real() const {return re;}
__device__ T& imag() {return im;}
__device__ const T& imag() const {return im;}
private:
T re;
T im;
};
template<class T> __device__ RComplex<T>
operator*(const RComplex<T>& __restrict__ l,
const RComplex<T>& __restrict__ r)
{
return RComplex<T>(l.real()*r.real() - l.imag()*r.imag(),
l.real()*r.imag() + l.imag()*r.real());
}
template<class T> __device__ RComplex<T>
operator/(const RComplex<T>& l, const RComplex<T>& r)
{
T tmp = T(1.0) / (r.real()*r.real() + r.imag()*r.imag());
return RComplex<T>((l.real()*r.real() + l.imag()*r.imag()) * tmp,
(l.imag()*r.real() - l.real()*r.imag()) * tmp);
}
template<class T> __device__ RComplex<T>
operator*(const RComplex<T>& l, const RScalar<T>& r)
{
return RComplex<T>(l.real()*r.elem(),
l.imag()*r.elem());
}
//
//
//////////////////////////////////////////////
#define REALT float
#define Nc 3
struct PrimitiveClovTriang
{
RScalar<REALT> diag[2][2*Nc];
RComplex<REALT> offd[2][2*Nc*Nc-Nc];
};
__global__ void kernel(bool hasOrderedRep, int * siteTable,
PrimitiveClovTriang* tri)
{
RScalar<REALT> zip=0;
int N = 2*Nc;
int site;
//
// First if-block results in an error,
// second, runs fine! Since hasOrderedRep
// is true, the code blocks should be
// identical.
//
#if 1
if (hasOrderedRep) {
site = blockDim.x * blockIdx.x +
blockDim.x * gridDim.x * blockIdx.y +
threadIdx.x;
} else {
int idx0 = blockDim.x * blockIdx.x +
blockDim.x * gridDim.x * blockIdx.y +
threadIdx.x;
site = ((int*)(siteTable))[idx0];
}
#else
site = blockDim.x * blockIdx.x + blockDim.x * gridDim.x * blockIdx.y + threadIdx.x;
#endif
int site_neg_logdet=0;
for(int block=0; block < 2; block++) {
RScalar<REALT> inv_d[6];
RComplex<REALT> inv_offd[15];
RComplex<REALT> v[6];
RScalar<REALT> diag_g[6];
for(int i=0; i < N; i++) {
inv_d[i] = tri[site].diag[block][i];
}
for(int i=0; i < 15; i++) {
inv_offd[i] =tri[site].offd[block][i];
}
for(int j=0; j < N; ++j) {
for(int i=0; i < j; i++) {
int elem_ji = j*(j-1)/2 + i;
RComplex<REALT> A_ii = RComplex<REALT>( inv_d[i], zip );
v[i] = A_ii*RComplex<REALT>(inv_offd[elem_ji].real(),-inv_offd[elem_ji].imag());
}
v[j] = RComplex<REALT>(inv_d[j],zip);
for(int k=0; k < j; k++) {
int elem_jk = j*(j-1)/2 + k;
v[j] -= inv_offd[elem_jk]*v[k];
}
inv_d[j].elem() = v[j].real();
for(int k=j+1; k < N; k++) {
int elem_kj = k*(k-1)/2 + j;
for(int l=0; l < j; l++) {
int elem_kl = k*(k-1)/2 + l;
inv_offd[elem_kj] -= inv_offd[elem_kl] * v[l];
}
inv_offd[elem_kj] /= v[j];
}
}
RScalar<REALT> one;
one.elem() = (REALT)1;
for(int i=0; i < N; i++) {
diag_g[i].elem() = one.elem()/inv_d[i].elem();
// ((PScalar<PScalar<RScalar<float> > > *)(args->dev_ptr[ 1 ] ))[site] .elem().elem().elem() += log(fabs(inv_d[i].elem()));
if( inv_d[i].elem() < 0 ) {
site_neg_logdet++;
}
}
RComplex<REALT> sum;
for(int k = 0; k < N; ++k) {
for(int i = 0; i < k; ++i) {
v[i].real()=v[i].imag()=0;
}
v[k] = RComplex<REALT>(diag_g[k],zip);
for(int i = k+1; i < N; ++i) {
v[i].real()=v[i].imag()=0;
for(int j = k; j < i; ++j) {
int elem_ij = i*(i-1)/2+j;
v[i] -= inv_offd[elem_ij] *inv_d[j]*v[j];
}
v[i] *= diag_g[i];
}
for(int i = N-2; (int)i >= (int)k; --i) {
for(int j = i+1; j < N; ++j) {
int elem_ji = j*(j-1)/2 + i;
v[i] -= RComplex<REALT>(inv_offd[elem_ji].real(),-inv_offd[elem_ji].imag()) * v[j];
}
}
inv_d[k].elem() = v[k].real();
for(int i = k+1; i < N; ++i) {
int elem_ik = i*(i-1)/2+k;
inv_offd[elem_ik] = v[i];
}
}
for(int i=0; i < N; i++) {
tri[site].diag[block][i] = inv_d[i];
}
for(int i=0; i < 15; i++) {
tri[site].offd[block][i] = inv_offd[i];
}
}
if( site_neg_logdet != 0 ) {
}
}
int main()
{
int sites=1;
dim3 blocksPerGrid( 1 , 1 , 1 );
dim3 threadsPerBlock( sites , 1, 1);
PrimitiveClovTriang* tri_dev;
int * siteTable;
cudaMalloc( (void**)&tri_dev , sizeof(PrimitiveClovTriang) * sites );
cudaMalloc( (void**)&siteTable , sizeof(int) * sites );
bool ord=true;
kernel<<< blocksPerGrid , threadsPerBlock , 0 >>>( ord , siteTable , tri_dev );
cudaDeviceSynchronize();
cudaError_t kernel_call = cudaGetLastError();
cout << "kernel call: " << string(cudaGetErrorString(kernel_call)) << endl;
cudaFree(tri_dev);
cudaFree(siteTable);
return(0);
}
请注意,siteTable包含未初始化的数据。这很好,因为它没有使用。我需要它来显示错误。
更新
刚试过安装了CUDA 4.0的其他机器。那里没有出现错误(相同的费米卡型号)。可能真的是CUDA 4.2的NVCC错误。由于他们从CUDA 4.1切换到LLVM,这可能是一个错误。
答案 0 :(得分:1)
这个问题似乎是由基于LLVM的CUDA工具链的早期版本中的一个微妙的编译器错误引起的。错误不会在更现代的工具链版本(CUDA 6.x和7.x)上重现。
[此答案已添加为评论和编辑的社区wiki条目,以便将问题从CUDA标记的答案列表中删除]