OpenCL - copy Tree to device memory

帅比萌擦擦* 提交于 2019-12-02 12:25:09

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!