Test your awareness of multi-core and multi-thread
At present, multi-core multi-thread programming has become a trend, but most programmers have not come out of the thinking of serial programs. Even if some people have some knowledge about the concept of multi-core and multi-threaded programming, they are always confused.
Intel predicts that the CPU will reach 2013 cores by 256. It may take five years, but it leaves little time for our programmers. This is not an alarmist. The reality is true. If you begin to pay attention to this issue from now on, continue to learn and accumulate, and believe that in the near future, you may have an opportunity more than others.
I once made an interesting survey on my friends around me, and I have had multi-threaded coding experience in the survey, so as to learn more about multi-core and multi-thread recognition. Of course, it is undeniable that the questionnaire has a certain degree of one-sidedness due to its limited level of knowledge.
Sample program:
- # Ifdef _ cplusplus
- Extern "C"
- {
- # Endif
- # Include <stdio. h>
- # Include <sys/types. h>
- # Include <sys/time. h>
- # Include <pthread. h>
- # Include <unistd. h>
- # Define ORACLE _ max_value 1000000
- # Define apple_max_value 100000000
- # Define msecond 1000000
- Struct Apple
- {
- Unsigned long;
- Unsigned long B;
- };
- Struct orange
- {
- Int A [orange_max_value];
- Int B [orange_max_value];
- };
- Int main (INT argc, const char * argv []) {
- // Insert code here...
- Struct Apple test;
- Struct orange test1 = {0}, {0 }};
- Unsigned long sum = 0, Index = 0;
- Struct timeval tpstart, tpend;
- Float timeuse;
- Test. A = 0;
- Test. B = 0;
- /* Get start time */
- Gettimeofday (& tpstart, null );
- For (sum = 0; sum <apple_max_value; sum ++)
- {
- Test. A + = sum;
- Test. B + = sum;
- }
- For (Index = 0; index <orange_max_value; index ++)
- {
- Sum = test1.a [Index] + test1. B [Index];
- }
- /* Get start time */
- Gettimeofday (& tpend, null );
- /* Calculate time */
- Timeuse = msecond * (tpend. TV _sec-tpstart. TV _sec) + tpend. TV _usec-tpstart. TV _usec;
- Timeuse/= msecond;
- Printf ("main thread: % x, used time: % F/N", pthread_self (), timeuse );
- Printf ("A = % LlU, B = % LlU, sum = % LlU/N", test. A, test. B, sum );
- Return 0;
- }
- # Ifdef _ cplusplus
- }
- # Endif
Suppose there is a dual-core 2-generation dual-core machine, on which the above program is optimized as follows, how do you choose?
Q1: Do you think there is room for optimization for the sample program? If yes, the efficiency after optimization will be improved:
A. 1% ~ 30% B. 30% ~ 50% c. 50% ~ 90% D. 90% or above
Q2: If you change the sample program to two threads, one thread is used to calculate the sum of Apple and the other thread is used to calculate the sum of orange, who do you think is more efficient?
A. Two threads B. Single thread (sample program) C. uncertain
Q3: Based on Q2, the computing Apple thread is split into two threads. One thread is used to calculate the value of Apple A (Lock access ), another thread calculates the value of Apple B (Lock access), and the third thread calculates the sum of orange. Who do you think is more efficient?
A. Two threads B. Single thread (sample program) C. Three threads D. uncertain
Q4: Based on Q2, on a dual-core CPU system, bind the computing Apple thread to CPU 0 for running, bind the computing orange thread to CPU 1 for running, this method is called setting CPU affinity (CPU affinity, also called CPU binding). Who do you think is more efficient?
A. Two threads B. Single thread (sample program) C. Two threads (CPU bound) D. uncertain
Q5: After analysis, we found that orange computing is faster than Apple computing. Based on Q3, bind the thread for computing apple a and the thread for computing orange to CPU 0 for running, and bind the thread for computing Apple B to CPU 1 for running, who do you think is more efficient?
A. Three threads B. Single thread (sample program) C. Three threads (CPU bound) D. uncertain
Q6: In Q3, the program is split into multiple threads and requires a lock to access the values of Apple A and B. However, because they access different properties in the data structure, they can also be unlocked, who do you think is more efficient?
A. Lock access B. No lock access C. uncertain
If you are interested and want to know the answer, you can refer to my notebook titled program optimization using multi-core multithreading.
Another article is still in the creation process. It is also aimed at a series of Optimizations in the sample program process in this article, and the efficiency is eventually improved by 92%.
You are welcome to make a brick!