(Cuda)存儲器Memory(二)

本文地址:http://blog.csdn.net/mounty_fsc/article/details/51092925

本部分內容爲[1]CUDA_C_Programming_Guide中筆記

1 Device Memory

  • 這是對後邊的shared memory, global memory等的總稱
  • 可分爲linear memory和 CUDA arrays
  • CUDA arrays爲紋理獲取做了優化,見紋理存儲器

對於線性存儲器,一般用以下函數處理:

函數 描述
cudaMalloc()
cudaMemcpy()
cudaMallocPitch() 2D,返回的pitch需要在訪問時使用
cudaMemcpy2D() 2D
cudaMalloc3D() 3D
cudaMemcpy3D() 3D
cudaFree()

cudaMallocPitch例子

// Host code
int width = 64, height = 64;
float* devPtr;
size_t pitch;
cudaMallocPitch(&devPtr, &pitch,
width * sizeof(float), height);
MyKernel<<<100, 512>>>(devPtr, pitch, width, height);

// Device code
__global__ void MyKernel(float* devPtr,
size_t pitch, int width, int height)
{
    for (int r = 0; r < height; ++r) {
        float* row = (float*)((char*)devPtr + r * pitch);
        for (int c = 0; c < width; ++c) {
            float element = row[c];
        }
    }
}

2 shared Memory

2.1 不使用共享內存

這裏寫圖片描述

  1. 對於C中每個元素,使用一個線程去計算。
  2. 共訪問全局存儲器次數:對矩陣A中的每個元素,共B.width次,對矩陣B中的元素,共訪問了A.height次

2.2 使用共享內存

這裏寫圖片描述

  1. 本質上還是,使用一個線程去計算C中的一個元素。只是角度不一樣了,一個block計算一個Csub,一個block中的線程計算一個Csub中的元素。
  2. 策略是,對於同一個block中的線程,只讀取一次全局存儲器。
  3. 共訪問全局存儲器次數:對矩陣A中的每個元素,共(B.width/block_size)次,對矩陣B中的元素,共訪問了(A.height/block_size)次
  4. 共享存儲器示例代碼見最後一部分

3 Page-Locked Host Memory

  • 分頁鎖定主機存儲器(也叫pinned),區別爲malloc()分配的可分頁的主機存儲器(可分頁爲操作系統策略,將導致內存中只保存部分數據)
  • 分頁鎖定主機存儲器資源有限,比可分頁的要容易分配失敗。
  • 相關函數:

    函數 說明
    cudaHostAlloc()
    cudaFreeHost()
    cudaHostRegister() 分頁鎖定一段malloc()分配的內存
  • 類別

    中文 英文 說明 符號
    可分享存儲器 Portable Memory cudaHostAllocPortable,
    cudaHostRegisterPortable
    寫結合存儲器 Write-Combining Memory
    映射存儲器 Mapped Memory

4 Texture Memory

5 Surface Memory

相關代碼

  1. 共享存儲器示例代碼:
// Matrices are stored in row-major order:
// M(row, col) = *(M.elements + row * M.stride + col)
typedef struct {
    int width;
    int height;
    int stride; 
    float* elements;
} Matrix;

// Get a matrix element
__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;
}

// Thread block size
#define BLOCK_SIZE 16

// Forward declaration of the matrix multiplication kernel
__global__ void MatMulKernel(const Matrix, const Matrix, Matrix);

// Matrix multiplication - Host code
// Matrix dimensions are assumed to be multiples of BLOCK_SIZE
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);
    // 這裏因爲cuda中爲列優先,事實上對C作了個轉置
    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);
}

// Matrix multiplication kernel called by MatMul()
 __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);
}

[1]. CUDA_C_Programming_Guide

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章