D语言中的并行迭代器

cls*_*udt 2 parallel-processing iterator d graph

我试图在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);
    }
Run Code Online (Sandbox Code Playgroud)

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

/**
 * 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

ulong sum1 = 0;
G.forNodes((node v) {
    sum1 += v;
});
Run Code Online (Sandbox Code Playgroud)

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

void parallelForNodes(F)(F handle) {
    foreach (node v; taskPool.parallel(z)) {
        // call here
        handle(v);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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.'
Run Code Online (Sandbox Code Playgroud)

小智 6

parallel需要一个范围.使用std.range.iota得到的范围相当于0 .. z:foreach (v; parallel(iota(z))) {...}