仅当循环展开时,才没有未指定的启动错误

No unspecified launch error only if a loop is unrolled?

本文关键字:未指定 启动 错误 循环展开      更新时间:2023-10-16

更新:标题具有误导性。最初,我可以通过展开下面的代码中的block循环来使错误消失。现在,即使是简单的代码更改也会让它消失。(请参阅下面的代码示例(。

背景:

12x12 矩阵的 Cholesky 分解的 CUDA C 内核实现会导致一个相当大的 CUDA 内核(280 行代码,大量循环(。

我通过简化的设置重现了错误(下面的代码(。NVCC (CUDA 4.2( 调用

nvcc -arch sm_20 -o main main.cu

在 Linux 上的费米架构上执行:

kernel call: unspecified launch failure

内核主体包含一个条件预处理器块#if 1#else#endif。我插入它是为了在工作和非工作版本之间轻松切换。编译第一个备选结果在unspecified launch failure中。而第二种选择运行良好。

棘手的部分是,在任何一种情况下,实际执行的代码都应该相同。(有命令代表是真的!!

即使#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);
}

请注意,站点表包含未初始化的数据。这很好,因为它没有使用。我需要它来显示错误。

更新:

刚刚在安装了 CUDA 4.0 的另一台机器上尝试过。那里没有出现错误(相同的费米卡模型(。可能真的是 CUDA 4.2 的 NVCC 错误。由于他们从CUDA 4.1切换到LLVM,这可能是一个错误。

这个问题似乎是由基于 LLVM 的 CUDA 工具链早期版本中的一个微妙的编译器错误引起的。该错误不会在更现代的工具链版本(CUDA 6.x 和 7.x(上重现。

[此答案已从评论和编辑中添加为社区维基条目,以将问题从 CUDA 标签的回答列表中删除]