How to copy a tree to GPU memory allocation for tree on gpu

Hi ,i have a tree of nodes and i am trying to copy it to GPU memory. the Node looks like this:

struct Node

{

   char *Key;

   int ChildCount;

   Node *Children;

}

And my copy function looks like this:

void CopyTreeToDevice(Node* node_s, Node* node_d)

{

//allocate node on device and copy host node

     cudaMalloc( (void**)&node_d, sizeof(Node));

     cudaMemcpy(node_d, node_s, sizeof(Node), cudaMemcpyHostToDevice);

//test

     printf("ChildCount of node_s looks to be : %d\n", node_s->ChildCount);

     printf("Key of node_s looks to be : %s\n", node_s->Key);

Node *temp;

     temp =(Node *) malloc(sizeof(Node));

     cudaMemcpy(temp, node_d, sizeof(Node), cudaMemcpyDeviceToHost);

     printf("ChildCount of node_d on device is actually : %d\n", temp->ChildCount);

     printf("Key of node_d on device is actually : %s\n", temp->Key);

     free(temp);

//       continue with child nodes

     if(node_s->ChildCount > 0)

     {

         //problem here

         cudaMalloc( (void**)&(node_d->Children), sizeof(Node)*(node_s->ChildCount));

cudaMemcpy(node_d->Children, node_s->Children, 

                    sizeof(Node)*node_s->ChildCount, cudaMemcpyHostToDevice);

for(int i=0;i<node_s->ChildCount;i++)

         {

                 CopyTreeToDevice(&(node_s->Children[i]), &(node_d->Children[i]));

         }

     }

}

But i have a problem with the line :

cudaMalloc( (void**)&(node_d->Children), sizeof(Node)*(node_s->ChildCount));

Gives me access violation exception.Test section works smoothly.no problem at initializing fields.

Here is the output of test section :

ChildCount of node_s looks to be : 35

Key of node_s looks to be : root

ChildCount of node_d on device is actually : 35

Key of node_d on device is actually : root

What is the reason for this?

And is there more elegant way to represent trees on gpu?

Thanks.

Hi ,i have a tree of nodes and i am trying to copy it to GPU memory. the Node looks like this:

struct Node

{

   char *Key;

   int ChildCount;

   Node *Children;

}

And my copy function looks like this:

void CopyTreeToDevice(Node* node_s, Node* node_d)

{

//allocate node on device and copy host node

     cudaMalloc( (void**)&node_d, sizeof(Node));

     cudaMemcpy(node_d, node_s, sizeof(Node), cudaMemcpyHostToDevice);

//test

     printf("ChildCount of node_s looks to be : %d\n", node_s->ChildCount);

     printf("Key of node_s looks to be : %s\n", node_s->Key);

Node *temp;

     temp =(Node *) malloc(sizeof(Node));

     cudaMemcpy(temp, node_d, sizeof(Node), cudaMemcpyDeviceToHost);

     printf("ChildCount of node_d on device is actually : %d\n", temp->ChildCount);

     printf("Key of node_d on device is actually : %s\n", temp->Key);

     free(temp);

//       continue with child nodes

     if(node_s->ChildCount > 0)

     {

         //problem here

         cudaMalloc( (void**)&(node_d->Children), sizeof(Node)*(node_s->ChildCount));

cudaMemcpy(node_d->Children, node_s->Children, 

                    sizeof(Node)*node_s->ChildCount, cudaMemcpyHostToDevice);

for(int i=0;i<node_s->ChildCount;i++)

         {

                 CopyTreeToDevice(&(node_s->Children[i]), &(node_d->Children[i]));

         }

     }

}

But i have a problem with the line :

cudaMalloc( (void**)&(node_d->Children), sizeof(Node)*(node_s->ChildCount));

Gives me access violation exception.Test section works smoothly.no problem at initializing fields.

Here is the output of test section :

ChildCount of node_s looks to be : 35

Key of node_s looks to be : root

ChildCount of node_d on device is actually : 35

Key of node_d on device is actually : root

What is the reason for this?

And is there more elegant way to represent trees on gpu?

Thanks.

Hi,

I do not know if it’s possible (or better) but if you can change your struct by :

struct Node

{

   char *Key;

   int ChildCount;

   int *Children;

};

Where you have a global array of struct Node, and Children property Node refer to index in this array.

Otherwise if you can not change your struct Node, tried to copy the memory by a rising path and not descendant :

Copy children before copying the parent …

Hi,

I do not know if it’s possible (or better) but if you can change your struct by :

struct Node

{

   char *Key;

   int ChildCount;

   int *Children;

};

Where you have a global array of struct Node, and Children property Node refer to index in this array.

Otherwise if you can not change your struct Node, tried to copy the memory by a rising path and not descendant :

Copy children before copying the parent …

And the reason of your exception, it’s you try to access to device memory while you don’t have permission :

when you allocate memory on GPU device, you can manipulate the pointer (assignment, …) but can’t access its memory…

And the reason of your exception, it’s you try to access to device memory while you don’t have permission :

when you allocate memory on GPU device, you can manipulate the pointer (assignment, …) but can’t access its memory…

Trees , pointer chasing etc… are very bad programming/design strategies in GPU. Convert it to an array and dump it on the GPU.
Consider converting pointers to mere offsets into a base.

OR

if you still want a tree inside GPU, you can watch some documentary on wildlife… That way you can see lot of trees inside the GPU… OR If you are patient enough, throw some seeds inside the GPU n wait til they grow… he he he… Jest4Fun… :-)

Trees , pointer chasing etc… are very bad programming/design strategies in GPU. Convert it to an array and dump it on the GPU.
Consider converting pointers to mere offsets into a base.

OR

if you still want a tree inside GPU, you can watch some documentary on wildlife… That way you can see lot of trees inside the GPU… OR If you are patient enough, throw some seeds inside the GPU n wait til they grow… he he he… Jest4Fun… :-)

Ok, thank you both :)

I am just a newbie.

I’ll try to dump my tree into an array.

Are there any examples on this issue?

Ok, thank you both :)

I am just a newbie.

I’ll try to dump my tree into an array.

Are there any examples on this issue?

No specific examples…There was atleast one guy in the forum who was interested in all these…I think the guy who implemented GPUChess if my memory serves right… Therez at least one old thread on this topic. Use google to do site-search “xxxxx yyyy zzzz site:http://forums.nvidia.com

No specific examples…There was atleast one guy in the forum who was interested in all these…I think the guy who implemented GPUChess if my memory serves right… Therez at least one old thread on this topic. Use google to do site-search “xxxxx yyyy zzzz site:http://forums.nvidia.com

Thank you.I couldn’t find the example you mentioned but i managed it myself.
I converted tree into a “node” array and stored the offsets.

Thank you.I couldn’t find the example you mentioned but i managed it myself.
I converted tree into a “node” array and stored the offsets.