更新:标题具有误导性。最初我可以通过在下面的代码中展开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,这很可能是一个错误。