arduano / simdeez

easy simd
MIT License
337 stars 25 forks source link

Invalid memory #43

Closed cisaacson closed 4 years ago

cisaacson commented 4 years ago

I have a function that pretty closely follows the example code, and I am getting an intermittent: signal: 11, SIGSEGV: invalid memory reference saying the process didn't exit successfully.

The error seems to occur when I call the simdeez fn and access the single result value.

Here is the code:

simd_runtime_generate!(
    fn simd_sum_value_list(f64_value_slice: &[f64]) -> f64 {
        let init_vec = vec![0f64; S::VF64_WIDTH];
        let init_slice = init_vec.as_slice();
        let mut result_v = S::load_pd(&init_slice[0]);
        for i in (0..f64_value_slice.len()).step_by(S::VF64_WIDTH) {
            let loss_value_v = S::load_pd(&f64_value_slice[i]);
            result_v = S::add_pd(loss_value_v, result_v);
        }

        let mut f64_sum_result_vec: Vec<f64> = Vec::with_capacity(S::VF64_WIDTH);
        f64_sum_result_vec.set_len(S::VF64_WIDTH);
        S::storeu_pd(&mut f64_sum_result_vec[0], result_v);
        let mut result_sum_value = 0f64;
        for result_sum_value_result_value in f64_sum_result_vec {
            result_sum_value += result_sum_value;
        }

        result_sum_value
    }
);

pub fn sum_value_list(f64_value_list: Vec<f64>) -> f64 {
    let result_sum_value = simd_sum_value_list(f64_value_list.as_slice());

    result_sum_value
}

The code is called from a test that looks like this:

let result_sum_value = sum_value_list(f64_value_list);

Any ideas what would cause this?

cisaacson commented 4 years ago

I tracked it down, it is because I was using load_pd instead of loadu_pd. I understand now the byte alignment is required for load_pd. I'll research how to align bytes, or just continue to use the loadu... fns.