教堂总和减少:未解决的电话 'sum(eltType=type int(64))'
Chapel sum reduce: unresolved call 'sum(eltType=type int(64))'
我正在尝试使用 Chapel 来解决一个简单的问题:
Find the sum of multiples of 3 or 5 below 1000 (ProjectEuler001)
这是我的代码:
module Main {
const topValue = 1000;
var mostWanted : [0..#topValue] int;
proc main() {
forall (elem,i) in zip(mostWanted, 0..) {
if(i % 3 == 0 || i % 5 == 0) {
elem = i;
}
}
var total = sum reduce mostWanted;
writeln(total);
}
}
然后我收到消息:
001.chpl:6: In function 'main':
001.chpl:14: error: unresolved call 'sum(eltType=type int(64))'
但是如果我将单词 sum
更改为 max
,它会给出正确答案:999。
我错过了什么?我不明白为什么 max
有效而 sum
无效。
因为我正在阅读的论文中唯一的 reduce 运算符是 max
,所以我错误地认为所有 reduce 运算符都按照论文中的描述编写。
Chapel provides a number of standard reduction and scan operators,
such as sum, product, logical and bitwise operations, and max/min
(with or without location information).
http://chapel.cray.com/papers/BriefOverviewChapel.pdf
实际上,reduce 运算符的编写方式与普通运算符相同。正确的代码应该是:
var total = + reduce mostWanted;
Reduce is an operator that combines a set of values to produce a single value. Reduce is useful because in parallel computation it is
almost always necessary at some point to compare or combine results
produced by different threads. The syntax for reduce is:
var varName = reduce_operator reduce iterator_expression;
In the code
above, valid reduce_operators are: +, *, &, |, ^, &&, ||, min, max,
minloc, and maxloc. Furthermore, iterator_expression can be an
expression of any type that can be iterated over, provided the
reduction operator can be applied to the values yielded by the
iteration. For example, the bitwise-and operator can be applied to
arrays of boolean or integral types to compute the bitwise-and of all
the values.
To sum up all the elements of an array A of size 10, you write: var
sum = + reduce A;
我正在尝试使用 Chapel 来解决一个简单的问题:
Find the sum of multiples of 3 or 5 below 1000 (ProjectEuler001)
这是我的代码:
module Main {
const topValue = 1000;
var mostWanted : [0..#topValue] int;
proc main() {
forall (elem,i) in zip(mostWanted, 0..) {
if(i % 3 == 0 || i % 5 == 0) {
elem = i;
}
}
var total = sum reduce mostWanted;
writeln(total);
}
}
然后我收到消息:
001.chpl:6: In function 'main':
001.chpl:14: error: unresolved call 'sum(eltType=type int(64))'
但是如果我将单词 sum
更改为 max
,它会给出正确答案:999。
我错过了什么?我不明白为什么 max
有效而 sum
无效。
因为我正在阅读的论文中唯一的 reduce 运算符是 max
,所以我错误地认为所有 reduce 运算符都按照论文中的描述编写。
Chapel provides a number of standard reduction and scan operators, such as sum, product, logical and bitwise operations, and max/min (with or without location information). http://chapel.cray.com/papers/BriefOverviewChapel.pdf
实际上,reduce 运算符的编写方式与普通运算符相同。正确的代码应该是:
var total = + reduce mostWanted;
Reduce is an operator that combines a set of values to produce a single value. Reduce is useful because in parallel computation it is almost always necessary at some point to compare or combine results produced by different threads. The syntax for reduce is:
var varName = reduce_operator reduce iterator_expression;
In the code above, valid reduce_operators are: +, *, &, |, ^, &&, ||, min, max, minloc, and maxloc. Furthermore, iterator_expression can be an expression of any type that can be iterated over, provided the reduction operator can be applied to the values yielded by the iteration. For example, the bitwise-and operator can be applied to arrays of boolean or integral types to compute the bitwise-and of all the values.
To sum up all the elements of an array A of size 10, you write: var sum = + reduce A;