Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SSE for vector initialzation

Tags:

c++

vector

sse

I am relative new to C++ (moved from Java for performance for my scientific app) and I know nothing about SSE. Still, I need to improve the very simple following code:

    int myMax=INT_MAX;
    int size=18000003;
    vector<int> nodeCost(size);

    /* init part */
    for (int k=0;k<size;k++){
     nodeCost[k]=myMax;
    }

I have measured the time for the initialization part and it takes 13ms which is way too big for my scientific app (the entire algorithm runs in 22ms which means that the initialization takes 1/2 of the total time). Keep in mind that the initialization part will be repeated multiple times for the same vector.

As you see the size of the vector is not divided by 4. Is there a way to accelerate the initialization with SSE? Can you suggest how? Do I need to use arrays or SSE can be used with vectors as well?

Please, since I need your help let's all avoid a) "how did you measure the time" or b) "premature optimization is the root of all evil" which are both reasonable for you to ask but a) the measured time is correct b) I agree with it but I have no other choice. I do not want to parallelize the code with OpenMP, so SSE is the only fallback.

Thanks for your help

like image 540
Alexandros Avatar asked Jul 07 '26 14:07

Alexandros


1 Answers

Use the vector's constructor:

std::vector<int> nodeCost(size, myMax);

This will most likely use an optimized "memset"-type of implementation to fill the vector.

Also tell your compiler to generate architecture-specific code (e.g. -march=native -O3 on GCC). On my x86_64 machine, this produces the following code for filling the vector:

L5:
    add     r8, 1                    ;; increment counter
    vmovdqa YMMWORD PTR [rax], ymm0  ;; magic, ymm contains the data, and eax...
    add     rax, 32                  ;; ... the "end" pointer for the vector
    cmp     r8, rdi                  ;; loop condition, rdi holds the total size
    jb      .L5

The movdqa instruction, size-prefixed for 256-bit operations, copies 32 bytes to memory at once; it is part of the AVX instruction set.

like image 60
Kerrek SB Avatar answered Jul 09 '26 05:07

Kerrek SB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!