简体   繁体   English

将cuda设备指针传递给主机函数

[英]passing cuda device pointer to host function

I have a program I'm working on. 我正在开发一个程序。 I'm new with CUDA and C,so it really has been a bumpy ride for me. 我是CUDA和C的新手,所以对我来说确实是一个坎bump的旅程。 I'm trying to copy a struct into the device. 我正在尝试将结构复制到设备中。 And then I'm trying to get the struct back to host by copying it to the device. 然后,我尝试通过将结构复制到设备来使结构返回到主机。 Below is the code : 下面是代码:

typedef struct {
    int row;
    int col;
    float *arr;
    int numElements;
} Matrix;

Matrix *RMatrix = //definition here

Matrix *d_RMatrix;

    copyMatrix(d_RMatrix, RMatrix, hostToDevice);

    Matrix *check = createMatrix(0, 0, NULL, 0);

    copyMatrix(check, d_RMatrix, deviceToHost);

and the definition of copyMatrix : copyMatrix的定义:

void copyMatrix (Matrix *copyTo, Matrix *copyFrom, Copy_type type) 
{

    if(type == hostToDevice) {

        // create temporary host matrix and array
        Matrix *copyFrom_h = createMatrix(copyFrom->row, copyFrom->col, NULL, copyFrom->numElements);

        // allocate device memory, pointing to array in host. Copy array to device memory
        cudaMalloc((void**) &copyFrom_h->arr, sizeof(float) * copyFrom_h->numElements);
        cudaMemcpy(copyFrom_h->arr, copyFrom->arr, sizeof(float) * copyFrom_h->numElements, cudaMemcpyHostToDevice);

        // copy the temporary memory to device
        cudaMalloc((void**) &copyTo, sizeof(Matrix));
        cudaMemcpy(copyTo, copyFrom_h, sizeof(Matrix), cudaMemcpyHostToDevice);

        copyFrom_h = NULL;
        free(copyFrom_h);

    }

    else if(type == deviceToHost) {

        cudaMemcpy(copyTo, copyFrom, sizeof(Matrix), cudaMemcpyDeviceToHost);

        // allocate space for array in the copy to matrix
        copyTo->arr = makeArray(copyTo->col, copyTo->row);
        cudaMemcpy(copyTo->arr, copyFrom->arr, sizeof(float) * copyTo->numElements, cudaMemcpyDeviceToHost);

    }
}

The error says invalid memory access at 0x3 (value of d_RMatrix) for the 1st call to cudaMemcpy and results in segfault on the 2nd. 该错误表明,第一次对cudaMemcpy的调用在0x3(d_RMatrix的值)处进行了无效的内存访问,并在第二次导致了segfault。

Is there anything I'm missing here? 我在这里想念什么吗? Thanks for your help :) 谢谢你的帮助 :)

In C a pointer is an entity pointing to an object (in this case). 在C语言中,指针是指向对象的实体(在这种情况下)。 Creating a pointer DOES NOT create the object nor allocate space for it. 创建指针不会创建对象,也不会为其分配空间。

You have created a pointer Matrix *d_RMatrix; 您已经创建了一个指针Matrix *d_RMatrix; but it doesn't point to any valid object. 但它没有指向任何有效的对象。 You got lucky it crashed, because by accident it could manage to actually copy the data into some random place in the memory. 您很幸运它崩溃了,因为它偶然可以设法将数据实际复制到内存中的某个随机位置。

Matrix TheMatrix();
Matrix *PointerToTheMatrix = &TheMatrix;

Or 要么

Matrix *PointerToTheMatrix = createMatrix(...);//remember you will have to delete it eventually!

Function parameters are one way. 功能参数是一种方法。 If you assign something to copyTo inside function, the change will not be visible outside the function. 如果您向函数内部的copyTo分配了某些内容,则该更改在函数外部将不可见。

/edit: I have an idea: / edit:我有个主意:

Matrix* CreateMatrixInDevice(Matrix* copyFrom)
{
    Matrix* copyTo = NULL;
    cudaMalloc((void**) &copyTo, sizeof(Matrix));//create outer struct
    cudaMemcpy(copyTo, copyFrom, sizeof(Matrix), cudaMemcpyHostToDevice);//copy data from outer struct
    //the arr element in the device is now INVALID (pointing to host)

    cudaMalloc((void**) &copyTo->arr, sizeof(float) * copyFrom->numElements);//create inner array
    cudaMemcpy(copyTo->arr, copyFrom->arr, sizeof(float) * copyFrom->numElements, cudaMemcpyHostToDevice);//copy matrix data

    return copyTo;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM