GPU上的动态分配内存 [英] Dynamic Allocating memory on GPU

查看:79
本文介绍了GPU上的动态分配内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在内核内部的GPU全局内存上动态分配内存?
我不知道我的答案有多大,因此我需要一种为答案的每个部分分配内存的方法.CUDA 4.0允许我们使用RAM ...这是个好主意还是会降低速度?

Is it possible to dynamically allocate memory on a GPU's Global memory inside the Kernel?
i don't know how big will my answer be, therefore i need a way to allocate memory for each part of the answer. CUDA 4.0 alloww us to use the RAM... is it a good idea or will it reduce the speed??

推荐答案

可以在内核中使用malloc.检查以下摘自nvidia cuda指南的内容:

it is possible to use malloc inside a kernel. check the following which is taken from nvidia cuda guide:

__global__ void mallocTest() 
{ 
  char* ptr = (char*)malloc(123); 
  printf("Thread %d got pointer: %p\n", threadIdx.x, ptr); 
  free(ptr); 
} 
void main() 
{ 
  cudaThreadSetLimit(cudaLimitMallocHeapSize, 128*1024*1024); 
  mallocTest<<<1, 5>>>(); 
  cudaThreadSynchronize(); 
} 

will output: 
Thread 0 got pointer: 00057020 
Thread 1 got pointer: 0005708c 
Thread 2 got pointer: 000570f8 
Thread 3 got pointer: 00057164 

这篇关于GPU上的动态分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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