如何转换 C++将图结构转换为 Python/Numpy 图结构?
免责声明:本文作者对 C++ 和 Python 的了解有限,但对 Java 和 Ruby 有足够的了解。
《OpenCL 编程指南》一书中的示例使用了以下 OpenCL 定制的图形数据Dijkstra 算法在 OpenCL 设备上运行的结构:
void generateRandomGraph(GraphData *graph, int numVertices, int neighborsPerVertex)
{
graph->vertexCount = numVertices;
graph->vertexArray = (int*) malloc(graph->vertexCount * sizeof(int));
graph->edgeCount = numVertices * neighborsPerVertex;
graph->edgeArray = (int*)malloc(graph->edgeCount * sizeof(int));
graph->weightArray = (float*)malloc(graph->edgeCount * sizeof(float));
for(int i = 0; i < graph->vertexCount; i++)
{
graph->vertexArray[i] = i * neighborsPerVertex;
}
for(int i = 0; i < graph->edgeCount; i++)
{
graph->edgeArray[i] = (rand() % graph->vertexCount);
graph->weightArray[i] = (float)(rand() % 1000) / 1000.0f;
}
}
该数据结构基于 Pawan Harish 和 PJ Narayanan 撰写的“使用 CUDA 在 GPU 上加速大型图形算法”论文中的示例。
基本上,它具有三个数组:一个顶点数组 V
,其中每个顶点都指向边数组 E
中的邻居顶点(顶点 i+ 的邻居) 1
紧随数组 E
中顶点 i
的邻居。第三个数组用于边权重(还有两个更具体的 OpenCL 相关数组)。
如何在 Python/Numpy 中表示此数据结构?我想在 PyOpenCL 中使用它。
Disclaimer: The author of this post has a limited knowledge of C++ and Python, and has sufficient knowledge of Java and Ruby.
An example from the "OpenCL Programming Guide" book uses the following OpenCL-customized graph data structure for Dijkstra's algorithm to run on an OpenCL device:
void generateRandomGraph(GraphData *graph, int numVertices, int neighborsPerVertex)
{
graph->vertexCount = numVertices;
graph->vertexArray = (int*) malloc(graph->vertexCount * sizeof(int));
graph->edgeCount = numVertices * neighborsPerVertex;
graph->edgeArray = (int*)malloc(graph->edgeCount * sizeof(int));
graph->weightArray = (float*)malloc(graph->edgeCount * sizeof(float));
for(int i = 0; i < graph->vertexCount; i++)
{
graph->vertexArray[i] = i * neighborsPerVertex;
}
for(int i = 0; i < graph->edgeCount; i++)
{
graph->edgeArray[i] = (rand() % graph->vertexCount);
graph->weightArray[i] = (float)(rand() % 1000) / 1000.0f;
}
}
This data structure is based on the example from the "Accelerating large graph algorithms on the GPU using CUDA" paper by Pawan Harish and P. J. Narayanan.
Basically, it has three arrays: a vertex array V
each vertex of which points to its neighbour vertices in the edge array E
(the neighbours of a vertex i+1
follow the neighbours of the vertex i
immediately in the array E
). The third array is for edge weights (there are two more specific OpenCL related arrays).
How can I represent this data structure in Python/Numpy? I would like to use it in PyOpenCL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有该主题的专家,但这是某种邻接矩阵(http://en.wikipedia.org/wiki/Adjacency_matrix)。 Numpy 核心数据类型是 n 维数组,因此这应该非常简单。
No expert on the topic, but this is some kind of adjency matrix (http://en.wikipedia.org/wiki/Adjacency_matrix). Numpy core data type is the n-dimension array so this should be quite straighforward.