Skip to content

Commit

Permalink
Adding registration and fetch function
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxwell S Reeser committed Mar 21, 2019
1 parent 73f1203 commit 34b0fa0
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 103 deletions.
219 changes: 122 additions & 97 deletions src/dist_objects_4/src/template_dist_object.hpp
Original file line number Diff line number Diff line change
@@ -1,97 +1,122 @@
// Copyright (c) 2019 Weile Wei
// Copyright (c) 2019 Maxwell Resser
// Copyright (c) 2019 Hartmut Kaiser
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#if !defined(HPX_TEMPLATE_DIST_OBJECT_SERVER_MAR_20_2019_0328PM)
#define HPX_TEMPLATE_DIST_OBJECT_SERVER_MAR_20_2019_0328PM

#include <hpx/include/components.hpp>
#include <hpx/util/assert.hpp>

#include "server/template_dist_object.hpp"

#include <utility>

namespace dist_object {
template <typename T>
class dist_object
: hpx::components::client_base<dist_object<T>, server::partition<T>> {
typedef hpx::components::client_base<dist_object<T>, server::partition<T>>
base_type;

typedef typename server::partition<T>::data_type data_type;

private:
template <typename Arg>
static hpx::future<hpx::id_type> create_server(hpx::id_type where,
Arg &&value) {
return hpx::new_<server::partition<T>>(where, std::forward<Arg>(value));
}

public:
dist_object() {}

dist_object(hpx::id_type where, data_type const &data)
: base_type(create_server(where, data)) {}

dist_object(hpx::id_type where, data_type &&data)
: base_type(create_server(where, std::move(data))) {}

dist_object(hpx::future<hpx::id_type> &&id) : base_type(std::move(id)) {}

dist_object(hpx::id_type &&id) : base_type(std::move(id)) {}
size_t size() {
HPX_ASSERT(this->get_id());
ensure_ptr();
return (**ptr).size();
}

data_type const &operator*() const {
HPX_ASSERT(this->get_id());
ensure_ptr();
return **ptr;
}

data_type &operator*() {
HPX_ASSERT(this->get_id());
ensure_ptr();
return **ptr;
}

data_type const* operator->() const
{
HPX_ASSERT(this->get_id());
ensure_ptr();
return &**ptr;
}

data_type* operator->()
{
HPX_ASSERT(this->get_id());
ensure_ptr();
return &**ptr;
}

hpx::future<data_type> fetch()
{
HPX_ASSERT(this->get_id());

typedef typename server::partition<T>::fetch_action
action_type;
return hpx::async<action_type>(this->get_id());
}

private:
mutable std::shared_ptr<server::partition<T>> ptr;
void ensure_ptr() const {
if (!ptr) {
ptr = hpx::get_ptr<server::partition<T>>(hpx::launch::sync, get_id());
}
}
};
}

#endif
// Copyright (c) 2019 Weile Wei
// Copyright (c) 2019 Maxwell Resser
// Copyright (c) 2019 Hartmut Kaiser
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#if !defined(HPX_TEMPLATE_DIST_OBJECT_SERVER_MAR_20_2019_0328PM)
#define HPX_TEMPLATE_DIST_OBJECT_SERVER_MAR_20_2019_0328PM

#include <hpx/include/components.hpp>
#include <hpx/util/assert.hpp>

#include "server/template_dist_object.hpp"

#include <string>
#include <utility>

