2
我試圖在D語言中實現一個支持並行迭代遍歷節點和邊集的圖數據結構。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.'