簡體   English   中英

用於矩陣批量乘法的Cuda程序

[英]Cuda program for matrix batch multiplication

我是CUDA程序領域的新手,我正在嘗試重復cublasSgemmBatched的功能,這意味着我要執行一批矩陣的矩陣乘法。 我嘗試將我的想法實現為以下代碼。

#include <stdio.h>

__global__ void BatchMulCUDA(float* array1, float* array2, int narray1, int dim, float* result)
{
    int tx = blockIdx.x * blockDim.x + threadIdx.x;

    if (tx < narray1 * dim)
    {
        float temp = 0;
        int index = tx / dim;
#pragma

        for (int i = 0; i < dim; i++)
        {
            temp += array1[tx * dim + i] * array2[index * dim + i];
        }

        result[tx] = temp;
    }
} 

void BatchMulGPU(float* array1, float* array2, int narray1, int dim, float* result)
{
    dim3 threads(1024, 1);
    dim3 grid(narray1 / 1024 + 1, 1);
    int threadsPerBlock = threads.x * threads.y;
    int blocksPerGrid = grid.x * grid.y;
    printf("CUDA kernel launch with %d blocks of %d threads\n", blocksPerGrid, threadsPerBlock);
    BatchMulCUDA<<<grid, threads>>>(array1, array2, narray1, dim, result);
}

但是,奇怪的是,我發現在索引19730之前可以獲得正確的輸出。在19730的元素之后,GPU的輸出始終為0。我不知道問題出在哪里。 我的代碼和測試功能的CPU版本如下。 我沒有意識到任何硬件限制嗎?

#include "kernel.h"

#include <cuda_runtime.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <sys/time.h>
#include <math.h>

double cpuSecond()
{
    struct timeval tp;
    gettimeofday(&tp, NULL);
    return ((double) tp.tv_sec + (double)tp.tv_usec*1e-6);
}

void BatchMulCPU(float* array1, float* array2, int narray1, int dim, float* result)
{
    for (int i = 0; i < narray1 * dim; i++)
    {
        float temp = 0;
        int index = i / dim;
        for (int j = 0; j < dim; j++)
        {
            temp += array1[i * dim + j] * array2[index * dim + j];
        }
        result[i] = temp;
    }
}

int main(int argc, char** argv)
{
    int narray1 = 6980;
    int dim = 4;

    float* array1 = new float[narray1 * dim * dim];
    float* array2 = new float[narray1 * dim];
    float* resultGPU = new float[narray1 * dim];
    float* resultCPU = new float[narray1 * dim];

    float* d_array1;
    float* d_array2;
    float* d_result;

    for (int i = 0; i < narray1 * dim * dim; i++)
    {
        array1[i] = static_cast<float> (rand() / (static_cast<float> (RAND_MAX / 10)));
    }

    for (int i = 0; i < narray1 * dim; i++)
    {
        array2[i] = static_cast<float> (rand() / (static_cast<float> (RAND_MAX / 10)));
    }

    cudaError_t err;

    double iStart = cpuSecond();
    err = cudaMalloc((void**)&d_array1, narray1 * dim * dim * sizeof(float));
    err = cudaMalloc((void**)&d_array2, narray1 * dim * sizeof(float));
    err = cudaMalloc((void**)&d_result, narray1 * dim * sizeof(float));

    err = cudaMemcpy(d_array1, array1, narray1 * dim * dim * sizeof(float), cudaMemcpyHostToDevice);
    err = cudaMemcpy(d_array2, array2, narray1 * dim * sizeof(float), cudaMemcpyHostToDevice);

    BatchMulGPU(d_array1, d_array2, narray1, dim, d_result);

    err = cudaMemcpy(resultGPU, d_result, narray1 * dim * sizeof(float), cudaMemcpyDeviceToHost);

    double iElaps = cpuSecond() - iStart;

    printf("Total GPU computation time is %lf \n" , iElaps);

    iStart = cpuSecond();
    BatchMulCPU(array1, array2, narray1, dim, resultCPU);
    iElaps = cpuSecond() - iStart;

    printf("Total CPU computation time is %lf \n" , iElaps);

    float error = 0;
    float temp = 0;
    for (long i = 0; i < narray1 * dim; i++)
    {
        // temp = abs(resultCPU[i] - resultGPU[i]);
        // if (temp > 0.5)
        // {
        //  std::cout << i << std::endl;
        // }
        error += abs(resultCPU[i] - resultGPU[i]);

    }

    printf("Error is %f \n", error);

    // for (int i = 19730; i < 19750; i++)
    // {
    //  std::cout << "GPU " << resultGPU[i] << std::endl;
    //  std::cout << "CPU " << resultCPU[i] << std::endl;
    // }

    cudaFree(d_array1);
    cudaFree(d_array2);
    cudaFree(d_result);

    return 0;
}

除了注釋中討論的WDDM TDR超時的可能性外,該代碼還有錯誤。

很明顯,內核設計期望啟動的網格總數(線程總數)等於或大於數組數乘以側面尺寸:

int tx = blockIdx.x * blockDim.x + threadIdx.x;

if (tx < narray1 * dim)

narray1*dim是所需的線程數

但是,正在啟動的數字僅為narray1

dim3 threads(1024, 1);
dim3 grid(narray1 / 1024 + 1, 1);

如果我們將上面的最后一行更改為:

dim3 grid((narray1*dim) / 1024 + 1, 1);

此代碼設計錯誤將得到解決。

該代碼對於少量矩陣(最多256個)正確工作的原因是由於網格中的舍入效果將最小narray1 1024個線程,即256 * 4( narray1 * dim )。

cublasSgemmBatched從我所看到的來看,這段代碼在功能上與cublasSgemmBatched並不相似。 我不認為此代碼是我熟悉的任何矩陣乘法(矩陣點積)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM