CUDA c++,简单的矩阵乘法错误

CUDA c++, simple matrix multiplication error

本文关键字:错误 c++ 简单 CUDA      更新时间:2023-10-16

我对使用 c++ 进行 CUDA 编程很陌生,所以很抱歉这个简单的问题。我根本无法弄清楚我哪里出了问题。我正在尝试进行矩阵乘法。我从几个来源找到了灵感,所以可能是我混淆了一些不同的方法。我正在尝试将两个矩阵相乘h_a和h_b。我成功地生成了两个矩阵,但是当我为两个矩阵分配内存时,由于某种原因,我丢失了该矩阵中的值,即使在乘法之后,所有值都为零。下面是代码:

#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <ctime>
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;

__global__ void MulKernel(int *c, const int *a, const int *b, const int P)
{
    float tempsum;
    int row = blockIdx.y*blockDim.y + threadIdx.y;
    int col = blockIdx.x*blockDim.x + threadIdx.x;
    if (row < P && col < P){
        for (int i = 0; i < P; i++){
            tempsum += a[row*P + i] * b[i*P + col];
        }
    }
    c[row*P + col] = tempsum;
}

int main()
{
srand(time(NULL));
int *pointer;
int N = 16;
int SIZE = N*N;
int *h_a = new int[SIZE];
int *h_b = new int[SIZE];
int *h_c = new int[SIZE];
for (int i = 0; i < SIZE; i++) {
            h_a[i] = rand() % 1000;
            h_b[i] = rand() % 1000;
    } 
cout << "First values " << h_a[0] << " " << h_b[0] << endl;
    cudaMalloc(&h_a, sizeof(int)*SIZE);
    cudaMalloc(&h_b, sizeof(int)*SIZE);
    cudaMalloc(&h_c, sizeof(int)*SIZE);
    cudaMalloc(&pointer, sizeof(int));
    cout << "Second values " << h_a[0] << " " << h_b[0] << endl;
    cudaMemcpy(h_a, &h_a, sizeof(int), cudaMemcpyHostToDevice);
    cudaMemcpy(h_b, &h_b, sizeof(int), cudaMemcpyHostToDevice);
    cudaMemcpy(pointer, &N, sizeof(int), cudaMemcpyHostToDevice);
    cout << "Third values " << h_a[0] <<" "<< h_b[0] << endl;
    MulKernel <<<1, 256 >>>(h_c, h_a, h_b, N);
    cudaMemcpy(h_c, &h_c, sizeof(int), cudaMemcpyDeviceToHost);
    cudaMemcpy(h_a, &h_a, sizeof(int), cudaMemcpyDeviceToHost);
    cudaMemcpy(h_b, &h_b, sizeof(int), cudaMemcpyDeviceToHost);
    for (int i = 0; i < 5; i++){
        cout << h_c[i] << "=" << h_a[i] << h_b[i] << endl;
    }
    cout << h_c[1] << endl;
    cudaFree(h_a);
    cudaFree(h_b);
    cudaFree(h_c);
    return 0;
}

终端中的输出为:

First values 454 964
Second values 0 0
Third values 0 0
0=00
0=00
0=00
0=00
0=00
0
Press any key to continue . . .

我希望有人能发现错误

此致敬意

