1

I have C code which uses a pointer to a struct. I'm trying to figure out how to pass it to cuda without much luck.

I have

typedef struct node {        /* describes a tip species or an ancestor */
  struct node *next, *back;  /* pointers to nodes                      */
 etc...
} node;

Then

typedef node **pointptr;
static pointptr treenode;

In my code I iterate through all of these, and I'm trying to figure out how to pass them to the kernel so I can perform the following operation:

for (i = 1; i <= nonodes; i++) {
    treenode[i - 1]->back = NULL;
    etc....
}

But I can't figure out how to pass it. Any ideas?

0

2 Answers 2

1

The problem is that in order to use your tree inside the kernel, your next and back should probably point somewhere in device memory. Assuming you construct your tree on the host and then pass it, you could do something like:

node* traverse(node*n){
    if (n==NULL)
        return NULL;

    node x, *d;
    x.back = traverse(n->back);
    x.next = traverse(n->next);
    cudaMalloc(&d, sizeof(node));
    cudaMemcpy(d, &x, sizeof(node), cudaMemcpyHostToDevice);
    return d;
}

and by calling it on the root you'd end up with a pointer to the root of the tree in device memory, which you could pass to your kernel directly. I haven't tested this code, and you'd have to write something similar to delete the tree afterwards.

Alternatively, you could store your tree nodes contiguously inside an array, with indices in the back and next instead of pointers (possibly changing them back to pointers in device code if necessary).

Sign up to request clarification or add additional context in comments.

Comments

0

Check this question:

Copying a multi-branch tree to GPU memory

Although it does not answer your question exactly, I think it may clear some things out and ultimately help you tackle your problem.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.