File: hilbert_curve.h

package info (click to toggle)
python-escript 5.0-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 87,772 kB
  • ctags: 49,550
  • sloc: python: 585,488; cpp: 133,173; ansic: 18,675; xml: 3,283; sh: 690; makefile: 215
file content (269 lines) | stat: -rw-r--r-- 10,101 bytes parent folder | download | duplicates (4)
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
/*
 *  Copyright 2008-2009 NVIDIA Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/*
 * Modifications to this file:
 * Copyright (c) 2014-2015, The University of Queensland
 * Licensed under the Apache License, Version 2.0.
 *
 */


#include <cusp/exception.h>
#include <cusp/array1d.h>

#include <thrust/iterator/constant_iterator.h>
#include <thrust/gather.h>
#include <thrust/sort.h>
#include <thrust/transform.h>

namespace cusp
{
namespace graph
{
namespace detail
{
namespace host
{

// Tables and Hilbert transform codes adapted from HSFC implementation in Zoltan v3.601
static unsigned const int IMAX = ~(0U);
static const int MAXLEVEL_2d = 28; // 56 bits of significance, 28 per dimension
static const int MAXLEVEL_3d = 19; // 56 bits of significance, 18+ per dimension

static unsigned const int idata2d[] =  // 2 dimension to nkey conversion
{   0, 3, 1, 2,
    0, 1, 3, 2,
    2, 3, 1, 0,
    2, 1, 3, 0
};

static unsigned const int istate2d[] = // 2 dimension to nkey state transitions
{   1, 2, 0, 0,
    0, 1, 3, 1,
    2, 0, 2, 3,
    3, 3, 1, 2
};

static unsigned const idata3d [] = {   // 3 dimension to nkey conversion
    0,  7,  3,  4,  1,  6,  2,  5,
    0,  1,  3,  2,  7,  6,  4,  5,
    0,  3,  7,  4,  1,  2,  6,  5,
    2,  3,  5,  4,  1,  0,  6,  7,
    4,  5,  3,  2,  7,  6,  0,  1,
    4,  7,  3,  0,  5,  6,  2,  1,
    6,  7,  5,  4,  1,  0,  2,  3,
    0,  1,  7,  6,  3,  2,  4,  5,
    2,  1,  5,  6,  3,  0,  4,  7,
    6,  1,  5,  2,  7,  0,  4,  3,
    0,  7,  1,  6,  3,  4,  2,  5,
    2,  1,  3,  0,  5,  6,  4,  7,
    4,  7,  5,  6,  3,  0,  2,  1,
    4,  5,  7,  6,  3,  2,  0,  1,
    6,  1,  7,  0,  5,  2,  4,  3,
    0,  3,  1,  2,  7,  4,  6,  5,
    2,  3,  1,  0,  5,  4,  6,  7,
    6,  7,  1,  0,  5,  4,  2,  3,
    2,  5,  1,  6,  3,  4,  0,  7,
    4,  3,  7,  0,  5,  2,  6,  1,
    4,  3,  5,  2,  7,  0,  6,  1,
    6,  5,  1,  2,  7,  4,  0,  3,
    2,  5,  3,  4,  1,  6,  0,  7,
    6,  5,  7,  4,  1,  2,  0,  3
};

static unsigned const istate3d [] = { // 3 dimension to nkey state transitions
    1,  6,  3,  4,  2,  5,  0,  0,
    0,  7,  8,  1,  9,  4,  5,  1,
    15, 22, 23, 20,  0,  2, 19,  2,
    3, 23,  3, 15,  6, 20, 16, 22,
    11,  4, 12,  4, 20,  1, 22, 13,
    22, 12, 20, 11,  5,  0,  5, 19,
    17,  0,  6, 21,  3,  9,  6,  2,
    10,  1, 14, 13, 11,  7, 12,  7,
    8,  9,  8, 18, 14, 12, 10, 11,
    21,  8,  9,  9,  1,  6, 17,  7,
    7, 17, 15, 12, 16, 13, 10, 10,
    11, 14,  9,  5, 11, 22,  0,  8,
    18,  5, 12, 10, 19,  8, 12, 20,
    8, 13, 19,  7,  5, 13, 18,  4,
    23, 11,  7, 17, 14, 14,  6,  1,
    2, 18, 10, 15, 21, 19, 20, 15,
    16, 21, 17, 19, 16,  2,  3, 18,
    6, 10, 16, 14, 17, 23, 17, 15,
    18, 18, 21,  8, 17,  7, 13, 16,
    3,  4, 13, 16, 19, 19,  2,  5,
    16, 13, 20, 20,  4,  3, 15, 12,
    9, 21, 18, 21, 15, 14, 23, 10,
    22, 22,  6,  1, 23, 11,  4,  3,
    14, 23,  2,  9, 22, 23, 21,  0
};

static const unsigned *d2d[]= {idata2d,  idata2d  +4, idata2d  +8, idata2d  +12};
static const unsigned *s2d[]= {istate2d, istate2d +4, istate2d +8, istate2d +12};

static const unsigned int *d3d[] =
{   idata3d,      idata3d +8,   idata3d +16,  idata3d +24,
    idata3d +32,  idata3d +40,  idata3d +48,  idata3d +56,
    idata3d +64,  idata3d +72,  idata3d +80,  idata3d +88,
    idata3d +96,  idata3d +104, idata3d +112, idata3d +120,
    idata3d +128, idata3d +136, idata3d +144, idata3d +152,
    idata3d +160, idata3d +168, idata3d +176, idata3d +184
};

static const unsigned int *s3d[] =
{   istate3d,      istate3d +8,   istate3d +16,  istate3d +24,
    istate3d +32,  istate3d +40,  istate3d +48,  istate3d +56,
    istate3d +64,  istate3d +72,  istate3d +80,  istate3d +88,
    istate3d +96,  istate3d +104, istate3d +112, istate3d +120,
    istate3d +128, istate3d +136, istate3d +144, istate3d +152,
    istate3d +160, istate3d +168, istate3d +176, istate3d +184
};

struct hilbert_transform_2d : public thrust::unary_function<double,double>
{
    template<typename Tuple>
    __host__
    double operator()(const Tuple& t) const
    {
        const double x = thrust::get<0>(t);
        const double y = thrust::get<1>(t);

        int level;
        unsigned int key[2], c[2], temp, state;

        // convert x,y coordinates to integers in range [0, IMAX]
        c[0] = (unsigned int) (x * (double) IMAX);               // x
        c[1] = (unsigned int) (y * (double) IMAX);               // y

        // use state tables to convert nested quadrant's coordinates level by level
        key[0] = key[1] = 0;
        state = 0;
        for (level = 0; level < MAXLEVEL_2d; level++) {
            temp = ((c[0] >> (30-level)) & 2)    // extract 2 bits at current level
                   | ((c[1] >> (31-level)) & 1);

            // treat key[] as long shift register, shift in converted coordinate
            key[0] = (key[0] << 2) | (key[1] >> 30);
            key[1] = (key[1] << 2) | *(d2d[state] + temp);

            state = *(s2d[state] + temp);
        }

        // convert 2 part Hilbert key to double and return
        return ldexp ((double) key[0], -24)  +  ldexp ((double) key[1], -56);
    }
};

struct hilbert_transform_3d : public thrust::unary_function<double,double>
{
    template<typename Tuple>
    __host__
    double operator()(const Tuple& t) const
    {
        const double x = thrust::get<0>(t);
        const double y = thrust::get<1>(t);
        const double z = thrust::get<2>(t);

        int level;
        unsigned int key[2], c[3], temp, state;

        // convert x,y,z coordinates to integers in range [0, IMAX]
        c[0] = (unsigned int) (x * (double) IMAX);               // x
        c[1] = (unsigned int) (y * (double) IMAX);               // y
        c[2] = (unsigned int) (z * (double) IMAX);     		 // z

        // use state tables to convert nested quadrant's coordinates level by level
        key[0] = key[1] = 0;
        state = 0;
        for (level = 0; level < MAXLEVEL_3d; level++) {
            temp = ((c[0] >> (29-level)) & 4)  // extract 3 bits at current level
                   | ((c[1] >> (30-level)) & 2)
                   | ((c[2] >> (31-level)) & 1);

            // treat key[] as long shift register, shift in converted coordinate
            key[0] = (key[0] << 3) |  (key[1] >> 29);
            key[1] = (key[1] << 3) | *(d3d[state] + temp);

            state = *(s3d[state] + temp);
        }

        // convert 2 part Hilbert key to double and return
        return ldexp ((double) key[0], -25)  +  ldexp ((double) key[1], -57);
    }
};

template <class Array2d, class Array1d>
void hilbert_curve(const Array2d& coord,
            size_t num_parts,
            Array1d& parts)
{
    typedef typename Array1d::value_type PartType;
    typedef typename Array2d::const_column_view::iterator Iterator;
    typedef typename Array2d::value_type ValueType;
    typedef typename Array2d::memory_space MemorySpace;

    size_t num_points = coord.num_rows;
    size_t dims = coord.num_cols;

    if( (dims != 2) && (dims != 3) )
        throw cusp::invalid_input_exception("Hilbert curve partitioning only implemented for 2D or 3D data.");

    thrust::pair<Iterator,Iterator> x_iter = thrust::minmax_element(coord.column(0).begin(), coord.column(0).end());
    thrust::pair<Iterator,Iterator> y_iter = thrust::minmax_element(coord.column(1).begin(), coord.column(1).end());

    ValueType xmin = *x_iter.first;
    ValueType xmax = *x_iter.second;
    ValueType ymin = *y_iter.first;
    ValueType ymax = *y_iter.second;

    if( xmin < ValueType(0) || xmax > ValueType(1) || ymin < ValueType(0) || ymax > ValueType(1) )
        throw cusp::invalid_input_exception("Hilbert coordinates should be in the range [0,1]");

    cusp::array1d<double, MemorySpace> hilbert_keys(coord.num_rows);

    if( dims == 2 ) {
        thrust::transform(thrust::make_zip_iterator(thrust::make_tuple(coord.column(0).begin(), coord.column(1).begin())),
                          thrust::make_zip_iterator(thrust::make_tuple(coord.column(0).end(), coord.column(1).end())),
                          hilbert_keys.begin(), hilbert_transform_2d());
    } else {
        thrust::pair<Iterator,Iterator> z_iter = thrust::minmax_element(coord.column(2).begin(), coord.column(2).end());
        ValueType zmin = *z_iter.first;
        ValueType zmax = *z_iter.second;

        if( zmin < ValueType(0) || zmax > ValueType(1) )
            throw cusp::invalid_input_exception("Hilbert coordinates should be in the range [0,1]");

        thrust::transform(thrust::make_zip_iterator(thrust::make_tuple(coord.column(0).begin(), coord.column(1).begin(), coord.column(2).begin())),
                          thrust::make_zip_iterator(thrust::make_tuple(coord.column(0).end(), coord.column(1).end(), coord.column(2).end())),
                          hilbert_keys.begin(), hilbert_transform_3d());
    }

    cusp::array1d<PartType, MemorySpace> perm(num_points);
    thrust::sequence(perm.begin(), perm.end());
    thrust::sort_by_key(hilbert_keys.begin(), hilbert_keys.end(), perm.begin());

    cusp::array1d<PartType, MemorySpace> uniform_parts(num_points);
    thrust::transform(thrust::counting_iterator<PartType>(0), thrust::counting_iterator<PartType>(num_points),
                      thrust::constant_iterator<PartType>(num_points/num_parts), uniform_parts.begin(), thrust::divides<PartType>());
    thrust::gather(perm.begin(), perm.end(), uniform_parts.begin(), parts.begin());
}

} // end namespace host
} // end namespace detail
} // end namespace graph
} // end namespace cusp