LSLab
set.h
Go to the documentation of this file.
1 
4 #include "detail/slab_node.h"
5 #include "detail/traverse.h"
6 #include "device_allocator.h"
7 #include <thrust/device_vector.h>
8 #include <cuda.h>
9 #include <cuda/std/utility>
10 #include "hash.h"
11 #include "warp_mutex.h"
12 #include "detail/set_kernels.h"
13 
14 #pragma once
15 
16 namespace lslab {
17 
21 template<typename K, typename Allocator = device_allocator<detail::set_node<K>>, typename Hash = hash<K>>
22 class set {
23 public:
24 
26 
27  LSLAB_HOST set() : set(10) {
28 
29  }
30 
31  LSLAB_HOST set(unsigned n_log_2) : number_of_buckets_log_2(n_log_2) {
32  size_t size = 1 << n_log_2;
33  cudaMalloc(&lock_table, sizeof(warp_mutex) * size);
34 
35  cudaMemset(lock_table, 0, sizeof(warp_mutex) * size);
36 
37  cudaMalloc(&buckets_array, sizeof(detail::set_node<K>) * size);
38 
39  cudaMemset(buckets_array, 0, sizeof(detail::set_node<K>) * size);
40  }
41 
42  LSLAB_HOST set(unsigned n_log_2, Allocator&& a) : number_of_buckets_log_2(n_log_2), alloc(a) {
43  size_t size = 1 << n_log_2;
44  cudaMalloc(&lock_table, sizeof(warp_mutex) * size);
45 
46  cudaMemset(lock_table, 0, sizeof(warp_mutex) * size);
47 
48  cudaMalloc(&buckets_array, sizeof(detail::set_node<K>) * size);
49 
50  cudaMemset(buckets_array, 0, sizeof(detail::set_node<K>) * size);
51  }
52 
53  LSLAB_HOST_DEVICE set(warp_mutex* lt, detail::set_node<K>* s, unsigned n_log_2) : lock_table(lt), buckets_array(s), number_of_buckets_log_2(n_log_2) {
54 
55  }
56 
57  LSLAB_DEVICE set(warp_mutex* lt, detail::set_node<K>* s, unsigned n_log_2, Allocator&& a) : lock_table(lt), buckets_array(s), number_of_buckets_log_2(n_log_2), alloc(a) {
58 
59  }
60 
61  LSLAB_HOST_DEVICE ~set() {
62  }
63 
64  LSLAB_DEVICE bool contains(const K& key, bool thread_mask = true) {
65 
66  size_t hash = Hash{}(key);
67  hash &= ((1 << number_of_buckets_log_2) - 1);
68 
69  bool result = false;
70  detail::traverse<Allocator, detail::OPERATION_TYPE::FIND>{}(lock_table, buckets_array, key, result, alloc, hash, thread_mask);
71  return result;
72  }
73 
74  LSLAB_DEVICE bool insert(const K& key, bool thread_mask = true) {
76  traverse_t t;
77  size_t hash = Hash{}(key) & ((1 << number_of_buckets_log_2) - 1);
78  bool result = false;
79  t.template operator()<K>(lock_table, buckets_array, key, result, alloc, hash, thread_mask);
80  return result;
81  }
82 
83  LSLAB_DEVICE bool remove(const K& key, bool thread_mask = true) {
85  traverse_t t;
86  size_t hash = Hash{}(key) & ((1 << number_of_buckets_log_2) - 1);
87  bool result = false;
88  t.template operator()<K>(lock_table, buckets_array, key, result, alloc, hash, thread_mask);
89  return result;
90  }
91 
92  template<int block_size = 256>
93  LSLAB_HOST void contains(const K* keys, bool* output, size_t size, cudaStream_t stream = 0x0) {
94  set_kernels::contains_<block_size, this_t, K><<<(size + block_size - 1) / block_size, block_size, 0, stream>>>(*this, keys, output, size);
95  }
96 
97  template<int block_size = 256>
98  LSLAB_HOST void contains(const thrust::device_vector<K>& keys, thrust::device_vector<bool>& output, cudaStream_t stream = 0x0) {
99  size_t size = keys.size();
100  this->template contains<block_size>(keys.data().get(), output.data().get(), size, stream);
101  }
102 
103  template<int block_size = 256>
104  LSLAB_HOST void insert(const K* keys, bool* output, size_t size, cudaStream_t stream = 0x0) {
105  set_kernels::insert_<block_size, this_t, K><<<(size + block_size - 1) / block_size, block_size, 0, stream>>>(*this, keys, output, size);
106  }
107 
108  template<int block_size = 256>
109  LSLAB_HOST void insert(const thrust::device_vector<K>& keys, thrust::device_vector<bool>& output, cudaStream_t stream = 0x0) {
110  size_t size = keys.size();
111  this->template insert<block_size>(keys.data().get(), output.data().get(), size, stream);
112  }
113 
114  template<int block_size = 256>
115  LSLAB_HOST void remove(const K* keys, bool* output, size_t size, cudaStream_t stream = 0x0) {
116  set_kernels::remove_<block_size, this_t, K><<<(size + block_size - 1) / block_size, block_size, 0, stream>>>(*this, keys, output, size);
117  }
118 
119  template<int block_size = 256>
120  LSLAB_HOST void remove(const thrust::device_vector<K>& keys, thrust::device_vector<bool>& output, cudaStream_t stream = 0x0) {
121  size_t size = keys.size();
122  this->template remove<block_size>(keys.data().get(), output.data().get(), size, stream);
123  }
124 
125  LSLAB_HOST_DEVICE unsigned buckets() const {
126  return 1 << number_of_buckets_log_2;
127  }
128 
129 private:
130  warp_mutex* lock_table;
131  detail::set_node<K>* buckets_array;
132  unsigned number_of_buckets_log_2;
133  Allocator alloc;
134 };
135 
136 }
Definition: set.h:22
Definition: traverse.h:22
Definition: hash.h:11
Definition: warp_mutex.h:13