Advertisement
Guest User

Untitled

a guest
Jun 16th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. $ ./run.sh
  2. $ cat run.sh ----------------------------------------------
  3. #!/bin/bash
  4.  
  5. echo "\$ cat run.sh ----------------------------------------------"
  6. cat run.sh
  7. echo "\$ rm -f simple.* 2>/dev/null ------------------------------"
  8. rm -f simple.* 2>/dev/null
  9. echo "\$ g++ -std=c++11 -pthread simple_3.cpp -o simple.3 --------"
  10. g++ -std=c++11 -pthread simple_3.cpp -o simple.3
  11. echo "\$ g++ -std=c++11 -pthread simple_3_2.cpp -o simple.3_2 ----"
  12. g++ -std=c++11 -pthread simple_3_2.cpp -o simple.3_2
  13. echo "\$ time ./simple.3 >simple.3.out ---------------------------"
  14. date +%M:%S.%N
  15. time ./simple.3 >simple.3.out
  16. date +%M:%S.%N
  17. echo "\$ time ./simple.3_2 >simple.3_2.out -----------------------"
  18. date +%M:%S.%N
  19. time ./simple.3_2 >simple.3_2.out
  20. date +%M:%S.%N
  21. echo "\$ diff simple.3.out simple.3_2.out ------------------------"
  22. diff simple.3.out simple.3_2.out
  23. echo "$?"
  24. echo "\$ diff simple_3.cpp simple_3_2.cpp ------------------------"
  25. diff simple_3.cpp simple_3_2.cpp
  26.  
  27. $ rm -f simple.* 2>/dev/null ------------------------------
  28. $ g++ -std=c++11 -pthread simple_3.cpp -o simple.3 --------
  29. $ g++ -std=c++11 -pthread simple_3_2.cpp -o simple.3_2 ----
  30. $ time ./simple.3 >simple.3.out ---------------------------
  31. 54:51.414693127
  32.  
  33. real    0m14.731s
  34. user    0m14.148s
  35. sys 0m0.540s
  36. 55:06.147527702
  37. $ time ./simple.3_2 >simple.3_2.out -----------------------
  38. 55:06.148773745
  39.  
  40. real    0m11.722s
  41. user    0m15.024s
  42. sys 0m2.044s
  43. 55:17.872201307
  44. $ diff simple.3.out simple.3_2.out ------------------------
  45. 0
  46. $ diff simple_3.cpp simple_3_2.cpp ------------------------
  47. 24a25
  48. >   static void solvePrime3_2(CPrimeNumbers *sno, const uint v,const uint max); // Multithreading version: mark all N*v numbers (N - integer, N*v<=max) as non-prime
  49. 29a31
  50. >   void Initiate3_2(const uint max=6); // Marking-forward algorithm, multithreading using C++11, std::thread
  51. 37a40,70
  52. > /*static*/ void CPrimeNumbers::solvePrime3_2(CPrimeNumbers *sno, const uint head, const uint max)
  53. > {
  54. >   if ( sno->numbers[sno->NumberToIndex(head)]==1 ) { // Is prime/unchecked yet
  55. >     if (!sno->IsPrime(head)) { // Is not prime
  56. >       sno->numbers[sno->NumberToIndex(head)]=0;
  57. >     }
  58. >     else{ // Is prime
  59. >       MarkNonPrime3(sno,head,max);
  60. >     }
  61. >   }
  62. > };
  63. >
  64. > void CPrimeNumbers::Initiate3_2(const uint max)
  65. > {
  66. >   uint head,i;
  67. >   vector<thread> threads;
  68. >  
  69. >   numbers.assign(NumberToIndex(max)+1,1);
  70. >  
  71. >   for (head=3;head<=max;head+=2) {
  72. >     if ( threads.size()==MAX_THREAD) {
  73. >       threads[0].join();
  74. >       threads.erase(threads.begin());
  75. >     }
  76. >     threads.push_back( thread(solvePrime3_2,this,head,max) );
  77. >   }
  78. >  
  79. >   for (auto t=threads.begin();t!=threads.end();t++)
  80. >     t->join();
  81. > };
  82. >
  83. 224c257
  84. <   s.Initiate3(200000);
  85. ---
  86. >   s.Initiate3_2(200000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement