warning precision is not precise due to the float error accumulation when size is large enough for more precision use double or a kahan sum else results can diverge from the CPU implementation
| 26 | /// or a kahan sum else results can diverge |
| 27 | /// from the CPU implementation |
| 28 | compute::program make_sma_program(const compute::context& context) |
| 29 | { |
| 30 | const char source[] = BOOST_COMPUTE_STRINGIZE_SOURCE( |
| 31 | __kernel void SMA(__global const float *scannedValues, int size, __global float *output, int wSize) |
| 32 | { |
| 33 | const int gid = get_global_id(0); |
| 34 | |
| 35 | float cumValues = 0.f; |
| 36 | int endIdx = gid + wSize/2; |
| 37 | int startIdx = gid -1 - wSize/2; |
| 38 | |
| 39 | if(endIdx > size -1) |
| 40 | endIdx = size -1; |
| 41 | |
| 42 | cumValues += scannedValues[endIdx]; |
| 43 | if(startIdx < 0) |
| 44 | startIdx = -1; |
| 45 | else |
| 46 | cumValues -= scannedValues[startIdx]; |
| 47 | |
| 48 | output[gid] =(float)( cumValues / ( float )(endIdx - startIdx)); |
| 49 | } |
| 50 | ); |
| 51 | |
| 52 | // create sma program |
| 53 | return compute::program::build_with_source(source,context); |
| 54 | } |
| 55 | |
| 56 | bool check_results(const std::vector<float>& values, const std::vector<float>& smoothValues, unsigned int wSize) |
| 57 | { |