c++ - CUDA - cudaMallocPitch 和 cudaMemcpy2D 使用,错误 : InvalidValue, InvalidPitchValue

标签 c++ cuda

好吧,我正在尝试为 cuda 获取一个 2D 数组,但它变得很痛苦。错误在标题中并出现在 cudaMemcpy2D 中。我认为这个问题对于训练有素的眼睛来说是显而易见的。在此先感谢您的帮助,我已经领先于目前正在学习指针的类(class)。

#include <cuda_runtime.h>
#include <iostream>
#pragma comment (lib, "cudart")

/* Program purpose: pass a 10 x 10 matrix and multiply it by another 10x10 matrix */

float matrix1_host[100][100];
float matrix2_host[100][100];

float* matrix1_device;
float* matrix2_device;  
size_t pitch;
cudaError_t err;

__global__ void addMatrix(float* matrix1_device,float* matrix2_device, size_t pitch){
    // How this works
    // first we start to cycle through the rows by using the thread's ID
    // then we calculate an address from the address of a point in the row, by adding the pitch (size of each row) and  * it by
    // the amount of rows we've already completed, then we can use that address of somewhere at a start of a row to get the colums 
    // in the row with a normal array grab. 

    int r = threadIdx.x;

        float* rowofMat1 = (float*)((char*)matrix1_device + r * pitch);
        float* rowofMat2 = (float*)((char*)matrix2_device + r * pitch);
        for (int c = 0; c < 100; ++c) {
             rowofMat1[c] += rowofMat2[c];
        }

}

void initCuda(){
    err = cudaMallocPitch((void**)matrix1_device, &pitch, 100 * sizeof(float), 100);
    err = cudaMallocPitch((void**)matrix2_device, &pitch, 100 * sizeof(float), 100); 
    //err = cudaMemcpy(matrix1_device, matrix1_host, 100*100*sizeof(float), cudaMemcpyHostToDevice);
    //err = cudaMemcpy(matrix2_device, matrix2_host, 100*100*sizeof(float), cudaMemcpyHostToDevice);
    err = cudaMemcpy2D(matrix1_device, 100*sizeof(float), matrix1_host, pitch, 100*sizeof(float), 100, cudaMemcpyHostToDevice);
    err = cudaMemcpy2D(matrix2_device, 100*sizeof(float), matrix2_host, pitch, 100*sizeof(float), 100, cudaMemcpyHostToDevice);
}

void populateArrays(){
    for(int x = 0; x < 100; x++){
        for(int y = 0; y < 100; y++){
            matrix1_host[x][y] = (float) x + y;
            matrix2_host[y][x] = (float) x + y;
        }
    }
}

void runCuda(){
    dim3 dimBlock ( 100 );
    dim3 dimGrid ( 1 );
    addMatrix<<<dimGrid, dimBlock>>>(matrix1_device, matrix2_device, 100*sizeof(float)); 
    //err = cudaMemcpy(matrix1_host, matrix1_device, 100*100*sizeof(float), cudaMemcpyDeviceToHost);
    err = cudaMemcpy2D(matrix1_host, 100*sizeof(float), matrix1_device, pitch, 100*sizeof(float),100, cudaMemcpyDeviceToHost);
    //cudaMemcpy(matrix1_host, matrix1_device, 100*100*sizeof(float), cudaMemcpyDeviceToHost);
}

void cleanCuda(){
    err = cudaFree(matrix1_device);
    err = cudaFree(matrix2_device);

    err = cudaDeviceReset();
}


int main(){
    populateArrays();
    initCuda();
    runCuda();
    cleanCuda();
    std::cout << cudaGetErrorString(cudaGetLastError());
    system("pause");
    return 0;
}

最佳答案

首先,通常您应该为 matrix1 和 matrix2 设置一个单独的间距变量。在这种情况下,它们将与 API 调用返回的值相同 cudaMallocPitch , 但在一般情况下它们可能不是。

在您的 cudaMemcpy2D 行中,the second parameter to the call是目的地球场。这只是您为此特定目标矩阵(即第一个参数)执行 cudaMallocPitch 调用时返回的音调值。

第四个参数是音源音高。由于这是通过普通主机分配分配的,因此除了以字节为单位的宽度外,它没有间距。

因此您交换了第二个和第四个参数。

所以不是这个:

err = cudaMemcpy2D(matrix1_device, 100*sizeof(float), matrix1_host, pitch, 100*sizeof(float), 100, cudaMemcpyHostToDevice);

试试这个:

err = cudaMemcpy2D(matrix1_device, pitch, matrix1_host, 100*sizeof(float), 100*sizeof(float), 100, cudaMemcpyHostToDevice);

第二次调用 cudaMemcpy2D 时类似。第三次调用实际上没问题,因为它的方向相反,交换了源矩阵和目标矩阵,因此它们与您的音调参数正确对齐。

关于c++ - CUDA - cudaMallocPitch 和 cudaMemcpy2D 使用,错误 : InvalidValue, InvalidPitchValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15424738/

相关文章:

c++ - 估计函数参数数组的维度

c++ - 无法理解此 C++ 表达式的语法/结构

c - 费米架构上的 int2 与 int4 负载

cuda - Nsight Eclipse not found/CUDA11.1安装问题

c++ - 基于不同字段的对象 vector 的排序函数

c++ - 如何在屏幕上显示对象列表?

python - Pycuda - 如何添加-ccbin clang-3.8

linux - __ldg 在某些情况下会导致执行时间变慢

c++ - 如何将 std::vector<void*> * 作为 std::vector<MyStruct*>* 传递给函数

CMake:通过 NVCC 传递编译器标志列表