2012-07-16 11 views
7

私はCUDAプログラミングの新機能で、nvidiaが提供する「CUDA Cプログラミングガイド」を読んでいました。 (http://developer.download.nvidia.com/compute/DevZone/docs/html/C/doc/CUDA_C_Programming_Guide.pdf2つのビデオカードを使ったCUDA Cプログラミング

25ページには、マトリックス乗算を行う次のCコードがあります。このコードを2つのデバイスで実行するにはどうすればいいですか? (私のコンピュータに2台のnvida CUDA対応カードがインストールされている場合)。あなたは私の例を見せてくれますか?

// 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); 
    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); 
} 
+0

私は、NVIDIAがCUDA 3.0を搭載した複数のデバイスAPIをいくつか改良したことを知っています。 CUDAはGPUと対話するたびにスレッドのコンテキストでこれを行います。複数のGPUと対話するには、コード内で手動で手動で行う必要がありますが、手動で特定の数学演算を手動で分解する必要があります(おそらくそれほど難しいわけではないが、map/reduceのようなアプローチが必要なので、それほど簡単なものではない)。編集:それはあなたが探しているだけを指定する場合、あなたを助けるのが簡単になります。 – Svend

答えて

6

複数のGPUでCUDAカーネルを実行する「自動」な方法はありません。

行列の乗算の問題を並列に実行できる独立した演算に分解する方法を考案する必要があります(並列のGPUごとに1つ)。簡単な例として:

C = A.BB1B2が適切B|が列方向concantenationを表す行列の列を含むサイズの行列であるC = [A].[B1|B2] = [A.B1|A.B2]と等価です。別の行列乗算演算としてA.B1A.B2を計算し、結果の部分行列をホストメモリにコピーするときに連結を実行できます。

適切な分解スキームがあれば、CUDA 4.x APIの標準のマルチgpu機能を使用して分解スキームを実装します。 CUDA APIを使用したマルチGPUプログラミングの概要については、Paulius MicikeviciusのストリーミングビデオとPDF hereとして入手可能なGTC 2012の優れた講演をお勧めします。

+0

すべてのあなたの答えをありがとうございました。彼らは多くの助けをした。 –

2

基本は基本的に、あなたはあなたの現在のホストスレッドがcudaSetDevice()を呼び出すことによって、上の動作GPUた上で設定することができますCUDA C Programming Guide under section 3.2.6.

に記述されています。それでも、複数のGPUに分割されるようにルーチンを分解するために、独自のコードを記述する必要があります。