本文介绍了D语言中的并行迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在D语言中实现一个图形数据结构,它支持节点和边集上的并行迭代。

 别名ulong索引; 
别名索引节点;
别名ulong count;

class图{
index z; //最大节点索引
count n; //节点数
count m; //边的数量
node [] [] adja; //邻接列表
count [] deg; //节点度

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

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

  / ** 
*遍历图和调用处理函数的所有节点(lambda闭包)。
* /
void forNodes(F)(F handle){
foreach(node v; 0..z){
// call here
handle(v );




$ b

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

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

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

  void parallelForNodes(F)(F handle){
foreach(node v; taskPool.parallel(z)){
/ /打电话给
处理(v);
}
}

但是这给我一个编译器错误。

  cls〜/ workspace / Prototypes / PLPd $ ./main.d 
/ usr / local / Cellar / dmd / 2.063 / src / phobos / std / parallelism.d(3795):错误:不能有参数类型void
/usr/local/Cellar/dmd/2.063/src/phobos/ std / parallelism.d(3796):错误:不能具有void
类型的参数/usr/local/Cellar/dmd/2.063/src/phobos/std/parallelism.d(1539):错误:模板实例std .parallelism.ParallelForeach!(ulong)实例化
Graph.d(90):从这里实例化:parallel!(ulong)
./main.d(100):从这里实例化:parallelForNodes!( void delegate(ulong v)nothrow @safe)
Graph.d(90):错误:模板实例std.parallelism.TaskPool.parallel!(ulong)错误实例化
./main.d(100) (void delegate(ulong v)nothrow @safe)
./main.d(100):Error:模板实例Graph.Graph.parallelForNodes!(void delegate(ulong v)nothrow @安全)错误实例化
Fai导致:'dmd''-v''-o-''./main.d''-I。'


解决方案

parallel 取一个范围。使用获取范围相当于 0 .. z : foreach(v; parallel(iota(z))){...}

I am trying to implement a graph data structure in the D language which supports parallel iteration over the node and edge sets.

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:

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. Use std.range.iota to get the range equivalent of 0 .. z: foreach (v; parallel(iota(z))) {...}

这篇关于D语言中的并行迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 04:55