为什么反转这个功能不工作 [英] Why does reverse this function not work

查看:115
本文介绍了为什么反转这个功能不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在构造函数中,我在设备侧填充数组。

In the constructor I fill the array on the device side.

但现在我想对数组执行反向函数。

but now I want to execute reverse function on the array.

 using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <iostream>


__global__ void generateVector(int *data,int count){
    int tid = blockIdx.x;
    data[tid] = -tid;
}

__global__ void reverseArray(int *data,int count){
    int tid = blockIdx.x;
    data[tid] = tid;
}

class FData{
private:
    int *data;
    int size;
public:
    FData(int sizeP){
        size = sizeP;
        data = new int[size];
        int *devA;

        cudaMalloc((void**) &devA, size * sizeof(int));
        generateVector<<<size,1>>>(devA,size);
        cudaMemcpy(data,devA, size * sizeof(int),cudaMemcpyDeviceToHost);

        cudaFree(devA);
    }

    ~FData(){
        delete [] data;
    }

    int getSize(){
        return size;
    }



    int elementAt(int i){
        return data[i];
    }

    void reverse(){
        int *devA;
        cudaMalloc((void**) &devA, sizeof(int));
        reverseArray<<<size,1>>>(devA,size);
        cudaMemcpy(data,devA,size * sizeof(int),cudaMemcpyDeviceToHost);
        cudaFree(devA);

    }


};


int main(void) {

    FData arr(30);

    cout << arr.elementAt(1);


    arr.reverse();
    cout << arr.elementAt(1);


    return 0;

}

它仍然打印我在构造函数中填写的值。这里有什么问题?我怎么能解决呢?

It still prints the values which I filled in the constructor. What is the problem here? How can i solve it? What is going wrong?

推荐答案

您的内核不会反转任何内容。他们只是否定价值观,所以如果你看到任何东西被扭转,我会很惊讶。说到这里,如果你添加错误检查你的代码(见这个其他SO post 如何最好地做错误检查)然后你会看到你的代码将失败调用 cudaMalloc reverse 函数中。你可以通过将 devA 更改为一个简单的指针来解决这个问题(你可以将它分配为主机数组,不会在主机上使用它。)

Your kernels aren't reversing anything. They're just negating the values, so if anything I would be quite surprised if you saw anything get reversed. With that said, if you add error checking to your code (see this other SO post on how best to do the error checking) then you'll see that your code will fail on the call to cudaMalloc in your reverse function. You can fix this by changing devA to be a plain pointer (it doesn't really make sense for you to be allocating it as a host-array anyways, as you're not using it on the host to begin with).

void reverse(){
    int *devA;
    cudaMalloc((void**) &devA, size * sizeof(int));       
    reverseArray<<<size,1>>>(devA,size);
    cudaMemcpy(data,devA,size * sizeof(int), cudaMemcpyDeviceToHost);
    cudaFree(devA);
}

此外,你也应该释放你的内存,你有主机端和设备端内存泄漏。每当你有一个 cudaMalloc 调用,你应该有一个相应的 cudaFree 。此外,考虑添加析构函数来释放主机端数据成员,因为您也有内存泄漏。

Also, you should free your memory too, you have both host-side and device-side memory leaks. Whenever you have a cudaMalloc call, you should havea corresponding cudaFree. Also, consider adding a destructor to free your host-side data member, as you have a memory leak there too.

~FData()
{
    delete [] data;
}

这篇关于为什么反转这个功能不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