Advertisement
froleyks

main_concurrent_queue.cpp

Jan 14th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.57 KB | None | 0 0
  1. #include "../implementation/concurrent_queue.hpp"
  2. #include "parameter_processor.hpp"
  3.  
  4. #include <chrono>
  5. #include <functional>
  6. #include <iostream>
  7. #include <thread>
  8.  
  9. // std::atomic<size_t> num_inserted = 0;
  10.  
  11. size_t Q_SIZE;
  12. size_t NUM_PRODUCERS;
  13. size_t NUM_CONSUMERS;
  14. size_t NUM_ELEMENTS_FOR_EACH;
  15.  
  16. uint EOW = uint();
  17.  
  18. void produce(uint id, ConcurrentQueue<uint> &q) {
  19.   const size_t begin = id * NUM_ELEMENTS_FOR_EACH;
  20.   const size_t end   = begin + NUM_ELEMENTS_FOR_EACH;
  21.   for (size_t i = begin; i < end; ++i) {
  22.     // num_inserted++;
  23.  
  24.     // std::cout << "p: " << i << std::endl;
  25.     q.push_back(id, i + 1);
  26.   }
  27.   // std::cout << "done pushing: " << id << std::endl;
  28. }
  29.  
  30. void consume(uint id, ConcurrentQueue<uint> &q) {
  31.   bool active = true;
  32.   while (active) {
  33.     uint current = q.pop_front(id);
  34.     if (current == EOW) {
  35.       active = false;
  36.     }
  37.   }
  38. }
  39.  
  40. template <class T> void print(std::ostream &out, const T &t, size_t w) {
  41.   out.width(w);
  42.   out << t << " " << std::flush;
  43.   // std::cout.width(w);
  44.   // std::cout << t << " " << std::flush;
  45. }
  46.  
  47. void print_timing(std::ostream &out, size_t s, size_t p, size_t c, size_t w,
  48.                   double construction, double work) {
  49.   print(out, s, 9);
  50.   print(out, p, 3);
  51.   print(out, c, 3);
  52.   print(out, w, 9);
  53.   print(out, construction, 16);
  54.   print(out, work, 13);
  55.   out << std::endl;
  56. }
  57.  
  58. int main(int argc, char *argv[]) {
  59.   ParameterProcessor param(argc, argv);
  60.   Q_SIZE                = param.getInt("s", 10);
  61.   NUM_PRODUCERS         = param.getInt("p", 2);
  62.   NUM_CONSUMERS         = param.getInt("c", 1);
  63.   NUM_ELEMENTS_FOR_EACH = param.getInt("w", 9);
  64.  
  65.   // double distance = Dijkstra(id1, id2, graph);
  66.  
  67.   auto q_construction_start = std::chrono::high_resolution_clock::now();
  68.   ConcurrentQueue<uint> q(Q_SIZE, NUM_PRODUCERS + NUM_CONSUMERS,
  69.                           NUM_ELEMENTS_FOR_EACH * NUM_PRODUCERS);
  70.   auto q_construction_end = std::chrono::high_resolution_clock::now();
  71.  
  72.   std::vector<std::thread> producers;
  73.   producers.reserve(NUM_PRODUCERS);
  74.  
  75.   std::vector<std::thread> consumers;
  76.   consumers.reserve(NUM_CONSUMERS);
  77.  
  78.   auto work_start = std::chrono::high_resolution_clock::now();
  79.   for (size_t i = 0; i < NUM_PRODUCERS; ++i) {
  80.     producers.push_back(std::thread(produce, i, std::ref(q)));
  81.   }
  82.  
  83.   // for (size_t i = 0; i < NUM_PRODUCERS; ++i) {
  84.   //   producers[i].join();
  85.   // }
  86.   // for (auto e : check) {
  87.   //   if (!e) {
  88.   //     std::cout << "Element was not inserted" << std::endl;
  89.   //   }
  90.   // }
  91.  
  92.   for (size_t i = 0; i < NUM_CONSUMERS; ++i) {
  93.     consumers.push_back(std::thread(consume, NUM_PRODUCERS + i, std::ref(q)));
  94.   }
  95.  
  96.   for (size_t i = 0; i < NUM_PRODUCERS; ++i) {
  97.     producers[i].join();
  98.   }
  99.  
  100.   for (size_t i = 0; i < NUM_CONSUMERS; ++i) {
  101.     consumers[i].join();
  102.   }
  103.  
  104.   auto work_end = std::chrono::high_resolution_clock::now();
  105.  
  106.   double q_construction_time =
  107.       std::chrono::duration_cast<std::chrono::microseconds>(
  108.           q_construction_end - q_construction_start)
  109.           .count() /
  110.       1000.;
  111.  
  112.   double work_time = (std::chrono::duration_cast<std::chrono::microseconds>(
  113.                           work_end - work_start)
  114.                           .count() /
  115.                       1000.) /
  116.                      (NUM_ELEMENTS_FOR_EACH * NUM_PRODUCERS / 1000.0);
  117.  
  118.   // print_headline(cout);
  119.   print_timing(cout, Q_SIZE, NUM_PRODUCERS, NUM_CONSUMERS,
  120.                NUM_ELEMENTS_FOR_EACH, q_construction_time, work_time);
  121.  
  122.   // if (num_inserted != NUM_PRODUCERS * NUM_ELEMENTS_FOR_EACH) {
  123.   //   std::cerr << "Error: " << num_inserted << " inserted not "
  124.   //             << NUM_PRODUCERS * NUM_ELEMENTS_FOR_EACH << std::endl;
  125.   // }
  126.  
  127.   return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement