CUDA中常量内存的动态分配

时间:2022-07-21 17:02:50

I'm trying to take advantage of the constant memory, but I'm having a hard time figuring out how to nest arrays. What I have is an array of data that has counts for internal data but those are different for each entry. So based around the following simplified code I have two problems. First I don't know how to allocate the data pointed to by the members of my data structure. Second, since I can't use cudaGetSymbolAddress for constant memory I'm not sure if I can just pass the global pointer (which you cannot do with plain __device__ memory).

我正在尝试利用常量内存,但我很难搞清楚如何嵌套数组。我所拥有的是一系列数据,这些数据包含内部数据,但每个条目的数据都不同。所以基于以下简化代码我有两个问题。首先,我不知道如何分配我的数据结构成员指向的数据。其次,因为我不能使用cudaGetSymbolAddress来获取常量内存,所以我不确定我是否可以只传递全局指针(你无法使用普通的__device__内存)。


struct __align(16)__ data{
int nFiles;
int nNames;
int* files;
int* names;
};

__device__ __constant__ data *mydata;

__host__ void initMemory(...)
{
    cudaMalloc( (void **) &(mydata), sizeof(data)*dynamicsize );
    for(int i=; i lessthan dynamicsize; i++)
    {
        cudaMemcpyToSymbol(mydata, &(nFiles[i]), sizeof(int), sizeof(data)*i, cudaMemcpyHostToDevice);
        //...
        //Problem 1: Allocate & Set mydata[i].files
    }
}

__global__ void myKernel(data *constDataPtr)
{
    //Problem 2: Access constDataPtr[n].files, etc
}

int main()
{
    //...
    myKernel grid, threads (mydata);
}

Thanks for any help offered. :-)

感谢您提供的任何帮助。 :-)

4 个解决方案

#1


2  

These two threads should help you:

这两个线程应该可以帮助你:

http://forums.nvidia.com/index.php?showtopic=30269&hl=embedded

#2


2  

I think constant memory is 64K and you cannot allocate it dynamically using CudaMalloc. It has to be declared constant, say,

我认为常量内存是64K,你不能使用CudaMalloc动态分配它。它必须被宣布为常数,比方说,

__device__ __constant__ data mydata[100];

Similarly you also don't need to free it. Also, you shouldn't pass the reference to it via pointer, just access it as a global variable. I tried doing a similar thing and it gave me segfault (in devicemu).

同样,您也不需要释放它。此外,您不应通过指针传递对它的引用,只需将其作为全局变量访问。我尝试做类似的事情,它给了我segfault(在devicemu)。

#3


1  

No, you cant do that.

不,你不能这样做。

Constant memory (64KB max) can only be hard-coded before compilation.

常量内存(最大64KB)只能在编译之前进行硬编码。

However you can assign texture memory on the fly which is also cached on the Device.

但是,您可以动态分配纹理内存,它也会缓存在设备上。

#4


0  

Why don't you just use the so-called "packed" data representation? This approach allows you to place all the data you need into one-dimension byte array. E.g., if you need to store

你为什么不只使用所谓的“打包”数据表示?此方法允许您将所需的所有数据放入一维字节数组中。例如,如果你需要存储

struct data
{
    int nFiles;
    int nNames;
    int* files;
    int* names;
}

You can just store this data in the array this way:

您可以通过以下方式将此数据存储在数组中:

[struct data (7*4=28 bytes)
    [int nFiles=3 (4 bytes)]
    [int nNames=2 (4 bytes)]
    [file0 (4 bytes)]
    [file1 (4 bytes)]
    [file2 (4 bytes)]
    [name0 (4 bytes)]
    [name1 (4 bytes)]
]

#1


2  

These two threads should help you:

这两个线程应该可以帮助你:

http://forums.nvidia.com/index.php?showtopic=30269&hl=embedded

#2


2  

I think constant memory is 64K and you cannot allocate it dynamically using CudaMalloc. It has to be declared constant, say,

我认为常量内存是64K,你不能使用CudaMalloc动态分配它。它必须被宣布为常数,比方说,

__device__ __constant__ data mydata[100];

Similarly you also don't need to free it. Also, you shouldn't pass the reference to it via pointer, just access it as a global variable. I tried doing a similar thing and it gave me segfault (in devicemu).

同样,您也不需要释放它。此外,您不应通过指针传递对它的引用,只需将其作为全局变量访问。我尝试做类似的事情,它给了我segfault(在devicemu)。

#3


1  

No, you cant do that.

不,你不能这样做。

Constant memory (64KB max) can only be hard-coded before compilation.

常量内存(最大64KB)只能在编译之前进行硬编码。

However you can assign texture memory on the fly which is also cached on the Device.

但是,您可以动态分配纹理内存,它也会缓存在设备上。

#4


0  

Why don't you just use the so-called "packed" data representation? This approach allows you to place all the data you need into one-dimension byte array. E.g., if you need to store

你为什么不只使用所谓的“打包”数据表示?此方法允许您将所需的所有数据放入一维字节数组中。例如,如果你需要存储

struct data
{
    int nFiles;
    int nNames;
    int* files;
    int* names;
}

You can just store this data in the array this way:

您可以通过以下方式将此数据存储在数组中:

[struct data (7*4=28 bytes)
    [int nFiles=3 (4 bytes)]
    [int nNames=2 (4 bytes)]
    [file0 (4 bytes)]
    [file1 (4 bytes)]
    [file2 (4 bytes)]
    [name0 (4 bytes)]
    [name1 (4 bytes)]
]