Advertisement
Guest User

Untitled

a guest
Jul 7th, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.73 KB | None | 0 0
  1. #include <hpx/hpx_main.hpp>
  2. #include <hpx/include/lcos.hpp>
  3. #include <hpx/include/actions.hpp>
  4. #include <hpx/include/components.hpp>
  5. #include <hpx/include/iostreams.hpp>
  6.  
  7. #include <boost/format.hpp>
  8.  
  9. #include <cstddef>
  10. #include <list>
  11. #include <mutex>
  12. #include <set>
  13. #include <vector>
  14.  
  15. #include "../../common.h"
  16.  
  17. std::size_t ep_worker(std::size_t desired)
  18. {
  19.     int i;
  20.     double *a = (double *) malloc( n * sizeof(double));
  21.     double *b = (double *) malloc( n * sizeof(double));
  22.     double *c = (double *) malloc( n * sizeof(double));
  23.  
  24.     /* fill it with random */
  25.     for( i = 0; i < n; i++ ) {
  26.         a[i] = rand() % 10;
  27.         b[i] = rand() % 10;
  28.     }  
  29.  
  30.     for( i = 0; i < n; i++ ) {
  31.         c[i] = a[i] * b[i];
  32.     }  
  33.  
  34.     free(a);
  35.     free(b);
  36.     free(c);
  37.  
  38.     return desired;
  39. }
  40.  
  41. HPX_PLAIN_ACTION(ep_worker, ep_worker_action);
  42.  
  43. void ep_foreman()
  44. {
  45.     std::size_t const os_threads = hpx::get_os_thread_count();
  46.  
  47.     cores = os_threads;
  48.  
  49.     hpx::naming::id_type const here = hpx::find_here();
  50.  
  51.     std::set<std::size_t> attendance;
  52.     for (std::size_t os_thread = 0; os_thread < os_threads; ++os_thread)
  53.         attendance.insert(os_thread);
  54.  
  55.     while (!attendance.empty())
  56.     {  
  57.         std::vector<hpx::lcos::future<std::size_t> > futures;
  58.         futures.reserve(attendance.size());
  59.  
  60.         for (std::size_t worker : attendance)
  61.         {
  62.            typedef ep_worker_action action_type;
  63.             int i, total_tasks = 100000;
  64.             for( i = 0; i < total_tasks/attendance.size(); i++ ) {
  65.                 futures.push_back(hpx::async<action_type>(here, worker));
  66.             }
  67.         }
  68.  
  69.         hpx::lcos::local::spinlock mtx;
  70.         hpx::lcos::wait_each(
  71.             hpx::util::unwrapped([&](std::size_t t) {
  72.                 if (std::size_t(-1) != t)
  73.                 {
  74.                     std::lock_guard<hpx::lcos::local::spinlock> lk(mtx);
  75.                     attendance.erase(t);
  76.                 }
  77.             }),
  78.             futures);
  79.     }
  80. }
  81.  
  82. HPX_PLAIN_ACTION(ep_foreman, ep_foreman_action);
  83.  
  84. int main(int argc, char **argv)
  85. {
  86.     cores = 1;
  87.     n = 1000;
  88.     total_tasks = 1000;
  89.  
  90.     process_input(&argc, &argv, 3);
  91.  
  92.     std::vector<hpx::naming::id_type> localities =
  93.         hpx::find_all_localities();
  94.  
  95.     std::vector<hpx::lcos::future<void> > futures;
  96.     futures.reserve(localities.size());
  97.  
  98.     for (hpx::naming::id_type const& node : localities)
  99.     {
  100.         typedef ep_foreman_action action_type;
  101.         futures.push_back(hpx::async<action_type>(node));
  102.     }
  103.  
  104.     START_TIMER;
  105.  
  106.     hpx::wait_all(futures);
  107.  
  108.     /* stop timer */
  109.     STOP_TIMER_AND_PRINT(("Cores: %d Tasks: %d\tn: %d\n", cores, total_tasks, n));
  110.  
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement