-1

I have an array of pointers unsigned char *dev_src[2];. Actually those pointers have to point the memory allocated by cudaMalloc().

I am able to get the address of allocated memory if I do it inside the main() using the following code:

//Inside the same function
int memsize = 100;

int main()
{
    unsigned char *dev_src[2];

    //Allocate memomry 
    for (int i = 0; i < numCudaStreams; i++)
    {
        cudaMalloc((void**)&dev_src[i], memsize);
    }

    return 0;
}

PROBLEM: I want to get the addresses of allocated memories inside an another function for which I need to pass this array of pointer to that function. I already know how to pass an array to an another function and make changes inside it but in this case, I am not able to get the correct way.

//Inside another function
int memsize = 100;

void memallocation(unsigned char ????)
{
    //Allocate memomry 
    for (int i = 0; i < numCudaStreams; i++)
    {
        cudaMalloc((void**)??????????, memsize);
    }

}

int main()
{
    unsigned char *dev_src[2];  

    //Initialization of pointers before passing them to a function
    for (int i = 0; i < 2; i++)
        *dev_src[i] = NULL;

    memallocation(&dev_src);

    return 0;
}

UPDATE: It is not a duplicate of this question. I have already mentioned that I know how to pass an array by reference to change its elements but in this case the element itself have to be the pointers assigned by cudaMalloc().

Community
  • 1
  • 1
skm
  • 5,015
  • 8
  • 43
  • 104
  • @m.s. : It is not the duplicate of that questions. I have already mentioned that I know, how to change the values inside an array in an another function by passing it by reference. But in this case, the question is different. The normal method of passing an array by reference is not working here. – skm Aug 06 '15 at 10:47
  • 1
    You need to pass the address of that array then, simply casting doesn't work. – πάντα ῥεῖ Aug 06 '15 at 10:58
  • @ πάντα ῥεῖ: That's exactly what i want to do but I am not able to find the proper way for that. – skm Aug 06 '15 at 11:01
  • 1
    This is a perfect duplicate. You have an array of pointers. And you want to pass that array of pointers by reference. (When I say "by reference", this may mean strictly via C++ `&`-reference -- or perhaps using C pointers-to-arrays instead.) Anyway, if the other solutions aren't working, @skm, then you must show us the code you're trying and which isn't working. And show us the error message. It is *not* good enough to just say "The normal method of passing an array by reference is not working here." – Aaron McDaid Aug 06 '15 at 11:25
  • ... Although, to be fair to @skm, I think the top answer at the other question could be improved somewhat - it's not as clear, nor as general, as it could be – Aaron McDaid Aug 06 '15 at 11:26
  • 1
    @AaronMcDaid: I have already accepted the first answer. I did not use `(&arr)` which I still need to understand. The function call `memallocationA(dev_src);` in that answer could also work with the argument `*arr []` in definition so, I don't undertstand the difference between `*arr []` and `*(&arr)[N]` exactly. – skm Aug 06 '15 at 11:30

1 Answers1

2

You can do something like the following. I am passing the array as reference and to make it work for different array sizes I have made it a template. Replace the memory allocation line with your cudaMalloc.

template<int N>
void memallocationA(unsigned char *(&arr)[N])
{
    //Allocate memomry 
    for (int i = 0; i < N; i++)
    {
        //cudaMalloc((void**) &arr[i] , memsize);
        arr[i] = new unsigned char[10];
    }

}

int main()
{
    unsigned char *dev_src[2];

    //Initialization of pointers before passing them to a function
    for (int i = 0; i < 2; i++)
        dev_src[i] = NULL;

    memallocationA(dev_src);

    return 0;
}
Paani
  • 560
  • 3
  • 11
  • "Replace the memory allocation line with your cudaMalloc". Perhaps it would be simpler if you already did this in your answer? – Aaron McDaid Aug 06 '15 at 10:59
  • 2
    It is done, I don't have cuda library so the line is commented out. He just needs to uncomment the line :-) – Paani Aug 06 '15 at 11:01
  • @Paani: thanks, it worked but could you please explain about it. I don't understand why do we use `*(&arr)` in the argument of the function. I mean, what does it signifies. With `memallocationA(dev_src);` type of call, we could have used `unsigned char *arr []` also as an argument. What is the difference ? – skm Aug 06 '15 at 11:09
  • Refer to http://stackoverflow.com/questions/5724171/passing-an-array-by-reference . When you pass an array by reference the typical signature is `unsigned char (&arr)[2]`. In your case the array is of type unsigned char* so the * is associated with it making it `unsigned char *(&arr)[2]` – Paani Aug 06 '15 at 11:11