繁体   English   中英

D语言的并行迭代器

[英]Parallel Iterators in the D language

我正在尝试以D语言实现一种图形数据结构,该结构支持对节点和边集进行并行迭代。

alias ulong index;
alias index node;
alias ulong count;

class Graph {
    index z;    // max node index
    count n;    // number of nodes
    count m;    // number of edges
    node[][] adja;  // adjacency list
    count[] deg;    // node degree

    this(count n = 0) {
        this.z = n;
        this.n = n;
        this.m = 0;
        this.adja = new node[][](this.z, 0);
        this.deg = new count[](this.z);
    }

这是一个顺序节点迭代器方法:

/**
 * Iterate over all nodes of the graph and call handler (lambda closure).
 */
void forNodes(F)(F handle) {
    foreach (node v; 0 .. z) {
        // call here
        handle(v);
    }
}

像这样工作,并且似乎工作正常:

ulong sum1 = 0;
G.forNodes((node v) {
    sum1 += v;
});

现在,我尝试使用'std.parallelism'模块的并行版本:

void parallelForNodes(F)(F handle) {
    foreach (node v; taskPool.parallel(z)) {
        // call here
        handle(v);
    }
}

但这给了我一个编译器错误。 我在这里做错了什么?

cls ~/workspace/Prototypes/PLPd $ ./main.d
/usr/local/Cellar/dmd/2.063/src/phobos/std/parallelism.d(3795): Error: cannot have parameter of type void
/usr/local/Cellar/dmd/2.063/src/phobos/std/parallelism.d(3796): Error: cannot have parameter of type void
/usr/local/Cellar/dmd/2.063/src/phobos/std/parallelism.d(1539): Error: template instance std.parallelism.ParallelForeach!(ulong) error instantiating
Graph.d(90):        instantiated from here: parallel!(ulong)
./main.d(100):        instantiated from here: parallelForNodes!(void delegate(ulong v) nothrow @safe)
Graph.d(90): Error: template instance std.parallelism.TaskPool.parallel!(ulong) error instantiating
./main.d(100):        instantiated from here: parallelForNodes!(void delegate(ulong v) nothrow @safe)
./main.d(100): Error: template instance Graph.Graph.parallelForNodes!(void delegate(ulong v) nothrow @safe) error instantiating
Failed: 'dmd' '-v' '-o-' './main.d' '-I.'

parallel需要一个范围。 使用std.range.iota获得等于0 .. z的范围std.range.iotaforeach (v; parallel(iota(z))) {...}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM