为什么我在GPU上看到与顺序CPU相比的不同结果?
import numpy
from numba import cuda
from functools import reduce
A = (numpy.arange(100, dtype=numpy.float64)) + 1
cuda.reduce(lambda a, b: a + b * 20)(A)
# result 12952749821.0
reduce(lambda a, b: a + b * 20, A)
# result 100981.0
import numba
numba.__version__
# '0.34.0+5.g1762237'
使用Java Stream API并行化CPU减少时会发生类似的行为:
int n = 10;
float inputArray[] = new float[n];
ArrayList<Float> inputList = new ArrayList<Float>();
for (int i=0; i<n; i++)
{
inputArray[i] = i+1;
inputList.add(inputArray[i]);
}
Optional<Float> resultStream = inputList.stream().parallel().reduce((x, y) -> x+y*20);
float sequentialResult = array[0];
for (int i = 1; i < array.length; i++)
{
sequentialResult = sequentialResult + array[i] * 20;
}
System.out.println("Sequential Result "+sequentialResult);
// Sequential Result 10541.0
System.out.println("Stream Result "+resultStream.get());
// Stream Result 1.2466232E8
答案 0 :(得分:2)
正如Numba's team指出的那样,lambda a, b: a + b * 20
似乎不是 associative and commutative缩减函数,这会产生这种意外结果。