-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbulk_map.cu
More file actions
364 lines (307 loc) · 12.7 KB
/
bulk_map.cu
File metadata and controls
364 lines (307 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include <thrust/device_vector.h>
#include <thrust/iterator/zip_iterator.h>
#include <thrust/sort.h>
#include <thrust/merge.h>
#include <thrust/unique.h>
#include <thrust/set_operations.h>
#include <thrust/inner_product.h>
#include <thrust/detail/internal_functional.h> // for thrust::detail::not2
// TODO consider bulk_unordered_map with a hash(key) for faster duplicate testing?
// TODO keep a smaller auxillary bulk_map around until its large enough for merge (or forced to merge)?
// TODO consider non-member functions (e.g. union(A,B,C), intersection(A,B,C))
// XXX provide insert_intersection method?
// XXX why is map<K,V>::value_type pair<const K, V>
// XXX Note: "value" is interpreted as pair<Key,T> in the STL, rather than just T
template <typename Compare>
struct compare_tuple0
{
Compare comp;
compare_tuple0(Compare comp)
: comp(comp)
{}
template <typename Tuple1, typename Tuple2>
__host__ __device__
bool operator()(const Tuple1& a, const Tuple2& b) const
{
return comp(thrust::get<0>(a), thrust::get<0>(b));
}
};
template <typename InputIterator1,
typename InputIterator2,
typename InputIterator3,
typename InputIterator4,
typename OutputIterator1,
typename OutputIterator2,
typename StrictWeakCompare>
thrust::pair<OutputIterator1,OutputIterator2>
merge_by_key(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2,
InputIterator3 first3, InputIterator3 last3,
InputIterator4 first4,
OutputIterator1 output1,
OutputIterator2 output2,
StrictWeakCompare comp)
{
thrust::tuple<OutputIterator1,OutputIterator2> output =
thrust::merge
(thrust::make_zip_iterator(thrust::make_tuple(first1, first2)),
thrust::make_zip_iterator(thrust::make_tuple(last1, first2)),
thrust::make_zip_iterator(thrust::make_tuple(first3, first4)),
thrust::make_zip_iterator(thrust::make_tuple(last3, first4)),
thrust::make_zip_iterator(thrust::make_tuple(output1, output2)),
compare_tuple0<StrictWeakCompare>(comp)).get_iterator_tuple();
return thrust::make_pair(thrust::get<0>(output), thrust::get<1>(output));
}
template <typename InputIterator1,
typename InputIterator2,
typename InputIterator3,
typename InputIterator4,
typename OutputIterator1,
typename OutputIterator2,
typename StrictWeakCompare>
thrust::pair<OutputIterator1,OutputIterator2>
set_union_by_key(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2,
InputIterator3 first3, InputIterator3 last3,
InputIterator4 first4,
OutputIterator1 output1,
OutputIterator2 output2,
StrictWeakCompare comp)
{
thrust::tuple<OutputIterator1,OutputIterator2> output =
thrust::set_union
(thrust::make_zip_iterator(thrust::make_tuple(first1, first2)),
thrust::make_zip_iterator(thrust::make_tuple(last1, first2)),
thrust::make_zip_iterator(thrust::make_tuple(first3, first4)),
thrust::make_zip_iterator(thrust::make_tuple(last3, first4)),
thrust::make_zip_iterator(thrust::make_tuple(output1, output2)),
compare_tuple0<StrictWeakCompare>(comp)).get_iterator_tuple();
return thrust::make_pair(thrust::get<0>(output), thrust::get<1>(output));
}
template <typename InputIterator1,
typename InputIterator2,
typename InputIterator3,
typename InputIterator4,
typename OutputIterator1,
typename OutputIterator2,
typename StrictWeakCompare>
thrust::pair<OutputIterator1,OutputIterator2>
set_difference_by_key(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2,
InputIterator3 first3, InputIterator3 last3,
InputIterator4 first4,
OutputIterator1 output1,
OutputIterator2 output2,
StrictWeakCompare comp)
{
thrust::tuple<OutputIterator1,OutputIterator2> output =
thrust::set_difference
(thrust::make_zip_iterator(thrust::make_tuple(first1, first2)),
thrust::make_zip_iterator(thrust::make_tuple(last1, first2)),
thrust::make_zip_iterator(thrust::make_tuple(first3, first4)),
thrust::make_zip_iterator(thrust::make_tuple(last3, first4)),
thrust::make_zip_iterator(thrust::make_tuple(output1, output2)),
compare_tuple0<StrictWeakCompare>(comp)).get_iterator_tuple();
return thrust::make_pair(thrust::get<0>(output), thrust::get<1>(output));
}
template <typename InputIterator,
typename BinaryPredicate>
size_t unique_count(InputIterator first, InputIterator last, BinaryPredicate pred)
{
// XXX violates InputIterator semantics
if (first == last)
return 0;
else
return thrust::inner_product(first, last - 1,
first + 1,
size_t(1),
thrust::plus<size_t>(),
thrust::detail::not2(pred));
}
template <typename KeyContainer,
typename ValueContainer, // MappedContainer? TContainer? ValueContainer?
typename KeyCompare = thrust::less<typename KeyContainer::value_type> >
class bulk_map
{
public:
typedef KeyContainer key_container_type; // like std::priority_queue
typedef ValueContainer value_container_type;
// XXX should we expose these?
// these types have no STL analog, but are similar to Python dictionary iterators (i.e. dict.keys() dict.values())
typedef typename key_container_type::iterator key_iterator;
typedef typename value_container_type::iterator value_iterator;
typedef typename key_container_type::const_iterator const_key_iterator;
typedef typename value_container_type::const_iterator const_value_iterator;
// XXX make these return pairs instead w/ transform_iterator<zip_iterator<...>, ... > ?
typedef thrust::zip_iterator<thrust::tuple<key_iterator, value_iterator> > iterator;
typedef thrust::zip_iterator<thrust::tuple<const_key_iterator, const_value_iterator> > const_iterator;
typedef KeyCompare key_compare; // like std::map
typedef typename KeyContainer::value_type key_type; // like std::map or std::unordered_map
typedef typename ValueContainer::value_type mapped_type; // like std::map or std::unordered_map
// XXX is this the right value type?
//typedef thrust::pair<const key_type, mapped_type> value_type; // like std::map or std::unordered_map
typedef thrust::tuple<key_type, mapped_type> value_type;
// TODO reverse_iterator, const_reverse_iterator
// TODO value_compare
protected:
KeyContainer m_keys;
ValueContainer m_vals;
KeyCompare m_comp;
void normalize(void)
{
// sort pairs and remove duplicates
thrust::sort_by_key(m_keys.begin(), m_keys.end(), m_vals.begin(), m_comp);
m_keys.resize(thrust::unique_by_key(m_keys.begin(), m_keys.end(), m_vals.begin(), thrust::detail::not2(m_comp)).first - m_keys.begin());
}
void check_sizes(void) const
{
if (m_keys.size() != m_vals.size())
throw std::invalid_argument("key and value vectors must have same size");
}
public:
bulk_map(void)
{}
// TODO enable sorting bypass w/ special tag? or provide bulk_map_view<KeyRange,ValueRange, .. >
bulk_map(const KeyContainer& keys,
const ValueContainer& values,
const KeyCompare& comp = KeyCompare())
: m_keys(keys), m_vals(values), m_comp(comp)
{
check_sizes();
normalize();
}
template <typename InputIterator1, typename InputIterator2>
bulk_map(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
const KeyCompare& comp = KeyCompare())
: m_keys(first1, last1), m_vals(first2, last2), m_comp(comp)
{
check_sizes();
normalize();
}
// TODO compare src->temp/swap approach against copy/temp->this approach
void insert(const bulk_map& other)
{
// XXX optimize when *this < other or other < *this or this == &other
KeyContainer temp_keys(size() + other.size()); // XXX fill value? ideally we'd use uninitialized storage w/ special iterator adaptor
ValueContainer temp_vals(size() + other.size());
// note: other's keys must come first
temp_keys.resize
(set_union_by_key(other.m_keys.begin(), other.m_keys.end(),
other.m_vals.begin(),
m_keys.begin(), m_keys.end(),
m_vals.begin(),
temp_keys.begin(),
temp_vals.begin(),
m_comp).first - temp_keys.begin());
temp_vals.resize(temp_keys.size());
m_keys.swap(temp_keys);
m_vals.swap(temp_vals);
}
void erase(const bulk_map& other)
{
// XXX optimize when *this < other or other < *this or this == &other
KeyContainer temp_keys(m_keys);
ValueContainer temp_vals(m_vals);
m_keys.resize
(set_difference_by_key(temp_keys.begin(), temp_keys.end(),
temp_vals.begin(),
other.m_keys.begin(), other.m_keys.end(),
other.m_vals.begin(),
m_keys.begin(),
m_vals.begin(),
m_comp).first - m_keys.begin());
m_vals.resize(m_keys.size());
}
// XXX is combine the best word? want something that implies two-ness instead of 1 + many
template <typename BinaryFunction>
void combine(const bulk_map& other, BinaryFunction binary_op)
{
// XXX optimize when *this < other or other < *this or this == &other
KeyContainer temp_keys(size() + other.size()); // XXX fill value? ideally we'd use uninitialized storage w/ special iterator adaptor
ValueContainer temp_vals(size() + other.size());
// note: other's keys must come first
merge_by_key(other.m_keys.begin(), other.m_keys.end(),
other.m_vals.begin(),
m_keys.begin(), m_keys.end(),
m_vals.begin(),
temp_keys.begin(),
temp_vals.begin(),
m_comp);
size_t num_unique = unique_count(temp_keys.begin(), temp_keys.end(), thrust::detail::not2(m_comp));
m_keys.resize(num_unique);
m_vals.resize(num_unique);
thrust::reduce_by_key(temp_keys.begin(), temp_keys.end(),
temp_vals.begin(),
m_keys.begin(),
m_vals.begin(),
thrust::detail::not2(m_comp),
binary_op);
}
size_t size(void) const
{
return m_keys.size();
}
void shrink_to_fit(void)
{
m_keys.shrink_to_fit();
m_vals.shrink_to_fit();
}
iterator begin(void)
{
return iterator(thrust::make_tuple(m_keys.begin(), m_vals.begin()));
}
iterator cbegin(void) const
{
return const_iterator(thrust::make_tuple(m_keys.cbegin(), m_vals.cbegin()));
}
iterator begin(void) const
{
return cbegin();
}
iterator end(void)
{
return iterator(thrust::make_tuple(m_keys.end(), m_vals.end()));
}
iterator cend(void) const
{
return const_iterator(thrust::make_tuple(m_keys.cend(), m_vals.cend()));
}
iterator end(void) const
{
return cend();
}
};
template <typename BulkMap>
void print(std::string s, BulkMap& m)
{
std::cout << s << " :";
for (typename BulkMap::const_iterator iter = m.begin(); iter != m.end(); ++iter)
{
typedef typename BulkMap::value_type T;
T x = *iter;
std::cout << " (" << thrust::get<0>(x) << "," << thrust::get<1>(x) << ")";
}
std::cout << std::endl;
}
int main(void)
{
int K1[] = {3,0,9,3,8,2,4,0,9,8};
int V1[] = {1,1,1,1,1,1,1,1,1,1};
size_t N1 = sizeof(K1) / sizeof(*K1);
int K2[] = {5,3,0,6,2,1,4,7,2};
int V2[] = {2,2,2,2,2,2,2,2,2};
size_t N2 = sizeof(K2) / sizeof(*K2);
typedef thrust::device_vector<int> KeyContainer;
typedef thrust::device_vector<int> ValueContainer;
typedef bulk_map<KeyContainer,ValueContainer> BulkMap;
thrust::plus<int> Op;
BulkMap A(K1, K1 + N1, V1, V1 + N1);
BulkMap B(K2, K2 + N2, V2, V2 + N2);
print("A",A);
print("B",B);
{ BulkMap C(A); C.insert(B); print("A.insert(B) ",C); }
{ BulkMap C(A); C.erase(B); print("A.erase(B) ",C); }
{ BulkMap C(A); C.combine(B,Op); print("A.combine(B,+)",C); }
return 0;
}