你的代码有很多问题。

    每当您在使用 cuda 代码
  1. 时遇到问题时,我建议您进行适当的 cuda 错误检查以及使用 cuda-memcheck 运行您的代码。 在这种情况下,您犯了编程错误,实际上会导致 seg 错误,因此这些方法并不那么有用。

  2. 您的内核大部分是可行的。 有3个问题。 首先,您正在执行int乘法,但已将tempsum变量声明为 float 。 这可能不是一个大问题,但与您的内核不一致。 其次,您没有初始化tempsum(它应该设置为零(。 第三,你有你的线程检查(即 if -语句(稍微放错了地方。 您应该对内核进行调节,以便在线程越界时写入c

  3. 您可能对主机和设备变量感到困惑。 我们不会使用 new 分配主机变量,然后对同一个指针执行cudaMalloc操作。 事情不是这样运作的。 我们需要创建一组等效的变量来在设备上存储数据。 让我们称这些*d_a等。我们将调用 cudaMalloc 来分配设备空间,然后在cudaMemcpy操作中使用这些变量作为设备端变量。

  4. 您的内核需要一个 2D 线程数组(以便内核中的 .x.y 内置变量有意义(。 但是您正在使用 1D 变量定义线程数组。 这需要在内核启动中修复(即使用dim3变量定义 2D 数组(。 同样,内核启动应指定作为设备指针的d_a等变量。

  5. 您可能会对将变量(如 N(传递给内核时如何处理该变量感到困惑。 我们可以直接(按值(传递它,而无需使用您创建的pointer进行任何体操。

  6. 您的cudaMemcpy操作中的传输大小错误。 与memcpy一样,您需要以字节为单位指定传输大小,因此我们需要将大部分传输大小乘以 SIZE .

下面是代码的修改版本,解决了上述问题:

$ cat t1073.cu
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <ctime>
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;

__global__ void MulKernel(int *c, const int *a, const int *b, const int P)
{
    int tempsum=0;
    int row = blockIdx.y*blockDim.y + threadIdx.y;
    int col = blockIdx.x*blockDim.x + threadIdx.x;
    if (row < P && col < P){
        for (int i = 0; i < P; i++){
            tempsum += a[row*P + i] * b[i*P + col];
        }
        c[row*P + col] = tempsum;
    }
}

int main()
{
    srand(time(NULL));
    int N = 16;
    int SIZE = N*N;
    int *h_a = new int[SIZE];
    int *h_b = new int[SIZE];
    int *h_c = new int[SIZE];
    for (int i = 0; i < SIZE; i++) {
            h_a[i] = rand() % 1000;
            h_b[i] = rand() % 1000;
    }
    cout << "First values " << h_a[0] << " " << h_b[0] << endl;
    int *d_a, *d_b, *d_c;
    cudaMalloc(&d_a, sizeof(int)*SIZE);
    cudaMalloc(&d_b, sizeof(int)*SIZE);
    cudaMalloc(&d_c, sizeof(int)*SIZE);
    cout << "Second values " << h_a[0] << " " << h_b[0] << endl;
    cudaMemcpy(d_a, h_a, sizeof(int)*SIZE, cudaMemcpyHostToDevice);
    cudaMemcpy(d_b, h_b, sizeof(int)*SIZE, cudaMemcpyHostToDevice);
    cout << "Third values " << h_a[0] <<" "<< h_b[0] << endl;
    MulKernel <<<1, dim3(N,N) >>>(d_c, d_a, d_b, N);
    cudaMemcpy(h_c, d_c, sizeof(int)*SIZE, cudaMemcpyDeviceToHost);
    cudaMemcpy(h_a, d_a, sizeof(int)*SIZE, cudaMemcpyDeviceToHost);
    cudaMemcpy(h_b, d_b, sizeof(int)*SIZE, cudaMemcpyDeviceToHost);
    for (int i = 0; i < 5; i++){
        cout << h_c[i] << "=" << h_a[i] << h_b[i] << endl;
    }
    cout << h_c[1] << endl;
    cudaFree(d_a);
    cudaFree(d_b);
    cudaFree(d_c);
    return 0;
}
$ nvcc -o t1073 t1073.cu
$ cuda-memcheck ./t1073
========= CUDA-MEMCHECK
First values 698 173
Second values 698 173
Third values 698 173
5502745=698173
5866060=120710
3945532=646669
4432346=582703
4971909=746272
5866060
========= ERROR SUMMARY: 0 errors
$

就个人而言,我无法轻松解释输出,我不确定您为什么选择=符号。 对于矩阵乘法,c[i] 不等于 a[i]*b[i],如果你是这样想的。 如果您想要一个易于直观理解的简单测试,请尝试将 a 和 b 矩阵设置为全部 1。 然后,您可以轻松找到正确的输出,它应该都是N.

另请注意,为简洁起见,我并没有试图在这个问题中教你 CUDA 编程的各个方面,只是修复了一些错误。 仅举一个例子,如果将N设置为大于 32 的值,则此代码将中断。 您可能需要了解有关 CUDA 编程的更多信息才能理解为什么会这样。