Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cs267
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
recolic-hust
cs267
Commits
a260d4af
There was an error fetching the commit references. Please try again later.
Commit
a260d4af
authored
6 years ago
by
Recolic Keghart
Browse files
Options
Downloads
Patches
Plain Diff
better lock
parent
06fabddd
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
hw3/Makefile
+5
-1
5 additions, 1 deletion
hw3/Makefile
hw3/dist_kv_store.hpp
+29
-17
29 additions, 17 deletions
hw3/dist_kv_store.hpp
hw3/hash_map.hpp
+5
-1
5 additions, 1 deletion
hw3/hash_map.hpp
with
39 additions
and
19 deletions
hw3/Makefile
+
5
−
1
View file @
a260d4af
# source `modules.sh` before attempting to build or run.
CXX
?=
upcxx
# The hashtable slots_per_node will be int(PASSED_SIZE/SIZE_HINT_FACTOR).
# Make it larger if you have more nodes!
# If SIZEHINT is smaller, the program is always quicker, and always consumes more RAM.
SIZEHINT
?=
3.0
# Single process
CXX
=
upcxx
-O3
-std
=
c++14
-DHASHMAP_SIZE_HINT_FACTOR
=
$(
SIZEHINT
)
# upcxx-meta PPFLAGS are really CFLAGS to be used during compilation
# upcxx-meta LDFLAGS are really CFLAGS to be used during linking
...
...
This diff is collapsed.
Click to expand it.
hw3/dist_kv_store.hpp
+
29
−
17
View file @
a260d4af
...
...
@@ -14,8 +14,8 @@
#include
<mutex>
#include
"rlib.stdio.min.hpp"
#ifndef R267_KVS_SLOT_PER_NODE
#define R267_KVS_SLOT_PER_NODE 1024
#ifndef R267_KVS_
DEF_
SLOT_PER_NODE
#define R267_KVS_
DEF_
SLOT_PER_NODE 1024
#endif
template
<
typename
KeyType
,
typename
ValueType
,
typename
HashEngineType
=
std
::
hash
<
KeyType
>,
typename
EqualEngineType
=
std
::
equal_to
<
KeyType
>>
...
...
@@ -25,13 +25,18 @@ public:
using
key_type
=
KeyType
;
using
value_type
=
ValueType
;
using
hash_engine_type
=
HashEngineType
;
//using hash_type = typename hash_engine_type::result_type;
using
hash_type
=
std
::
size_t
;
using
hash_type
=
decltype
(
hash_engine_type
{}(
key_type
{}));
//using hash_type = std::size_t;
static_assert
(
std
::
is_same
<
hash_type
,
std
::
size_t
>
(),
"Invalid hash function: ISO C++17 standard requires that hash_type is always std::size_t"
);
using
equal_engine_type
=
EqualEngineType
;
// kv_type should be default_constructable.
kv_store
(
size_t
my_rank
,
size_t
n_rank
)
:
local_buf
(
slot_per_node
),
my_rank
(
my_rank
),
n_rank
(
n_rank
)
{
private
:
using
slot_type
=
std
::
list
<
std
::
pair
<
key_type
,
value_type
>>
;
public
:
kv_store
(
size_t
my_rank
,
size_t
n_rank
,
size_t
slot_per_node
=
R267_KVS_DEF_SLOT_PER_NODE
)
:
local_buf
(
slot_per_node
),
mut_local_buf
(
slot_per_node
),
my_rank
(
my_rank
),
n_rank
(
n_rank
)
{
}
void
push
(
const
key_type
&
k
,
const
value_type
&
v
)
{
...
...
@@ -90,7 +95,9 @@ private:
};
void
do_insert
(
const
key_type
&
k
,
const
value_type
&
v
)
{
auto
&
target_ls
=
find_slot
(
k
);
auto
target_ls_and_lock
=
find_slot
(
k
);
auto
&
target_ls
=
target_ls_and_lock
.
first
;
auto
&
local_buf_mut
=
target_ls_and_lock
.
second
;
{
std
::
lock_guard
<
std
::
mutex
>
_
(
local_buf_mut
);
for
(
auto
&
ele
:
target_ls
)
{
...
...
@@ -105,7 +112,9 @@ private:
}
const
value_type
&
do_find
(
const
key_type
&
k
)
const
{
const
auto
&
target_ls
=
find_slot
(
k
);
auto
target_ls_and_lock
=
find_slot
(
k
);
const
auto
&
target_ls
=
target_ls_and_lock
.
first
;
auto
&
local_buf_mut
=
target_ls_and_lock
.
second
;
{
std
::
lock_guard
<
std
::
mutex
>
_
(
local_buf_mut
);
for
(
const
auto
&
ele
:
target_ls
)
{
...
...
@@ -116,7 +125,9 @@ private:
throw
std
::
out_of_range
(
"Element not found."
);
}
value_type
&
do_find
(
const
key_type
&
k
)
{
auto
&
target_ls
=
find_slot
(
k
);
auto
target_ls_and_lock
=
find_slot
(
k
);
auto
&
target_ls
=
target_ls_and_lock
.
first
;
auto
&
local_buf_mut
=
target_ls_and_lock
.
second
;
{
std
::
lock_guard
<
std
::
mutex
>
_
(
local_buf_mut
);
for
(
auto
&
ele
:
target_ls
)
{
...
...
@@ -127,28 +138,30 @@ private:
throw
std
::
out_of_range
(
"Element not found."
);
}
auto
&
find_slot
(
const
key_type
&
k
)
{
auto
find_slot
(
const
key_type
&
k
)
const
{
auto
hash
=
find_hash_for_ele
(
k
);
if
(
my_rank
!=
find_rank_for_hash
(
hash
))
{
throw
std
::
invalid_argument
(
"This key doesn't belong to me."
);
}
return
local_buf
.
at
(
find_local_slot_num_for_hash
(
hash
));
auto
pos
=
find_local_slot_num_for_hash
(
hash
);
return
std
::
pair
<
const
slot_type
&
,
std
::
mutex
&>
{
std
::
cref
(
local_buf
.
at
(
pos
)),
std
::
ref
(
mut_local_buf
.
at
(
pos
))};
}
const
auto
&
find_slot
(
const
key_type
&
k
)
const
{
auto
find_slot
(
const
key_type
&
k
)
{
auto
hash
=
find_hash_for_ele
(
k
);
if
(
my_rank
!=
find_rank_for_hash
(
hash
))
{
throw
std
::
invalid_argument
(
"This key doesn't belong to me."
);
}
return
local_buf
.
at
(
find_local_slot_num_for_hash
(
hash
));
auto
pos
=
find_local_slot_num_for_hash
(
hash
);
return
std
::
pair
<
slot_type
&
,
std
::
mutex
&>
{
std
::
ref
(
local_buf
.
at
(
pos
)),
std
::
ref
(
mut_local_buf
.
at
(
pos
))};
}
private
:
inline
auto
find_rank_for_hash
(
hash_type
h
)
const
{
return
h
%
n_rank
;
}
inline
auto
find_local_slot_num_for_hash
(
hash_type
h
)
const
{
// The result is the same in all nodes.
const
auto
slot_per_node
=
local_buf
.
size
();
return
h
/
n_rank
%
slot_per_node
;
}
inline
auto
find_hash_for_ele
(
const
key_type
&
k
)
const
{
...
...
@@ -156,10 +169,9 @@ private:
return
h
;
}
std
::
vector
<
s
td
::
list
<
std
::
pair
<
key_type
,
value_type
>>
>
local_buf
;
static
constexpr
size_t
slot_per_node
=
R267_KVS_SLOT_PER_NODE
;
std
::
vector
<
s
lot_type
>
local_buf
;
mutable
std
::
vector
<
std
::
mutex
>
mut_local_buf
;
size_t
my_rank
,
n_rank
;
mutable
std
::
mutex
local_buf_mut
;
// TODO: Every vector entry need a distinct lock! DO NOT USE GLOBAL LOCK!
};
#endif
...
...
This diff is collapsed.
Click to expand it.
hw3/hash_map.hpp
+
5
−
1
View file @
a260d4af
...
...
@@ -13,8 +13,12 @@ namespace std {
};
}
#ifndef HASHMAP_SIZE_HINT_FACTOR
#define HASHMAP_SIZE_HINT_FACTOR 8
#endif
struct
HashMap
{
HashMap
(
std
::
size_t
)
:
real_db
(
upcxx
::
rank_me
(),
upcxx
::
rank_n
())
{
HashMap
(
std
::
size_t
size_hint
)
:
real_db
(
upcxx
::
rank_me
(),
upcxx
::
rank_n
()
,
(
float
)
size_hint
/
HASHMAP_SIZE_HINT_FACTOR
)
{
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment