Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <omp.h>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <vector>
  6.  
  7. const double dx = 0.0000001;
  8. const int monteCarloCount = 900000;
  9.  
  10. double function(double x) {
  11. return 4.0 / (1 + x*x);
  12. }
  13.  
  14. void SingleThread() {
  15. long lLength = 1 / dx;
  16. double dSum = 0;
  17. for (long x = 0; x<lLength; ++x)
  18. dSum += function(x * dx) * dx;
  19.  
  20. std::cout << dSum << std::endl;
  21. }
  22.  
  23. void ThreadedWithCommon() {
  24. long lLength = 1 / dx;
  25. double adSum[8] = { 0,0,0,0,0,0,0,0 };
  26. #pragma omp parallel
  27. {
  28. const int nThreadNum = omp_get_thread_num();
  29. const int nTotalThreads = omp_get_num_threads();
  30. for (long x = nThreadNum; x<lLength; x += nTotalThreads)
  31. adSum[nThreadNum] += function(x * dx) * dx;
  32. }
  33. double dSum = 0;
  34. for (int i = 0; i < 8; ++i) {
  35. dSum += adSum[i];
  36. }
  37.  
  38. std::cout << dSum << std::endl;
  39. }
  40.  
  41. void Threaded() {
  42. long lLength = 1 / dx;
  43. double adSum[8] = { 0,0,0,0,0,0,0,0 };
  44. #pragma omp parallel
  45. {
  46. double dSum = 0;
  47. const int nThreadNum = omp_get_thread_num();
  48. const int nTotalThreads = omp_get_num_threads();
  49. for (long x = nThreadNum; x<lLength; x += nTotalThreads)
  50. dSum += function(x * dx) * dx;
  51. adSum[nThreadNum] = dSum;
  52. }
  53. double sum = 0;
  54. for (int i = 0; i < 8; ++i) {
  55. sum += adSum[i];
  56. }
  57. std::cout << sum << std::endl;
  58. }
  59.  
  60. void ThreadedForReduction() {
  61. long lLength = 1 / dx;
  62. double dSum = 0;
  63. #pragma omp parallel for reduction (+:dSum)
  64. for (long x = 0; x<lLength; ++x)
  65. dSum += function(x * dx) * dx;
  66. std::cout << dSum << std::endl;
  67. }
  68.  
  69. void MonteCarloSingleThread() {
  70. int in = 0;
  71. double x, y;
  72. for (int i = 0; i < monteCarloCount; ++i) {
  73. x = rand() / (double)RAND_MAX;
  74. y = rand() / (double)RAND_MAX;
  75. if (x*x + y*y<1)
  76. ++in;
  77. }
  78. std::cout << 4.0*in / monteCarloCount << std::endl;
  79. }
  80.  
  81. void MonteCarloThreaded() {
  82. const int n = 1;
  83. std::cout << n << " threads" << std::endl;
  84. std::vector<int> in_total;
  85. unsigned int in_sum = 0;
  86. //#pragma omp parallel num_threads(n)
  87. {
  88. srand(int(time(NULL)) ^ omp_get_thread_num());
  89. unsigned int in = 0, iterations = monteCarloCount / n;
  90. double x, y;
  91. for (int i = 0; i < iterations; ++i) {
  92. x = rand() / (double)RAND_MAX;
  93. y = rand() / (double)RAND_MAX;
  94. if (x*x + y*y < 1)
  95. ++in;
  96. }
  97. in_total.push_back(in);
  98. }
  99. for (int i = 0; i < in_total.size(); i++) {
  100. in_sum += in_total.at(i);
  101. }
  102. std::cout << 4.0*in_sum / monteCarloCount << std::endl;
  103. }
  104.  
  105. struct StopWatch {
  106. int nClockStart;
  107. int nClockEnd;
  108. void Start() {
  109. nClockStart = clock();
  110. }
  111. void End() {
  112. nClockEnd = clock();
  113.  
  114. double dDiffTicks = nClockEnd - nClockStart;
  115. double dDiffTime = (dDiffTicks) / (CLOCKS_PER_SEC / 1000);
  116. std::cout << "Time: " << dDiffTime << "ms" << std::endl;
  117. }
  118. };
  119.  
  120. int main(int argc, char *argv[]) {
  121. StopWatch stopWatch;
  122.  
  123. std::cout << "Single Thread" << std::endl;
  124. stopWatch.Start();
  125. SingleThread();
  126. stopWatch.End();
  127.  
  128.  
  129. std::cout << "Threaded with common variable" << std::endl;
  130. stopWatch.Start();
  131. ThreadedWithCommon();
  132. stopWatch.End();
  133.  
  134. std::cout << "Threaded with variable in each thread" << std::endl;
  135. stopWatch.Start();
  136. Threaded();
  137. stopWatch.End();
  138.  
  139. std::cout << "Threaded with for reduction" << std::endl;
  140. stopWatch.Start();
  141. ThreadedForReduction();
  142. stopWatch.End();
  143.  
  144. std::cout << "Monte Carlo Threaded: ";
  145. stopWatch.Start();
  146. MonteCarloThreaded();
  147. stopWatch.End();
  148.  
  149. std::cout << "Monte Carlo Single Thread" << std::endl;
  150. stopWatch.Start();
  151. MonteCarloSingleThread();
  152. stopWatch.End();
  153.  
  154. std::cin.get();
  155.  
  156. return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement