1

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 * 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.

7
  • I would ike to help you, but your question is really unclear, please clean it up. Oct 4, 2011 at 13:25
  • I think it should be clear, no?
    – olidev
    Oct 4, 2011 at 13:32
  • 1
    No, you declare 'abc' and then use a 'GPU_input' and a 'SIZE' which we have no idea what they are. We can only try to guess. That error has nothing to do with CUDA anyway.
    – jmsu
    Oct 4, 2011 at 13:39
  • What is 'abc' and how does it correspond to the cudaMalloc call? what is in the SIZE variables? and who are you multiplying them? could it be that you are trying to malloc something in two dimensions? Oct 4, 2011 at 13:40
  • Hi, that is my mistake. I have updated it! I am sorry. thanks guys!
    – olidev
    Oct 4, 2011 at 14:46

2 Answers 2

7

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.

1
  • Furthermore, if you want to do a 2D allocation, you almost certainly want a "pitched" allocation (use cudaMallocPitch()), so the CUDA driver can pad the allocation in a way that is palatable to whatever hardware you are running on. Oct 5, 2011 at 13:54
1

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*));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.