How to use _mm256_load_pd function load Vec data?

I try to load data from a Vec instance by SIMD AVX function _mm256_load_pd, however it does not work correctly.

My code is:

use std::arch::x86_64::*;
let mut test: Vec<f64> = Vec::with_capacity(16);
test.resize(test.capacity(), 0.0);
let avx_vec = unsafe { _mm256_load_pd(test.as_ptr().offset(0)) }

I think the problem might be memory alignment of Vec.

Did you try _mm256_mask_loadu_pd? What do you mean by "does not work correctly"?

I don't know whether I use this function correctly. I just want to move a C++ program to Rust and I have no idea about the memory structure of Vec or is it similar as pointer in C++? I would try _mm256_mask_loadu_pd later.

It might already been technically wrong in the C++ code. It will typically not be a practical problem because allocations are typically more aligned than the actual requirement. Additionally, afaik some CPUs do not complain about unaligned loads with aligned instructions.

I don't know what you mean exactly by internal structure of Vec. The layout of Vec itself is unspecified but it consists of a pointer, a capacity and the length of initialized elements. In that regard it is the same as std::vector in C++.

2 Likes

Thank you very much!
Vec data could be loaded to __m256d by function _mm256_loadu_pd. I guess that as a struct member with other primary type variable in a same memory block it might not be aligned.

This is the definition of struct type

use std::arch::x86_64::*;

/* definition */

pub struct Base {
    left_pt: usize,
    top_pt: usize,
    loc: Vec<f64>
}

/* definition */

unsafe fn base_load(src: &Base, len: usize, offs: isize) -> __m256d {
    src.loc = Vec::capacity_with(usize);
    src.loc.resize(sec.capacity(), 0.0);
    _mm256_loadu_pd(src.loc.as_ptr().offset(offs));
}

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.