Questionnaire: test your awareness of multi-core and multi-thread

Source: Internet
Author: User
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:

  1. # Ifdef _ cplusplus
  2. Extern "C"
  3. {
  4. # Endif
  5. # Include <stdio. h>
  6. # Include <sys/types. h>
  7. # Include <sys/time. h>
  8. # Include <pthread. h>
  9. # Include <unistd. h>
  10. # Define ORACLE _ max_value 1000000
  11. # Define apple_max_value 100000000
  12. # Define msecond 1000000
  13. Struct Apple
  14. {
  15. Unsigned long;
  16. Unsigned long B;
  17. };

  18. Struct orange
  19. {
  20. Int A [orange_max_value];
  21. Int B [orange_max_value];
  22. };

  23. Int main (INT argc, const char * argv []) {
  24. // Insert code here...
  25. Struct Apple test;
  26. Struct orange test1 = {0}, {0 }};
  27. Unsigned long sum = 0, Index = 0;
  28. Struct timeval tpstart, tpend;
  29. Float timeuse;
  30. Test. A = 0;
  31. Test. B = 0;
  32. /* Get start time */
  33. Gettimeofday (& tpstart, null );
  34. For (sum = 0; sum <apple_max_value; sum ++)
  35. {
  36. Test. A + = sum;
  37. Test. B + = sum;
  38. }
  39. For (Index = 0; index <orange_max_value; index ++)
  40. {
  41. Sum = test1.a [Index] + test1. B [Index];
  42. }
  43. /* Get start time */
  44. Gettimeofday (& tpend, null );
  45. /* Calculate time */
  46. Timeuse = msecond * (tpend. TV _sec-tpstart. TV _sec) + tpend. TV _usec-tpstart. TV _usec;
  47. Timeuse/= msecond;
  48. Printf ("main thread: % x, used time: % F/N", pthread_self (), timeuse );
  49. Printf ("A = % LlU, B = % LlU, sum = % LlU/N", test. A, test. B, sum );
  50. Return 0;
  51. }
  52. # Ifdef _ cplusplus
  53. }
  54. # 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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.