Home > Net >  How can I programing multiple cores in C
How can I programing multiple cores in C

Time:10-10

I have a program and want to increase the runtime performance more.

let a = 1;
let b = 2;

let c = a   b;
let d = c   2;

let e = 3;

let f = c   d;
let g = a   e;

Step 1: Because a, b and e are independent so I want to execute them in parallel (different cores).

Step 2: Because c is depended on a and b; g is depended on a and e, but c and g are independent each other so execute c and g after step 1 but in parallel.

Step 3: Because d is depended on c so they are executed after step 2.

Step 4: Because f is depended on c and d, so it is executed after step 3.

Can we achieve this one with C or any programing language support this natively?

CodePudding user response:

Multi-threading is clearly not suited for your problem. The synchronization/data-movement time is far bigger than the time to compute an addition of two native-typed values (eg. floating-point number, integers, etc.). Indeed, adding two integers take about 1 cycle on mainstream x86-64 processors while the time to move data from one cache of a core to another one takes at least several dozens of cycles (if not hundreds regarding the target architecture). Thus, using multiple cores will actually slow down massively the code. Multi-threading only worth it for a relatively heavy grained computation (at least few microseconds and generally even a bit more).

Fortunately, modern processors can execute multiple instructions in parallel per cycle (see Instruction-level parallelism and Superscalar processor). For example, an Intel Skylake can execute 4 addition per cycle. It can also execute instructions in an out-of-order way. A processor can detect dependencies for you so you do not need to do much. You just need to ensure instructions are independent so they can be executed in parallel.

CodePudding user response:

The concept you are looking for is "multi-threading". Most modern progressing languages have built-in support for this concept (for example C 's std::thread, C is a bit ancient but multi-threading support was added in the 2011 standard update (known as C11) using the threads.h header. The new API is optional and while many common C implementations do offer support, the one you use may not.

Luckily, threading has been used extensively with C, before the 2011 standard update, and there are multiple non-standard APIs for threading that you can use as libraries. For example, check out pthread as a common cross platform threading library for C.

  • Related