[英]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.iota
: foreach (v; parallel(iota(z))) {...}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.