[英]Parallel Iterators in the D language
I am trying to implement a graph data structure in the D language which supports parallel iteration over the node and edge sets. 我正在尝试以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);
}
Here's a sequential node iterator method: 这是一个顺序节点迭代器方法:
/**
* 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);
}
}
Works like this, and seems to work fine: 像这样工作,并且似乎工作正常:
ulong sum1 = 0;
G.forNodes((node v) {
sum1 += v;
});
Now I try a parallel version using the 'std.parallelism' module: 现在,我尝试使用'std.parallelism'模块的并行版本:
void parallelForNodes(F)(F handle) {
foreach (node v; taskPool.parallel(z)) {
// call here
handle(v);
}
}
But this gives me the a compiler error. 但这给了我一个编译器错误。 What am I doing wrong here? 我在这里做错了什么?
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
takes a range. parallel
需要一个范围。 Use std.range.iota
to get the range equivalent of 0 .. z
: foreach (v; parallel(iota(z))) {...}
使用std.range.iota
获得等于0 .. z
的范围std.range.iota
: foreach (v; parallel(iota(z))) {...}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.