Advertisement
Guest User

Untitled

a guest
Dec 8th, 2015
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.29 KB | None | 0 0
  1. #include <hpx/hpx_init.hpp>
  2. #include <hpx/hpx.hpp>
  3. #include <hpx/include/iostreams.hpp>
  4. #include <hpx/runtime/actions/plain_action.hpp>
  5. #include <hpx/include/components.hpp>
  6.  
  7. #include <hpx/runtime/serialization/serialize.hpp>
  8. #include <boost/shared_array.hpp>
  9.  
  10. #include <string>
  11.  
  12. struct cell {
  13.     cell()  {p=0;}
  14.  
  15.     cell(double a) {p=a;}
  16.  
  17.     double p;
  18. };
  19.  
  20. template<typename T>
  21. class array_deleter {
  22.     public:
  23.         void operator()(T const* p)
  24.         {
  25.             delete [] p;
  26.         }
  27. };
  28.  
  29. struct partition_data
  30. {
  31. private:
  32.     typedef hpx::serialization::serialize_buffer<cell> buffer_type;
  33.  
  34. public:
  35.     double c;
  36.     partition_data()
  37.     : size_x_(0),
  38.       size_y_(0),
  39.       size_(0)
  40.     {}
  41.  
  42.     partition_data(std::size_t size_x, std::size_t size_y)
  43.     : data_(new cell [size_x * size_y], size_x * size_y, buffer_type::take, array_deleter<cell>()),
  44.       size_x_(size_x),
  45.       size_y_(size_y),
  46.       size_(size_x * size_y)
  47.     {
  48.         HPX_ASSERT(size_x >= size_y);
  49.     }
  50.  
  51.     partition_data(std::size_t size_x, std::size_t size_y, double initial_value)
  52.     : data_(new cell [size_x * size_y], size_x * size_y, buffer_type::take, array_deleter<cell>()),
  53.       size_x_(size_x),
  54.       size_y_(size_y),
  55.       size_(size_x * size_y)
  56.     {
  57.         HPX_ASSERT(size_x >= size_y);
  58.  
  59.         for(std::size_t i = 0; i < size_; ++i)
  60.             data_[i] = cell(initial_value);
  61.     }
  62.  
  63.     partition_data(partition_data const& base)
  64.     {
  65.  
  66.         data_ = buffer_type(base.data_.data(), base.size(), buffer_type::reference);
  67.         size_x_ = base.size_x();
  68.         size_y_ = base.size_y();
  69.         size_ = base.size();
  70.     }
  71.  
  72.     std::size_t size_x() const { return size_x_;}
  73.     std::size_t size_y() const { return size_y_;}
  74.     std::size_t size() const { return size_;}
  75.  
  76.     cell get_cell(std::size_t idx, std::size_t idy) const { return data_[index(idx, idy)];}
  77.     cell& get_cell_ref(std::size_t idx, std::size_t idy) { return data_[index(idx, idy)];}
  78.  
  79.     cell operator[](std::size_t idx) const { return data_[idx];}
  80.     cell& operator[](std::size_t idx) { return data_[idx];}
  81.  
  82. private:
  83.     // Serialization support: even if all of the code below runs on one
  84.     // locality only, we need to provide an (empty) implementation for the
  85.     // serialization as all arguments passed to actions have to support this.
  86.     friend class hpx::serialization::access;
  87.  
  88.     template <typename Archive>
  89.     void serialize(Archive& ar, const unsigned int version)
  90.     {
  91.         ar & data_ & size_x_ & size_y_ & size_;
  92.     }
  93.  
  94.     std::size_t index(std::size_t idx, std::size_t idy) const
  95.     {
  96.         std::size_t id = idy*size_x_ + idx;
  97.         HPX_ASSERT(id >= 0 && id < size_);
  98.         return id;
  99.     }
  100.  
  101. private:
  102.     buffer_type data_;
  103.     std::size_t size_x_;
  104.     std::size_t size_y_;
  105.     std::size_t size_;
  106. };
  107.  
  108. struct HPX_COMPONENT_EXPORT partition_server
  109.     : hpx::components::component_base<partition_server>
  110. {
  111.     partition_server() {}
  112.  
  113.     partition_server(partition_data const& data)
  114.     : data_(data)
  115.     {}
  116.  
  117.     partition_server(std::size_t size_x, std::size_t size_y, double initial_value)
  118.     : data_(size_x, size_y, initial_value)
  119.     {}
  120.  
  121.     partition_data get_data() const
  122.     {
  123.         hpx::cout << "invoked get_data  on " << hpx::get_locality_id()
  124.         << " getting size " << data_.size() << hpx::endl << hpx::flush;
  125.         return partition_data(data_);
  126.     }
  127.  
  128.     HPX_DEFINE_COMPONENT_DIRECT_ACTION(partition_server, get_data, get_data_action);
  129.  
  130. private:
  131.     partition_data data_;
  132. };
  133.  
  134. typedef hpx::components::component<partition_server> partition_server_type;
  135.  
  136. HPX_REGISTER_COMPONENT(partition_server_type, partition_server);
  137.  
  138. HPX_REGISTER_ACTION(partition_server::get_data_action);
  139.  
  140. struct partition
  141.     : hpx::components::client_base<partition, partition_server>
  142. {
  143.     typedef hpx::components::client_base<partition, partition_server> base_type;
  144.  
  145.     partition() {}
  146.  
  147.     partition(hpx::id_type where, std::size_t size_x, std::size_t size_y, double initial_value)
  148.         : base_type(hpx::new_<partition_server>(where, size_x, size_y, initial_value))
  149.     {}
  150.  
  151.     // Create a new component on the locality co-located to the id 'where'. The
  152.     // new instance will be initialized from the given partition_data.
  153.     partition(hpx::id_type where, partition_data const& data)
  154.       : base_type(hpx::new_<partition_server>(hpx::colocated(where), data))
  155.     {}
  156.  
  157.     // Attach a future representing a (possibly remote) partition.
  158.     partition(hpx::future<hpx::id_type> && id)
  159.       : base_type(std::move(id))
  160.     {}
  161.  
  162.     // Unwrap a future<partition> (a partition already holds a future to the
  163.     // id of the referenced object, thus unwrapping accesses this inner future).
  164.     partition(hpx::future<partition> && c)
  165.       : base_type(std::move(c))
  166.     {}
  167.  
  168.     hpx::future<partition_data> get_data() const
  169.     {
  170.         partition_server::get_data_action act;
  171.         return hpx::async(act, get_id());
  172.     }
  173. };
  174.  
  175. int test(partition part) {
  176.     hpx::cout << "received on " << hpx::get_locality_id() << ": " << hpx::endl <<  hpx::flush;
  177.     partition_data pdata = part.get_data().get();
  178.     hpx::cout << " pdata " << pdata[0].p << hpx::endl << hpx::flush;
  179.     return 1;
  180. }
  181.  
  182. HPX_PLAIN_ACTION(test, test_act);
  183.  
  184. int hpx_main(int argc, char* argv[])
  185. {
  186.     std::vector<hpx::id_type> localities = hpx::find_all_localities();
  187.     std::size_t num_localities = localities.size();
  188.  
  189.     partition part = partition(hpx::find_here(), 2, 2, hpx::get_locality_id());
  190.     test_act act;
  191.     hpx::cout << "num locs " << num_localities << hpx::endl << hpx::flush;
  192.  
  193.     std::vector<hpx::future<int> > futures;
  194.  
  195.     partition_data pdata = part.get_data().get();
  196.  
  197.      for (int i = 0; i<num_localities; i++){
  198.             if(localities[i] != hpx::find_here())
  199.             {
  200.                 hpx::cout << "applying from " << hpx::get_locality_id() <<" on " << localities[i] << hpx::endl << hpx::flush;
  201.                 futures.push_back(hpx::async(act, localities[i], part));
  202.             }
  203.     }
  204.  
  205.     hpx::wait_all(futures);
  206.     hpx::cout << "done" << hpx::endl << hpx::flush;
  207.  
  208.     return hpx::finalize();
  209. }
  210.  
  211. int main(int argc, char* argv[])
  212. {
  213.     // Initialize and run HPX, this executes hpx_main above.
  214.     return hpx::init(argc, argv);
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement