【问题标题】:CUDA: unified memory, using arraysCUDA:统一内存,使用数组
【发布时间】:2015-01-28 15:00:43
【问题描述】:

我正在尝试让统一内存与类一起工作,并通过内核调用在统一内存中传递和操作数组。我想通过引用传递所有内容。

所以我重写了类和数组的新方法,以便 GPU 可以访问它们,但我认为我需要添加更多代码才能在统一内存中拥有数组,但不太确定如何执行此操作。调用 fillArray() 方法时出现内存访问错误。

如果我必须进行数百次此类操作(数组的算术运算和不同大小的数组之间的复制),统一内存是一种好方法还是应该坚持在 cpu 和 gpu 内存之间手动复制?非常感谢!

#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#include <iostream>
#include <stdio.h>


#define TILE_WIDTH 4

#ifdef __CUDACC__
#define CUDA_CALLABLE_MEMBER __host__ __device__
#else
#define CUDA_CALLABLE_MEMBER
#endif

__global__ void add1(int height, int width, int *a, int *resultArray)
{
    int w = blockIdx.x * blockDim.x + threadIdx.x; // Col // width
    int h = blockIdx.y * blockDim.y + threadIdx.y;
    int index = h * width + w;

    if ((w < width) && (h < height))
        resultArray[index] = a[index] + 1;
}

class Managed 
{
public:
    void *operator new(size_t len) 
    {
        void *ptr;
        cudaMallocManaged(&ptr, len);
        return ptr;
    }

    void Managed::operator delete(void *ptr) 
    {
        cudaFree(ptr);
    }

    void* operator new[] (size_t len) {
        void *ptr; 
        cudaMallocManaged(&ptr, len);
        return ptr;
    }
        void Managed::operator delete[] (void* ptr) {
        cudaFree(ptr);
    }
};

class testArray : public Managed
{
public: 
    testArray()
    {
        height = 16;
        width = 8;
        myArray = new int[height*width];
    }
    ~testArray()
    {
        delete[] myArray;
    }

    CUDA_CALLABLE_MEMBER void runTest()
    {
        fillArray(myArray);
        printArray(myArray);

        dim3 dimGridWidth((width - 1) / TILE_WIDTH + 1, (height - 1)/TILE_WIDTH + 1, 1);
        dim3 dimBlock(TILE_WIDTH, TILE_WIDTH, 1);

        add1<<<dimGridWidth,dimBlock>>>(height, width, myArray, myArray);
        cudaDeviceSynchronize();
        printArray(myArray);
    }

private:

    int *myArray;
    int height; 
    int width;

    void fillArray(int *myArray)
    {
        for (int i = 0; i < height; i++){
            for (int j = 0; j < width; j++)
                myArray[i*width+j] = i*width+j;
        }
    }

    void printArray(int *myArray)
    {
        for (int i = 0; i < height; i++){
            for (int j = 0; j < width; j++)
                printf("%i ",myArray[i*width+j]);
            printf("\n");
        }
    }
};

int main()
{
    testArray *test = new testArray;
    test->runTest();

    //testArray test;
    //test.runTest();

    system("pause");
    return 0;
}

【问题讨论】:

    标签: c++ arrays class cuda operator-overloading


    【解决方案1】:

    你的错误很简单:myArray是在主机上分配的,不是统一内存。

    原因是,虽然 testArray 来自 Managed(因此您的 testArray *test = new testArray 分配统一内存),但在其构造函数内部完成的分配分配了一个 ints 数组,它们是不是源自Managed

    因此,您的指针位于统一内存中,但指向主机内存。

    就在我的脑海中,以下应该会有所帮助:

    struct UnifiedInt : int, Managed { /* implement a few convenience functions */ };
    

    【讨论】:

      【解决方案2】:

      这很简单,但我不知道该怎么做。无论如何,如果您更改这行代码:

      myArray = new int[height*width];
      

      以下,它看起来像它的作品。

      cudaMallocManaged(&myArray, height * width * sizeof(int));
      

      如果您有其他方法,我很想看看。

      【讨论】:

        猜你喜欢
        • 2015-03-26
        • 1970-01-01
        • 1970-01-01
        • 2020-06-01
        • 2016-12-18
        • 1970-01-01
        • 2017-04-30
        • 2014-04-19
        • 2018-11-13
        相关资源
        最近更新 更多