gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:17:27 25 4
gpt4 key购买 nike

我想知道在使用 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

所有意图都在 Chapel language spec 的“数据并行”部分中定义。 .由于此代码希望将任务的 j 值减少为单个值,因此您可以使用 reduce intentforall 循环中计算迭代次数:

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);

关于chapel - 如何计算 Chapel forall 循环中的迭代次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48771784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com