Advertisement
dmkozyrev

task-unmodified.cpp

Oct 11th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.93 KB | None | 0 0
  1. /*
  2.     File: task-unmodified.cpp
  3.    
  4.     g++ -march=native -O2 -std=c++14 -Wall -Wextra -Wshadow -Wconversion -fmax-errors=2 task-unmodified.cpp -o task-unmodified.o
  5. */
  6.  
  7. #include <chrono>
  8. #include <thread>
  9. #include <functional>
  10. #include <random>
  11. #include <cassert>
  12. #include <iostream>
  13. #include <numeric>
  14.  
  15. void matMultVec(double *result, double *mat, unsigned rows, unsigned cols, double *vec) {
  16.      for (unsigned i = 0; i < rows; ++i)
  17.      {
  18.          for (unsigned j = 0; j < cols; ++j)
  19.          {
  20.               result[i] += mat[i*cols + j] * vec[j];
  21.          }
  22.      }
  23. }
  24.  
  25. class Timer {
  26.  
  27.     std::chrono::time_point<std::chrono::system_clock> time_point;
  28.    
  29.     size_t value;
  30.    
  31. public:
  32.    
  33.     void start() {
  34.         time_point = std::chrono::system_clock::now();
  35.     }
  36.  
  37.     void finish() {
  38.         auto curr = std::chrono::system_clock::now();    
  39.         auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(curr - time_point);
  40.         value = elapsed.count();
  41.     }
  42.    
  43.     size_t get() const {
  44.         return value;
  45.     }    
  46. };
  47.  
  48.  
  49.  
  50. class Producer {
  51.     const unsigned totalJobs;
  52.     unsigned jobsIssued;
  53.     unsigned jobsReady;
  54.  
  55.     const unsigned jobWidth;
  56.     const unsigned jobHeight;
  57.  
  58. public:
  59.     Producer(): totalJobs(100), jobsIssued(0), jobsReady(0), jobWidth(1000), jobHeight(2000) {}
  60.     unsigned getTotalJobs() { return totalJobs; }
  61.     std::function<double(void)> getJob()
  62.     {
  63.         assert(jobsIssued < totalJobs);
  64.         if (!jobsReady) {
  65.             std::chrono::milliseconds generationTime(100);
  66.             std::this_thread::sleep_for(generationTime);
  67.             unsigned newJobs = 2;
  68.             if (newJobs + jobsIssued > totalJobs)
  69.             newJobs = totalJobs - jobsIssued;
  70.             jobsReady += newJobs;
  71.             std::clog << "Added " << newJobs << " new jobs." << std::endl;
  72.         }
  73.         jobsReady--;
  74.         jobsIssued++;
  75.         unsigned jobInit = jobsIssued;
  76.         return [jobInit, this] () {
  77.             Timer timer;
  78.             size_t totalMultTime = 0, totalGenTime = 0;
  79.             double sum = 0;
  80.             for (unsigned iter = 0; iter != 10; iter++)
  81.             {
  82.                 // Prepare matrix and vector:
  83.                 timer.start();
  84.                 std::vector<double> matrix(jobWidth*jobHeight);
  85.                 for (unsigned i = 0; i != jobWidth*jobHeight; i++)
  86.                 {
  87.                     matrix[i] = (0.00001*((jobInit + 10)*(jobInit - iter)) + i*0.02 + 0.1*(jobInit%4))/100000;
  88.                 }
  89.                 std::vector<double> vec(jobWidth);
  90.                 for (unsigned i = 0; i != jobWidth; i++)
  91.                 {
  92.                     vec[i] = -(0.0001*((jobInit + 10) - iter) - i*0.01)/1000;
  93.                 }
  94.                 std::vector<double> result(jobHeight, 0);
  95.                 timer.finish();
  96.                 totalGenTime += timer.get();
  97.                 // Call multiplication:
  98.                 timer.start();
  99.                 matMultVec(&result[0], &matrix[0], jobHeight, jobWidth, &vec[0]);
  100.                 timer.finish();
  101.                 totalMultTime += timer.get();
  102.                 // Update answer:
  103.                 sum += std::accumulate(result.begin(), result.end(), 0)/1000.0;
  104.             }
  105.             std::clog << "Task " << jobInit << " produced " << sum;
  106.             std::clog << ", totalMultTime is " << totalMultTime << " ms";
  107.             std::clog << ", totalGenTime is " << totalGenTime << " ms" << std::endl;
  108.             return sum;
  109.         };
  110.     }
  111. };
  112.  
  113. int main()
  114. {
  115.     Timer timer;
  116.     timer.start();
  117.    
  118.     Producer jobSource;
  119.     unsigned jobsLeft = jobSource.getTotalJobs();
  120.     double result = 0;
  121.     while (jobsLeft) {
  122.         auto job = jobSource.getJob();
  123.         result += job();
  124.         jobsLeft--;
  125.     }
  126.    
  127.     timer.finish();
  128.    
  129.     std::clog << "Done. Result is " << result << ", working time is " << timer.get() << " ms" << std::endl;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement