Git Product home page Git Product logo

c-simd-avx2-example's Introduction

C SIMD Example with AVX2

Compilation

gcc -mavx2 main.c -o output

Example

__m256i first = _mm256_set_epi32(10, 20, 30, 40, 50, 60, 70, 80);
__m256i second = _mm256_set_epi32(5, 5, 5, 5, 5, 5, 5, 5);
__m256i result = _mm256_add_epi32(first, second);

int* values = (int*) &result;

Scalar VS Vectorial

Image 1

SIMD solves the problem to execute many times the same instruction on a lot of data. If the operation is always the same, and the data always have the same data type, then using SIMD is more efficient.

In fact, instead of generating "basic" assembly instructions like multiple mov and add, it simply generates a few specific instructions (enough to handle the data), that are able to map to handle "vectors" in memory (shrunk of data of the same type) and operate the operation only once on all those data. This is called "vectorization".

Coding using vectorization directly depends of the architecture and the processor. The example above uses AVX2 from Intel for x86 processors. Instead of creating a for loop in order to make the addition of every item of the first array with the second one, we simply make two vectors and execute a simple addition between them.

GPU are more reliable to use vectorization, as they have an architecture made to handle multiple very repetitive operations (pixels on the screen, points in a 3D space to move...). CPU are also able to handle vectorization but are not directly made for that. Of course, using SIMD also increases a lot the execution time as there amount of instructions to execute might be reduced a lot.

More about AVX2

c-simd-avx2-example's People

Contributors

jean553 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.