forked from jaredhoberock/bulk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecomposition.hpp
219 lines (173 loc) · 4.33 KB
/
decomposition.hpp
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
#pragma once
#include <thrust/pair.h>
template<typename Size>
class trivial_decomposition
{
public:
typedef Size size_type;
typedef thrust::pair<size_type,size_type> range;
__host__ __device__
trivial_decomposition()
: m_n(0)
{}
__host__ __device__
trivial_decomposition(size_type n)
: m_n(n)
{}
__host__ __device__
range operator[](size_type) const
{
return range(0, n());
}
__host__ __device__
size_type size() const
{
return 1;
}
// XXX think of a better name for this
__host__ __device__
size_type n() const
{
return m_n;
}
private:
Size m_n;
};
template<typename Size>
__host__ __device__
trivial_decomposition<Size> make_trivial_decomposition(Size n)
{
return trivial_decomposition<Size>(n);
}
template<typename Size>
class blocked_decomposition
{
public:
typedef Size size_type;
typedef thrust::pair<size_type,size_type> range;
__host__ __device__
blocked_decomposition()
: m_n(0),
m_block_size(0),
m_num_partitions(0)
{}
__host__ __device__
blocked_decomposition(size_type n, Size block_size)
: m_n(n),
m_block_size(block_size),
m_num_partitions((n + block_size - 1) / block_size)
{}
__host__ __device__
range operator[](size_type i) const
{
size_type first = i * m_block_size;
size_type last = thrust::min(m_n, first + m_block_size);
return range(first, last);
}
__host__ __device__
size_type size() const
{
return m_num_partitions;
}
// XXX think of a better name for this
__host__ __device__
size_type n() const
{
return m_n;
}
private:
Size m_n;
Size m_block_size;
Size m_num_partitions;
};
template<typename Size>
__host__ __device__
blocked_decomposition<Size> make_blocked_decomposition(Size n, Size block_size)
{
return blocked_decomposition<Size>(n,block_size);
}
template<typename Size>
class uniform_decomposition
: public blocked_decomposition<Size>
{
private:
typedef blocked_decomposition<Size> super_t;
public:
__host__ __device__
uniform_decomposition()
: super_t()
{}
__host__ __device__
uniform_decomposition(Size n, Size num_partitions)
: super_t(n, n / num_partitions)
{}
};
template<typename Size>
__host__ __device__
uniform_decomposition<Size> make_uniform_decomposition(Size n, Size num_partitions)
{
return uniform_decomposition<Size>(n,num_partitions);
}
template<typename Size>
class aligned_decomposition
{
public:
typedef Size size_type;
typedef thrust::pair<size_type,size_type> range;
__host__ __device__
aligned_decomposition()
: m_n(0),
m_num_partitions(0),
m_tile_size(0)
{}
__host__ __device__
aligned_decomposition(Size n, Size num_partitions, Size aligned_size)
: m_n(n),
m_num_partitions(num_partitions),
m_tile_size(aligned_size)
{
size_type num_tiles = (n + m_tile_size - 1) / m_tile_size;
m_num_tiles_per_partition = num_tiles / size();
m_last_partial_tile_size = num_tiles % size();
}
__host__ __device__
range operator[](Size i) const
{
range result = range_in_tiles(i);
result.first *= m_tile_size;
result.second = thrust::min<size_type>(m_n, result.second * m_tile_size);
return result;
}
__host__ __device__
size_type size() const
{
return m_num_partitions;
}
// XXX think of a better name for this
__host__ __device__
size_type n() const
{
return m_n;
}
private:
__host__ __device__
range range_in_tiles(size_type i) const
{
range result;
result.first = m_num_tiles_per_partition * i;
result.first += thrust::min<size_type>(i, m_last_partial_tile_size);
result.second = result.first + m_num_tiles_per_partition + (i < m_last_partial_tile_size);
return result;
}
size_type m_n;
size_type m_num_partitions;
size_type m_num_tiles_per_partition;
size_type m_tile_size;
size_type m_last_partial_tile_size;
};
template<typename Size>
__host__ __device__
aligned_decomposition<Size> make_aligned_decomposition(Size n, Size num_partitions, Size aligned_size)
{
return aligned_decomposition<Size>(n,num_partitions,aligned_size);
}