arduano / simdeez

easy simd
MIT License
334 stars 25 forks source link

Inconsistent result of horizontal_add_ps with avx2 #11

Closed Qpadev closed 5 years ago

Qpadev commented 5 years ago

While doing some tests with this crate I noticed that the implementation of horizontal_add_ps return incorrect results when compared with sse and normal scalar functions, for the testing I used the following code:

extern crate packed_simd;
extern crate paste;
extern crate rand;

use simdeez::avx2::*;
use simdeez::scalar::*;
use simdeez::sse2::*;
use simdeez::sse41::*;
use simdeez::*;

simd_runtime_generate!(
    fn sum_simdeez_horizontal(x: &[f32]) -> f32 {
        assert!(x.len() % S::VF32_WIDTH == 0);

        S::horizontal_add_ps(
            x.chunks_exact(S::VF32_WIDTH)
                .map(|y| S::loadu_ps(&y[0]))
                .fold(S::setzero_ps(), |x, y| S::add_ps(x, y))
        )
    }
);

simd_runtime_generate!(
    fn sum_simdeez_cycle(x: &[f32]) -> f32 {
        assert!(x.len() % S::VF32_WIDTH == 0);

        let sum_vec = x
            .chunks_exact(S::VF32_WIDTH)
            .map(|y| S::loadu_ps(&y[0]))
            .fold(S::setzero_ps(), |x, y| S::add_ps(x, y));

        let mut sum = 0.;
        for i in 0..S::VF32_WIDTH {
            sum += sum_vec[i];
        }
        sum
    }
);

fn init(n: usize) -> Vec<f32> {
    use rand::distributions::Standard;
    use rand::prelude::*;
    thread_rng().sample_iter(&Standard).take(n).collect()
}

fn main() {
    let x = init(4000);
    let avx2_horizontal_res;
    let sse_res;
    let avx2_cycle_res;
    let scalar_horizontal;

    unsafe {
        avx2_horizontal_res = sum_simdeez_horizontal_avx2(&x);
    }

    unsafe {
        avx2_cycle_res = sum_simdeez_cycle_avx2(&x);
    }

    unsafe {
        sse_res = sum_simdeez_horizontal_sse2(&x);
    }

    unsafe {
        scalar_horizontal = sum_simdeez_horizontal_scalar(&x);
    }

    println!(
        "Avx horizontal: {}\nAvx cycle: {}\nSSE: {}\nScalar: {}",
        avx2_horizontal_res, avx2_cycle_res, sse_res,scalar_horizontal
    );
}

The output on my machine is:

Avx horizontal: 2014.6807
Avx cycle: 1982.3975
SSE: 1982.3977
Scalar: 1982.398

The difference between the sse implementation and the scalar is minimal but the avx2 result when using the horizontal_add_ps is very far from the others. I've restricted the problem to the horizontal_add_ps by adding another function that use a cycle to do the horizontal sum of the elements of the array, when using this method the result is close to the sse and scalar results so I assume that the problem is in the implementation of the horizontal_add_ps for avx2.

Cargo version:

cargo 1.35.0-nightly (0e35bd8af 2019-03-13)

lscpu:

Architecture:        x86_64
CPU op-mode(s):      32-bit, 64-bit
Byte Order:          Little Endian
Address sizes:       39 bits physical, 48 bits virtual
CPU(s):              8
On-line CPU(s) list: 0-7
Thread(s) per core:  2
Core(s) per socket:  4
Socket(s):           1
NUMA node(s):        1
Vendor ID:           GenuineIntel
CPU family:          6
Model:               142
Model name:          Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
Stepping:            10
CPU MHz:             800.053
CPU max MHz:         3400,0000
CPU min MHz:         400,0000
BogoMIPS:            3601.00
Virtualization:      VT-x
L1d cache:           32K
L1i cache:           32K
L2 cache:            256K
L3 cache:            6144K
NUMA node0 CPU(s):   0-7
Flags:               fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp flush_l1d
jackmott commented 5 years ago

Thanks, I'll look into this. Also, I did not know about chunks_exact! that is handy

jackmott commented 5 years ago

fix is up, should perform better now as well, and I've added this case to the unit tests