-1

I have the following structure in the host

struct cluster {
    double *centroid;
    int num_elements;
    double *sum_elements;
 };

I want to call a CUDA kernel by dynamically allocating shared memory. The shared memory is an array of the above structure. The above structure has also two dynamic arrays that will be allocated dynamically within the kernel, but they are known before hand, let's call the size of the array - d. I tried to call the kernel using:

kernel<<<block, thread, 2*d*sizeof(double) + sizeof(int)>>>

but I found out that the memory size of the a structure is not equal to the sum of the memory size of its elements. My question is: How can I dynamically allocate the shared memory? Thank you.

  • Umm what make you think that `sizeof(double)` should be the same as `sizeof(*double)` ? This is as confusing as your previous (very similar) question. What are you really asking? Is it how to allocate shared memory and assign it to the two pointers within the structure? – talonmies Apr 26 '15 at 19:45
  • 2
    Surely you want to allocate `d * sizeof(cluster)` bytes of shared memory for an array of `d` structures? – talonmies Apr 26 '15 at 19:56
  • I would love to know what I am being thanked for. Could you perhaps write your own answer to this question and accept it (that is perfectly OK on [SO]), or edit the question so someone else could? Because the goal here is to answer every question, and I can't do that because I still don't know what it was that you were asking in the first place...... – talonmies Apr 28 '15 at 05:03

1 Answers1

1

Since @talomnies calls for an answer to be accepted... my answer is, the size you need to pass is d*sizeof(cluster)

I'd also suggest you look at this answer explaining struct sizes if you want to know more about them.

Community
  • 1
  • 1
Francesco Dondi
  • 1,064
  • 9
  • 17