- Jun 27, 2002
- 2,908
- 0
- 76
Are there compilers out there that will intelligently optimize code for multiple processors? You might say that code HAS to be written for multiple processors, but a lot of code isn't that could be.
Consider a simple little app like this, written in crappy pseudocode. I put line numbers so you can see the steps involved.
1. Initialize a variable "a"
2. Initialize a variable "b"
3. Initialize a variable "c"
4. Initialize a variable "d"
5. Store 10 into a
6. Store 15 into b
7. Store 20 into c
8. Store 25 into d
9. Add a and b and store it to C
10. Multiply D with C and store it to A
Now, what if a compiler optimized this code for multiple processors? You see, the problem with multithreading is dependency on previous operations. What if a code skimmed through and realized that, for example, variable A wasn't being used in lines 2,3, 4, 6, 7, or 8? An optimized code for two processors could look something like this:
1. Initialze "a" and "b"
2. Initialize "c" and "d"
3. Store 10 into "a" and 15 into "b"
4. Add "a" and "b" and store it into "c"
5. Multiply "d" with "c" and store it into "a"
The whole idea is based on data dependency on previous and future operations.
Let's take a more extreme example:
1. Initialize "a"
2. Initialize "b"
3. Initialize "c"
4. Initialize "d"
5. Initialize "e"
6. Initialize "f"
7. Initialize "g"
8. Initialize "h"
9. Store 1.414 into "a"
10. Store 1.732 into "b"
11. Store 2.236 into "c"
12. Store 2.646 into "d"
13. Store 3.317 into "e"
14. Store 3.606 into "f"
15. Store 4.123 into "g"
16. Store 4.359 into "h"
17. Add "e" and "f" and store it to "g"
18. Add "a" and "b" and store it to "c"
19. Multiply "d" and "c" and store it to "a"
20. Multiply "h" and "g" and store it to "e"
This can be rewritten for two threads:
1. Initialize "a" ; Initialize "b"
2. Initialize "c" ; Initialize "d"
3. Initialize "e" ; Initialize "f"
4. Initialize "g" ; Initialize "h"
5. Store 1.414 into "a" ; Store 1.732 into "b"
6. Store 2.236 into "c" ; Store 2.646 into "d"
7. Store 3.317 into "e" ; Store 3.606 into "f"
8. Store 4.123 into "g" ; Store 4.359 into "h"
9. Add "e" and "f" and store it to "g" ; Add "a" and "b" and store it to "c"
10. Multiply "d" and "c" and store it to "a" ; Multiply "h" and "g" and store it to "e"
What do you think?
Consider a simple little app like this, written in crappy pseudocode. I put line numbers so you can see the steps involved.
1. Initialize a variable "a"
2. Initialize a variable "b"
3. Initialize a variable "c"
4. Initialize a variable "d"
5. Store 10 into a
6. Store 15 into b
7. Store 20 into c
8. Store 25 into d
9. Add a and b and store it to C
10. Multiply D with C and store it to A
Now, what if a compiler optimized this code for multiple processors? You see, the problem with multithreading is dependency on previous operations. What if a code skimmed through and realized that, for example, variable A wasn't being used in lines 2,3, 4, 6, 7, or 8? An optimized code for two processors could look something like this:
1. Initialze "a" and "b"
2. Initialize "c" and "d"
3. Store 10 into "a" and 15 into "b"
4. Add "a" and "b" and store it into "c"
5. Multiply "d" with "c" and store it into "a"
The whole idea is based on data dependency on previous and future operations.
Let's take a more extreme example:
1. Initialize "a"
2. Initialize "b"
3. Initialize "c"
4. Initialize "d"
5. Initialize "e"
6. Initialize "f"
7. Initialize "g"
8. Initialize "h"
9. Store 1.414 into "a"
10. Store 1.732 into "b"
11. Store 2.236 into "c"
12. Store 2.646 into "d"
13. Store 3.317 into "e"
14. Store 3.606 into "f"
15. Store 4.123 into "g"
16. Store 4.359 into "h"
17. Add "e" and "f" and store it to "g"
18. Add "a" and "b" and store it to "c"
19. Multiply "d" and "c" and store it to "a"
20. Multiply "h" and "g" and store it to "e"
This can be rewritten for two threads:
1. Initialize "a" ; Initialize "b"
2. Initialize "c" ; Initialize "d"
3. Initialize "e" ; Initialize "f"
4. Initialize "g" ; Initialize "h"
5. Store 1.414 into "a" ; Store 1.732 into "b"
6. Store 2.236 into "c" ; Store 2.646 into "d"
7. Store 3.317 into "e" ; Store 3.606 into "f"
8. Store 4.123 into "g" ; Store 4.359 into "h"
9. Add "e" and "f" and store it to "g" ; Add "a" and "b" and store it to "c"
10. Multiply "d" and "c" and store it to "a" ; Multiply "h" and "g" and store it to "e"
What do you think?
