Is there any way to dynamically allocate constant memory? CUDA

不羁的心 提交于 2021-02-19 08:05:23

问题


I'm confused about copying arrays to constant memory.

According to programming guide there's at least one way to allocate constant memory and use it in order to store an array of values. And this is called static memory allocation:

__constant__ float constData[256];
float data[256];
cudaMemcpyToSymbol(constData, data, sizeof(data));
cudaMemcpyFromSymbol(data, constData, sizeof(data));

According to programming guide again we can use:

__device__ float* devPointer;
float* ptr;
cudaMalloc(&ptr, 256 * sizeof(float));
cudaMemcpyToSymbol(devPointer, &ptr, sizeof(ptr));

It looks like dynamic constant memory allocation is used, but I'm not sure about it. And also no qualifier __constant__ is used here.

So here are some questions:

  1. Is this pointer stored in constant memory?
  2. Is assigned (by this pointer) memory stored in constant memory too?
  3. Is this pointer constant? And it's not allowed to change that pointer using device or host function. But is changing values of array prohibited or not? If changing values of array is allowed, then does it mean that constant memory is not used to store this values?

回答1:


The developer can declare up to 64K of constant memory at file scope. In SM 1.0, the constant memory used by the toolchain (e.g. to hold compile-time constants) was separate and distinct from the constant memory available to developers, and I don't think this has changed since. The driver dynamically manages switching between different views of constant memory as it launches kernels that reside in different compilation units. Although you cannot allocate constant memory dynamically, this pattern suffices because the 64K limit is not system-wide, it only applies to compilation units.

Use the first pattern cited in your question: statically declare the constant data and update it with cudaMemcpyToSymbol before launching kernels that reference it. In the second pattern, only reads of the pointer itself will go through constant memory. Reads using the pointer will be serviced by the normal L1/L2 cache hierarchy.



来源:https://stackoverflow.com/questions/28635635/is-there-any-way-to-dynamically-allocate-constant-memory-cuda

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!