1

I'm making a C++ cuda program, in which (as far as I know):

cudaMallocManaged(&data, size);

and

cudaFree(&data);

is analogous to malloc/new and delete.

Say I have a class

struct vec{

    int size;
    int* data;

    vec(int size){
        cudaMallocManaged(&data, x * y * sizeof(int));
    }

};

and I overload the + operator:

vec operator+(vec b){

        vec c;
        for (i = 0; i < size; i++)
        {
            c.data[i] = a.data[i] + b.data[i];
        }
        return c;

    }

mat<T> operator+(int b){

        vec c;
        for (i = 0; i < size; i++)
        {
            c.data[i] = a.data[i] + b;
        }
        return c;

    }

now say I have some function:

vec f(vec x){
    return x + ​x + 1;

​}

Since this function creates 2 vectors, If this function is called repeatedly all the gpu memory is used up. The obvious solution is adding a deconstructor to the vec class which frees the memory when the object goes out of scope:

~vec(){
    cudaFree(data);
}

The problem that arises is that since a pointer is being deleted, and the returned vector stores its data in the same pointer, the returned data is also deleted and the program crashes with a 0xc0000006 error when the data is next referenced.

One thing I considered is something like this:

void add(vec a, vec b, vec out){

        vec c;
        for (i = 0; i < size; i++)
        {
            c.data[i] = a.data[i] + b.data[i];
        }
        copy(c to out); //I'm not 100% sure if memcpy works in cuda

}

However, that is not only inconvenient but also doesn't allow for operator overloading. Thanks in Advance for your help.

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
QWERTYL
  • 1,355
  • 1
  • 7
  • 11
  • 5
    search for the rule of three. – Jeffrey May 06 '21 at 19:25
  • 1
    What does this question have to do with the C language? – eerorika May 06 '21 at 19:27
  • 1
    If your attempt to add your `~vec()` function led to a crash, you have a bug somewhere else - not described in this question. If I were to guess, you omitted a copy constructor leading to multiple `vec`s sharing the same data pointer. (Again, rule of 3) – Drew Dormann May 06 '21 at 19:33
  • 1
    Yes, I was previously unaware of the rule of 3 and a copy constructor. Thank you all for your help. – QWERTYL May 06 '21 at 19:38

0 Answers0