【问题标题】:Calculate matrix determinants with cublas device API使用 cublas 设备 API 计算矩阵行列式
【发布时间】:2017-04-13 09:09:11
【问题描述】:

我正在尝试评估一个标量函数 f(x),其中 x 是一个 k 维向量(即 f:R^k->R)。在评估过程中,我必须执行许多矩阵运算:求逆、乘法和找到矩阵行列式以及中等大小矩阵的迹线(其中大多数小于 30x30)。现在我想通过在 GPU 上使用不同的线程同时评估许多不同 xs 的函数。这就是我需要设备 api 的原因。

我编写了以下代码来测试通过 cublas 设备 API cublasSgetrfBatched 计算矩阵行列式,其中我首先找到矩阵的 LU 分解并计算 U 矩阵中所有对角线元素的乘积。我已经使用 cublas 返回的结果在 GPU 线程和 CPU 上完成了此操作。但是来自 GPU 的结果没有任何意义,而 CPU 上的结果是正确的。我使用了 cuda-memcheck,但没有发现错误。有人可以帮助阐明这个问题吗?非常感谢。

    cat test2.cu

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <cuda_runtime.h>
#include <cublas_v2.h>


__host__ __device__ unsigned int IDX(unsigned int i,unsigned  int j,unsigned int ld){return j*ld+i;}

#define PERR(call) \
  if (call) {\
   fprintf(stderr, "%s:%d Error [%s] on "#call"\n", __FILE__, __LINE__,\
      cudaGetErrorString(cudaGetLastError()));\
   exit(1);\
  }
#define ERRCHECK \
  if (cudaPeekAtLastError()) { \
    fprintf(stderr, "%s:%d Error [%s]\n", __FILE__, __LINE__,\
       cudaGetErrorString(cudaGetLastError()));\
    exit(1);\
  }

__device__ float
det_kernel(float *a_copy,unsigned int *n,cublasHandle_t *hdl){
  int *info = (int *)malloc(sizeof(int));info[0]=0;
  int batch=1;int *p = (int *)malloc(*n*sizeof(int));  
  float **a = (float **)malloc(sizeof(float *));
  *a = a_copy;  
  cublasStatus_t status=cublasSgetrfBatched(*hdl, *n, a, *n, p, info, batch);  
  unsigned int i1;
  float res=1;
  for(i1=0;i1<(*n);++i1)res*=a_copy[IDX(i1,i1,*n)];
  return res;
}

__global__ void runtest(float *a_i,unsigned int n){
  cublasHandle_t hdl;cublasCreate_v2(&hdl);
  printf("det on GPU:%f\n",det_kernel(a_i,&n,&hdl));  
  cublasDestroy_v2(hdl);
}

int
main(int argc, char **argv)
{
  float a[] = {
    1,   2,   3,
    0,   4,   5,
    1,   0,   0};
  cudaSetDevice(1);//GTX780Ti on my machine,0 for GTX1080
  unsigned int n=3,nn=n*n;
  printf("a is \n");
  for (int i = 0; i < n; ++i){    
    for (int j = 0; j < n; j++) printf("%f, ",a[IDX(i,j,n)]);    
    printf("\n");}
  float *a_d;
  PERR(cudaMalloc((void **)&a_d, nn*sizeof(float)));
  PERR(cudaMemcpy(a_d, a, nn*sizeof(float), cudaMemcpyHostToDevice));
  runtest<<<1, 1>>>(a_d,n);
  cudaDeviceSynchronize();
  ERRCHECK;

  PERR(cudaMemcpy(a, a_d, nn*sizeof(float), cudaMemcpyDeviceToHost));
  float res=1;
  for (int i = 0; i < n; ++i)res*=a[IDX(i,i,n)];
  printf("det on CPU:%f\n",res);
}

  nvcc -arch=sm_35 -rdc=true -o test test2.cu -lcublas_device -lcudadevrt
./test
a is 
1.000000, 0.000000, 1.000000, 
2.000000, 4.000000, 0.000000, 
3.000000, 5.000000, 0.000000, 
det on GPU:0.000000
det on CPU:-2.000000

【问题讨论】:

    标签: c++ matrix cuda cublas


    【解决方案1】:

    cublas 设备调用是异步的

    这意味着它们在 cublas 调用完成之前将控制权返回给调用线程。

    如果您希望调用线程能够直接处理结果(正如您在此处计算 res 所做的那样),您必须在开始计算之前强制同步等待结果。

    您在主机端计算中看不到这一点,因为在父内核终止之前,任何设备活动(包括 cublas 设备动态并行性)都存在隐式同步。

    因此,如果您在设备 cublas 调用之后添加同步,如下所示:

    cublasStatus_t status=cublasSgetrfBatched(*hdl, *n, a, *n, p, info, batch); 
    cudaDeviceSynchronize(); // add this line
    

    我想你会看到设备计算和主机计算之间的匹配,正如你所期望的那样。

    【讨论】:

    • 非常感谢,罗伯特。您的建议有效并且可以产生预期的结果。我有另一个设备例程,它首先使用 cublasSgetrfBatched 来获取 LU 分解,然后使用 cublasSgetriBatched 从 LU 输出中获取逆。在它们之间,我需要使用 cudaDeviceSynchronize() 吗?对于小矩阵(3 x 3),结果似乎相同。
    • 另一个问题是关于释放设备例程中malloc创建的内存。如果我使用 free() 释放内存,当使用 -lcublas_device 编译代码时,cuda-memcheck 会产生错误(没有它也可以)。你碰巧有任何想法来解决这个问题吗?如果我不释放内存会有什么后果?非常感谢。
    • 关于第一个问题,cuda stream semantics同样适用于设备端操作。未指定流的设备端启动应启动到默认流(每个线程!),这意味着设备 cublas 操作 A 后跟由同一线程向同一流发出的设备 cublas 操作 B 应该序列化。 B 不应该在 A 完成之前开始。关于第二个问题,我需要确切知道您在哪里进行free() 操作。
    • 在原帖的代码中,我使用 malloc 在 det_kernel 中创建了 *p。如果我将 free(p) 放在例程的末尾,cuda-memcheck 会产生错误,尽管程序仍然可以运行。
    • 如果这是对原始帖子代码的唯一更改,那么这是一个问题。您需要添加我在回答中已经提到的同步。如果不这样做,那么当异步 cublas 例程仍在尝试使用它时,您将释放 p。
    猜你喜欢
    • 2013-05-12
    • 2015-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-16
    • 2018-11-19
    相关资源
    最近更新 更多