Skip to content
Snippets Groups Projects
Commit 8b462847 authored by Recolic Keghart's avatar Recolic Keghart
Browse files

change push/find to set/get

parent 0a7afb0b
No related branches found
No related tags found
No related merge requests found
...@@ -41,79 +41,79 @@ public: ...@@ -41,79 +41,79 @@ public:
: local_buf(slot_per_node), my_rank(my_rank), n_rank(n_rank) { : local_buf(slot_per_node), my_rank(my_rank), n_rank(n_rank) {
} }
void push(const key_type &k, const value_type &v) { void set(const key_type &k, const value_type &v) {
auto target_rank = find_rank_for_hash(find_hash_for_ele(k)); auto target_rank = get_rank_for_hash(get_hash_for_ele(k));
if(my_rank == target_rank) { if(my_rank == target_rank) {
return do_insert(k, v); return do_set(k, v);
} }
else { else {
bool succ = upcxx::rpc(target_rank, std::bind(&this_type::do_rpc_insert, this, k, v)).wait(); bool succ = upcxx::rpc(target_rank, std::bind(&this_type::do_rpc_set, this, k, v)).wait();
if(not succ) if(not succ)
throw std::runtime_error("RPC insert failed."); throw std::runtime_error("RPC set failed.");
} }
} }
std::pair<bool, value_type> operator[](const key_type &k) const { std::pair<bool, value_type> get(const key_type &k) const {
auto target_rank = find_rank_for_hash(find_hash_for_ele(k)); auto target_rank = get_rank_for_hash(get_hash_for_ele(k));
if(my_rank == target_rank) { if(my_rank == target_rank) {
return std::make_pair(true, *do_find(k)); return std::make_pair(true, *do_get(k));
} }
else { else {
auto res = upcxx::rpc(target_rank, std::bind(&this_type::do_rpc_find, this, k)).wait(); auto res = upcxx::rpc(target_rank, std::bind(&this_type::do_rpc_get, this, k)).wait();
if(not res.success) if(not res.success)
throw std::runtime_error("RPC find failed."); throw std::runtime_error("RPC get failed.");
return std::make_pair(res.found, res.val); return std::make_pair(res.found, res.val);
} }
} }
bool push_if_is_mine(const key_type &k, const value_type &v) { bool set_if_is_mine(const key_type &k, const value_type &v) {
auto target_rank = find_rank_for_hash(find_hash_for_ele(k)); auto target_rank = get_rank_for_hash(get_hash_for_ele(k));
if(my_rank == target_rank) { if(my_rank == target_rank) {
do_insert(k, v); do_set(k, v);
} }
return my_rank == target_rank; return my_rank == target_rank;
} }
std::pair<bool, value_type> find_if_is_mine(const key_type &k) { std::pair<bool, value_type> get_if_is_mine(const key_type &k) {
auto target_rank = find_rank_for_hash(find_hash_for_ele(k)); auto target_rank = get_rank_for_hash(get_hash_for_ele(k));
if(my_rank == target_rank) if(my_rank == target_rank)
return std::make_pair(true, *do_find(k)); return std::make_pair(true, *do_get(k));
else else
return std::make_pair(false, value_type{}); return std::make_pair(false, value_type{});
} }
private: private:
bool do_rpc_insert(key_type k, value_type v) { bool do_rpc_set(key_type k, value_type v) {
try { try {
do_insert(k, v); do_set(k, v);
return true; return true;
} }
catch(std::exception &e) { catch(std::exception &e) {
rlib::println(std::cerr, "Error: exception while executing rpc insert: ", e.what()); rlib::println(std::cerr, "Error: exception while executing rpc set: ", e.what());
return false; return false;
} }
} }
auto do_rpc_find(key_type k) const { auto do_rpc_get(key_type k) const {
try { try {
const auto *res = do_find(k, true); const auto *res = do_get(k, true);
if(res) if(res)
return rpc_find_result{true, true, *res}; return rpc_get_result{true, true, *res};
else else
return rpc_find_result{false, true, value_type{}}; return rpc_get_result{false, true, value_type{}};
} }
catch(std::exception &e) { catch(std::exception &e) {
rlib::println(std::cerr, "Error: exception while executing rpc find: ", e.what()); rlib::println(std::cerr, "Error: exception while executing rpc get: ", e.what());
return rpc_find_result{false, false, value_type{}}; return rpc_get_result{false, false, value_type{}};
} }
} }
private: private:
struct rpc_find_result { struct rpc_get_result {
bool found, success; bool found, success;
value_type val; value_type val;
}; };
void do_insert(const key_type &k, const value_type &v) { void do_set(const key_type &k, const value_type &v) {
auto &target_ls = find_slot(k); auto &target_ls = get_slot(k);
{ {
for(auto &ele : target_ls) { for(auto &ele : target_ls) {
if(equal_engine_type{}(ele.first, k)) { if(equal_engine_type{}(ele.first, k)) {
...@@ -127,8 +127,8 @@ private: ...@@ -127,8 +127,8 @@ private:
} }
} }
const value_type *do_find(const key_type &k, bool no_throw = false) const { const value_type *do_get(const key_type &k, bool no_throw = false) const {
const auto &target_ls = find_slot(k); const auto &target_ls = get_slot(k);
{ {
for(const auto &ele : target_ls) { for(const auto &ele : target_ls) {
if(equal_engine_type{}(ele.first, k)) if(equal_engine_type{}(ele.first, k))
...@@ -139,8 +139,8 @@ private: ...@@ -139,8 +139,8 @@ private:
throw std::out_of_range("Element not found."); throw std::out_of_range("Element not found.");
return nullptr; return nullptr;
} }
value_type *do_find(const key_type &k, bool no_throw = false) { value_type *do_get(const key_type &k, bool no_throw = false) {
auto &target_ls = find_slot(k); auto &target_ls = get_slot(k);
{ {
for(auto &ele : target_ls) { for(auto &ele : target_ls) {
if(equal_engine_type{}(ele.first, k)) if(equal_engine_type{}(ele.first, k))
...@@ -152,33 +152,33 @@ private: ...@@ -152,33 +152,33 @@ private:
return nullptr; return nullptr;
} }
const auto &find_slot(const key_type &k) const { const auto &get_slot(const key_type &k) const {
auto hash = find_hash_for_ele(k); auto hash = get_hash_for_ele(k);
if(my_rank != find_rank_for_hash(hash)) { if(my_rank != get_rank_for_hash(hash)) {
throw std::invalid_argument("This key doesn't belong to me."); throw std::invalid_argument("This key doesn't belong to me.");
} }
auto pos = find_local_slot_num_for_hash(hash); auto pos = get_local_slot_num_for_hash(hash);
return local_buf.at(pos); return local_buf.at(pos);
} }
auto &find_slot(const key_type &k) { auto &get_slot(const key_type &k) {
auto hash = find_hash_for_ele(k); auto hash = get_hash_for_ele(k);
if(my_rank != find_rank_for_hash(hash)) { if(my_rank != get_rank_for_hash(hash)) {
throw std::invalid_argument("This key doesn't belong to me."); throw std::invalid_argument("This key doesn't belong to me.");
} }
auto pos = find_local_slot_num_for_hash(hash); auto pos = get_local_slot_num_for_hash(hash);
return local_buf.at(pos); return local_buf.at(pos);
} }
private: private:
inline auto find_rank_for_hash(hash_type h) const { inline auto get_rank_for_hash(hash_type h) const {
return h % n_rank; return h % n_rank;
} }
inline auto find_local_slot_num_for_hash(hash_type h) const { inline auto get_local_slot_num_for_hash(hash_type h) const {
// The result is the same in all nodes. // The result is the same in all nodes.
const auto slot_per_node = local_buf.size(); const auto slot_per_node = local_buf.size();
return h / n_rank % slot_per_node; return h / n_rank % slot_per_node;
} }
inline auto find_hash_for_ele(const key_type &k) const { inline auto get_hash_for_ele(const key_type &k) const {
hash_type h = hash_engine_type{}(k); hash_type h = hash_engine_type{}(k);
return h; return h;
} }
......
...@@ -23,10 +23,10 @@ struct HashMap { ...@@ -23,10 +23,10 @@ struct HashMap {
} }
bool insert(const kmer_pair &kmer) { bool insert(const kmer_pair &kmer) {
return real_db.push_if_is_mine(kmer.kmer, kmer); return real_db.set_if_is_mine(kmer.kmer, kmer);
} }
bool find(const pkmer_t &key_kmer, kmer_pair &val_kmer) { bool find(const pkmer_t &key_kmer, kmer_pair &val_kmer) {
auto res = real_db[key_kmer]; auto res = real_db.get_if_is_mine(key_kmer);
if(res.first) if(res.first)
val_kmer = res.second; val_kmer = res.second;
return res.first; return res.first;
......
...@@ -6,7 +6,7 @@ int main() { ...@@ -6,7 +6,7 @@ int main() {
upcxx::init(); upcxx::init();
kv_store<double, int> kvs(upcxx::rank_me(), upcxx::rank_n()); kv_store<double, int> kvs(upcxx::rank_me(), upcxx::rank_n());
try { try {
auto [succ, val] = kvs[1.23]; auto [succ, val] = kvs.get(1.23);
if(succ) if(succ)
rlib::println("FUCK! succ! val is", val); rlib::println("FUCK! succ! val is", val);
} }
...@@ -16,16 +16,16 @@ int main() { ...@@ -16,16 +16,16 @@ int main() {
upcxx::barrier(); // without this barrier, the access above may success at rank1. upcxx::barrier(); // without this barrier, the access above may success at rank1.
if(upcxx::rank_me() == 0) { if(upcxx::rank_me() == 0) {
kvs.push(1.23, 666); kvs.set(1.23, 666);
kvs.push(6.666, 123); kvs.set(6.666, 123);
kvs.push(0.01, 99); kvs.set(0.01, 99);
kvs.push(1., 111); kvs.set(1., 111);
rlib::println("pushed!"); rlib::println("pushed!");
} }
upcxx::barrier(); upcxx::barrier();
auto [succ, val] = kvs[6.666]; auto [succ, val] = kvs.get(6.666);
if(not succ) if(not succ)
rlib::println("not succ!"); rlib::println("not succ!");
rlib::println(upcxx::rank_me(), val); rlib::println(upcxx::rank_me(), val);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment