2016-11-09 117 views
1

的结构我新,并通过CUDA工具包文件去。在那里我发现了一个矩阵乘法使用共享内存的例子。这里,当从主机存储器复制矩阵结构到设备存储器时,只有数据元素被复制。我不明白的是其他变量如何复制到设备内存。复制到CUDA到设备内存CUDA

矩阵结构如下

typedef struct { 
    int width; 
    int height; 
    int stride; 
    float* elements; 
} Matrix; 

然后这里是代码示例,其中数据传输发生

void MatMul(const Matrix A, const Matrix B, Matrix C) 
{ 
    // Load A and B to device memory 
    Matrix d_A; 
    d_A.width = d_A.stride = A.width; d_A.height = A.height; 
    size_t size = A.width * A.height * sizeof(float); 
    cudaMalloc(&d_A.elements, size); 
    cudaMemcpy(d_A.elements, A.elements, size, 
       cudaMemcpyHostToDevice); 
    Matrix d_B; 
    d_B.width = d_B.stride = B.width; d_B.height = B.height; 
    size = B.width * B.height * sizeof(float); 
    cudaMalloc(&d_B.elements, size); 
    cudaMemcpy(d_B.elements, B.elements, size, 
    cudaMemcpyHostToDevice); 

    // Allocate C in device memory 
    Matrix d_C; 
    d_C.width = d_C.stride = C.width; d_C.height = C.height; 
    size = C.width * C.height * sizeof(float); 
    cudaMalloc(&d_C.elements, size); 

    // Invoke kernel 
    dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE); 
    dim3 dimGrid(B.width/dimBlock.x, A.height/dimBlock.y); 
    MatMulKernel<<<dimGrid, dimBlock>>>(d_A, d_B, d_C); 

    // Read C from device memory 
    cudaMemcpy(C.elements, d_C.elements, size, 
       cudaMemcpyDeviceToHost); 

    // Free device memory 
    cudaFree(d_A.elements); 
    cudaFree(d_B.elements); 
    cudaFree(d_C.elements); 
} 

这里是什么我不明白是怎么宽度,步幅和高度将被复制到设备内存。因为这里的cudaMalloc和cudaMemcpy仅适用于元素。在理解这件事时我有没有想过?

的籽粒代码

__device__ float GetElement(const Matrix A, int row, int col) 
{ 
    return A.elements[row * A.stride + col]; 
} 

// Set a matrix element 
__device__ void SetElement(Matrix A, int row, int col, 
          float value) 
{ 
    A.elements[row * A.stride + col] = value; 
} 

// Get the BLOCK_SIZExBLOCK_SIZE sub-matrix Asub of A that is 
// located col sub-matrices to the right and row sub-matrices down 
// from the upper-left corner of A 
__device__ Matrix GetSubMatrix(Matrix A, int row, int col) 
{ 
    Matrix Asub; 
    Asub.width = BLOCK_SIZE; 
    Asub.height = BLOCK_SIZE; 
    Asub.stride = A.stride; 
    Asub.elements = &A.elements[A.stride * BLOCK_SIZE * row 
             + BLOCK_SIZE * col]; 
    return Asub; 
} 

矩阵乘法籽粒代码

__global__ void MatMulKernel(Matrix A, Matrix B, Matrix C) 
{ 
    // Block row and column 
    int blockRow = blockIdx.y; 
    int blockCol = blockIdx.x; 

    // Each thread block computes one sub-matrix Csub of C 
    Matrix Csub = GetSubMatrix(C, blockRow, blockCol); 

    // Each thread computes one element of Csub 
    // by accumulating results into Cvalue 
    float Cvalue = 0; 

    // Thread row and column within Csub 
    int row = threadIdx.y; 
    int col = threadIdx.x; 

    // Loop over all the sub-matrices of A and B that are 
    // required to compute Csub 
    // Multiply each pair of sub-matrices together 
    // and accumulate the results 
    for (int m = 0; m < (A.width/BLOCK_SIZE); ++m) { 

     // Get sub-matrix Asub of A 
     Matrix Asub = GetSubMatrix(A, blockRow, m); 

     // Get sub-matrix Bsub of B 
     Matrix Bsub = GetSubMatrix(B, m, blockCol); 

     // Shared memory used to store Asub and Bsub respectively 
     __shared__ float As[BLOCK_SIZE][BLOCK_SIZE]; 
     __shared__ float Bs[BLOCK_SIZE][BLOCK_SIZE]; 

     // Load Asub and Bsub from device memory to shared memory 
     // Each thread loads one element of each sub-matrix 
     As[row][col] = GetElement(Asub, row, col); 
     Bs[row][col] = GetElement(Bsub, row, col); 

     // Synchronize to make sure the sub-matrices are loaded 
     // before starting the computation 
     __syncthreads(); 
     // Multiply Asub and Bsub together 
     for (int e = 0; e < BLOCK_SIZE; ++e) 
      Cvalue += As[row][e] * Bs[e][col]; 

     // Synchronize to make sure that the preceding 
     // computation is done before loading two new 
     // sub-matrices of A and B in the next iteration 
     __syncthreads(); 
    } 

    // Write Csub to device memory 
    // Each thread writes one element 
    SetElement(Csub, row, col, Cvalue); 
} 
+0

请添加内核代码。我想,没有必要明确地拷贝'width','height'和'stride',因为它们可能是由内核根据网格和块大小计算出来的。 – Sergey

+0

这里在MatMulKernal中已经使用了A.width变量,它从输入矩阵参数中得到了内核。但是在函数matmul中没有对该变量进行内存复制。有一个名为d_A的矩阵被创建,其宽度变量的设置与我们在普通C代码中的一样。 –

回答

2

对于那些谁不知道,我们谈论的示例代码是在这里NVIDIA的CUDA Toolkit文档,共享内存主题: CUDA C Programming guide : Shared memory

那么,为什么这样的工作? 是,仅“元素”阵列上设备侧利用cudaMalloc和cudaMemcpy功能发送。 是,矩阵尺寸上装置侧的内核内使用而不被显式地复制到与cudaMemcpy设备存储器中。

您需要以同样的方式不考虑数组和参数。让我解释如何将这些值发送给内核。

  1. 我们声明上CPU侧的矩阵,所有的成员都初始化
  2. 我们指定的尺寸,指针仍然是未初始化
  3. 我们分配和对设备侧复制存储器与API函数,该指针被初始化但它瞄准设备内存,不能像普通主机阵列一样使用
  4. 我们将矩阵作为内核的参数。不是通过指针,而是通过价值。

这就是诀窍。完整的结构实例被给定为一个参数,它包含:

  1. 三个整数,所述矩阵
  2. 的指针数组,包含矩阵数据

给予整数作为的尺寸内核启动中的参数显然是可能的,并且工作正常。 给指针指向一个数组也是可能的:指针被复制,这意味着我们创建另一个指向内存中同一个区域的指针。如果我们的目标数组在主机内存上,它会导致错误,但由于它已经在设备端用API函数初始化,所以它工作正常。

+0

非常感谢你的解释。 –

+0

不客气,很高兴我可以帮助:) – Taro