如何计算 Chapel forall 循环中的迭代次数

How do I count iterations in a Chapel forall loop

我想知道在使用 Chapel 的 forall 时循环执行了多少次。使用 CDO Library 的这段代码失败了,我确信这样做是正确的。谁能给我一个很好的例子吗?

var j:int = 0;
writeln("\n=== FORALL LOOP ===");
forall row in cursor {
  writeln("from: ", row['from_nm'], "\tto: ", row['to_nm']);
  j += 1;
}
writeln("Expected 10 rows, got ", j);

错误是

faerr.chpl:59: error: illegal lvalue in assignment
faerr.chpl:57: note: The shadow variable 'j' is constant due to forall intents in this loop

默认情况下,大多数变量类型(数组除外)都被复制到 forall 循环中,因此您无法修改它们。这样做是为了避免常见情况下的竞争条件。如果你想在 forall 循环中修改 j,你需要使用 forall intent

所有意图都在 forall 循环中 Chapel language spec. Since this code wants to reduce the j values of the tasks to a single value, you can use a reduce intent 的 "Data Parallelism" 部分定义以计算迭代次数:

var j:int = 0;
writeln("\n=== FORALL LOOP ===");

forall row in cursor with (+ reduce j) {
  writeln("from: ", row['from_nm'], "\tto: ", row['to_nm']);
  j += 1;
}

writeln("Expected 10 rows, got ", j);