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?
>Solution :
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.