如何有效地将矢量重复到cuda中的矩阵?

时间:2014-08-22 17:27:35

标签: c++ matrix cuda gpu gpgpu

我想重复一个向量在cuda中形成一个矩阵,避免过多的memcopy。向量和矩阵都在GPU上分配。

例如:

我有一个矢量:

a = [1 2 3 4]

将其展开为矩阵:

b = [1 2 3 4;
     1 2 3 4;
     .......
     1 2 3 4]

我试过的是分配b的每个元素。但这涉及到GPU内存复制的大量GPU内存。

我知道这在matlab中很容易(使用repmat),但如何在cuda中有效地完成它?我没有在古巴拉斯找到任何例行公事。

1 个答案:

答案 0 :(得分:3)

编辑根据评论,我已将代码更新为可处理行主要或列主要底层存储的版本。

这样的事情应该相当快:

// for row_major, blocks*threads should be a multiple of vlen
// for column_major, blocks should be equal to vlen
template <typename T>
__global__ void expand_kernel(const T* vector, const unsigned vlen, T* matrix, const unsigned mdim, const unsigned col_major=0){
  if (col_major){
    int idx = threadIdx.x+blockIdx.x*mdim;
    T myval = vector[blockIdx.x];
    while (idx < ((blockIdx.x+1)*mdim)){
      matrix[idx] = myval;
      idx += blockDim.x;
      }
    }
  else{
    int idx = threadIdx.x + blockDim.x * blockIdx.x;
    T myval = vector[idx%vlen];
    while (idx < mdim*vlen){
      matrix[idx] = myval;
      idx += gridDim.x*blockDim.x;
      }
    }
}

这假设您的矩阵的尺寸为mdim行x vlen列(似乎是您在问题中概述的内容。)

您可以调整网格和块尺寸,以找出适合您特定GPU的最快速度。对于行主要情况,从每个块的256或512个线程开始,并将块数设置为等于或大于GPU中SM数量的4倍。选择网格和块尺寸的乘积等于矢量长度vlen的整数倍。如果这很困难,选择一个任意的,但是#34;大&#34;线程块大小(例如250或500)不应导致效率损失。

对于列主要情况,每块选择256或512个线程,并选择等于vlen的块数,即矢量长度。如果vlen&gt; 65535,您需要为计算能力3.0或更高版本编译它。如果vlen很小,可能小于32,则该方法的效率可能会显着降低。如果您将每个块的线程数增加到GPU的最大值(512或1024),则会发现一些缓解。可能还有其他&#34;展开&#34;可能更适合列专业的实现&#34; narrow&#34;矩阵案例。例如,对列主码的直接修改将允许每个向量元素两个块,或每个向量元素四个块,并且总启动块将为2 * vlen或4 * vlen ,例如。

以下是一个完整的示例,以及一系列带宽测试,以证明上述代码达到bandwidthTest指示的吞吐量的约90%:

$ cat t546.cu
#include <stdio.h>

#define W 512
#define H (512*1024)
// for row_major, blocks*threads should be a multiple of vlen
// for column_major, blocks should be equal to vlen
template <typename T>
__global__ void expand_kernel(const T* vector, const unsigned vlen, T* matrix, const unsigned mdim, const unsigned col_major=0){
  if (col_major){
    int idx = threadIdx.x+blockIdx.x*mdim;
    T myval = vector[blockIdx.x];
    while (idx < ((blockIdx.x+1)*mdim)){
      matrix[idx] = myval;
      idx += blockDim.x;
      }
    }
  else{
    int idx = threadIdx.x + blockDim.x * blockIdx.x;
    T myval = vector[idx%vlen];
    while (idx < mdim*vlen){
      matrix[idx] = myval;
      idx += gridDim.x*blockDim.x;
      }
    }
}

template <typename T>
__global__ void check_kernel(const T* vector, const unsigned vlen, T* matrix, const unsigned mdim, const unsigned col_major=0){
  unsigned i = 0;
  while (i<(vlen*mdim)){
    unsigned idx = (col_major)?(i/mdim):(i%vlen);
    if (matrix[i] != vector[idx]) {printf("mismatch at offset %d\n",i); return;}
    i++;}
}

int main(){

  int *v, *m;
  cudaMalloc(&v, W*sizeof(int));
  cudaMalloc(&m, W*H*sizeof(int));
  int *h_v = (int *)malloc(W*sizeof(int));
  for (int i = 0; i < W; i++)
    h_v[i] = i;
  cudaMemcpy(v, h_v, W*sizeof(int), cudaMemcpyHostToDevice);

  // test row-major

  cudaEvent_t start, stop;
  cudaEventCreate(&start);
  cudaEventCreate(&stop);
  cudaEventRecord(start);
  expand_kernel<<<44, W>>>(v, W, m, H);
  cudaEventRecord(stop);
  float et;
  cudaEventSynchronize(stop);
  cudaEventElapsedTime(&et, start, stop);
  printf("row-majortime: %fms, bandwidth: %.0fMB/s\n", et, W*H*sizeof(int)/(1024*et));
  check_kernel<<<1,1>>>(v, W, m, H);
  cudaDeviceSynchronize();
  // test col-major

  cudaEventRecord(start);
  expand_kernel<<<W, 256>>>(v, W, m, H, 1);
  cudaEventRecord(stop);
  cudaEventSynchronize(stop);
  cudaEventElapsedTime(&et, start, stop);
  printf("col-majortime: %fms, bandwidth: %.0fMB/s\n", et, W*H*sizeof(int)/(1024*et));
  check_kernel<<<1,1>>>(v, W, m, H, 1);
  cudaDeviceSynchronize();
  return 0;
}

$ nvcc -arch=sm_20 -o t546 t546.cu
$ ./t546
row-majortime: 13.066944ms, bandwidth: 80246MB/s
col-majortime: 12.806720ms, bandwidth: 81877MB/s
$ /usr/local/cuda/samples/bin/x86_64/linux/release/bandwidthTest
[CUDA Bandwidth Test] - Starting...
Running on...

 Device 0: Quadro 5000
 Quick Mode

 Host to Device Bandwidth, 1 Device(s)
 PINNED Memory Transfers
   Transfer Size (Bytes)        Bandwidth(MB/s)
   33554432                     5864.2

 Device to Host Bandwidth, 1 Device(s)
 PINNED Memory Transfers
   Transfer Size (Bytes)        Bandwidth(MB/s)
   33554432                     6333.1

 Device to Device Bandwidth, 1 Device(s)
 PINNED Memory Transfers
   Transfer Size (Bytes)        Bandwidth(MB/s)
   33554432                     88178.6

Result = PASS
$

CUDA 6.5,RHEL 5.5

这也可以使用CUBLAS Rank-1 update function来实现,但它会比上述方法慢得多。