开发者

allocate memory with cudaMalloc

开发者 https://www.devze.com 2023-04-11 04:17 出处:网络
If I declare a variable: define SIZE = 900; // width and height of an image float ** abc; So if I want to allocate the memory for it on GPU, should I allocate it like this?

If I declare a variable:

define SIZE = 900; // width and height of an image

float ** abc;

So if I want to allocate the memory for it on GPU, should I allocate it like this?

cudaMalloc(&abc, SIZE * SIZE开发者_Python百科 * sizeof(float));

Because I got a warning: integer operation result is out of range.

If I declare it like this:

cudaMalloc(&abc, SIZE * sizeof(float));

Then it it fine, I dont know whether with array 2 dimension, what should I allocate?

Thanks in advance.


Does this code give you the same warning?

  const size_t SIZE = 900;
  float *abc;
  cudaMalloc((void **)&abc, SIZE * SIZE * sizeof(float));

Try exactly this code, not something like it... if this isn't working, my guess is that there is a serious problem that's not your fault.

Why would you declare abc to be a doubly indirect pointer to float? malloc() and cudaMalloc() only allocate contiguous chunks of memory... if you want to interpret abc as a two-dimensional array, you'll have to work out the logic for doing so (translate to/from two-dimensional and linear indices) on your own. What you're asking nvcc to do is to allocate 3,240,000 bytes of memory for a float*, which should only need 4 bytes to store.


cudaMalloc does not allocate 2-dimensional array, you can translate 1-dimensional array to a 2-dimensional one, or you have to first allocate a 1-dimensional pointer array for float **abc, then allocate float array for each pointer in **abc, like this:

float ***abc;
float ***h_abc = malloc(SIZE * sizeof(float*));
cudaMalloc(&abc,SIZE * sizeof(float*));
for(int i = 0 ; i < SIZE ; i++ ){
     cudaMalloc(&(h_abc[i]), SIZE * sizeof(float)):
}
cudaMemcpy(&abc,h_abc,SIZE * sizeof(float*));
0

精彩评论

暂无评论...
验证码 换一张
取 消