gpt4 book ai didi

c - cudaMalloc在哪个内存空间分配内存?

转载 作者:太空宇宙 更新时间:2023-11-04 07:45:23 25 4
gpt4 key购买 nike

如果我理解正确的话,CUDA 设备有一些不同的 memory spaces . (例如注册、本地、共享、全局等)。调用cudaMalloc()时,分配的内存在哪个内存空间?

例如:

__global__ mykernel (void *p) {
/* What memory space does p point to? */
printf("p: %p\n", p);
}

int main() {
void *p;
assert(cudaMalloc (&p, 1024) == CUDA_SUCCESS);
mykernel<<<1,1024>>> (p);
}

documentation没有提到在什么级别分配内存。它只是说

Allocates size bytes of linear memory on the device and returns a pointer to the allocated memory. The allocated memory is suitably aligned for any kind of variable. The memory is not cleared.

似乎内存必须驻留在全局/常量/纹理空间之一,但哪个?

假设内存永远不会在本地/寄存器/共享内存空间中是否也安全?

最佳答案

全局

cudaMalloc 在全局内存中分配。全局内存分配的另一种方法是在内核中使用 newdelete

__global__ void myKernel(int N)
{
int* a = new int[N]; // not recommended
delete [] a;
}

分享

对于动态共享内存,你可以使用类似的东西

extern __shared__ int s[];

然后像这样启动内核

myKernel<<<1,n,n*sizeof(int)>>();

或者只是__shared__ int s[4];(内核内部)用于静态共享内存


注册

对于寄存器,你可以想到C++中的自动分配(仅从语法的角度来看):

int example = 0;
int moreExample[4]

主要区别在于,如果您用完了寄存器内存,您将发生寄存器溢出,并且变量可能最终进入全局内存而不是寄存器。

关于c - cudaMalloc在哪个内存空间分配内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57597831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com