namespace dist_object {
template <typename T>
class dist_object
: hpx::components::client_base<dist_object<T>, server::partition<T>> {
typedef hpx::components::client_base<dist_object<T>, server::partition<T>>
base_type;

typedef typename server::partition<T>::data_type data_type;

private:
template <typename Arg>
static hpx::future<hpx::id_type> create_server(hpx::id_type where,
Arg &&value) {
return hpx::new_<server::partition<T>>(where, std::forward<Arg>(value));
}

public:
dist_object() {}

dist_object(std::string base, data_type const &data)
: base_type(create_server(hpx::find_here(), data)) {
int num_locs = hpx::find_all_localities().size();
hpx::register_with_basename(base + std::to_string(hpx::get_locality_id()), get_id());
locs.resize(num_locs);
unwrapped.resize(num_locs);
is_gotten.resize(num_locs);
for (int i = 0; i < num_locs; i++) {
is_gotten[i] = false;
if (i == hpx::get_locality_id()) {
continue;
}
locs[i] = hpx::find_from_basename(base + std::to_string(i), i);
}
}

dist_object(hpx::id_type where, data_type &&data)
: base_type(create_server(where, std::move(data))) {}

dist_object(hpx::future<hpx::id_type> &&id) : base_type(std::move(id)) {}

dist_object(hpx::id_type &&id) : base_type(std::move(id)) {}

size_t size() {
HPX_ASSERT(this->get_id());
ensure_ptr();
return (**ptr).size();
}

data_type const &operator*() const {
HPX_ASSERT(this->get_id());
ensure_ptr();
return **ptr;
}

data_type &operator*() {
HPX_ASSERT(this->get_id());
ensure_ptr();
return **ptr;
}

data_type const* operator->() const
{
HPX_ASSERT(this->get_id());
ensure_ptr();
return &**ptr;
}

data_type* operator->()
{
HPX_ASSERT(this->get_id());
ensure_ptr();
return &**ptr;
}

hpx::future<data_type> fetch(int id)
{
HPX_ASSERT(this->get_id());
hpx::id_type serv;
if (is_gotten[id])
serv = unwrapped[id];
else {
serv = locs[id].get();
unwrapped[id] = serv;
}

typedef typename server::partition<T>::fetch_action
action_type;
return hpx::async<action_type>(serv);
}

private:
mutable std::shared_ptr<server::partition<T>> ptr;
std::vector<hpx::future<hpx::id_type>> locs;
std::vector<hpx::id_type> unwrapped;
std::vector<bool> is_gotten;
void ensure_ptr() const {
if (!ptr) {
ptr = hpx::get_ptr<server::partition<T>>(hpx::launch::sync, get_id());
}
}
};
}

#endif
17 changes: 11 additions & 6 deletions src/dist_objects_4/src/template_dist_object_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ void run_dist_object_vector() {
std::vector<int> c(len, 0);

// construct int type dist_objects to be used later
dist_object::dist_object<int> A(hpx::find_here(), a);
dist_object::dist_object<int> B(hpx::find_here(), b);
dist_object::dist_object<int> C(hpx::find_here(), c);
dist_object::dist_object<int> A("a", a);
dist_object::dist_object<int> B("b", b);
dist_object::dist_object<int> C("c", c);

// perform element-wise addition between dist_objects
for (int i = 0; i < len; i++) {
Expand All @@ -63,9 +63,11 @@ void run_dist_object_matrix() {
std::vector<std::vector<int>> m2(rows, std::vector<int>(cols, val));
std::vector<std::vector<int>> m3(rows, std::vector<int>(cols, 0));

dist_object::dist_object<std::vector<int>> M1(hpx::find_here(), m1);
dist_object::dist_object<std::vector<int>> M2(hpx::find_here(), m2);
dist_object::dist_object<std::vector<int>> M3(hpx::find_here(), m3);
dist_object::dist_object<std::vector<int>> M1("m1", m1);
dist_object::dist_object<std::vector<int>> M2("m2", m2);
dist_object::dist_object<std::vector<int>> M3("m3", m3);



for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
Expand All @@ -78,12 +80,15 @@ void run_dist_object_matrix() {
m3[i][j] = m1[i][j] + m2[i][j];
}
}
hpx::future<std::vector<std::vector<int>>> k = M3.fetch((hpx::get_locality_id() + 1) % hpx::find_all_localities().size());
std::cout << "The value of other partition's first element is " << k.get()[0][0] << std::endl;
assert((*M3) == m3);
}

int hpx_main() {
run_dist_object_vector();
run_dist_object_matrix();
std::cout << "Hello world from locality " << hpx::get_locality_id() << std::endl;
return hpx::finalize();
}

Expand Down

0 comments on commit 34b0fa0

Please sign in to comment.