如何转换 C++将图结构转换为 Python/Numpy 图结构?

发布于 2024-12-10 07:46:21 字数 1255 浏览 0 评论 0原文

免责声明:本文作者对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

折戟 2024-12-17 07:46:21
等待我真够勒 2024-12-17 07:46:21

没有该主题的专家,但这是某种邻接矩阵(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文