I'm implemented a Binary-Search-Tree in C code. Each of my tree nodes looks like this:
typedef struct treeNode {
int key;
struct treeNode *right;
struct treeNode *left;
} treeNode_t;
The construction of the Tree made by the Host. The query of the tree made by the device.
Now, let's assumed that I'm already finished building my Tree in host memory. I'm want to copy the root of my tree to the memory of my device.
Copying the root of the tree it self isn't enough. Because the right \ left child isn't located in the device memory. This is a problem.
So, my question is what is the easiest way to copy my whole tree to the device memory?
If your device supports OpenCL 2.0 then you can use Shared Virtual Memory
. The pointers created on the host will be valid on the device too. Here is the description and the binary search tree example: opencl-2-shared-virtual-memory.
The easiest (and likely also best) way is to change your structure to use node indexes instead of pointers. The issue with pointers is that the device has different pointers and even if you copy all nodes separately, it would still not work as the pointers also need to be updated to device pointers. And unfortunately OpenCL 1.2 does not even guarantee that device pointers stay valid longer than a single kernel invocation. For this reason you have to use indexes instead of pointers at least on the device.
Modify your structure like this:
typedef struct treeNode {
int key;
int left;
int right;
} treeNode_t;
Before you build the tree you allocate one big array of tree nodes, large enough to hold all nodes.
treeNode_t nodes[MAX_NODES]; // or dynamic allocation
int first_free_node=0;
Every time you would normally allocate a new node, you now use nodes[first_free_node] to store the data and increment the first_free_node counter. When you are done building your tree, you can just use a single clEnqueueCopyBuffer call to copy all nodes to the device. You only need to copy first_free_node*sizeof(treeNode_t) bytes from the start of the nodes array to the device. If you cannot change you host tree building code, you can use a simple recursive deep first travesal of the tree to count the number of nodes and convert the nodes from the pointer based format to the index based format.
On some devices you might get a higher performance if you convert the structure of your tree from array of structures to structure of arrays. Padding the structure to 16 byte per node could also help.
来源:https://stackoverflow.com/questions/31893419/opencl-copy-tree-to-device-memory