gpt4 book ai didi

c++ - 并行迭代宏的替代方案?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:11:47 27 4
gpt4 key购买 nike

这将是一个很长的故事,但也许你们中的一些人愿意研究这个案例。

我正在从事并行图算法开发。我选择了一个名为 STINGER 的尖端 HPC 并行图数据结构。 . STINGER 的使命声明如下:

"STINGER should provide a common abstract data structure such that the large graph community can quickly leverage each others' research developments. [...] Algorithms written for STINGER can easily be translated/ported between multiple languages and frameworks [...] It is recognized that no single data structure is optimal for every graph algorithm. The objective of STINGER is to configure a sensible data structure that can run most algorithms well. There should be no significant performance reduction for using STINGER when compared with another general data structure across a broad set of typical graph algorithms."

STINGER 可能相当高效,适合共享内存并行。另一方面,它不是很抽象、通用或简洁。 STINGER 提供的接口(interface)对我来说并不令人满意,原因有以下几个:它太冗长(函数需要的参数对我的情况来说并不重要);它只对一个有向图建模,而我需要一个无向图;等原因。

但是,我回避自己实现一个新的并行图数据结构。

所以我已经开始用我自己的 Graph 类封装一个 STINGER 实例。例如,要检查是否存在无向边,我现在可以调用 Graph::hasEdge(node u, node v) 而不是写入我的算法:

int to = stinger_has_typed_successor(stinger, etype, u, v);
int back = stinger_has_typed_successor(stinger, etype, v, u);
bool answer = to && back;

到目前为止,这很有效。现在进入迭代主题。

STINGER通过宏实现遍历(遍历节点、边、节点的入射边等)。比如你写

STINGER_PARALLEL_FORALL_EDGES_BEGIN(G.asSTINGER(), etype) {
node u = STINGER_EDGE_SOURCE;
node v = STINGER_EDGE_DEST;
std::printf("found edge (%d, %d)", u, v);
} STINGER_PARALLEL_FORALL_EDGES_END();

此处 STINGER_PARALLEL_FORALL_EDGES_BEGIN 扩展为

do {                                    \
\
\
for(uint64_t p__ = 0; p__ < (G.asSTINGER())->ETA[(etype)].high; p__++) { \
struct stinger_eb * current_eb__ = ebpool + (G.asSTINGER())->ETA[(etype)].blocks[p__]; \
int64_t source__ = current_eb__->vertexID; \
int64_t type__ = current_eb__->etype; \
for(uint64_t i__ = 0; i__ < stinger_eb_high(current_eb__); i__++) { \
if(!stinger_eb_is_blank(current_eb__, i__)) { \
struct stinger_edge * current_edge__ = current_eb__->edges + i__;

宏隐藏了数据结构的内部结构,显然需要将其完全暴露才能进行高效(并行)迭代。有用于各种组合的宏,包括 STINGER_FORALL_EDGES_BEGINSTINGER_READ_ONLY_FORALL_EDGES_BEGINSTINGER_READ_ONLY_PARALLEL_FORALL_EDGES_BEGIN...

是的,我可以使用这些宏,但我想知道是否有更优雅的方法来实现迭代。如果我希望有一个界面,它看起来应该类似于

G.forallEdges(readonly=true, parallel=true, {..})

GraphIterTools.forallEdges(G, readonly=true, parallel=true, {...})

其中 {...} 只是一个函数、一个闭包或一个“代码块”,然后将被适本地执行。但是,我缺乏实现它的 C++ 经验。我想知道在这个问题上你能给我什么建议。也可能是“你应该使用宏,因为……”。

最佳答案

利用现有的宏,您可以像这样在图形类上实现一个成员函数:

template<typename Callback>
void forallEdges(int etype, Callback callback)
{
STINGER_PARALLEL_FORALL_EDGES_BEGIN(this->asSTINGER(), etype) {
node u = STINGER_EDGE_SOURCE;
node v = STINGER_EDGE_DEST;

// call the supplied callback
callback(u, v);

} STINGER_PARALLEL_FORALL_EDGES_END();
}

然后定义一个回调函数并将其传递给您的新方法:

void my_callback(node u, node v) { ... }
...
G.forallEdges(etype, my_callback);

或者在 C++11 中,您可以使用 lambda 函数:

G.forallEdges(etype, [](node u, node v) { ... });

关于c++ - 并行迭代宏的替代方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13727470/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com