Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. #include <thread>
  2. #include <vector>
  3. #include <iostream>
  4. #include <chrono>
  5.  
  6. using namespace std;
  7.  
  8. int threadsNumber = 8;
  9.  
  10. double t = 0.01;
  11.  
  12. int iterationCount = 10000000;
  13.  
  14. double fRand(double fMin, double fMax) {
  15.  
  16. double f = (double)rand() / RAND_MAX;
  17. return fMin + f * (fMax - fMin);
  18. }
  19.  
  20. void calculatePointsOfFigure (int tid, double a, double b, double c) {
  21.  
  22. int xd = sched_getcpu()+1;
  23.  
  24.  
  25. double x = 0.1;
  26. double y = 0.1;
  27. double z = 0.1;
  28.  
  29. tid++;
  30.  
  31. for (int i = 0; i < iterationCount; i++ ) {
  32. double xt = x + t * a * (y - x);
  33. double yt = y + t * (x * (b - z) - y);
  34. double zt = z + t * (x * y - c * z);
  35.  
  36. x = xt;
  37. y = yt;
  38. z = zt;
  39. }
  40.  
  41. printf("Thread #%d, started on core #%d, executed on core #%d\n", tid, xd, sched_getcpu()+1);
  42.  
  43. printf("x = %f\n", x);
  44. printf("y = %f\n", y);
  45. printf("z = %f\n", z);
  46. printf("\n");
  47. }
  48.  
  49. int main() {
  50.  
  51. typedef chrono::high_resolution_clock Time;
  52. typedef chrono::milliseconds ms;
  53. typedef chrono::duration<float> fsec;
  54.  
  55.  
  56. unsigned numCPU = thread::hardware_concurrency();
  57. cout << "Number of cores on this machine is " << numCPU << endl;
  58.  
  59. vector<thread> threads;
  60.  
  61. auto begin = Time::now();
  62.  
  63. for (int i = 0; i < threadsNumber; i++) {
  64.  
  65. double randomA = fRand(5.0, 20.0);
  66. double randomB = fRand(20.0, 35.4);
  67. double randomC = fRand(1.0, 3.7);
  68.  
  69. threads.emplace_back(thread(calculatePointsOfFigure, i, randomA, randomB, randomC));
  70. }
  71.  
  72. for (auto& th : threads)
  73. th.join();
  74.  
  75. auto end = Time::now();
  76. fsec fs = end - begin;
  77. cout << fs.count() << " second" << endl;
  78.  
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement